Home | History | Annotate | Line # | Download | only in fsck
partutil.c revision 1.17
      1  1.17       bad /*	$NetBSD: partutil.c,v 1.17 2019/09/28 18:03:18 bad Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5   1.1  christos  * All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  christos  * by Christos Zoulas.
      9   1.1  christos  *
     10   1.1  christos  * Redistribution and use in source and binary forms, with or without
     11   1.1  christos  * modification, are permitted provided that the following conditions
     12   1.1  christos  * are met:
     13   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  christos  *    documentation and/or other materials provided with the distribution.
     18   1.1  christos  *
     19   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  christos  */
     31   1.1  christos 
     32   1.1  christos #include <sys/cdefs.h>
     33  1.17       bad __RCSID("$NetBSD: partutil.c,v 1.17 2019/09/28 18:03:18 bad Exp $");
     34   1.1  christos 
     35   1.1  christos #include <sys/types.h>
     36  1.13  christos #include <sys/param.h>
     37   1.1  christos #include <sys/disklabel.h>
     38   1.1  christos #include <sys/disk.h>
     39   1.1  christos #include <sys/ioctl.h>
     40   1.1  christos #include <sys/stat.h>
     41   1.1  christos 
     42   1.5      haad 
     43   1.1  christos #include <disktab.h>
     44   1.5      haad #include <err.h>
     45   1.5      haad #include <errno.h>
     46   1.1  christos #include <fcntl.h>
     47   1.1  christos #include <util.h>
     48   1.1  christos #include <unistd.h>
     49   1.5      haad #include <stdlib.h>
     50   1.1  christos #include <string.h>
     51   1.5      haad 
     52   1.5      haad #include <prop/proplib.h>
     53   1.1  christos 
     54   1.1  christos #include "partutil.h"
     55   1.1  christos 
     56   1.7      haad /*
     57   1.7      haad  * Convert disklabel geometry info to disk_geom.
     58   1.7      haad  */
     59   1.7      haad static void
     60   1.7      haad label2geom(struct disk_geom *geo, const struct disklabel *lp)
     61   1.7      haad {
     62   1.7      haad 	geo->dg_secperunit = lp->d_secperunit;
     63   1.7      haad 	geo->dg_secsize = lp->d_secsize;
     64   1.7      haad 	geo->dg_nsectors = lp->d_nsectors;
     65   1.7      haad 	geo->dg_ntracks = lp->d_ntracks;
     66   1.7      haad 	geo->dg_ncylinders = lp->d_ncylinders;
     67   1.7      haad 	geo->dg_secpercyl = lp->d_secpercyl;
     68   1.7      haad 	geo->dg_pcylinders = lp->d_ncylinders;
     69   1.7      haad 	geo->dg_sparespertrack = lp->d_sparespertrack;
     70   1.7      haad 	geo->dg_sparespercyl = lp->d_sparespercyl;
     71   1.7      haad 	geo->dg_acylinders = lp->d_acylinders;
     72   1.7      haad }
     73   1.5      haad 
     74   1.5      haad /*
     75   1.5      haad  * Set what we need to know about disk geometry.
     76   1.5      haad  */
     77   1.1  christos static void
     78   1.5      haad dict2geom(struct disk_geom *geo, prop_dictionary_t dict)
     79   1.1  christos {
     80  1.10  christos 	(void)memset(geo, 0, sizeof(struct disk_geom));
     81  1.10  christos 	prop_dictionary_get_int64(dict, "sectors-per-unit",
     82  1.10  christos 	    &geo->dg_secperunit);
     83   1.5      haad 	prop_dictionary_get_uint32(dict, "sector-size", &geo->dg_secsize);
     84  1.10  christos 	prop_dictionary_get_uint32(dict, "sectors-per-track",
     85  1.10  christos 	    &geo->dg_nsectors);
     86  1.10  christos 	prop_dictionary_get_uint32(dict, "tracks-per-cylinder",
     87  1.10  christos 	    &geo->dg_ntracks);
     88  1.10  christos 	prop_dictionary_get_uint32(dict, "cylinders-per-unit",
     89  1.10  christos 	    &geo->dg_ncylinders);
     90   1.1  christos }
     91   1.1  christos 
     92   1.5      haad 
     93   1.1  christos int
     94   1.1  christos getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo,
     95   1.1  christos     struct dkwedge_info *dkw)
     96   1.1  christos {
     97   1.1  christos 	struct disklabel lab;
     98   1.1  christos 	struct disklabel *lp = &lab;
     99   1.5      haad 	prop_dictionary_t disk_dict, geom_dict;
    100  1.12  jakllsch 	struct stat sb;
    101  1.12  jakllsch 	const struct partition *pp;
    102  1.15    martin 	int ptn, error;
    103   1.1  christos 
    104   1.1  christos 	if (dt) {
    105   1.1  christos 		lp = getdiskbyname(dt);
    106   1.1  christos 		if (lp == NULL)
    107  1.10  christos 			errx(1, "unknown disk type `%s'", dt);
    108   1.1  christos 	}
    109   1.1  christos 
    110   1.5      haad 	/* Get disk description dictionary */
    111  1.17       bad 	disk_dict = NULL;
    112  1.15    martin 	error = prop_dictionary_recv_ioctl(fd, DIOCGDISKINFO, &disk_dict);
    113  1.15    martin 
    114  1.15    martin 	/* fail quickly if the device does not exist at all */
    115  1.15    martin 	if (error == ENXIO)
    116  1.15    martin 		return -1;
    117  1.15    martin 
    118  1.15    martin 	if (error) {
    119   1.7      haad 		/*
    120   1.7      haad 		 * Ask for disklabel if DIOCGDISKINFO failed. This is
    121   1.7      haad 		 * compatibility call and can be removed when all devices
    122   1.7      haad 		 * will support DIOCGDISKINFO.
    123   1.8      haad 		 * cgd, ccd pseudo disk drives doesn't support DIOCGDDISKINFO
    124   1.7      haad 		 */
    125   1.9    dyoung 		if (ioctl(fd, DIOCGDINFO, lp) == -1) {
    126  1.15    martin 			if (errno != ENXIO)
    127  1.15    martin 				warn("DIOCGDINFO on %s failed", s);
    128   1.9    dyoung 			return -1;
    129   1.7      haad 		}
    130   1.7      haad 		label2geom(geo, lp);
    131   1.7      haad 	} else {
    132   1.7      haad 		geom_dict = prop_dictionary_get(disk_dict, "geometry");
    133   1.7      haad 		dict2geom(geo, geom_dict);
    134   1.1  christos 	}
    135  1.16    martin 	if (disk_dict != NULL)
    136  1.16    martin 		prop_object_release(disk_dict);
    137  1.12  jakllsch 
    138  1.14  christos 	if (dkw == NULL)
    139  1.14  christos 		return 0;
    140  1.14  christos 
    141   1.5      haad 	/* Get info about partition/wedge */
    142  1.12  jakllsch 	if (ioctl(fd, DIOCGWEDGEINFO, dkw) != -1) {
    143  1.12  jakllsch 		/* DIOCGWEDGEINFO didn't fail, we're done */
    144  1.12  jakllsch 		return 0;
    145  1.12  jakllsch 	}
    146  1.12  jakllsch 
    147  1.12  jakllsch 	if (ioctl(fd, DIOCGDINFO, lp) == -1) {
    148  1.12  jakllsch 		err(1, "Please implement DIOCGWEDGEINFO or "
    149  1.12  jakllsch 		    "DIOCGDINFO for disk device %s", s);
    150  1.12  jakllsch 	}
    151  1.12  jakllsch 
    152  1.12  jakllsch 	/* DIOCGDINFO didn't fail */
    153  1.12  jakllsch 	(void)memset(dkw, 0, sizeof(*dkw));
    154  1.12  jakllsch 
    155  1.12  jakllsch 	if (stat(s, &sb) == -1)
    156  1.12  jakllsch 		return 0;
    157  1.12  jakllsch 
    158  1.12  jakllsch 	ptn = strchr(s, '\0')[-1] - 'a';
    159  1.12  jakllsch 	if ((unsigned)ptn >= lp->d_npartitions ||
    160  1.12  jakllsch 	    (devminor_t)ptn != DISKPART(sb.st_rdev))
    161  1.12  jakllsch 		return 0;
    162   1.5      haad 
    163  1.12  jakllsch 	pp = &lp->d_partitions[ptn];
    164  1.12  jakllsch 	if (ptn != getrawpartition()) {
    165  1.12  jakllsch 		dkw->dkw_offset = pp->p_offset;
    166  1.12  jakllsch 		dkw->dkw_size = pp->p_size;
    167  1.12  jakllsch 	} else {
    168  1.12  jakllsch 		dkw->dkw_offset = 0;
    169  1.12  jakllsch 		dkw->dkw_size = geo->dg_secperunit;
    170   1.5      haad 	}
    171  1.12  jakllsch 	dkw->dkw_parent[0] = '*';
    172  1.12  jakllsch 	strlcpy(dkw->dkw_ptype, getfstypename(pp->p_fstype),
    173  1.12  jakllsch 	    sizeof(dkw->dkw_ptype));
    174   1.5      haad 
    175   1.1  christos 	return 0;
    176   1.1  christos }
    177  1.13  christos 
    178  1.13  christos int
    179  1.13  christos getdisksize(const char *name, u_int *secsize, off_t *mediasize)
    180  1.13  christos {
    181  1.13  christos 	char buf[MAXPATHLEN];
    182  1.13  christos 	struct disk_geom geo;
    183  1.13  christos 	int fd, error;
    184  1.13  christos 
    185  1.13  christos 	if ((fd = opendisk(name, O_RDONLY, buf, sizeof(buf), 0)) == -1)
    186  1.13  christos 		return -1;
    187  1.13  christos 
    188  1.13  christos 	error = getdiskinfo(name, fd, NULL, &geo, NULL);
    189  1.13  christos 	close(fd);
    190  1.13  christos 	if (error)
    191  1.13  christos 		return error;
    192  1.13  christos 
    193  1.13  christos 	*secsize = geo.dg_secsize;
    194  1.13  christos 	*mediasize = geo.dg_secsize * geo.dg_secperunit;
    195  1.13  christos 	return 0;
    196  1.13  christos }
    197