xref: /openbsd/sbin/fdisk/disk.c (revision cca36db2)
1 /*	$OpenBSD: disk.c,v 1.32 2011/07/05 21:39:09 krw Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 2001 Tobias Weingartner
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <err.h>
29 #include <util.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/fcntl.h>
34 #include <sys/ioctl.h>
35 #include <sys/dkio.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/disklabel.h>
39 #include <sys/param.h>
40 #include "disk.h"
41 #include "misc.h"
42 
43 struct disklabel dl;
44 
45 DISK_metrics *DISK_getlabelmetrics(char *name);
46 
47 int
48 DISK_open(char *disk, int mode)
49 {
50 	int fd;
51 	struct stat st;
52 
53 	fd = opendev(disk, mode, OPENDEV_PART, NULL);
54 	if (fd == -1)
55 		err(1, "%s", disk);
56 	if (fstat(fd, &st) == -1)
57 		err(1, "%s", disk);
58 	if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode))
59 		err(1, "%s is not a character device or a regular file", disk);
60 	return (fd);
61 }
62 
63 /* Routine to go after the disklabel for geometry
64  * information.  This should work everywhere, but
65  * in the land of PC, things are not always what
66  * they seem.
67  */
68 DISK_metrics *
69 DISK_getlabelmetrics(char *name)
70 {
71 	DISK_metrics *lm = NULL;
72 	int fd;
73 
74 	/* Get label metrics */
75 	if ((fd = DISK_open(name, O_RDONLY)) != -1) {
76 		lm = malloc(sizeof(DISK_metrics));
77 		if (lm == NULL)
78 			err(1, NULL);
79 
80 		if (ioctl(fd, DIOCGPDINFO, &dl) == -1) {
81 			warn("DIOCGPDINFO");
82 			free(lm);
83 			lm = NULL;
84 		} else {
85 			lm->cylinders = dl.d_ncylinders;
86 			lm->heads = dl.d_ntracks;
87 			lm->sectors = dl.d_nsectors;
88 			lm->size = dl.d_secperunit;
89 			unit_types[SECTORS].conversion = dl.d_secsize;
90 		}
91 		close(fd);
92 	}
93 
94 	return (lm);
95 }
96 
97 /* This is ugly, and convoluted.  All the magic
98  * for disk geo/size happens here.  Basically,
99  * the real size is the one we will use in the
100  * rest of the program, the label size is what we
101  * got from the disklabel.  If the disklabel fails,
102  * we assume we are working with a normal file,
103  * and should request the user to specify the
104  * geometry he/she wishes to use.
105  */
106 int
107 DISK_getmetrics(disk_t *disk, DISK_metrics *user)
108 {
109 
110 	disk->label = DISK_getlabelmetrics(disk->name);
111 
112 	/* If user supplied, use that */
113 	if (user) {
114 		disk->real = user;
115 		return (0);
116 	}
117 
118 	/* If we have a label, use that */
119 	if (disk->label) {
120 		disk->real = disk->label;
121 		return (0);
122 	}
123 
124 	/* Can not get geometry, punt */
125 	disk->real = NULL;
126 	return (1);
127 }
128 
129 /*
130  * Print the disk geometry information. Take an optional modifier
131  * to indicate the units that should be used for display.
132  */
133 int
134 DISK_printmetrics(disk_t *disk, char *units)
135 {
136 	const int secsize = unit_types[SECTORS].conversion;
137 	double size;
138 	int i;
139 
140 	i = unit_lookup(units);
141 	size = ((double)disk->real->size * secsize) / unit_types[i].conversion;
142 	printf("Disk: %s\t", disk->name);
143 	if (disk->real) {
144 		printf("geometry: %d/%d/%d [%.0f ",
145 		    disk->real->cylinders, disk->real->heads,
146 		    disk->real->sectors, size);
147 		if (i == SECTORS && secsize != DEV_BSIZE)
148 			printf("%d-byte ", secsize);
149 		printf("%s]\n", unit_types[i].lname);
150 	} else
151 		printf("geometry: <none>\n");
152 
153 	return (0);
154 }
155 
156