Home | History | Annotate | Line # | Download | only in fsck
partutil.c revision 1.5
      1 /*	$NetBSD: partutil.c,v 1.5 2009/06/05 21:52:31 haad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __RCSID("$NetBSD: partutil.c,v 1.5 2009/06/05 21:52:31 haad Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/disklabel.h>
     37 #include <sys/disk.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/stat.h>
     40 
     41 
     42 #include <disktab.h>
     43 #include <err.h>
     44 #include <errno.h>
     45 #include <fcntl.h>
     46 #include <util.h>
     47 #include <unistd.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 
     51 #include <prop/proplib.h>
     52 
     53 #include "partutil.h"
     54 
     55 
     56 /*
     57  * Set what we need to know about disk geometry.
     58  */
     59 static void
     60 dict2geom(struct disk_geom *geo, prop_dictionary_t dict)
     61 {
     62 	memset(geo, 0, sizeof(struct disk_geom));
     63 	prop_dictionary_get_int64(dict, "sectors-per-unit", &geo->dg_secperunit);
     64 	prop_dictionary_get_uint32(dict, "sector-size", &geo->dg_secsize);
     65 	prop_dictionary_get_uint32(dict, "sectors-per-track", &geo->dg_nsectors);
     66 	prop_dictionary_get_uint32(dict, "tracks-per-cylinder", &geo->dg_ntracks);
     67 	prop_dictionary_get_uint32(dict, "cylinders-per-unit", &geo->dg_ncylinders);
     68 }
     69 
     70 
     71 static void
     72 part2wedge(struct dkwedge_info *dkw, const struct disklabel *lp, const char *s)
     73 {
     74 	struct stat sb;
     75 	const struct partition *pp;
     76 	int ptn;
     77 
     78 	(void)memset(dkw, 0, sizeof(*dkw));
     79 	if (stat(s, &sb) == -1)
     80 		return;
     81 
     82 	ptn = strchr(s, '\0')[-1] - 'a';
     83 	if ((unsigned)ptn >= lp->d_npartitions ||
     84 	    (devminor_t)ptn != DISKPART(sb.st_rdev))
     85 		return;
     86 
     87 	pp = &lp->d_partitions[ptn];
     88 	dkw->dkw_offset = pp->p_offset;
     89 	dkw->dkw_size = pp->p_size;
     90 	dkw->dkw_parent[0] = '*';
     91 	switch (pp->p_fstype) {
     92 	default:
     93 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNKNOWN);
     94 		break;
     95 	case FS_UNUSED:
     96 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED);
     97 		break;
     98 	case FS_SWAP:
     99 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_SWAP);
    100 		break;
    101 	case FS_BSDFFS:
    102 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
    103 		break;
    104 	case FS_BSDLFS:
    105 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_LFS);
    106 		break;
    107 	case FS_EX2FS:
    108 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_EXT2FS);
    109 		break;
    110 	case FS_ISO9660:
    111 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_ISO9660);
    112 		break;
    113 	case FS_ADOS:
    114 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_AMIGADOS);
    115 		break;
    116 	case FS_HFS:
    117 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEHFS);
    118 		break;
    119 	case FS_MSDOS:
    120 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FAT);
    121 		break;
    122 	case FS_FILECORE:
    123 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FILECORE);
    124 		break;
    125 	case FS_APPLEUFS:
    126 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEUFS);
    127 		break;
    128 	case FS_NTFS:
    129 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_NTFS);
    130 		break;
    131 	}
    132 }
    133 
    134 int
    135 getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo,
    136     struct dkwedge_info *dkw)
    137 {
    138 	struct disklabel lab;
    139 	struct disklabel *lp = &lab;
    140 	prop_dictionary_t disk_dict, geom_dict;
    141 
    142 	if (dt) {
    143 		lp = getdiskbyname(dt);
    144 		if (lp == NULL)
    145 			errx(1, "%s: unknown disk type", dt);
    146 	}
    147 
    148 	/* Get disk description dictionary */
    149 	if (prop_dictionary_recv_ioctl(fd, DIOCGDISKINFO, &disk_dict) != 0) {
    150 		warn("Please implement DIOCGDISKINFO for %s\n disk driver\n", s);
    151 		return (errno);
    152 	}
    153 
    154 	geom_dict = prop_dictionary_get(disk_dict, "geometry");
    155 	dict2geom(geo, geom_dict);
    156 
    157 	/* Get info about partition/wedge */
    158 	if (ioctl(fd, DIOCGWEDGEINFO, dkw) == -1) {
    159 		warn("ioctl (DIOCGWEDGEINFO)");
    160 		printf("Using old disklabel method\n");
    161 		if (ioctl(fd, DIOCGDINFO, lp) == -1)
    162 			errx(errno, "Please implement DIOCGWEDGEINFO or DIOCGDINFO for disk device %s\n", s);
    163 
    164 		part2wedge(dkw, lp, s);
    165 	}
    166 
    167 	return 0;
    168 }
    169