Home | History | Annotate | Line # | Download | only in kern
subr_disk.c revision 1.132.6.1
      1  1.132.6.1   thorpej /*	$NetBSD: subr_disk.c,v 1.132.6.1 2021/06/17 04:46:33 thorpej Exp $	*/
      2       1.22   thorpej 
      3       1.22   thorpej /*-
      4       1.95        ad  * Copyright (c) 1996, 1997, 1999, 2000, 2009 The NetBSD Foundation, Inc.
      5       1.22   thorpej  * All rights reserved.
      6       1.22   thorpej  *
      7       1.22   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.22   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9       1.22   thorpej  * NASA Ames Research Center.
     10       1.22   thorpej  *
     11       1.22   thorpej  * Redistribution and use in source and binary forms, with or without
     12       1.22   thorpej  * modification, are permitted provided that the following conditions
     13       1.22   thorpej  * are met:
     14       1.22   thorpej  * 1. Redistributions of source code must retain the above copyright
     15       1.22   thorpej  *    notice, this list of conditions and the following disclaimer.
     16       1.22   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.22   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18       1.22   thorpej  *    documentation and/or other materials provided with the distribution.
     19       1.22   thorpej  *
     20       1.22   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.22   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.22   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.22   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.22   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.22   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.22   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.22   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.22   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.22   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.22   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31       1.22   thorpej  */
     32       1.12       cgd 
     33       1.11   mycroft /*
     34       1.11   mycroft  * Copyright (c) 1982, 1986, 1988, 1993
     35       1.11   mycroft  *	The Regents of the University of California.  All rights reserved.
     36       1.11   mycroft  * (c) UNIX System Laboratories, Inc.
     37       1.11   mycroft  * All or some portions of this file are derived from material licensed
     38       1.11   mycroft  * to the University of California by American Telephone and Telegraph
     39       1.11   mycroft  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40       1.11   mycroft  * the permission of UNIX System Laboratories, Inc.
     41       1.11   mycroft  *
     42       1.11   mycroft  * Redistribution and use in source and binary forms, with or without
     43       1.11   mycroft  * modification, are permitted provided that the following conditions
     44       1.11   mycroft  * are met:
     45       1.11   mycroft  * 1. Redistributions of source code must retain the above copyright
     46       1.11   mycroft  *    notice, this list of conditions and the following disclaimer.
     47       1.11   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     48       1.11   mycroft  *    notice, this list of conditions and the following disclaimer in the
     49       1.11   mycroft  *    documentation and/or other materials provided with the distribution.
     50       1.53       agc  * 3. Neither the name of the University nor the names of its contributors
     51       1.11   mycroft  *    may be used to endorse or promote products derived from this software
     52       1.11   mycroft  *    without specific prior written permission.
     53       1.11   mycroft  *
     54       1.11   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55       1.11   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56       1.11   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57       1.11   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58       1.11   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59       1.11   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60       1.11   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61       1.11   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62       1.11   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63       1.11   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64       1.11   mycroft  * SUCH DAMAGE.
     65       1.11   mycroft  *
     66       1.12       cgd  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     67       1.11   mycroft  */
     68       1.31     lukem 
     69       1.31     lukem #include <sys/cdefs.h>
     70  1.132.6.1   thorpej __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.132.6.1 2021/06/17 04:46:33 thorpej Exp $");
     71       1.48       mrg 
     72       1.11   mycroft #include <sys/param.h>
     73       1.15   thorpej #include <sys/kernel.h>
     74       1.94      yamt #include <sys/kmem.h>
     75       1.11   mycroft #include <sys/buf.h>
     76      1.107  christos #include <sys/fcntl.h>
     77       1.15   thorpej #include <sys/syslog.h>
     78       1.11   mycroft #include <sys/disklabel.h>
     79       1.15   thorpej #include <sys/disk.h>
     80       1.33    simonb #include <sys/sysctl.h>
     81       1.47       mrg #include <lib/libkern/libkern.h>
     82       1.14   thorpej 
     83       1.14   thorpej /*
     84       1.11   mycroft  * Disk error is the preface to plaintive error messages
     85       1.11   mycroft  * about failing disk transfers.  It prints messages of the form
     86       1.11   mycroft 
     87       1.11   mycroft hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
     88       1.11   mycroft 
     89       1.11   mycroft  * if the offset of the error in the transfer and a disk label
     90       1.11   mycroft  * are both available.  blkdone should be -1 if the position of the error
     91       1.11   mycroft  * is unknown; the disklabel pointer may be null from drivers that have not
     92       1.20  christos  * been converted to use them.  The message is printed with printf
     93       1.11   mycroft  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
     94       1.20  christos  * The message should be completed (with at least a newline) with printf
     95       1.11   mycroft  * or addlog, respectively.  There is no trailing space.
     96       1.11   mycroft  */
     97       1.51       dsl #ifndef PRIdaddr
     98       1.51       dsl #define PRIdaddr PRId64
     99       1.51       dsl #endif
    100       1.11   mycroft void
    101       1.38      yamt diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
    102       1.38      yamt     int blkdone, const struct disklabel *lp)
    103       1.11   mycroft {
    104       1.25  drochner 	int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
    105      1.101  christos 	void (*pr)(const char *, ...) __printflike(1, 2);
    106       1.11   mycroft 	char partname = 'a' + part;
    107       1.51       dsl 	daddr_t sn;
    108       1.51       dsl 
    109       1.52       dsl 	if (/*CONSTCOND*/0)
    110      1.122       kre 		/* Compiler will error this if the format is wrong... */
    111       1.51       dsl 		printf("%" PRIdaddr, bp->b_blkno);
    112       1.11   mycroft 
    113       1.11   mycroft 	if (pri != LOG_PRINTF) {
    114       1.17  christos 		static const char fmt[] = "";
    115       1.17  christos 		log(pri, fmt);
    116       1.11   mycroft 		pr = addlog;
    117       1.11   mycroft 	} else
    118      1.121     joerg 		pr = printf;
    119       1.11   mycroft 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
    120       1.11   mycroft 	    bp->b_flags & B_READ ? "read" : "writ");
    121       1.11   mycroft 	sn = bp->b_blkno;
    122       1.11   mycroft 	if (bp->b_bcount <= DEV_BSIZE)
    123       1.51       dsl 		(*pr)("%" PRIdaddr, sn);
    124       1.11   mycroft 	else {
    125       1.11   mycroft 		if (blkdone >= 0) {
    126       1.11   mycroft 			sn += blkdone;
    127       1.51       dsl 			(*pr)("%" PRIdaddr " of ", sn);
    128       1.11   mycroft 		}
    129       1.51       dsl 		(*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
    130       1.11   mycroft 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
    131       1.11   mycroft 	}
    132       1.11   mycroft 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
    133       1.11   mycroft 		sn += lp->d_partitions[part].p_offset;
    134       1.51       dsl 		(*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
    135       1.51       dsl 		    dname, unit, sn, sn / lp->d_secpercyl);
    136       1.11   mycroft 		sn %= lp->d_secpercyl;
    137       1.51       dsl 		(*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
    138       1.51       dsl 		    sn / lp->d_nsectors, sn % lp->d_nsectors);
    139       1.11   mycroft 	}
    140       1.15   thorpej }
    141       1.15   thorpej 
    142       1.15   thorpej /*
    143       1.74     blymn  * Searches the iostatlist for the disk corresponding to the
    144       1.15   thorpej  * name provided.
    145       1.15   thorpej  */
    146       1.15   thorpej struct disk *
    147       1.78      yamt disk_find(const char *name)
    148       1.15   thorpej {
    149       1.74     blymn 	struct io_stats *stat;
    150       1.15   thorpej 
    151       1.74     blymn 	stat = iostat_find(name);
    152       1.15   thorpej 
    153       1.75     blymn 	if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
    154       1.75     blymn 		return stat->io_parent;
    155       1.15   thorpej 
    156       1.76      yamt 	return (NULL);
    157       1.15   thorpej }
    158       1.15   thorpej 
    159       1.89        ad void
    160       1.92      matt disk_init(struct disk *diskp, const char *name, const struct dkdriver *driver)
    161       1.70      yamt {
    162      1.106   mlelstv 	u_int blocksize = DEV_BSIZE;
    163       1.70      yamt 
    164       1.70      yamt 	/*
    165       1.70      yamt 	 * Initialize the wedge-related locks and other fields.
    166       1.70      yamt 	 */
    167       1.87        ad 	mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
    168       1.87        ad 	mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
    169       1.70      yamt 	LIST_INIT(&diskp->dk_wedges);
    170       1.70      yamt 	diskp->dk_nwedges = 0;
    171       1.83       scw 	diskp->dk_labelsector = LABELSECTOR;
    172      1.106   mlelstv 	diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
    173      1.106   mlelstv 	diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
    174       1.89        ad 	diskp->dk_name = name;
    175       1.89        ad 	diskp->dk_driver = driver;
    176       1.70      yamt }
    177       1.70      yamt 
    178       1.89        ad /*
    179      1.128   hannken  * Rename a disk.
    180      1.128   hannken  */
    181      1.128   hannken void
    182      1.128   hannken disk_rename(struct disk *diskp, const char *name)
    183      1.128   hannken {
    184      1.128   hannken 
    185      1.128   hannken 	diskp->dk_name = name;
    186      1.128   hannken 	iostat_rename(diskp->dk_stats, diskp->dk_name);
    187      1.128   hannken }
    188      1.128   hannken 
    189      1.128   hannken /*
    190       1.89        ad  * Attach a disk.
    191       1.89        ad  */
    192       1.89        ad void
    193       1.89        ad disk_attach(struct disk *diskp)
    194       1.15   thorpej {
    195       1.79    kardel 
    196       1.15   thorpej 	/*
    197       1.94      yamt 	 * Allocate and initialize the disklabel structures.
    198       1.15   thorpej 	 */
    199       1.94      yamt 	diskp->dk_label = kmem_zalloc(sizeof(struct disklabel), KM_SLEEP);
    200       1.94      yamt 	diskp->dk_cpulabel = kmem_zalloc(sizeof(struct cpu_disklabel),
    201       1.94      yamt 	    KM_SLEEP);
    202       1.15   thorpej 
    203       1.15   thorpej 	/*
    204       1.74     blymn 	 * Set up the stats collection.
    205       1.15   thorpej 	 */
    206       1.80  christos 	diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
    207       1.15   thorpej }
    208       1.15   thorpej 
    209       1.96    dyoung int
    210       1.97    dyoung disk_begindetach(struct disk *dk, int (*lastclose)(device_t),
    211       1.96    dyoung     device_t self, int flags)
    212       1.96    dyoung {
    213       1.96    dyoung 	int rc;
    214       1.96    dyoung 
    215       1.96    dyoung 	rc = 0;
    216       1.96    dyoung 	mutex_enter(&dk->dk_openlock);
    217       1.96    dyoung 	if (dk->dk_openmask == 0)
    218       1.96    dyoung 		;	/* nothing to do */
    219       1.96    dyoung 	else if ((flags & DETACH_FORCE) == 0)
    220       1.96    dyoung 		rc = EBUSY;
    221       1.96    dyoung 	else if (lastclose != NULL)
    222       1.96    dyoung 		rc = (*lastclose)(self);
    223       1.96    dyoung 	mutex_exit(&dk->dk_openlock);
    224       1.96    dyoung 
    225       1.96    dyoung 	return rc;
    226       1.96    dyoung }
    227       1.96    dyoung 
    228       1.89        ad /*
    229       1.89        ad  * Detach a disk.
    230       1.89        ad  */
    231       1.89        ad void
    232       1.89        ad disk_detach(struct disk *diskp)
    233       1.15   thorpej {
    234       1.15   thorpej 
    235       1.15   thorpej 	/*
    236       1.74     blymn 	 * Remove from the drivelist.
    237       1.23   thorpej 	 */
    238       1.76      yamt 	iostat_free(diskp->dk_stats);
    239       1.23   thorpej 
    240       1.23   thorpej 	/*
    241       1.81   thorpej 	 * Release the disk-info dictionary.
    242       1.81   thorpej 	 */
    243       1.81   thorpej 	if (diskp->dk_info) {
    244       1.81   thorpej 		prop_object_release(diskp->dk_info);
    245       1.81   thorpej 		diskp->dk_info = NULL;
    246       1.81   thorpej 	}
    247       1.81   thorpej 
    248       1.81   thorpej 	/*
    249       1.15   thorpej 	 * Free the space used by the disklabel structures.
    250       1.15   thorpej 	 */
    251       1.94      yamt 	kmem_free(diskp->dk_label, sizeof(*diskp->dk_label));
    252       1.94      yamt 	kmem_free(diskp->dk_cpulabel, sizeof(*diskp->dk_cpulabel));
    253       1.15   thorpej }
    254       1.15   thorpej 
    255       1.70      yamt void
    256       1.89        ad disk_destroy(struct disk *diskp)
    257       1.70      yamt {
    258       1.70      yamt 
    259       1.87        ad 	mutex_destroy(&diskp->dk_openlock);
    260       1.87        ad 	mutex_destroy(&diskp->dk_rawlock);
    261       1.70      yamt }
    262       1.70      yamt 
    263       1.70      yamt /*
    264      1.118   mlelstv  * Mark the disk as having work queued for metrics collection.
    265      1.118   mlelstv  */
    266      1.118   mlelstv void
    267      1.118   mlelstv disk_wait(struct disk *diskp)
    268      1.118   mlelstv {
    269      1.118   mlelstv 
    270      1.118   mlelstv 	iostat_wait(diskp->dk_stats);
    271      1.118   mlelstv }
    272      1.118   mlelstv 
    273      1.118   mlelstv /*
    274       1.74     blymn  * Mark the disk as busy for metrics collection.
    275       1.15   thorpej  */
    276       1.15   thorpej void
    277       1.30    simonb disk_busy(struct disk *diskp)
    278       1.15   thorpej {
    279       1.76      yamt 
    280       1.74     blymn 	iostat_busy(diskp->dk_stats);
    281       1.15   thorpej }
    282       1.15   thorpej 
    283       1.15   thorpej /*
    284       1.74     blymn  * Finished disk operations, gather metrics.
    285       1.15   thorpej  */
    286       1.15   thorpej void
    287       1.45       mrg disk_unbusy(struct disk *diskp, long bcount, int read)
    288       1.15   thorpej {
    289       1.76      yamt 
    290       1.74     blymn 	iostat_unbusy(diskp->dk_stats, bcount, read);
    291       1.39   hannken }
    292       1.39   hannken 
    293       1.40   hannken /*
    294       1.95        ad  * Return true if disk has an I/O operation in flight.
    295       1.95        ad  */
    296       1.95        ad bool
    297       1.95        ad disk_isbusy(struct disk *diskp)
    298       1.95        ad {
    299       1.95        ad 
    300       1.95        ad 	return iostat_isbusy(diskp->dk_stats);
    301       1.95        ad }
    302       1.95        ad 
    303       1.95        ad /*
    304       1.50      fvdl  * Bounds checking against the media size, used for the raw partition.
    305       1.99       dsl  * secsize, mediasize and b_blkno must all be the same units.
    306       1.99       dsl  * Possibly this has to be DEV_BSIZE (512).
    307       1.50      fvdl  */
    308       1.50      fvdl int
    309       1.73     perry bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
    310       1.50      fvdl {
    311       1.67      fvdl 	int64_t sz;
    312       1.50      fvdl 
    313      1.113       chs 	if (bp->b_blkno < 0) {
    314      1.113       chs 		/* Reject negative offsets immediately. */
    315      1.113       chs 		bp->b_error = EINVAL;
    316      1.113       chs 		return 0;
    317      1.113       chs 	}
    318      1.113       chs 
    319      1.113       chs 	sz = howmany((int64_t)bp->b_bcount, secsize);
    320      1.113       chs 
    321      1.113       chs 	/*
    322      1.113       chs 	 * bp->b_bcount is a 32-bit value, and we rejected a negative
    323      1.113       chs 	 * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
    324      1.113       chs 	 */
    325       1.50      fvdl 
    326       1.50      fvdl 	if (bp->b_blkno + sz > mediasize) {
    327       1.50      fvdl 		sz = mediasize - bp->b_blkno;
    328       1.50      fvdl 		if (sz == 0) {
    329       1.50      fvdl 			/* If exactly at end of disk, return EOF. */
    330       1.50      fvdl 			bp->b_resid = bp->b_bcount;
    331       1.88        ad 			return 0;
    332       1.50      fvdl 		}
    333       1.50      fvdl 		if (sz < 0) {
    334       1.50      fvdl 			/* If past end of disk, return EINVAL. */
    335       1.50      fvdl 			bp->b_error = EINVAL;
    336       1.88        ad 			return 0;
    337       1.50      fvdl 		}
    338       1.50      fvdl 		/* Otherwise, truncate request. */
    339       1.99       dsl 		bp->b_bcount = sz * secsize;
    340       1.50      fvdl 	}
    341       1.50      fvdl 
    342       1.50      fvdl 	return 1;
    343       1.11   mycroft }
    344       1.82   thorpej 
    345       1.82   thorpej /*
    346       1.83       scw  * Determine the size of the transfer, and make sure it is
    347       1.83       scw  * within the boundaries of the partition. Adjust transfer
    348       1.83       scw  * if needed, and signal errors or early completion.
    349       1.83       scw  */
    350       1.83       scw int
    351       1.83       scw bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
    352       1.83       scw {
    353       1.83       scw 	struct disklabel *lp = dk->dk_label;
    354       1.83       scw 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
    355       1.83       scw 	uint64_t p_size, p_offset, labelsector;
    356       1.83       scw 	int64_t sz;
    357       1.83       scw 
    358      1.113       chs 	if (bp->b_blkno < 0) {
    359      1.113       chs 		/* Reject negative offsets immediately. */
    360      1.113       chs 		bp->b_error = EINVAL;
    361      1.113       chs 		return -1;
    362      1.113       chs 	}
    363      1.113       chs 
    364       1.83       scw 	/* Protect against division by zero. XXX: Should never happen?!?! */
    365      1.129      cnst 	if ((lp->d_secsize / DEV_BSIZE) == 0 || lp->d_secpercyl == 0) {
    366       1.83       scw 		bp->b_error = EINVAL;
    367       1.88        ad 		return -1;
    368       1.83       scw 	}
    369       1.83       scw 
    370      1.100       mrg 	p_size = (uint64_t)p->p_size << dk->dk_blkshift;
    371      1.100       mrg 	p_offset = (uint64_t)p->p_offset << dk->dk_blkshift;
    372       1.83       scw #if RAW_PART == 3
    373       1.83       scw 	labelsector = lp->d_partitions[2].p_offset;
    374       1.83       scw #else
    375       1.83       scw 	labelsector = lp->d_partitions[RAW_PART].p_offset;
    376       1.83       scw #endif
    377       1.83       scw 	labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
    378       1.83       scw 
    379      1.113       chs 	sz = howmany((int64_t)bp->b_bcount, DEV_BSIZE);
    380      1.113       chs 
    381      1.113       chs 	/*
    382      1.113       chs 	 * bp->b_bcount is a 32-bit value, and we rejected a negative
    383      1.113       chs 	 * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
    384      1.113       chs 	 */
    385      1.113       chs 
    386      1.113       chs 	if (bp->b_blkno + sz > p_size) {
    387       1.83       scw 		sz = p_size - bp->b_blkno;
    388       1.83       scw 		if (sz == 0) {
    389       1.83       scw 			/* If exactly at end of disk, return EOF. */
    390       1.83       scw 			bp->b_resid = bp->b_bcount;
    391       1.88        ad 			return 0;
    392       1.83       scw 		}
    393       1.83       scw 		if (sz < 0) {
    394       1.83       scw 			/* If past end of disk, return EINVAL. */
    395       1.83       scw 			bp->b_error = EINVAL;
    396       1.88        ad 			return -1;
    397       1.83       scw 		}
    398       1.83       scw 		/* Otherwise, truncate request. */
    399       1.83       scw 		bp->b_bcount = sz << DEV_BSHIFT;
    400       1.83       scw 	}
    401       1.83       scw 
    402       1.83       scw 	/* Overwriting disk label? */
    403       1.83       scw 	if (bp->b_blkno + p_offset <= labelsector &&
    404       1.83       scw 	    bp->b_blkno + p_offset + sz > labelsector &&
    405       1.83       scw 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
    406       1.83       scw 		bp->b_error = EROFS;
    407       1.88        ad 		return -1;
    408       1.83       scw 	}
    409       1.83       scw 
    410       1.83       scw 	/* calculate cylinder for disksort to order transfers with */
    411       1.83       scw 	bp->b_cylinder = (bp->b_blkno + p->p_offset) /
    412       1.83       scw 	    (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
    413       1.88        ad 	return 1;
    414       1.83       scw }
    415       1.83       scw 
    416       1.86    dyoung int
    417       1.86    dyoung disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
    418       1.86    dyoung     struct buf *bp, unsigned int sector, int count)
    419       1.86    dyoung {
    420      1.130   mlelstv 
    421      1.130   mlelstv 	if ((lp->d_secsize / DEV_BSIZE) == 0 || lp->d_secpercyl == 0)
    422      1.130   mlelstv 		return EINVAL;
    423      1.130   mlelstv 
    424      1.114   mlelstv 	bp->b_blkno = btodb((off_t)sector * lp->d_secsize);
    425       1.86    dyoung 	bp->b_bcount = count * lp->d_secsize;
    426       1.90        ad 	bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;
    427       1.90        ad 	bp->b_oflags &= ~BO_DONE;
    428       1.86    dyoung 	bp->b_cylinder = sector / lp->d_secpercyl;
    429       1.86    dyoung 	(*strat)(bp);
    430       1.86    dyoung 	return biowait(bp);
    431       1.86    dyoung }
    432       1.86    dyoung 
    433       1.86    dyoung const char *
    434       1.86    dyoung convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
    435       1.86    dyoung     struct buf *bp, uint32_t secperunit)
    436       1.86    dyoung {
    437       1.86    dyoung 	struct partition rp, *altp, *p;
    438       1.86    dyoung 	int geom_ok;
    439      1.116  christos 	const char *str;
    440       1.86    dyoung 
    441       1.86    dyoung 	memset(&rp, 0, sizeof(rp));
    442       1.86    dyoung 	rp.p_size = secperunit;
    443       1.86    dyoung 	rp.p_fstype = FS_UNUSED;
    444       1.86    dyoung 
    445       1.86    dyoung 	/* If we can seek to d_secperunit - 1, believe the disk geometry. */
    446       1.86    dyoung 	if (secperunit != 0 &&
    447       1.86    dyoung 	    disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
    448       1.86    dyoung 		geom_ok = 1;
    449       1.86    dyoung 	else
    450       1.86    dyoung 		geom_ok = 0;
    451       1.86    dyoung 
    452       1.86    dyoung #if 0
    453       1.86    dyoung 	printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
    454       1.86    dyoung 	    secperunit, geom_ok ? "ok" : "not ok");
    455       1.86    dyoung #endif
    456       1.86    dyoung 
    457       1.86    dyoung 	p = &lp->d_partitions[RAW_PART];
    458       1.86    dyoung 	if (RAW_PART == 'c' - 'a')
    459       1.86    dyoung 		altp = &lp->d_partitions['d' - 'a'];
    460       1.86    dyoung 	else
    461       1.86    dyoung 		altp = &lp->d_partitions['c' - 'a'];
    462       1.86    dyoung 
    463       1.86    dyoung 	if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
    464      1.116  christos 		return NULL;	/* already a raw partition */
    465       1.86    dyoung 	else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
    466       1.86    dyoung 		 altp->p_offset == 0 && altp->p_size != 0) {
    467       1.86    dyoung 		/* alternate partition ('c' or 'd') is suitable for raw slot,
    468       1.86    dyoung 		 * swap with 'd' or 'c'.
    469       1.86    dyoung 		 */
    470       1.86    dyoung 		rp = *p;
    471       1.86    dyoung 		*p = *altp;
    472       1.86    dyoung 		*altp = rp;
    473      1.116  christos 		return NULL;
    474       1.86    dyoung 	} else if (lp->d_npartitions <= RAW_PART &&
    475       1.86    dyoung 	           lp->d_npartitions > 'c' - 'a') {
    476       1.86    dyoung 		/* No raw partition is present, but the alternate is present.
    477       1.86    dyoung 		 * Copy alternate to raw partition.
    478       1.86    dyoung 		 */
    479       1.86    dyoung 		lp->d_npartitions = RAW_PART + 1;
    480       1.86    dyoung 		*p = *altp;
    481      1.116  christos 		return NULL;
    482       1.86    dyoung 	} else if (!geom_ok)
    483      1.116  christos 		str = "no raw partition and disk reports bad geometry";
    484       1.86    dyoung 	else if (lp->d_npartitions <= RAW_PART) {
    485       1.86    dyoung 		memset(&lp->d_partitions[lp->d_npartitions], 0,
    486       1.86    dyoung 		    sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
    487       1.86    dyoung 		*p = rp;
    488       1.86    dyoung 		lp->d_npartitions = RAW_PART + 1;
    489      1.116  christos 		return NULL;
    490       1.86    dyoung 	} else if (lp->d_npartitions < MAXPARTITIONS) {
    491       1.86    dyoung 		memmove(p + 1, p,
    492       1.86    dyoung 		    sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
    493       1.86    dyoung 		*p = rp;
    494       1.86    dyoung 		lp->d_npartitions++;
    495      1.116  christos 		return NULL;
    496       1.86    dyoung 	} else
    497      1.116  christos 		str = "no raw partition and partition table is full";
    498      1.116  christos #ifdef DIAGNOSTIC
    499      1.116  christos 	printf("Bad partition: %s\n", str);
    500      1.116  christos 	printf("type = %u, subtype = %u, typename = %s\n",
    501      1.116  christos 	    lp->d_type, lp->d_subtype, lp->d_typename);
    502      1.116  christos 	printf("secsize = %u, nsectors = %u, ntracks = %u\n",
    503      1.116  christos 	    lp->d_secsize, lp->d_nsectors, lp->d_ntracks);
    504      1.116  christos 	printf("ncylinders = %u, secpercyl = %u, secperunit = %u\n",
    505      1.116  christos 	    lp->d_ncylinders, lp->d_secpercyl, lp->d_secperunit);
    506      1.116  christos 	printf("npartitions = %u\n", lp->d_npartitions);
    507      1.116  christos 
    508      1.116  christos 	for (size_t i = 0; i < MIN(lp->d_npartitions, MAXPARTITIONS); i++) {
    509      1.116  christos 		p = &lp->d_partitions[i];
    510      1.116  christos 		printf("\t%c: offset = %u size = %u fstype = %u\n",
    511      1.116  christos 		    (char)(i + 'a'), p->p_offset, p->p_size, p->p_fstype);
    512      1.116  christos 	}
    513      1.116  christos #endif
    514      1.116  christos 	return str;
    515       1.86    dyoung }
    516       1.86    dyoung 
    517       1.83       scw /*
    518       1.82   thorpej  * disk_ioctl --
    519       1.82   thorpej  *	Generic disk ioctl handling.
    520       1.82   thorpej  */
    521       1.82   thorpej int
    522      1.108  christos disk_ioctl(struct disk *dk, dev_t dev, u_long cmd, void *data, int flag,
    523      1.108  christos     struct lwp *l)
    524       1.82   thorpej {
    525      1.107  christos 	struct dkwedge_info *dkw;
    526      1.115  christos 	struct partinfo *pi;
    527      1.115  christos 	struct partition *dp;
    528      1.108  christos #ifdef __HAVE_OLD_DISKLABEL
    529      1.108  christos 	struct disklabel newlabel;
    530      1.108  christos #endif
    531       1.82   thorpej 
    532       1.82   thorpej 	switch (cmd) {
    533      1.109  christos 	case DIOCGDISKINFO:
    534      1.109  christos 		if (dk->dk_info == NULL)
    535      1.109  christos 			return ENOTSUP;
    536      1.109  christos 		return prop_dictionary_copyout_ioctl(data, cmd, dk->dk_info);
    537      1.109  christos 
    538      1.109  christos 	case DIOCGSECTORSIZE:
    539      1.109  christos 		*(u_int *)data = dk->dk_geom.dg_secsize;
    540      1.109  christos 		return 0;
    541      1.109  christos 
    542      1.109  christos 	case DIOCGMEDIASIZE:
    543      1.109  christos 		*(off_t *)data = (off_t)dk->dk_geom.dg_secsize *
    544      1.109  christos 		    dk->dk_geom.dg_secperunit;
    545      1.109  christos 		return 0;
    546      1.109  christos 	default:
    547      1.109  christos 		break;
    548      1.109  christos 	}
    549      1.109  christos 
    550      1.111  christos 	if (dev == NODEV)
    551      1.109  christos 		return EPASSTHROUGH;
    552      1.109  christos 
    553      1.109  christos 	/* The following should be moved to dk_ioctl */
    554      1.109  christos 	switch (cmd) {
    555      1.108  christos 	case DIOCGDINFO:
    556      1.115  christos 		if (dk->dk_label == NULL)
    557      1.115  christos 			return EBUSY;
    558      1.108  christos 		memcpy(data, dk->dk_label, sizeof (*dk->dk_label));
    559      1.108  christos 		return 0;
    560      1.108  christos 
    561      1.108  christos #ifdef __HAVE_OLD_DISKLABEL
    562      1.108  christos 	case ODIOCGDINFO:
    563      1.115  christos 		if (dk->dk_label == NULL)
    564      1.115  christos 			return EBUSY;
    565      1.108  christos 		memcpy(&newlabel, dk->dk_label, sizeof(newlabel));
    566      1.108  christos 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    567      1.108  christos 			return ENOTTY;
    568      1.108  christos 		memcpy(data, &newlabel, sizeof(struct olddisklabel));
    569      1.108  christos 		return 0;
    570      1.108  christos #endif
    571      1.108  christos 
    572      1.115  christos 	case DIOCGPARTINFO:
    573      1.115  christos 		pi = data;
    574      1.115  christos 		memset(pi, 0, sizeof(*pi));
    575      1.115  christos 		pi->pi_secsize = dk->dk_geom.dg_secsize;
    576      1.117  jakllsch 		pi->pi_bsize = MAX(BLKDEV_IOSIZE, pi->pi_secsize);
    577      1.115  christos 
    578      1.115  christos 		if (DISKPART(dev) == RAW_PART) {
    579      1.115  christos 			pi->pi_size = dk->dk_geom.dg_secperunit;
    580      1.115  christos 			return 0;
    581      1.115  christos 		}
    582      1.115  christos 
    583      1.111  christos 		if (dk->dk_label == NULL)
    584      1.111  christos 			return EBUSY;
    585      1.115  christos 
    586      1.115  christos 		dp = &dk->dk_label->d_partitions[DISKPART(dev)];
    587      1.115  christos 		pi->pi_offset = dp->p_offset;
    588      1.115  christos 		pi->pi_size = dp->p_size;
    589      1.115  christos 
    590      1.115  christos 		pi->pi_fstype = dp->p_fstype;
    591      1.115  christos 		pi->pi_frag = dp->p_frag;
    592      1.115  christos 		pi->pi_fsize = dp->p_fsize;
    593      1.115  christos 		pi->pi_cpg = dp->p_cpg;
    594      1.115  christos 
    595      1.115  christos 		/*
    596      1.115  christos 		 * dholland 20130616: XXX this logic should not be
    597      1.115  christos 		 * here. It is here because the old buffer cache
    598      1.115  christos 		 * demands that all accesses to the same blocks need
    599      1.115  christos 		 * to be the same size; but it only works for FFS and
    600      1.115  christos 		 * nowadays I think it'll fail silently if the size
    601      1.115  christos 		 * info in the disklabel is wrong. (Or missing.) The
    602      1.115  christos 		 * buffer cache needs to be smarter; or failing that
    603      1.115  christos 		 * we need a reliable way here to get the right block
    604      1.115  christos 		 * size; or a reliable way to guarantee that (a) the
    605      1.115  christos 		 * fs is not mounted when we get here and (b) any
    606      1.115  christos 		 * buffers generated here will get purged when the fs
    607      1.115  christos 		 * does get mounted.
    608      1.115  christos 		 */
    609      1.115  christos 		if (dp->p_fstype == FS_BSDFFS &&
    610      1.115  christos 		    dp->p_frag != 0 && dp->p_fsize != 0)
    611      1.115  christos 			pi->pi_bsize = dp->p_frag * dp->p_fsize;
    612      1.108  christos 		return 0;
    613      1.108  christos 
    614      1.107  christos 	case DIOCAWEDGE:
    615      1.107  christos 		if ((flag & FWRITE) == 0)
    616      1.107  christos 			return EBADF;
    617      1.107  christos 
    618      1.107  christos 		dkw = data;
    619      1.107  christos 		strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
    620      1.107  christos 		return dkwedge_add(dkw);
    621      1.107  christos 
    622      1.107  christos 	case DIOCDWEDGE:
    623      1.107  christos 		if ((flag & FWRITE) == 0)
    624      1.107  christos 			return EBADF;
    625      1.107  christos 
    626      1.107  christos 		dkw = data;
    627      1.107  christos 		strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
    628      1.107  christos 		return dkwedge_del(dkw);
    629      1.107  christos 
    630      1.107  christos 	case DIOCLWEDGES:
    631      1.107  christos 		return dkwedge_list(dk, data, l);
    632      1.107  christos 
    633      1.107  christos 	case DIOCMWEDGES:
    634      1.107  christos 		if ((flag & FWRITE) == 0)
    635      1.107  christos 			return EBADF;
    636      1.107  christos 
    637      1.107  christos 		dkwedge_discover(dk);
    638      1.107  christos 		return 0;
    639      1.107  christos 
    640      1.123    martin 	case DIOCRMWEDGES:
    641      1.123    martin 		if ((flag & FWRITE) == 0)
    642      1.123    martin 			return EBADF;
    643      1.123    martin 
    644      1.123    martin 		dkwedge_delall(dk);
    645      1.123    martin 		return 0;
    646      1.123    martin 
    647       1.82   thorpej 	default:
    648      1.107  christos 		return EPASSTHROUGH;
    649       1.82   thorpej 	}
    650       1.82   thorpej }
    651      1.102  christos 
    652      1.102  christos void
    653      1.102  christos disk_set_info(device_t dev, struct disk *dk, const char *type)
    654      1.102  christos {
    655      1.102  christos 	struct disk_geom *dg = &dk->dk_geom;
    656      1.102  christos 
    657      1.112   mlelstv 	if (dg->dg_secsize == 0) {
    658      1.112   mlelstv #ifdef DIAGNOSTIC
    659      1.112   mlelstv 		printf("%s: fixing 0 sector size\n", dk->dk_name);
    660      1.112   mlelstv #endif
    661      1.112   mlelstv 		dg->dg_secsize = DEV_BSIZE;
    662      1.112   mlelstv 	}
    663      1.112   mlelstv 
    664      1.112   mlelstv 	dk->dk_blkshift = DK_BSIZE2BLKSHIFT(dg->dg_secsize);
    665      1.112   mlelstv 	dk->dk_byteshift = DK_BSIZE2BYTESHIFT(dg->dg_secsize);
    666      1.112   mlelstv 
    667      1.132   mlelstv 	if (dg->dg_secperunit == 0) {
    668      1.102  christos #ifdef DIAGNOSTIC
    669      1.132   mlelstv 		if (dg->dg_ncylinders == 0) {
    670      1.132   mlelstv 			printf("%s: secperunit and ncylinders are zero\n",
    671      1.132   mlelstv 			    dk->dk_name);
    672      1.132   mlelstv 		}
    673      1.102  christos 		if (dg->dg_nsectors == 0 || dg->dg_ntracks == 0) {
    674      1.102  christos 			printf("%s: secperunit and (sectors or tracks) "
    675      1.102  christos 			    "are zero\n", dk->dk_name);
    676      1.132   mlelstv 		}
    677      1.102  christos #endif
    678      1.103   mlelstv 		dg->dg_secperunit = (int64_t) dg->dg_nsectors *
    679      1.102  christos 		    dg->dg_ntracks * dg->dg_ncylinders;
    680      1.102  christos 	}
    681      1.102  christos 
    682      1.102  christos 	if (dg->dg_ncylinders == 0) {
    683      1.102  christos 		if (dg->dg_ntracks && dg->dg_nsectors)
    684      1.102  christos 			dg->dg_ncylinders = dg->dg_secperunit /
    685      1.102  christos 			    (dg->dg_ntracks * dg->dg_nsectors);
    686      1.102  christos 	}
    687      1.102  christos 
    688      1.102  christos 	prop_dictionary_t disk_info, odisk_info, geom;
    689      1.102  christos 
    690      1.102  christos 	disk_info = prop_dictionary_create();
    691      1.102  christos 	geom = prop_dictionary_create();
    692      1.102  christos 
    693      1.102  christos 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
    694      1.102  christos 	    dg->dg_secperunit);
    695      1.102  christos 
    696      1.102  christos 	prop_dictionary_set_uint32(geom, "sector-size", dg->dg_secsize);
    697      1.102  christos 
    698      1.102  christos 	if (dg->dg_nsectors)
    699      1.102  christos 		prop_dictionary_set_uint16(geom, "sectors-per-track",
    700      1.102  christos 		    dg->dg_nsectors);
    701      1.102  christos 
    702      1.102  christos 	if (dg->dg_ntracks)
    703      1.102  christos 		prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
    704      1.102  christos 		    dg->dg_ntracks);
    705      1.102  christos 
    706      1.102  christos 	if (dg->dg_ncylinders)
    707      1.102  christos 		prop_dictionary_set_uint64(geom, "cylinders-per-unit",
    708      1.102  christos 		    dg->dg_ncylinders);
    709      1.102  christos 
    710      1.102  christos 	prop_dictionary_set(disk_info, "geometry", geom);
    711      1.102  christos 
    712      1.102  christos 	if (type)
    713      1.131   thorpej 		prop_dictionary_set_string_nocopy(disk_info, "type", type);
    714      1.102  christos 
    715      1.102  christos 	prop_object_release(geom);
    716      1.102  christos 
    717      1.102  christos 	odisk_info = dk->dk_info;
    718      1.102  christos 	dk->dk_info = disk_info;
    719      1.102  christos 
    720      1.102  christos 	if (dev)
    721      1.102  christos 		prop_dictionary_set(device_properties(dev), "disk-info",
    722      1.102  christos 		    disk_info);
    723      1.102  christos 
    724      1.102  christos 	/*
    725      1.102  christos 	 * Don't release disk_info here; we keep a reference to it.
    726      1.102  christos 	 * disk_detach() will release it when we go away.
    727      1.102  christos 	 */
    728      1.102  christos 	if (odisk_info)
    729      1.102  christos 		prop_object_release(odisk_info);
    730      1.102  christos }
    731