Home | History | Annotate | Line # | Download | only in kern
subr_disk_open.c revision 1.11
      1  1.11       chs /*	$NetBSD: subr_disk_open.c,v 1.11 2012/10/27 17:18:39 chs 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       chs __KERNEL_RCSID(0, "$NetBSD: subr_disk_open.c,v 1.11 2012/10/27 17:18:39 chs 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.2   mlelstv 	error = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, NOCRED);
    102   1.2   mlelstv 	if (error == 0) {
    103   1.9   tsutsui 		secsize = dpart.disklab->d_secsize;
    104   1.9   tsutsui 		numsec  = dpart.part->p_size;
    105   1.9   tsutsui 	} else {
    106   1.9   tsutsui 		error = VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED);
    107   1.9   tsutsui 		if (error == 0) {
    108   1.9   tsutsui 			pdk = disk_find(dkw.dkw_parent);
    109   1.9   tsutsui 			if (pdk != NULL) {
    110   1.9   tsutsui 				secsize = DEV_BSIZE << pdk->dk_blkshift;
    111   1.9   tsutsui 				numsec  = dkw.dkw_size;
    112   1.9   tsutsui 			} else
    113   1.9   tsutsui 				error = ENODEV;
    114   1.9   tsutsui 		}
    115   1.2   mlelstv 	}
    116   1.2   mlelstv 
    117   1.9   tsutsui 	if (error == 0 &&
    118   1.9   tsutsui 	    (secsize == 0 || secsize > MAXBSIZE || !powerof2(secsize) ||
    119   1.9   tsutsui 	     numsec == 0)) {
    120   1.9   tsutsui #ifdef DIAGNOSTIC
    121   1.9   tsutsui 		printf("%s: %s returns invalid disksize values"
    122   1.9   tsutsui 		    " (secsize = %u, numsec = %" PRIu64 ")\n",
    123   1.9   tsutsui 		    __func__,
    124   1.9   tsutsui 		    devsw_blk2name(major(vp->v_specnode->sn_rdev)),
    125   1.9   tsutsui 		    secsize, numsec);
    126   1.9   tsutsui #endif
    127   1.9   tsutsui 		error = EINVAL;
    128   1.9   tsutsui 	}
    129   1.2   mlelstv 	if (error == 0) {
    130   1.9   tsutsui 		*secsizep = secsize;
    131   1.9   tsutsui 		*numsecp  = numsec;
    132   1.2   mlelstv 	}
    133   1.2   mlelstv 
    134   1.2   mlelstv 	return error;
    135   1.2   mlelstv }
    136   1.4  christos 
    137   1.4  christos int
    138   1.4  christos getdiskinfo(struct vnode *vp, struct dkwedge_info *dkw)
    139   1.4  christos {
    140   1.4  christos 	struct partinfo dpart;
    141   1.4  christos 	int error;
    142   1.4  christos 	dev_t dev = vp->v_specnode->sn_rdev;
    143   1.4  christos 
    144   1.4  christos 	if (VOP_IOCTL(vp, DIOCGWEDGEINFO, dkw, FREAD, NOCRED) == 0)
    145   1.4  christos 		return 0;
    146   1.4  christos 
    147   1.4  christos 	if ((error = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, NOCRED)) != 0)
    148   1.4  christos 		return error;
    149   1.4  christos 
    150   1.4  christos 	snprintf(dkw->dkw_devname, sizeof(dkw->dkw_devname), "%s%" PRId32 "%c",
    151   1.4  christos 	    devsw_blk2name(major(dev)), DISKUNIT(dev), (char)DISKPART(dev) +
    152   1.4  christos 	    'a');
    153   1.4  christos 
    154   1.4  christos 	dkw->dkw_wname[0] = '\0';
    155   1.4  christos 
    156   1.4  christos 	strlcpy(dkw->dkw_parent, dkw->dkw_devname, sizeof(dkw->dkw_parent));
    157   1.4  christos 
    158   1.4  christos 	dkw->dkw_size = dpart.part->p_size;
    159   1.4  christos 	dkw->dkw_offset = dpart.part->p_offset;
    160   1.4  christos 
    161   1.4  christos 	strlcpy(dkw->dkw_ptype, getfstypename(dpart.part->p_fstype),
    162   1.4  christos 	    sizeof(dkw->dkw_ptype));
    163   1.4  christos 
    164   1.4  christos 	return 0;
    165   1.4  christos }
    166