Home | History | Annotate | Line # | Download | only in fsck
partutil.c revision 1.4
      1 /*	$NetBSD: partutil.c,v 1.4 2009/04/11 06:48:36 lukem 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.4 2009/04/11 06:48:36 lukem 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 #include <disktab.h>
     42 #include <fcntl.h>
     43 #include <util.h>
     44 #include <unistd.h>
     45 #include <err.h>
     46 #include <string.h>
     47 #include <errno.h>
     48 
     49 #include "partutil.h"
     50 
     51 static void
     52 label2geom(struct disk_geom *geo, const struct disklabel *lp)
     53 {
     54 	geo->dg_secperunit = lp->d_secperunit;
     55 	geo->dg_secsize = lp->d_secsize;
     56 	geo->dg_nsectors = lp->d_nsectors;
     57 	geo->dg_ntracks = lp->d_ntracks;
     58 	geo->dg_ncylinders = lp->d_ncylinders;
     59 	geo->dg_secpercyl = lp->d_secpercyl;
     60 	geo->dg_pcylinders = lp->d_ncylinders;
     61 	geo->dg_sparespertrack = lp->d_sparespertrack;
     62 	geo->dg_sparespercyl = lp->d_sparespercyl;
     63 	geo->dg_acylinders = lp->d_acylinders;
     64 }
     65 
     66 static void
     67 part2wedge(struct dkwedge_info *dkw, const struct disklabel *lp, const char *s)
     68 {
     69 	struct stat sb;
     70 	const struct partition *pp;
     71 	int ptn;
     72 
     73 	(void)memset(dkw, 0, sizeof(*dkw));
     74 	if (stat(s, &sb) == -1)
     75 		return;
     76 
     77 	ptn = strchr(s, '\0')[-1] - 'a';
     78 	if ((unsigned)ptn >= lp->d_npartitions ||
     79 	    (devminor_t)ptn != DISKPART(sb.st_rdev))
     80 		return;
     81 
     82 	pp = &lp->d_partitions[ptn];
     83 	dkw->dkw_offset = pp->p_offset;
     84 	dkw->dkw_size = pp->p_size;
     85 	dkw->dkw_parent[0] = '*';
     86 	switch (pp->p_fstype) {
     87 	default:
     88 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNKNOWN);
     89 		break;
     90 	case FS_UNUSED:
     91 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED);
     92 		break;
     93 	case FS_SWAP:
     94 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_SWAP);
     95 		break;
     96 	case FS_BSDFFS:
     97 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
     98 		break;
     99 	case FS_BSDLFS:
    100 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_LFS);
    101 		break;
    102 	case FS_EX2FS:
    103 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_EXT2FS);
    104 		break;
    105 	case FS_ISO9660:
    106 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_ISO9660);
    107 		break;
    108 	case FS_ADOS:
    109 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_AMIGADOS);
    110 		break;
    111 	case FS_HFS:
    112 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEHFS);
    113 		break;
    114 	case FS_MSDOS:
    115 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FAT);
    116 		break;
    117 	case FS_FILECORE:
    118 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FILECORE);
    119 		break;
    120 	case FS_APPLEUFS:
    121 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEUFS);
    122 		break;
    123 	case FS_NTFS:
    124 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_NTFS);
    125 		break;
    126 	}
    127 }
    128 
    129 int
    130 getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo,
    131     struct dkwedge_info *dkw)
    132 {
    133 	struct disklabel lab;
    134 	struct disklabel *lp = &lab;
    135 	char parent[1024];
    136 
    137 	if (dt) {
    138 		lp = getdiskbyname(dt);
    139 		if (lp == NULL)
    140 			errx(1, "%s: unknown disk type", dt);
    141 		goto part;
    142 	}
    143 
    144 	if (ioctl(fd, DIOCGDINFO, lp) == -1) {
    145 		if (errno == ENOTTY) {
    146 			int pfd;
    147 			if (ioctl(fd, DIOCGWEDGEINFO, dkw) == -1) {
    148 				warn("ioctl (DIOCGWEDGEINFO)");
    149 				goto bad;
    150 			}
    151 			pfd = opendisk(dkw->dkw_parent, O_RDONLY,
    152 			    parent, sizeof(parent), 0);
    153 			if (pfd == -1) {
    154 				warn("Cannot open `%s'", dkw->dkw_parent);
    155 				goto bad;
    156 			}
    157 			if (ioctl(pfd, DIOCGDINFO, lp) != -1) {
    158 				(void)close(pfd);
    159 				goto label;
    160 			} else {
    161 				int serrno = errno;
    162 				(void)close(pfd);
    163 				errno = serrno;
    164 			}
    165 		}
    166 		warn("ioctl (DIOCGDINFO)");
    167 		goto bad;
    168 	}
    169 part:
    170 	part2wedge(dkw, lp, s);
    171 label:
    172 	label2geom(geo, lp);
    173 	return 0;
    174 bad:
    175 	return -1;
    176 }
    177