xref: /openbsd-src/sbin/fdisk/disk.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: disk.c,v 1.28 2007/04/27 11:42:44 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/types.h>
36 #include <sys/stat.h>
37 #include <sys/disklabel.h>
38 #include <sys/param.h>
39 #include "disk.h"
40 #include "misc.h"
41 
42 DISK_metrics *DISK_getlabelmetrics(char *name);
43 
44 int
45 DISK_open(char *disk, int mode)
46 {
47 	int fd;
48 	struct stat st;
49 
50 	fd = opendev(disk, mode, OPENDEV_PART, NULL);
51 	if (fd == -1)
52 		err(1, "%s", disk);
53 	if (fstat(fd, &st) == -1)
54 		err(1, "%s", disk);
55 	if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode))
56 		err(1, "%s is not a character device or a regular file", disk);
57 	return (fd);
58 }
59 
60 int
61 DISK_close(int fd)
62 {
63 
64 	return (close(fd));
65 }
66 
67 /* Routine to go after the disklabel for geometry
68  * information.  This should work everywhere, but
69  * in the land of PC, things are not always what
70  * they seem.
71  */
72 DISK_metrics *
73 DISK_getlabelmetrics(char *name)
74 {
75 	DISK_metrics *lm = NULL;
76 	struct disklabel dl;
77 	int fd;
78 
79 	/* Get label metrics */
80 	if ((fd = DISK_open(name, O_RDONLY)) != -1) {
81 		lm = malloc(sizeof(DISK_metrics));
82 		if (lm == NULL)
83 			err(1, NULL);
84 
85 		if (ioctl(fd, DIOCGPDINFO, &dl) == -1 &&
86 		    ioctl(fd, DIOCGDINFO, &dl) == -1) {
87 			warn("DIOCGDINFO");
88 			free(lm);
89 			lm = NULL;
90 		} else {
91 			lm->cylinders = dl.d_ncylinders;
92 			lm->heads = dl.d_ntracks;
93 			lm->sectors = dl.d_nsectors;
94 			lm->size = dl.d_secperunit;
95 			unit_types[SECTORS].conversion = dl.d_secsize;
96 		}
97 		DISK_close(fd);
98 	}
99 
100 	return (lm);
101 }
102 
103 /* This is ugly, and convoluted.  All the magic
104  * for disk geo/size happens here.  Basically,
105  * the real size is the one we will use in the
106  * rest of the program, the label size is what we
107  * got from the disklabel.  If the disklabel fails,
108  * we assume we are working with a normal file,
109  * and should request the user to specify the
110  * geometry he/she wishes to use.
111  */
112 int
113 DISK_getmetrics(disk_t *disk, DISK_metrics *user)
114 {
115 
116 	disk->label = DISK_getlabelmetrics(disk->name);
117 
118 	/* If user supplied, use that */
119 	if (user) {
120 		disk->real = user;
121 		return (0);
122 	}
123 
124 	/* If we have a label, use that */
125 	if (disk->label) {
126 		disk->real = disk->label;
127 		return (0);
128 	}
129 
130 	/* Can not get geometry, punt */
131 	disk->real = NULL;
132 	return (1);
133 }
134 
135 /*
136  * Print the disk geometry information. Take an optional modifier
137  * to indicate the units that should be used for display.
138  */
139 int
140 DISK_printmetrics(disk_t *disk, char *units)
141 {
142 	const int secsize = unit_types[SECTORS].conversion;
143 	double size;
144 	int i;
145 
146 	i = unit_lookup(units);
147 	size = ((double)disk->real->size * secsize) / unit_types[i].conversion;
148 	printf("Disk: %s\t", disk->name);
149 	if (disk->real) {
150 		printf("geometry: %d/%d/%d [%.0f ",
151 		    disk->real->cylinders, disk->real->heads,
152 		    disk->real->sectors, size);
153 		if (i == SECTORS && secsize != DEV_BSIZE)
154 			printf("%d-byte ", secsize);
155 		printf("%s]\n", unit_types[i].lname);
156 	} else
157 		printf("geometry: <none>\n");
158 
159 	return (0);
160 }
161 
162