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