Home | History | Annotate | Line # | Download | only in kern
      1  1.15   mlelstv /*	$NetBSD: subr_disk_open.c,v 1.15 2020/02/29 14:44:44 mlelstv 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.15   mlelstv __KERNEL_RCSID(0, "$NetBSD: subr_disk_open.c,v 1.15 2020/02/29 14:44:44 mlelstv 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.14   hannken 	vn_lock(tmpvn, LK_EXCLUSIVE | LK_RETRY);
     69   1.3  jmcneill 	error = VOP_OPEN(tmpvn, FREAD | FSILENT, NOCRED);
     70   1.1     pooka 	if (error) {
     71   1.1     pooka 		/*
     72   1.1     pooka 		 * Ignore errors caused by missing device, partition,
     73   1.7  christos 		 * medium, or busy [presumably because of a wedge covering it]
     74   1.1     pooka 		 */
     75   1.7  christos 		switch (error) {
     76   1.7  christos 		case ENXIO:
     77   1.7  christos 		case ENODEV:
     78   1.7  christos 		case EBUSY:
     79   1.7  christos 			break;
     80   1.7  christos 		default:
     81   1.1     pooka 			printf("%s: can't open dev %s (%d)\n",
     82   1.1     pooka 			    __func__, device_xname(dv), error);
     83   1.7  christos 			break;
     84   1.7  christos 		}
     85   1.1     pooka 		vput(tmpvn);
     86   1.1     pooka 		return NULL;
     87   1.1     pooka 	}
     88   1.1     pooka 
     89   1.1     pooka 	return tmpvn;
     90   1.1     pooka }
     91   1.2   mlelstv 
     92   1.2   mlelstv int
     93  1.10   tsutsui getdisksize(struct vnode *vp, uint64_t *numsecp, unsigned int *secsizep)
     94   1.2   mlelstv {
     95  1.13  christos 	struct partinfo pi;
     96   1.2   mlelstv 	struct dkwedge_info dkw;
     97   1.2   mlelstv 	struct disk *pdk;
     98   1.9   tsutsui 	unsigned int secsize;
     99   1.9   tsutsui 	uint64_t numsec;
    100   1.2   mlelstv 	int error;
    101   1.2   mlelstv 
    102  1.12  christos 	/*
    103  1.12  christos 	 * We attempt to get the wedge information first if it exists,
    104  1.12  christos 	 * because the label does not support larger size disks.
    105  1.12  christos 	 */
    106  1.12  christos 	error = VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED);
    107   1.2   mlelstv 	if (error == 0) {
    108  1.12  christos 		pdk = disk_find(dkw.dkw_parent);
    109  1.12  christos 		if (pdk != NULL) {
    110  1.12  christos 			secsize = DEV_BSIZE << pdk->dk_blkshift;
    111  1.12  christos 			numsec  = dkw.dkw_size;
    112  1.12  christos 		} else
    113  1.12  christos 			error = ENODEV;
    114  1.12  christos 	}
    115  1.12  christos 
    116  1.12  christos 	if (error) {
    117  1.13  christos 		error = VOP_IOCTL(vp, DIOCGPARTINFO, &pi, FREAD, NOCRED);
    118   1.9   tsutsui 		if (error == 0) {
    119  1.13  christos 			secsize = pi.pi_secsize;
    120  1.13  christos 			numsec  = pi.pi_size;
    121   1.9   tsutsui 		}
    122   1.2   mlelstv 	}
    123   1.2   mlelstv 
    124   1.9   tsutsui 	if (error == 0 &&
    125   1.9   tsutsui 	    (secsize == 0 || secsize > MAXBSIZE || !powerof2(secsize) ||
    126   1.9   tsutsui 	     numsec == 0)) {
    127   1.9   tsutsui #ifdef DIAGNOSTIC
    128   1.9   tsutsui 		printf("%s: %s returns invalid disksize values"
    129   1.9   tsutsui 		    " (secsize = %u, numsec = %" PRIu64 ")\n",
    130   1.9   tsutsui 		    __func__,
    131   1.9   tsutsui 		    devsw_blk2name(major(vp->v_specnode->sn_rdev)),
    132   1.9   tsutsui 		    secsize, numsec);
    133   1.9   tsutsui #endif
    134   1.9   tsutsui 		error = EINVAL;
    135   1.9   tsutsui 	}
    136   1.2   mlelstv 	if (error == 0) {
    137   1.9   tsutsui 		*secsizep = secsize;
    138   1.9   tsutsui 		*numsecp  = numsec;
    139   1.2   mlelstv 	}
    140   1.2   mlelstv 
    141   1.2   mlelstv 	return error;
    142   1.2   mlelstv }
    143   1.4  christos 
    144   1.4  christos int
    145   1.4  christos getdiskinfo(struct vnode *vp, struct dkwedge_info *dkw)
    146   1.4  christos {
    147  1.13  christos 	struct partinfo pi;
    148   1.4  christos 	int error;
    149   1.4  christos 	dev_t dev = vp->v_specnode->sn_rdev;
    150   1.4  christos 
    151   1.4  christos 	if (VOP_IOCTL(vp, DIOCGWEDGEINFO, dkw, FREAD, NOCRED) == 0)
    152   1.4  christos 		return 0;
    153   1.4  christos 
    154  1.13  christos 	if ((error = VOP_IOCTL(vp, DIOCGPARTINFO, &pi, FREAD, NOCRED)) != 0)
    155   1.4  christos 		return error;
    156   1.4  christos 
    157   1.4  christos 	snprintf(dkw->dkw_devname, sizeof(dkw->dkw_devname), "%s%" PRId32 "%c",
    158   1.4  christos 	    devsw_blk2name(major(dev)), DISKUNIT(dev), (char)DISKPART(dev) +
    159   1.4  christos 	    'a');
    160   1.4  christos 
    161   1.4  christos 	dkw->dkw_wname[0] = '\0';
    162   1.4  christos 
    163  1.15   mlelstv 	snprintf(dkw->dkw_parent, sizeof(dkw->dkw_parent), "%s%" PRId32,
    164  1.15   mlelstv 	    devsw_blk2name(major(dev)), DISKUNIT(dev));
    165   1.4  christos 
    166  1.13  christos 	dkw->dkw_size = pi.pi_size;
    167  1.13  christos 	dkw->dkw_offset = pi.pi_offset;
    168  1.13  christos 	strlcpy(dkw->dkw_ptype, getfstypename(pi.pi_fstype),
    169   1.4  christos 	    sizeof(dkw->dkw_ptype));
    170   1.4  christos 
    171   1.4  christos 	return 0;
    172   1.4  christos }
    173