]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/blkdev.c
login: fix -f description in the man-page
[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/file.h>
10 #include <sys/ioctl.h>
11 #include <unistd.h>
12 #include <stdint.h>
13
14 #ifdef HAVE_LINUX_FD_H
15 #include <linux/fd.h>
16 #endif
17
18 #ifdef HAVE_SYS_DISKLABEL_H
19 #include <sys/disklabel.h>
20 #endif
21
22 #ifdef HAVE_SYS_DISK_H
23 # include <sys/disk.h>
24 #endif
25
26 #ifndef EBADFD
27 # define EBADFD 77 /* File descriptor in bad state */
28 #endif
29
30 #include "blkdev.h"
31 #include "c.h"
32 #include "linux_version.h"
33 #include "fileutils.h"
34 #include "nls.h"
35
36 static long
37 blkdev_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
47 int is_blkdev(int fd)
48 {
49 struct stat st;
50 return (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode));
51 }
52
53 off_t
54 blkdev_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 */
83 int
84 blkdev_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 if (ioctl(fd, BLKGETSIZE64, bytes) >= 0)
96 return 0;
97 #endif
98
99 #ifdef BLKGETSIZE
100 {
101 unsigned long size;
102
103 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
104 *bytes = ((unsigned long long)size << 9);
105 return 0;
106 }
107 }
108
109 #endif /* BLKGETSIZE */
110
111 #ifdef DIOCGMEDIASIZE
112 /* FreeBSD */
113 if (ioctl(fd, DIOCGMEDIASIZE, bytes) >= 0)
114 return 0;
115 #endif
116
117 #ifdef FDGETPRM
118 {
119 struct floppy_struct this_floppy;
120
121 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
122 *bytes = ((unsigned long long) this_floppy.size) << 9;
123 return 0;
124 }
125 }
126 #endif /* FDGETPRM */
127
128 #if defined(HAVE_SYS_DISKLABEL_H) && defined(DIOCGDINFO)
129 {
130 /*
131 * This code works for FreeBSD 4.11 i386, except for the full device
132 * (such as /dev/ad0). It doesn't work properly for newer FreeBSD
133 * though. FreeBSD >= 5.0 should be covered by the DIOCGMEDIASIZE
134 * above however.
135 *
136 * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
137 * character) devices, so we need to check for S_ISCHR, too.
138 */
139 int part = -1;
140 struct disklabel lab;
141 struct partition *pp;
142 struct stat st;
143
144 if ((fstat(fd, &st) >= 0) &&
145 (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)))
146 part = st.st_rdev & 7;
147
148 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
149 pp = &lab.d_partitions[part];
150 if (pp->p_size) {
151 *bytes = pp->p_size << 9;
152 return 0;
153 }
154 }
155 }
156 #endif /* defined(HAVE_SYS_DISKLABEL_H) && defined(DIOCGDINFO) */
157
158 {
159 struct stat st;
160
161 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
162 *bytes = st.st_size;
163 return 0;
164 }
165 if (!S_ISBLK(st.st_mode))
166 return -1;
167 }
168
169 *bytes = blkdev_find_size(fd);
170 return 0;
171 }
172
173 /* get 512-byte sector count */
174 int
175 blkdev_get_sectors(int fd, unsigned long long *sectors)
176 {
177 unsigned long long bytes;
178
179 if (blkdev_get_size(fd, &bytes) == 0) {
180 *sectors = (bytes >> 9);
181 return 0;
182 }
183
184 return -1;
185 }
186
187 /*
188 * Get logical sector size.
189 *
190 * This is the smallest unit the storage device can
191 * address. It is typically 512 bytes.
192 */
193 #ifdef BLKSSZGET
194 int blkdev_get_sector_size(int fd, int *sector_size)
195 {
196 if (ioctl(fd, BLKSSZGET, sector_size) >= 0)
197 return 0;
198 return -1;
199 }
200 #else
201 int blkdev_get_sector_size(int fd __attribute__((__unused__)), int *sector_size)
202 {
203 *sector_size = DEFAULT_SECTOR_SIZE;
204 return 0;
205 }
206 #endif
207
208 /*
209 * Get physical block device size. The BLKPBSZGET is supported since Linux
210 * 2.6.32. For old kernels is probably the best to assume that physical sector
211 * size is the same as logical sector size.
212 *
213 * Example:
214 *
215 * rc = blkdev_get_physector_size(fd, &physec);
216 * if (rc || physec == 0) {
217 * rc = blkdev_get_sector_size(fd, &physec);
218 * if (rc)
219 * physec = DEFAULT_SECTOR_SIZE;
220 * }
221 */
222 #ifdef BLKPBSZGET
223 int blkdev_get_physector_size(int fd, int *sector_size)
224 {
225 if (ioctl(fd, BLKPBSZGET, &sector_size) >= 0)
226 return 0;
227 return -1;
228 }
229 #else
230 int blkdev_get_physector_size(int fd __attribute__((__unused__)), int *sector_size)
231 {
232 *sector_size = DEFAULT_SECTOR_SIZE;
233 return 0;
234 }
235 #endif
236
237 /*
238 * Return the alignment status of a device
239 */
240 #ifdef BLKALIGNOFF
241 int blkdev_is_misaligned(int fd)
242 {
243 int aligned;
244
245 if (ioctl(fd, BLKALIGNOFF, &aligned) < 0)
246 return 0; /* probably kernel < 2.6.32 */
247 /*
248 * Note that kernel returns -1 as alignment offset if no compatible
249 * sizes and alignments exist for stacked devices
250 */
251 return aligned != 0 ? 1 : 0;
252 }
253 #else
254 int blkdev_is_misaligned(int fd __attribute__((__unused__)))
255 {
256 return 0;
257 }
258 #endif
259
260 int open_blkdev_or_file(const struct stat *st, const char *name, const int oflag)
261 {
262 int fd;
263
264 if (S_ISBLK(st->st_mode)) {
265 fd = open(name, oflag | O_EXCL);
266 } else
267 fd = open(name, oflag);
268 if (-1 < fd && !is_same_inode(fd, st)) {
269 close(fd);
270 errno = EBADFD;
271 return -1;
272 }
273 if (-1 < fd && S_ISBLK(st->st_mode) && blkdev_is_misaligned(fd))
274 warnx(_("warning: %s is misaligned"), name);
275 return fd;
276 }
277
278 #ifdef CDROM_GET_CAPABILITY
279 int blkdev_is_cdrom(int fd)
280 {
281 int ret;
282
283 if ((ret = ioctl(fd, CDROM_GET_CAPABILITY, NULL)) < 0)
284 return 0;
285
286 return ret;
287 }
288 #else
289 int blkdev_is_cdrom(int fd __attribute__((__unused__)))
290 {
291 return 0;
292 }
293 #endif
294
295 /*
296 * Get kernel's interpretation of the device's geometry.
297 *
298 * Returns the heads and sectors - but not cylinders
299 * as it's truncated for disks with more than 65535 tracks.
300 *
301 * Note that this is deprecated in favor of LBA addressing.
302 */
303 #ifdef HDIO_GETGEO
304 int blkdev_get_geometry(int fd, unsigned int *h, unsigned int *s)
305 {
306 struct hd_geometry geometry;
307
308 if (ioctl(fd, HDIO_GETGEO, &geometry) == 0) {
309 *h = geometry.heads;
310 *s = geometry.sectors;
311 return 0;
312 }
313 #else
314 int blkdev_get_geometry(int fd __attribute__((__unused__)),
315 unsigned int *h, unsigned int *s)
316 {
317 *h = 0;
318 *s = 0;
319 #endif
320 return -1;
321 }
322
323 /*
324 * Convert scsi type to human readable string.
325 */
326 const char *blkdev_scsi_type_to_name(int type)
327 {
328 switch (type) {
329 case SCSI_TYPE_DISK:
330 return "disk";
331 case SCSI_TYPE_TAPE:
332 return "tape";
333 case SCSI_TYPE_PRINTER:
334 return "printer";
335 case SCSI_TYPE_PROCESSOR:
336 return "processor";
337 case SCSI_TYPE_WORM:
338 return "worm";
339 case SCSI_TYPE_ROM:
340 return "rom";
341 case SCSI_TYPE_SCANNER:
342 return "scanner";
343 case SCSI_TYPE_MOD:
344 return "mo-disk";
345 case SCSI_TYPE_MEDIUM_CHANGER:
346 return "changer";
347 case SCSI_TYPE_COMM:
348 return "comm";
349 case SCSI_TYPE_RAID:
350 return "raid";
351 case SCSI_TYPE_ENCLOSURE:
352 return "enclosure";
353 case SCSI_TYPE_RBC:
354 return "rbc";
355 case SCSI_TYPE_OSD:
356 return "osd";
357 case SCSI_TYPE_NO_LUN:
358 return "no-lun";
359 default:
360 break;
361 }
362 return NULL;
363 }
364
365 /* return 0 on success */
366 int blkdev_lock(int fd, const char *devname, const char *lockmode)
367 {
368 int oper, rc, msg = 0;
369
370 if (!lockmode)
371 lockmode = getenv("LOCK_BLOCK_DEVICE");
372 if (!lockmode)
373 return 0;
374
375 if (strcasecmp(lockmode, "yes") == 0 ||
376 strcmp(lockmode, "1") == 0)
377 oper = LOCK_EX;
378
379 else if (strcasecmp(lockmode, "nonblock") == 0)
380 oper = LOCK_EX | LOCK_NB;
381
382 else if (strcasecmp(lockmode, "no") == 0 ||
383 strcmp(lockmode, "0") == 0)
384 return 0;
385 else {
386 warnx(_("unsupported lock mode: %s"), lockmode);
387 return -EINVAL;
388 }
389
390 if (!(oper & LOCK_NB)) {
391 /* Try non-block first to provide message */
392 rc = flock(fd, oper | LOCK_NB);
393 if (rc == 0)
394 return 0;
395 if (rc != 0 && errno == EWOULDBLOCK) {
396 fprintf(stderr, _("%s: %s: device already locked, waiting to get lock ... "),
397 program_invocation_short_name, devname);
398 msg = 1;
399 }
400 }
401 rc = flock(fd, oper);
402 if (rc != 0) {
403 switch (errno) {
404 case EWOULDBLOCK: /* LOCK_NB */
405 warnx(_("%s: device already locked"), devname);
406 break;
407 default:
408 warn(_("%s: failed to get lock"), devname);
409 }
410 } else if (msg)
411 fprintf(stderr, _("OK\n"));
412 return rc;
413 }
414
415
416 #ifdef TEST_PROGRAM_BLKDEV
417 #include <stdio.h>
418 #include <stdlib.h>
419 #include <fcntl.h>
420 int
421 main(int argc, char **argv)
422 {
423 unsigned long long bytes;
424 unsigned long long sectors;
425 int sector_size, phy_sector_size;
426 int fd;
427
428 if (argc != 2) {
429 fprintf(stderr, "usage: %s device\n", argv[0]);
430 exit(EXIT_FAILURE);
431 }
432
433 if ((fd = open(argv[1], O_RDONLY|O_CLOEXEC)) < 0)
434 err(EXIT_FAILURE, "open %s failed", argv[1]);
435
436 if (blkdev_get_size(fd, &bytes) < 0)
437 err(EXIT_FAILURE, "blkdev_get_size() failed");
438 if (blkdev_get_sectors(fd, &sectors) < 0)
439 err(EXIT_FAILURE, "blkdev_get_sectors() failed");
440 if (blkdev_get_sector_size(fd, &sector_size) < 0)
441 err(EXIT_FAILURE, "blkdev_get_sector_size() failed");
442 if (blkdev_get_physector_size(fd, &phy_sector_size) < 0)
443 err(EXIT_FAILURE, "blkdev_get_physector_size() failed");
444
445 printf(" bytes: %llu\n", bytes);
446 printf(" sectors: %llu\n", sectors);
447 printf(" sector size: %d\n", sector_size);
448 printf("phy-sector size: %d\n", phy_sector_size);
449
450 return EXIT_SUCCESS;
451 }
452 #endif /* TEST_PROGRAM_BLKDEV */