1 /* $OpenBSD: disk.c,v 1.29 2009/02/08 18:03:18 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 /* Routine to go after the disklabel for geometry 61 * information. This should work everywhere, but 62 * in the land of PC, things are not always what 63 * they seem. 64 */ 65 DISK_metrics * 66 DISK_getlabelmetrics(char *name) 67 { 68 DISK_metrics *lm = NULL; 69 struct disklabel dl; 70 int fd; 71 72 /* Get label metrics */ 73 if ((fd = DISK_open(name, O_RDONLY)) != -1) { 74 lm = malloc(sizeof(DISK_metrics)); 75 if (lm == NULL) 76 err(1, NULL); 77 78 if (ioctl(fd, DIOCGPDINFO, &dl) == -1 && 79 ioctl(fd, DIOCGDINFO, &dl) == -1) { 80 warn("DIOCGDINFO"); 81 free(lm); 82 lm = NULL; 83 } else { 84 lm->cylinders = dl.d_ncylinders; 85 lm->heads = dl.d_ntracks; 86 lm->sectors = dl.d_nsectors; 87 lm->size = dl.d_secperunit; 88 unit_types[SECTORS].conversion = dl.d_secsize; 89 } 90 close(fd); 91 } 92 93 return (lm); 94 } 95 96 /* This is ugly, and convoluted. All the magic 97 * for disk geo/size happens here. Basically, 98 * the real size is the one we will use in the 99 * rest of the program, the label size is what we 100 * got from the disklabel. If the disklabel fails, 101 * we assume we are working with a normal file, 102 * and should request the user to specify the 103 * geometry he/she wishes to use. 104 */ 105 int 106 DISK_getmetrics(disk_t *disk, DISK_metrics *user) 107 { 108 109 disk->label = DISK_getlabelmetrics(disk->name); 110 111 /* If user supplied, use that */ 112 if (user) { 113 disk->real = user; 114 return (0); 115 } 116 117 /* If we have a label, use that */ 118 if (disk->label) { 119 disk->real = disk->label; 120 return (0); 121 } 122 123 /* Can not get geometry, punt */ 124 disk->real = NULL; 125 return (1); 126 } 127 128 /* 129 * Print the disk geometry information. Take an optional modifier 130 * to indicate the units that should be used for display. 131 */ 132 int 133 DISK_printmetrics(disk_t *disk, char *units) 134 { 135 const int secsize = unit_types[SECTORS].conversion; 136 double size; 137 int i; 138 139 i = unit_lookup(units); 140 size = ((double)disk->real->size * secsize) / unit_types[i].conversion; 141 printf("Disk: %s\t", disk->name); 142 if (disk->real) { 143 printf("geometry: %d/%d/%d [%.0f ", 144 disk->real->cylinders, disk->real->heads, 145 disk->real->sectors, size); 146 if (i == SECTORS && secsize != DEV_BSIZE) 147 printf("%d-byte ", secsize); 148 printf("%s]\n", unit_types[i].lname); 149 } else 150 printf("geometry: <none>\n"); 151 152 return (0); 153 } 154 155