Home | History | Annotate | Line # | Download | only in kern
subr_disk_open.c revision 1.11.14.1
      1  1.11.14.1     skrll /*	$NetBSD: subr_disk_open.c,v 1.11.14.1 2015/04/06 15:18:20 skrll Exp $	*/
      2        1.1     pooka 
      3        1.1     pooka /*-
      4        1.1     pooka  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5        1.1     pooka  * All rights reserved.
      6        1.1     pooka  *
      7        1.1     pooka  * Redistribution and use in source and binary forms, with or without
      8        1.1     pooka  * modification, are permitted provided that the following conditions
      9        1.1     pooka  * are met:
     10        1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     11        1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     12        1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     14        1.1     pooka  *    documentation and/or other materials provided with the distribution.
     15        1.1     pooka  *
     16        1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17        1.1     pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18        1.1     pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19        1.1     pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20        1.1     pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21        1.1     pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22        1.1     pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23        1.1     pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24        1.1     pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25        1.1     pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26        1.1     pooka  * POSSIBILITY OF SUCH DAMAGE.
     27        1.1     pooka  */
     28        1.1     pooka 
     29        1.1     pooka #include <sys/cdefs.h>
     30  1.11.14.1     skrll __KERNEL_RCSID(0, "$NetBSD: subr_disk_open.c,v 1.11.14.1 2015/04/06 15:18:20 skrll Exp $");
     31        1.1     pooka 
     32        1.1     pooka #include <sys/param.h>
     33        1.1     pooka #include <sys/conf.h>
     34        1.1     pooka #include <sys/device.h>
     35        1.1     pooka #include <sys/disk.h>
     36        1.1     pooka #include <sys/disklabel.h>
     37        1.1     pooka #include <sys/fcntl.h>
     38        1.1     pooka #include <sys/kauth.h>
     39        1.1     pooka #include <sys/vnode.h>
     40        1.4  christos #include <miscfs/specfs/specdev.h>
     41        1.1     pooka 
     42        1.1     pooka struct vnode *
     43       1.11       chs opendisk(device_t dv)
     44        1.1     pooka {
     45        1.8  drochner 	devmajor_t bmajor;
     46        1.8  drochner 	int unit;
     47        1.1     pooka 	struct vnode *tmpvn;
     48        1.1     pooka 	int error;
     49        1.1     pooka 	dev_t dev;
     50        1.1     pooka 
     51        1.1     pooka 	/*
     52        1.1     pooka 	 * Lookup major number for disk block device.
     53        1.1     pooka 	 */
     54        1.1     pooka 	bmajor = devsw_name2blk(device_xname(dv), NULL, 0);
     55        1.1     pooka 	if (bmajor == -1)
     56        1.1     pooka 		return NULL;
     57        1.1     pooka 
     58        1.8  drochner 	unit = device_unit(dv);
     59        1.1     pooka 	/*
     60        1.1     pooka 	 * Fake a temporary vnode for the disk, open it, and read
     61        1.1     pooka 	 * and hash the sectors.
     62        1.1     pooka 	 */
     63        1.8  drochner 	dev = device_is_a(dv, "dk") ? makedev(bmajor, unit) :
     64        1.8  drochner 	    MAKEDISKDEV(bmajor, unit, RAW_PART);
     65        1.1     pooka 	if (bdevvp(dev, &tmpvn))
     66        1.1     pooka 		panic("%s: can't alloc vnode for %s", __func__,
     67        1.1     pooka 		    device_xname(dv));
     68        1.3  jmcneill 	error = VOP_OPEN(tmpvn, FREAD | FSILENT, NOCRED);
     69        1.1     pooka 	if (error) {
     70        1.1     pooka 		/*
     71        1.1     pooka 		 * Ignore errors caused by missing device, partition,
     72        1.7  christos 		 * medium, or busy [presumably because of a wedge covering it]
     73        1.1     pooka 		 */
     74        1.7  christos 		switch (error) {
     75        1.7  christos 		case ENXIO:
     76        1.7  christos 		case ENODEV:
     77        1.7  christos 		case EBUSY:
     78        1.7  christos 			break;
     79        1.7  christos 		default:
     80        1.1     pooka 			printf("%s: can't open dev %s (%d)\n",
     81        1.1     pooka 			    __func__, device_xname(dv), error);
     82        1.7  christos 			break;
     83        1.7  christos 		}
     84        1.1     pooka 		vput(tmpvn);
     85        1.1     pooka 		return NULL;
     86        1.1     pooka 	}
     87        1.1     pooka 
     88        1.1     pooka 	return tmpvn;
     89        1.1     pooka }
     90        1.2   mlelstv 
     91        1.2   mlelstv int
     92       1.10   tsutsui getdisksize(struct vnode *vp, uint64_t *numsecp, unsigned int *secsizep)
     93        1.2   mlelstv {
     94        1.2   mlelstv 	struct partinfo dpart;
     95        1.2   mlelstv 	struct dkwedge_info dkw;
     96        1.2   mlelstv 	struct disk *pdk;
     97        1.9   tsutsui 	unsigned int secsize;
     98        1.9   tsutsui 	uint64_t numsec;
     99        1.2   mlelstv 	int error;
    100        1.2   mlelstv 
    101  1.11.14.1     skrll 	/*
    102  1.11.14.1     skrll 	 * We attempt to get the wedge information first if it exists,
    103  1.11.14.1     skrll 	 * because the label does not support larger size disks.
    104  1.11.14.1     skrll 	 */
    105  1.11.14.1     skrll 	error = VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED);
    106        1.2   mlelstv 	if (error == 0) {
    107  1.11.14.1     skrll 		pdk = disk_find(dkw.dkw_parent);
    108  1.11.14.1     skrll 		if (pdk != NULL) {
    109  1.11.14.1     skrll 			secsize = DEV_BSIZE << pdk->dk_blkshift;
    110  1.11.14.1     skrll 			numsec  = dkw.dkw_size;
    111  1.11.14.1     skrll 		} else
    112  1.11.14.1     skrll 			error = ENODEV;
    113  1.11.14.1     skrll 	}
    114  1.11.14.1     skrll 
    115  1.11.14.1     skrll 	if (error) {
    116  1.11.14.1     skrll 		error = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, NOCRED);
    117        1.9   tsutsui 		if (error == 0) {
    118  1.11.14.1     skrll 			secsize = dpart.disklab->d_secsize;
    119  1.11.14.1     skrll 			numsec  = dpart.part->p_size;
    120        1.9   tsutsui 		}
    121        1.2   mlelstv 	}
    122        1.2   mlelstv 
    123        1.9   tsutsui 	if (error == 0 &&
    124        1.9   tsutsui 	    (secsize == 0 || secsize > MAXBSIZE || !powerof2(secsize) ||
    125        1.9   tsutsui 	     numsec == 0)) {
    126        1.9   tsutsui #ifdef DIAGNOSTIC
    127        1.9   tsutsui 		printf("%s: %s returns invalid disksize values"
    128        1.9   tsutsui 		    " (secsize = %u, numsec = %" PRIu64 ")\n",
    129        1.9   tsutsui 		    __func__,
    130        1.9   tsutsui 		    devsw_blk2name(major(vp->v_specnode->sn_rdev)),
    131        1.9   tsutsui 		    secsize, numsec);
    132        1.9   tsutsui #endif
    133        1.9   tsutsui 		error = EINVAL;
    134        1.9   tsutsui 	}
    135        1.2   mlelstv 	if (error == 0) {
    136        1.9   tsutsui 		*secsizep = secsize;
    137        1.9   tsutsui 		*numsecp  = numsec;
    138        1.2   mlelstv 	}
    139        1.2   mlelstv 
    140        1.2   mlelstv 	return error;
    141        1.2   mlelstv }
    142        1.4  christos 
    143        1.4  christos int
    144        1.4  christos getdiskinfo(struct vnode *vp, struct dkwedge_info *dkw)
    145        1.4  christos {
    146        1.4  christos 	struct partinfo dpart;
    147        1.4  christos 	int error;
    148        1.4  christos 	dev_t dev = vp->v_specnode->sn_rdev;
    149        1.4  christos 
    150        1.4  christos 	if (VOP_IOCTL(vp, DIOCGWEDGEINFO, dkw, FREAD, NOCRED) == 0)
    151        1.4  christos 		return 0;
    152        1.4  christos 
    153        1.4  christos 	if ((error = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, NOCRED)) != 0)
    154        1.4  christos 		return error;
    155        1.4  christos 
    156        1.4  christos 	snprintf(dkw->dkw_devname, sizeof(dkw->dkw_devname), "%s%" PRId32 "%c",
    157        1.4  christos 	    devsw_blk2name(major(dev)), DISKUNIT(dev), (char)DISKPART(dev) +
    158        1.4  christos 	    'a');
    159        1.4  christos 
    160        1.4  christos 	dkw->dkw_wname[0] = '\0';
    161        1.4  christos 
    162        1.4  christos 	strlcpy(dkw->dkw_parent, dkw->dkw_devname, sizeof(dkw->dkw_parent));
    163        1.4  christos 
    164        1.4  christos 	dkw->dkw_size = dpart.part->p_size;
    165        1.4  christos 	dkw->dkw_offset = dpart.part->p_offset;
    166        1.4  christos 
    167        1.4  christos 	strlcpy(dkw->dkw_ptype, getfstypename(dpart.part->p_fstype),
    168        1.4  christos 	    sizeof(dkw->dkw_ptype));
    169        1.4  christos 
    170        1.4  christos 	return 0;
    171        1.4  christos }
    172