Home | History | Annotate | Line # | Download | only in fsck
partutil.c revision 1.1
      1  1.1  christos /*	$NetBSD: partutil.c,v 1.1 2006/08/26 21:54:05 christos 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1  christos  *    must display the following acknowledgement:
     20  1.1  christos  *        This product includes software developed by the NetBSD
     21  1.1  christos  *        Foundation, Inc. and its contributors.
     22  1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  christos  *    contributors may be used to endorse or promote products derived
     24  1.1  christos  *    from this software without specific prior written permission.
     25  1.1  christos  *
     26  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  christos  */
     38  1.1  christos 
     39  1.1  christos #include <sys/cdefs.h>
     40  1.1  christos __RCSID("$NetBSD: partutil.c,v 1.1 2006/08/26 21:54:05 christos Exp $");
     41  1.1  christos 
     42  1.1  christos #include <sys/types.h>
     43  1.1  christos #include <sys/disklabel.h>
     44  1.1  christos #include <sys/disk.h>
     45  1.1  christos #include <sys/ioctl.h>
     46  1.1  christos #include <sys/stat.h>
     47  1.1  christos 
     48  1.1  christos #include <disktab.h>
     49  1.1  christos #include <fcntl.h>
     50  1.1  christos #include <util.h>
     51  1.1  christos #include <unistd.h>
     52  1.1  christos #include <err.h>
     53  1.1  christos #include <string.h>
     54  1.1  christos #include <errno.h>
     55  1.1  christos 
     56  1.1  christos #include "partutil.h"
     57  1.1  christos 
     58  1.1  christos static void
     59  1.1  christos label2geom(struct disk_geom *geo, const struct disklabel *lp)
     60  1.1  christos {
     61  1.1  christos 	geo->dg_secperunit = lp->d_secperunit;
     62  1.1  christos 	geo->dg_secsize = lp->d_secsize;
     63  1.1  christos 	geo->dg_nsectors = lp->d_nsectors;
     64  1.1  christos 	geo->dg_ntracks = lp->d_ntracks;
     65  1.1  christos 	geo->dg_ncylinders = lp->d_ncylinders;
     66  1.1  christos 	geo->dg_secpercyl = lp->d_secpercyl;
     67  1.1  christos 	geo->dg_pcylinders = lp->d_ncylinders;
     68  1.1  christos 	geo->dg_sparespertrack = lp->d_sparespertrack;
     69  1.1  christos 	geo->dg_sparespercyl = lp->d_sparespercyl;
     70  1.1  christos 	geo->dg_acylinders = lp->d_acylinders;
     71  1.1  christos }
     72  1.1  christos 
     73  1.1  christos static void
     74  1.1  christos part2wedge(struct dkwedge_info *dkw, const struct disklabel *lp, const char *s)
     75  1.1  christos {
     76  1.1  christos 	struct stat sb;
     77  1.1  christos 	struct partition *pp;
     78  1.1  christos 	int ptn;
     79  1.1  christos 
     80  1.1  christos 	(void)memset(dkw, 0, sizeof(*dkw));
     81  1.1  christos 	if (stat(s, &sb) == -1)
     82  1.1  christos 		return;
     83  1.1  christos 
     84  1.1  christos 	ptn = strchr(s, '\0')[-1] - 'a';
     85  1.1  christos 	if (ptn >= lp->d_npartitions || ptn != DISKPART(sb.st_rdev))
     86  1.1  christos 		return;
     87  1.1  christos 
     88  1.1  christos 	pp = &lp->d_partitions[ptn];
     89  1.1  christos 	dkw->dkw_offset = pp->p_offset;
     90  1.1  christos 	dkw->dkw_size = pp->p_size;
     91  1.1  christos 	dkw->dkw_parent[0] = '*';
     92  1.1  christos 	switch (pp->p_fstype) {
     93  1.1  christos 	default:
     94  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNKNOWN);
     95  1.1  christos 		break;
     96  1.1  christos 	case FS_UNUSED:
     97  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED);
     98  1.1  christos 		break;
     99  1.1  christos 	case FS_SWAP:
    100  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_SWAP);
    101  1.1  christos 		break;
    102  1.1  christos 	case FS_BSDFFS:
    103  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
    104  1.1  christos 		break;
    105  1.1  christos 	case FS_BSDLFS:
    106  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_LFS);
    107  1.1  christos 		break;
    108  1.1  christos 	case FS_EX2FS:
    109  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_EXT2FS);
    110  1.1  christos 		break;
    111  1.1  christos 	case FS_ISO9660:
    112  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_ISO9660);
    113  1.1  christos 		break;
    114  1.1  christos 	case FS_ADOS:
    115  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_AMIGADOS);
    116  1.1  christos 		break;
    117  1.1  christos 	case FS_HFS:
    118  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEHFS);
    119  1.1  christos 		break;
    120  1.1  christos 	case FS_MSDOS:
    121  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FAT);
    122  1.1  christos 		break;
    123  1.1  christos 	case FS_FILECORE:
    124  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FILECORE);
    125  1.1  christos 		break;
    126  1.1  christos 	case FS_APPLEUFS:
    127  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEUFS);
    128  1.1  christos 		break;
    129  1.1  christos 	case FS_NTFS:
    130  1.1  christos 		(void)strcpy(dkw->dkw_ptype, DKW_PTYPE_NTFS);
    131  1.1  christos 		break;
    132  1.1  christos 	}
    133  1.1  christos }
    134  1.1  christos 
    135  1.1  christos int
    136  1.1  christos getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo,
    137  1.1  christos     struct dkwedge_info *dkw)
    138  1.1  christos {
    139  1.1  christos 	struct disklabel lab;
    140  1.1  christos 	struct disklabel *lp = &lab;
    141  1.1  christos 	char parent[1024];
    142  1.1  christos 
    143  1.1  christos 	if (dt) {
    144  1.1  christos 		lp = getdiskbyname(dt);
    145  1.1  christos 		if (lp == NULL)
    146  1.1  christos 			errx(1, "%s: unknown disk type", dt);
    147  1.1  christos 		goto part;
    148  1.1  christos 	}
    149  1.1  christos 
    150  1.1  christos 	if (ioctl(fd, DIOCGDINFO, lp) == -1) {
    151  1.1  christos 		if (errno == ENOTTY) {
    152  1.1  christos 			int pfd;
    153  1.1  christos 			if (ioctl(fd, DIOCGWEDGEINFO, dkw) == -1) {
    154  1.1  christos 				warn("ioctl (DIOCGWEDGEINFO)");
    155  1.1  christos 				goto bad;
    156  1.1  christos 			}
    157  1.1  christos 			pfd = opendisk(dkw->dkw_parent, O_RDONLY,
    158  1.1  christos 			    parent, sizeof(parent), 0);
    159  1.1  christos 			if (pfd == -1) {
    160  1.1  christos 				warn("Cannot open `%s'", dkw->dkw_parent);
    161  1.1  christos 				goto bad;
    162  1.1  christos 			}
    163  1.1  christos 			if (ioctl(pfd, DIOCGDINFO, lp) != -1) {
    164  1.1  christos 				(void)close(pfd);
    165  1.1  christos 				goto label;
    166  1.1  christos 			} else {
    167  1.1  christos 				int serrno = errno;
    168  1.1  christos 				(void)close(pfd);
    169  1.1  christos 				errno = serrno;
    170  1.1  christos 			}
    171  1.1  christos 		}
    172  1.1  christos 		warn("ioctl (DIOCGDINFO)");
    173  1.1  christos 		goto bad;
    174  1.1  christos 	}
    175  1.1  christos part:
    176  1.1  christos 	part2wedge(dkw, lp, s);
    177  1.1  christos label:
    178  1.1  christos 	label2geom(geo, lp);
    179  1.1  christos 	return 0;
    180  1.1  christos bad:
    181  1.1  christos 	return -1;
    182  1.1  christos }
    183