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