blob: a293529631cbe4218dc2a149d8e1176da306eed1 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 *
5 * Written by Karel Zak <kzak@redhat.com>
6 */
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <sys/ioctl.h>
10#include <unistd.h>
11#include <stdint.h>
12
13#ifdef HAVE_LINUX_FD_H
14#include <linux/fd.h>
15#endif
16
17#ifdef HAVE_SYS_DISKLABEL_H
18#include <sys/disklabel.h>
19#endif
20
21#ifdef HAVE_SYS_DISK_H
22#ifdef HAVE_SYS_QUEUE_H
23#include <sys/queue.h> /* for LIST_HEAD */
24#endif
25#include <sys/disk.h>
26#endif
27
bigbiff7b4c7a62015-01-01 19:44:14 -050028#ifdef __FreeBSD_kernel__
29#include <sys/disk.h>
30#endif
31
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050032#include "blkdev.h"
33#include "c.h"
34#include "linux_version.h"
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050035
36static long
37blkdev_valid_offset (int fd, off_t offset) {
38 char ch;
39
40 if (lseek (fd, offset, 0) < 0)
41 return 0;
42 if (read (fd, &ch, 1) < 1)
43 return 0;
44 return 1;
45}
46
47int is_blkdev(int fd)
48{
49 struct stat st;
50 return (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode));
51}
52
53off_t
54blkdev_find_size (int fd) {
55 uintmax_t high, low = 0;
56
57 for (high = 1024; blkdev_valid_offset (fd, high); ) {
58 if (high == UINTMAX_MAX)
59 return -1;
60
61 low = high;
62
63 if (high >= UINTMAX_MAX/2)
64 high = UINTMAX_MAX;
65 else
66 high *= 2;
67 }
68
69 while (low < high - 1)
70 {
71 uintmax_t mid = (low + high) / 2;
72
73 if (blkdev_valid_offset (fd, mid))
74 low = mid;
75 else
76 high = mid;
77 }
78 blkdev_valid_offset (fd, 0);
79 return (low + 1);
80}
81
82/* get size in bytes */
83int
84blkdev_get_size(int fd, unsigned long long *bytes)
85{
86#ifdef DKIOCGETBLOCKCOUNT
87 /* Apple Darwin */
88 if (ioctl(fd, DKIOCGETBLOCKCOUNT, bytes) >= 0) {
89 *bytes <<= 9;
90 return 0;
91 }
92#endif
93
94#ifdef BLKGETSIZE64
95 {
96#ifdef __linux__
97 int ver = get_linux_version();
98
99 /* kernels 2.4.15-2.4.17, had a broken BLKGETSIZE64 */
100 if (ver >= KERNEL_VERSION (2,6,0) ||
101 (ver >= KERNEL_VERSION (2,4,18) && ver < KERNEL_VERSION (2,5,0)))
102#endif
103 if (ioctl(fd, BLKGETSIZE64, bytes) >= 0)
104 return 0;
105 }
106#endif /* BLKGETSIZE64 */
107
108#ifdef BLKGETSIZE
109 {
110 unsigned long size;
111
112 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
113 *bytes = ((unsigned long long)size << 9);
114 return 0;
115 }
116 }
117
118#endif /* BLKGETSIZE */
119
120#ifdef DIOCGMEDIASIZE
121 /* FreeBSD */
122 if (ioctl(fd, DIOCGMEDIASIZE, bytes) >= 0)
123 return 0;
124#endif
125
126#ifdef FDGETPRM
127 {
128 struct floppy_struct this_floppy;
129
130 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
131 *bytes = this_floppy.size << 9;
132 return 0;
133 }
134 }
135#endif /* FDGETPRM */
136
137#ifdef HAVE_SYS_DISKLABEL_H
138 {
139 /*
140 * This code works for FreeBSD 4.11 i386, except for the full device
141 * (such as /dev/ad0). It doesn't work properly for newer FreeBSD
142 * though. FreeBSD >= 5.0 should be covered by the DIOCGMEDIASIZE
143 * above however.
144 *
145 * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
146 * character) devices, so we need to check for S_ISCHR, too.
147 */
148 int part = -1;
149 struct disklabel lab;
150 struct partition *pp;
151 char ch;
152 struct stat st;
153
154 if ((fstat(fd, &st) >= 0) &&
155 (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)))
156 part = st.st_rdev & 7;
157
158 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
159 pp = &lab.d_partitions[part];
160 if (pp->p_size) {
161 *bytes = pp->p_size << 9;
162 return 0;
163 }
164 }
165 }
166#endif /* HAVE_SYS_DISKLABEL_H */
167
168 {
169 struct stat st;
170
171 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
172 *bytes = st.st_size;
173 return 0;
174 }
175 if (!S_ISBLK(st.st_mode))
176 return -1;
177 }
178
179 *bytes = blkdev_find_size(fd);
180 return 0;
181}
182
183/* get 512-byte sector count */
184int
185blkdev_get_sectors(int fd, unsigned long long *sectors)
186{
187 unsigned long long bytes;
188
189 if (blkdev_get_size(fd, &bytes) == 0) {
190 *sectors = (bytes >> 9);
191 return 0;
192 }
193
194 return -1;
195}
196
197/*
198 * Get logical sector size.
199 *
200 * This is the smallest unit the storage device can
201 * address. It is typically 512 bytes.
202 */
203int blkdev_get_sector_size(int fd, int *sector_size)
204{
205#ifdef BLKSSZGET
206 if (ioctl(fd, BLKSSZGET, sector_size) >= 0)
207 return 0;
208 return -1;
209#else
210 *sector_size = DEFAULT_SECTOR_SIZE;
211 return 0;
212#endif
213}
214
215/*
216 * Get physical block device size. The BLKPBSZGET is supported since Linux
217 * 2.6.32. For old kernels is probably the best to assume that physical sector
218 * size is the same as logical sector size.
219 *
220 * Example:
221 *
222 * rc = blkdev_get_physector_size(fd, &physec);
223 * if (rc || physec == 0) {
224 * rc = blkdev_get_sector_size(fd, &physec);
225 * if (rc)
226 * physec = DEFAULT_SECTOR_SIZE;
227 * }
228 */
229int blkdev_get_physector_size(int fd, int *sector_size)
230{
231#ifdef BLKPBSZGET
232 if (ioctl(fd, BLKPBSZGET, &sector_size) >= 0)
233 return 0;
234 return -1;
235#else
236 *sector_size = DEFAULT_SECTOR_SIZE;
237 return 0;
238#endif
239}
240
241/*
242 * Return the alignment status of a device
243 */
244int blkdev_is_misaligned(int fd)
245{
246#ifdef BLKALIGNOFF
247 int aligned;
248
249 if (ioctl(fd, BLKALIGNOFF, &aligned) < 0)
250 return 0; /* probably kernel < 2.6.32 */
251 /*
252 * Note that kernel returns -1 as alignement offset if no compatible
253 * sizes and alignments exist for stacked devices
254 */
255 return aligned != 0 ? 1 : 0;
256#else
257 return 0;
258#endif
259}
260
261int blkdev_is_cdrom(int fd)
262{
263#ifdef CDROM_GET_CAPABILITY
264 int ret;
265
266 if ((ret = ioctl(fd, CDROM_GET_CAPABILITY, NULL)) < 0)
267 return 0;
268 else
269 return ret;
270#else
271 return 0;
272#endif
273}
274
275/*
276 * Get kernel's interpretation of the device's geometry.
277 *
278 * Returns the heads and sectors - but not cylinders
279 * as it's truncated for disks with more than 65535 tracks.
280 *
281 * Note that this is deprecated in favor of LBA addressing.
282 */
283int blkdev_get_geometry(int fd, unsigned int *h, unsigned int *s)
284{
285#ifdef HDIO_GETGEO
286 struct hd_geometry geometry;
287
288 if (ioctl(fd, HDIO_GETGEO, &geometry) == 0) {
289 *h = geometry.heads;
290 *s = geometry.sectors;
291 return 0;
292 }
293#else
294 *h = 0;
295 *s = 0;
296#endif
297 return -1;
298}
299
300/*
301 * Convert scsi type to human readable string.
302 */
303const char *blkdev_scsi_type_to_name(int type)
304{
305 switch (type) {
306 case SCSI_TYPE_DISK:
307 return "disk";
308 case SCSI_TYPE_TAPE:
309 return "tape";
310 case SCSI_TYPE_PRINTER:
311 return "printer";
312 case SCSI_TYPE_PROCESSOR:
313 return "processor";
314 case SCSI_TYPE_WORM:
315 return "worm";
316 case SCSI_TYPE_ROM:
317 return "rom";
318 case SCSI_TYPE_SCANNER:
319 return "scanner";
320 case SCSI_TYPE_MOD:
321 return "mo-disk";
322 case SCSI_TYPE_MEDIUM_CHANGER:
323 return "changer";
324 case SCSI_TYPE_COMM:
325 return "comm";
326 case SCSI_TYPE_RAID:
327 return "raid";
328 case SCSI_TYPE_ENCLOSURE:
329 return "enclosure";
330 case SCSI_TYPE_RBC:
331 return "rbc";
332 case SCSI_TYPE_OSD:
333 return "osd";
334 case SCSI_TYPE_NO_LUN:
335 return "no-lun";
336 default:
337 break;
338 }
339 return NULL;
340}
341
bigbiff7b4c7a62015-01-01 19:44:14 -0500342#ifdef TEST_PROGRAM_BLKDEV
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500343#include <stdio.h>
344#include <stdlib.h>
345#include <fcntl.h>
346int
347main(int argc, char **argv)
348{
349 unsigned long long bytes;
350 unsigned long long sectors;
351 int sector_size, phy_sector_size;
352 int fd;
353
354 if (argc != 2) {
355 fprintf(stderr, "usage: %s device\n", argv[0]);
356 exit(EXIT_FAILURE);
357 }
358
bigbiff7b4c7a62015-01-01 19:44:14 -0500359 if ((fd = open(argv[1], O_RDONLY|O_CLOEXEC)) < 0)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500360 err(EXIT_FAILURE, "open %s failed", argv[1]);
361
362 if (blkdev_get_size(fd, &bytes) < 0)
363 err(EXIT_FAILURE, "blkdev_get_size() failed");
364 if (blkdev_get_sectors(fd, &sectors) < 0)
365 err(EXIT_FAILURE, "blkdev_get_sectors() failed");
366 if (blkdev_get_sector_size(fd, &sector_size) < 0)
367 err(EXIT_FAILURE, "blkdev_get_sector_size() failed");
368 if (blkdev_get_physector_size(fd, &phy_sector_size) < 0)
369 err(EXIT_FAILURE, "blkdev_get_physector_size() failed");
370
371 printf(" bytes: %llu\n", bytes);
372 printf(" sectors: %llu\n", sectors);
373 printf(" sector size: %d\n", sector_size);
374 printf("phy-sector size: %d\n", phy_sector_size);
375
376 return EXIT_SUCCESS;
377}
378#endif /* TEST_PROGRAM */