1 /*
2 * probe.c - identify a block device by its contents, and return a dev
3 * struct with the details
4 *
5 * Copyright (C) 1999 by Andries Brouwer
6 * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
7 * Copyright (C) 2001 by Andreas Dilger
8 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
9 *
10 * %Begin-Header%
11 * This file may be redistributed under the terms of the
12 * GNU Lesser General Public License.
13 * %End-Header%
14 */
15
16 #include "config.h"
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <ctype.h>
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_STAT_H
26 #include <sys/stat.h>
27 #endif
28 #ifdef HAVE_SYS_MKDEV_H
29 #include <sys/mkdev.h>
30 #endif
31 #ifdef __linux__
32 #include <sys/utsname.h>
33 #endif
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37 #include "blkidP.h"
38 #include "uuid/uuid.h"
39 #include "probe.h"
40
figure_label_len(const unsigned char * label,int len)41 static int figure_label_len(const unsigned char *label, int len)
42 {
43 const unsigned char *end = label + len - 1;
44
45 while (end >= label && (*end == ' ' || *end == 0))
46 --end;
47 if (end >= label)
48 return end - label + 1;
49 return 0;
50 }
51
get_buffer(struct blkid_probe * pr,blkid_loff_t off,size_t len)52 static unsigned char *get_buffer(struct blkid_probe *pr,
53 blkid_loff_t off, size_t len)
54 {
55 ssize_t ret_read;
56 unsigned char *newbuf;
57
58 if (off + len <= SB_BUFFER_SIZE) {
59 if (!pr->sbbuf) {
60 pr->sbbuf = malloc(SB_BUFFER_SIZE);
61 if (!pr->sbbuf)
62 return NULL;
63 if (lseek(pr->fd, 0, SEEK_SET) < 0)
64 return NULL;
65 ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE);
66 if (ret_read < 0)
67 ret_read = 0;
68 pr->sb_valid = ret_read;
69 }
70 if (off+len > pr->sb_valid)
71 return NULL;
72 return pr->sbbuf + off;
73 } else {
74 if (len > pr->buf_max) {
75 newbuf = realloc(pr->buf, len);
76 if (newbuf == NULL)
77 return NULL;
78 pr->buf = newbuf;
79 pr->buf_max = len;
80 }
81 if (blkid_llseek(pr->fd, off, SEEK_SET) < 0)
82 return NULL;
83 ret_read = read(pr->fd, pr->buf, len);
84 if (ret_read != (ssize_t) len)
85 return NULL;
86 return pr->buf;
87 }
88 }
89
90
91 /*
92 * This is a special case code to check for an MDRAID device. We do
93 * this special since it requires checking for a superblock at the end
94 * of the device.
95 */
check_mdraid(int fd,unsigned char * ret_uuid)96 static int check_mdraid(int fd, unsigned char *ret_uuid)
97 {
98 struct mdp_superblock_s *md;
99 blkid_loff_t offset;
100 char buf[4096];
101
102 if (fd < 0)
103 return -BLKID_ERR_PARAM;
104
105 offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
106
107 if (blkid_llseek(fd, offset, 0) < 0 ||
108 read(fd, buf, 4096) != 4096)
109 return -BLKID_ERR_IO;
110 /* Check for magic number */
111 if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4))
112 return -BLKID_ERR_PARAM;
113
114 if (!ret_uuid)
115 return 0;
116 *ret_uuid = 0;
117
118 /* The MD UUID is not contiguous in the superblock, make it so */
119 md = (struct mdp_superblock_s *)buf;
120 if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
121 memcpy(ret_uuid, &md->set_uuid0, 4);
122 memcpy(ret_uuid + 4, &md->set_uuid1, 12);
123 }
124 return 0;
125 }
126
set_uuid(blkid_dev dev,uuid_t uuid,const char * tag)127 static void set_uuid(blkid_dev dev, uuid_t uuid, const char *tag)
128 {
129 char str[37];
130
131 if (!uuid_is_null(uuid)) {
132 uuid_unparse(uuid, str);
133 blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
134 }
135 }
136
get_ext2_info(blkid_dev dev,struct blkid_magic * id,unsigned char * buf)137 static void get_ext2_info(blkid_dev dev, struct blkid_magic *id,
138 unsigned char *buf)
139 {
140 struct ext2_super_block *es = (struct ext2_super_block *) buf;
141 const char *label = 0;
142
143 DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
144 blkid_le32(es->s_feature_compat),
145 blkid_le32(es->s_feature_incompat),
146 blkid_le32(es->s_feature_ro_compat)));
147
148 if (es->s_volume_name[0])
149 label = es->s_volume_name;
150 blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
151
152 set_uuid(dev, es->s_uuid, 0);
153
154 if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
155 !uuid_is_null(es->s_journal_uuid))
156 set_uuid(dev, es->s_journal_uuid, "EXT_JOURNAL");
157
158 if (strcmp(id->bim_type, "ext2") &&
159 ((blkid_le32(es->s_feature_incompat) &
160 EXT2_FEATURE_INCOMPAT_UNSUPPORTED) == 0))
161 blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2"));
162 }
163
164 /*
165 * Check to see if a filesystem is in /proc/filesystems.
166 * Returns 1 if found, 0 if not
167 */
fs_proc_check(const char * fs_name)168 static int fs_proc_check(const char *fs_name)
169 {
170 FILE *f;
171 char buf[80], *cp, *t;
172
173 f = fopen("/proc/filesystems", "r");
174 if (!f)
175 return (0);
176 while (!feof(f)) {
177 if (!fgets(buf, sizeof(buf), f))
178 break;
179 cp = buf;
180 if (!isspace(*cp)) {
181 while (*cp && !isspace(*cp))
182 cp++;
183 }
184 while (*cp && isspace(*cp))
185 cp++;
186 if ((t = strchr(cp, '\n')) != NULL)
187 *t = 0;
188 if ((t = strchr(cp, '\t')) != NULL)
189 *t = 0;
190 if ((t = strchr(cp, ' ')) != NULL)
191 *t = 0;
192 if (!strcmp(fs_name, cp)) {
193 fclose(f);
194 return (1);
195 }
196 }
197 fclose(f);
198 return (0);
199 }
200
201 /*
202 * Check to see if a filesystem is available as a module
203 * Returns 1 if found, 0 if not
204 */
check_for_modules(const char * fs_name)205 static int check_for_modules(const char *fs_name)
206 {
207 #ifdef __linux__
208 struct utsname uts;
209 FILE *f;
210 char buf[1024], *cp;
211 int namesz;
212
213 if (uname(&uts))
214 return (0);
215 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
216
217 f = fopen(buf, "r");
218 if (!f)
219 return (0);
220
221 namesz = strlen(fs_name);
222
223 while (!feof(f)) {
224 if (!fgets(buf, sizeof(buf), f))
225 break;
226 if ((cp = strchr(buf, ':')) != NULL)
227 *cp = 0;
228 else
229 continue;
230 if ((cp = strrchr(buf, '/')) != NULL)
231 cp++;
232 else
233 cp = buf;
234 if (!strncmp(cp, fs_name, namesz) &&
235 (!strcmp(cp + namesz, ".ko") ||
236 !strcmp(cp + namesz, ".ko.gz"))) {
237 fclose(f);
238 return (1);
239 }
240 }
241 fclose(f);
242 #endif
243 return (0);
244 }
245
linux_version_code(void)246 static int linux_version_code(void)
247 {
248 #ifdef __linux__
249 struct utsname ut;
250 static int version_code = -1;
251 int major, minor, rev;
252 char *endptr;
253 const char *cp;
254
255 if (version_code > 0)
256 return version_code;
257
258 if (uname(&ut))
259 return 0;
260 cp = ut.release;
261
262 major = strtol(cp, &endptr, 10);
263 if (cp == endptr || *endptr != '.')
264 return 0;
265 cp = endptr + 1;
266 minor = strtol(cp, &endptr, 10);
267 if (cp == endptr || *endptr != '.')
268 return 0;
269 cp = endptr + 1;
270 rev = strtol(cp, &endptr, 10);
271 if (cp == endptr)
272 return 0;
273 version_code = (((major * 256) + minor) * 256) + rev;
274 return version_code;
275 #else
276 return 0;
277 #endif
278 }
279
280 #define EXT4_SUPPORTS_EXT2 (2 * 65536 + 6*256 + 29)
281
system_supports_ext2(void)282 static int system_supports_ext2(void)
283 {
284 static time_t last_check = 0;
285 static int ret = -1;
286 time_t now = time(0);
287
288 if (ret != -1 || (now - last_check) < 5)
289 return ret;
290 last_check = now;
291 ret = (fs_proc_check("ext2") || check_for_modules("ext2"));
292 return ret;
293 }
294
system_supports_ext4(void)295 static int system_supports_ext4(void)
296 {
297 static time_t last_check = 0;
298 static int ret = -1;
299 time_t now = time(0);
300
301 if (ret != -1 || (now - last_check) < 5)
302 return ret;
303 last_check = now;
304 ret = (fs_proc_check("ext4") || check_for_modules("ext4"));
305 return ret;
306 }
307
system_supports_ext4dev(void)308 static int system_supports_ext4dev(void)
309 {
310 static time_t last_check = 0;
311 static int ret = -1;
312 time_t now = time(0);
313
314 if (ret != -1 || (now - last_check) < 5)
315 return ret;
316 last_check = now;
317 ret = (fs_proc_check("ext4dev") || check_for_modules("ext4dev"));
318 return ret;
319 }
320
probe_ext4dev(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)321 static int probe_ext4dev(struct blkid_probe *probe,
322 struct blkid_magic *id,
323 unsigned char *buf)
324 {
325 struct ext2_super_block *es;
326 es = (struct ext2_super_block *)buf;
327
328 /* Distinguish from jbd */
329 if (blkid_le32(es->s_feature_incompat) &
330 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
331 return -BLKID_ERR_PARAM;
332
333 /*
334 * If the filesystem does not have a journal and ext2 and ext4
335 * is not present, then force this to be detected as an
336 * ext4dev filesystem.
337 */
338 if (!(blkid_le32(es->s_feature_compat) &
339 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
340 !system_supports_ext2() && !system_supports_ext4() &&
341 system_supports_ext4dev() &&
342 linux_version_code() >= EXT4_SUPPORTS_EXT2)
343 goto force_ext4dev;
344
345 /*
346 * If the filesystem is marked as OK for use by in-development
347 * filesystem code, but ext4dev is not supported, and ext4 is,
348 * then don't call ourselves ext4dev, since we should be
349 * detected as ext4 in that case.
350 *
351 * If the filesystem is marked as in use by production
352 * filesystem, then it can only be used by ext4 and NOT by
353 * ext4dev, so always disclaim we are ext4dev in that case.
354 */
355 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
356 if (!system_supports_ext4dev() && system_supports_ext4())
357 return -BLKID_ERR_PARAM;
358 } else
359 return -BLKID_ERR_PARAM;
360
361 force_ext4dev:
362 get_ext2_info(probe->dev, id, buf);
363 return 0;
364 }
365
probe_ext4(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)366 static int probe_ext4(struct blkid_probe *probe, struct blkid_magic *id,
367 unsigned char *buf)
368 {
369 struct ext2_super_block *es;
370 es = (struct ext2_super_block *)buf;
371
372 /* Distinguish from jbd */
373 if (blkid_le32(es->s_feature_incompat) &
374 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
375 return -BLKID_ERR_PARAM;
376
377 /*
378 * If the filesystem does not have a journal and ext2 is not
379 * present, then force this to be detected as an ext2
380 * filesystem.
381 */
382 if (!(blkid_le32(es->s_feature_compat) &
383 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
384 !system_supports_ext2() && system_supports_ext4() &&
385 linux_version_code() >= EXT4_SUPPORTS_EXT2)
386 goto force_ext4;
387
388 /* Ext4 has at least one feature which ext3 doesn't understand */
389 if (!(blkid_le32(es->s_feature_ro_compat) &
390 EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) &&
391 !(blkid_le32(es->s_feature_incompat) &
392 EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
393 return -BLKID_ERR_PARAM;
394
395 force_ext4:
396 /*
397 * If the filesystem is a OK for use by in-development
398 * filesystem code, and ext4dev is supported or ext4 is not
399 * supported, then don't call ourselves ext4, so we can redo
400 * the detection and mark the filesystem as ext4dev.
401 *
402 * If the filesystem is marked as in use by production
403 * filesystem, then it can only be used by ext4 and NOT by
404 * ext4dev.
405 */
406 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
407 if (system_supports_ext4dev() || !system_supports_ext4())
408 return -BLKID_ERR_PARAM;
409 }
410 get_ext2_info(probe->dev, id, buf);
411 return 0;
412 }
413
probe_ext3(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)414 static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id,
415 unsigned char *buf)
416 {
417 struct ext2_super_block *es;
418 es = (struct ext2_super_block *)buf;
419
420 /* ext3 requires journal */
421 if (!(blkid_le32(es->s_feature_compat) &
422 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
423 return -BLKID_ERR_PARAM;
424
425 /* Any features which ext3 doesn't understand */
426 if ((blkid_le32(es->s_feature_ro_compat) &
427 EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) ||
428 (blkid_le32(es->s_feature_incompat) &
429 EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
430 return -BLKID_ERR_PARAM;
431
432 get_ext2_info(probe->dev, id, buf);
433 return 0;
434 }
435
probe_ext2(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)436 static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id,
437 unsigned char *buf)
438 {
439 struct ext2_super_block *es;
440
441 es = (struct ext2_super_block *)buf;
442
443 /* Distinguish between ext3 and ext2 */
444 if ((blkid_le32(es->s_feature_compat) &
445 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
446 return -BLKID_ERR_PARAM;
447
448 /* Any features which ext2 doesn't understand */
449 if ((blkid_le32(es->s_feature_ro_compat) &
450 EXT2_FEATURE_RO_COMPAT_UNSUPPORTED) ||
451 (blkid_le32(es->s_feature_incompat) &
452 EXT2_FEATURE_INCOMPAT_UNSUPPORTED))
453 return -BLKID_ERR_PARAM;
454
455 /*
456 * If ext2 is not present, but ext4 or ext4dev are, then
457 * disclaim we are ext2
458 */
459 if (!system_supports_ext2() &&
460 (system_supports_ext4() || system_supports_ext4dev()) &&
461 linux_version_code() >= EXT4_SUPPORTS_EXT2)
462 return -BLKID_ERR_PARAM;
463
464 get_ext2_info(probe->dev, id, buf);
465 return 0;
466 }
467
probe_jbd(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)468 static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id,
469 unsigned char *buf)
470 {
471 struct ext2_super_block *es = (struct ext2_super_block *) buf;
472
473 if (!(blkid_le32(es->s_feature_incompat) &
474 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
475 return -BLKID_ERR_PARAM;
476
477 get_ext2_info(probe->dev, id, buf);
478
479 return 0;
480 }
481
482 #define FAT_ATTR_VOLUME_ID 0x08
483 #define FAT_ATTR_DIR 0x10
484 #define FAT_ATTR_LONG_NAME 0x0f
485 #define FAT_ATTR_MASK 0x3f
486 #define FAT_ENTRY_FREE 0xe5
487
488 static const char *no_name = "NO NAME ";
489
search_fat_label(struct vfat_dir_entry * dir,int count)490 static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
491 {
492 int i;
493
494 for (i = 0; i < count; i++) {
495 if (dir[i].name[0] == 0x00)
496 break;
497
498 if ((dir[i].name[0] == FAT_ENTRY_FREE) ||
499 (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) ||
500 ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
501 continue;
502
503 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
504 FAT_ATTR_VOLUME_ID) {
505 return dir[i].name;
506 }
507 }
508 return 0;
509 }
510
511 /* FAT label extraction from the root directory taken from Kay
512 * Sievers's volume_id library */
probe_fat(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)513 static int probe_fat(struct blkid_probe *probe,
514 struct blkid_magic *id __BLKID_ATTR((unused)),
515 unsigned char *buf)
516 {
517 struct vfat_super_block *vs = (struct vfat_super_block *) buf;
518 struct msdos_super_block *ms = (struct msdos_super_block *) buf;
519 struct vfat_dir_entry *dir;
520 char serno[10];
521 const unsigned char *label = 0, *vol_label = 0, *tmp;
522 unsigned char *vol_serno;
523 int label_len = 0, maxloop = 100;
524 __u16 sector_size, dir_entries, reserved;
525 __u32 sect_count, fat_size, dir_size, cluster_count, fat_length;
526 __u32 buf_size, start_data_sect, next, root_start, root_dir_entries;
527
528 /* sector size check */
529 tmp = (unsigned char *)&ms->ms_sector_size;
530 sector_size = tmp[0] + (tmp[1] << 8);
531 if (sector_size != 0x200 && sector_size != 0x400 &&
532 sector_size != 0x800 && sector_size != 0x1000)
533 return 1;
534
535 tmp = (unsigned char *)&ms->ms_dir_entries;
536 dir_entries = tmp[0] + (tmp[1] << 8);
537 reserved = blkid_le16(ms->ms_reserved);
538 tmp = (unsigned char *)&ms->ms_sectors;
539 sect_count = tmp[0] + (tmp[1] << 8);
540 if (sect_count == 0)
541 sect_count = blkid_le32(ms->ms_total_sect);
542
543 fat_length = blkid_le16(ms->ms_fat_length);
544 if (fat_length == 0)
545 fat_length = blkid_le32(vs->vs_fat32_length);
546
547 fat_size = fat_length * ms->ms_fats;
548 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
549 (sector_size-1)) / sector_size;
550
551 cluster_count = sect_count - (reserved + fat_size + dir_size);
552 if (ms->ms_cluster_size == 0)
553 return 1;
554 cluster_count /= ms->ms_cluster_size;
555
556 if (cluster_count > FAT32_MAX)
557 return 1;
558
559 if (ms->ms_fat_length) {
560 /* the label may be an attribute in the root directory */
561 root_start = (reserved + fat_size) * sector_size;
562 root_dir_entries = vs->vs_dir_entries[0] +
563 (vs->vs_dir_entries[1] << 8);
564
565 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
566 dir = (struct vfat_dir_entry *) get_buffer(probe, root_start,
567 buf_size);
568 if (dir)
569 vol_label = search_fat_label(dir, root_dir_entries);
570
571 if (!vol_label || !memcmp(vol_label, no_name, 11))
572 vol_label = ms->ms_label;
573 vol_serno = ms->ms_serno;
574
575 blkid_set_tag(probe->dev, "SEC_TYPE", "msdos",
576 sizeof("msdos"));
577 } else {
578 /* Search the FAT32 root dir for the label attribute */
579 buf_size = vs->vs_cluster_size * sector_size;
580 start_data_sect = reserved + fat_size;
581
582 next = blkid_le32(vs->vs_root_cluster);
583 while (next && --maxloop) {
584 __u32 next_sect_off;
585 __u64 next_off, fat_entry_off;
586 int count;
587
588 next_sect_off = (next - 2) * vs->vs_cluster_size;
589 next_off = (__u64) (start_data_sect + next_sect_off) *
590 sector_size;
591
592 dir = (struct vfat_dir_entry *)
593 get_buffer(probe, next_off, buf_size);
594 if (dir == NULL)
595 break;
596
597 count = buf_size / sizeof(struct vfat_dir_entry);
598
599 vol_label = search_fat_label(dir, count);
600 if (vol_label)
601 break;
602
603 /* get FAT entry */
604 fat_entry_off =
605 ((unsigned int) reserved *
606 (unsigned int) sector_size) +
607 (next * sizeof(__u32));
608 buf = get_buffer(probe, fat_entry_off, buf_size);
609 if (buf == NULL)
610 break;
611
612 /* set next cluster */
613 next = blkid_le32(*((__u32 *) buf) & 0x0fffffff);
614 }
615
616 if (!vol_label || !memcmp(vol_label, no_name, 11))
617 vol_label = vs->vs_label;
618 vol_serno = vs->vs_serno;
619 }
620
621 if (vol_label && memcmp(vol_label, no_name, 11)) {
622 if ((label_len = figure_label_len(vol_label, 11)))
623 label = vol_label;
624 }
625
626 /* We can't just print them as %04X, because they are unaligned */
627 sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
628 vol_serno[1], vol_serno[0]);
629
630 blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len);
631 blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1);
632
633 return 0;
634 }
635
636 /*
637 * The FAT filesystem could be without a magic string in superblock
638 * (e.g. old floppies). This heuristic for FAT detection is inspired
639 * by http://vrfy.org/projects/volume_id/ and Linux kernel.
640 * [7-Jul-2005, Karel Zak <kzak@redhat.com>]
641 */
probe_fat_nomagic(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)642 static int probe_fat_nomagic(struct blkid_probe *probe,
643 struct blkid_magic *id __BLKID_ATTR((unused)),
644 unsigned char *buf)
645 {
646 struct msdos_super_block *ms;
647
648 ms = (struct msdos_super_block *)buf;
649
650 /* heads check */
651 if (ms->ms_heads == 0)
652 return 1;
653
654 /* cluster size check*/
655 if (ms->ms_cluster_size == 0 ||
656 (ms->ms_cluster_size & (ms->ms_cluster_size-1)))
657 return 1;
658
659 /* media check */
660 if (ms->ms_media < 0xf8 && ms->ms_media != 0xf0)
661 return 1;
662
663 /* fat counts(Linux kernel expects at least 1 FAT table) */
664 if (!ms->ms_fats)
665 return 1;
666
667 /*
668 * OS/2 and apparently DFSee will place a FAT12/16-like
669 * pseudo-superblock in the first 512 bytes of non-FAT
670 * filesystems --- at least JFS and HPFS, and possibly others.
671 * So we explicitly check for those filesystems at the
672 * FAT12/16 filesystem magic field identifier, and if they are
673 * present, we rule this out as a FAT filesystem, despite the
674 * FAT-like pseudo-header.
675 */
676 if ((memcmp(ms->ms_magic, "JFS ", 8) == 0) ||
677 (memcmp(ms->ms_magic, "HPFS ", 8) == 0))
678 return 1;
679
680 return probe_fat(probe, id, buf);
681 }
682
probe_ntfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)683 static int probe_ntfs(struct blkid_probe *probe,
684 struct blkid_magic *id __BLKID_ATTR((unused)),
685 unsigned char *buf)
686 {
687 struct ntfs_super_block *ns;
688 struct master_file_table_record *mft;
689 struct file_attribute *attr;
690 char uuid_str[17], label_str[129], *cp;
691 int bytes_per_sector, sectors_per_cluster;
692 int mft_record_size, attr_off, attr_len;
693 unsigned int i, attr_type, val_len;
694 int val_off;
695 __u64 nr_clusters;
696 blkid_loff_t off;
697 unsigned char *buf_mft, *val;
698
699 ns = (struct ntfs_super_block *) buf;
700
701 bytes_per_sector = ns->bios_parameter_block[0] +
702 (ns->bios_parameter_block[1] << 8);
703 sectors_per_cluster = ns->bios_parameter_block[2];
704
705 if ((bytes_per_sector < 512) || (sectors_per_cluster == 0))
706 return 1;
707
708 if (ns->cluster_per_mft_record < 0)
709 mft_record_size = 1 << (0-ns->cluster_per_mft_record);
710 else
711 mft_record_size = ns->cluster_per_mft_record *
712 sectors_per_cluster * bytes_per_sector;
713 nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster;
714
715 if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) ||
716 (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters))
717 return 1;
718
719 off = blkid_le64(ns->mft_mirror_cluster_location) *
720 bytes_per_sector * sectors_per_cluster;
721
722 buf_mft = get_buffer(probe, off, mft_record_size);
723 if (!buf_mft)
724 return 1;
725
726 if (memcmp(buf_mft, "FILE", 4))
727 return 1;
728
729 off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector *
730 sectors_per_cluster;
731
732 buf_mft = get_buffer(probe, off, mft_record_size);
733 if (!buf_mft)
734 return 1;
735
736 if (memcmp(buf_mft, "FILE", 4))
737 return 1;
738
739 off += MFT_RECORD_VOLUME * mft_record_size;
740
741 buf_mft = get_buffer(probe, off, mft_record_size);
742 if (!buf_mft)
743 return 1;
744
745 if (memcmp(buf_mft, "FILE", 4))
746 return 1;
747
748 mft = (struct master_file_table_record *) buf_mft;
749
750 attr_off = blkid_le16(mft->attrs_offset);
751 label_str[0] = 0;
752
753 while (1) {
754 attr = (struct file_attribute *) (buf_mft + attr_off);
755 attr_len = blkid_le16(attr->len);
756 attr_type = blkid_le32(attr->type);
757 val_off = blkid_le16(attr->value_offset);
758 val_len = blkid_le32(attr->value_len);
759
760 attr_off += attr_len;
761
762 if ((attr_off > mft_record_size) ||
763 (attr_len == 0))
764 break;
765
766 if (attr_type == MFT_RECORD_ATTR_END)
767 break;
768
769 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
770 if (val_len > sizeof(label_str))
771 val_len = sizeof(label_str)-1;
772
773 for (i=0, cp=label_str; i < val_len; i+=2,cp++) {
774 val = ((__u8 *) attr) + val_off + i;
775 *cp = val[0];
776 if (val[1])
777 *cp = '?';
778 }
779 *cp = 0;
780 }
781 }
782
783 sprintf(uuid_str, "%016llX", blkid_le64(ns->volume_serial));
784 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
785 if (label_str[0])
786 blkid_set_tag(probe->dev, "LABEL", label_str, 0);
787 return 0;
788 }
789
790
probe_xfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)791 static int probe_xfs(struct blkid_probe *probe,
792 struct blkid_magic *id __BLKID_ATTR((unused)),
793 unsigned char *buf)
794 {
795 struct xfs_super_block *xs;
796 const char *label = 0;
797
798 xs = (struct xfs_super_block *)buf;
799
800 if (strlen(xs->xs_fname))
801 label = xs->xs_fname;
802 blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname));
803 set_uuid(probe->dev, xs->xs_uuid, 0);
804 return 0;
805 }
806
probe_reiserfs(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)807 static int probe_reiserfs(struct blkid_probe *probe,
808 struct blkid_magic *id, unsigned char *buf)
809 {
810 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
811 unsigned int blocksize;
812 const char *label = 0;
813
814 blocksize = blkid_le16(rs->rs_blocksize);
815
816 /* The blocksize must be at least 1k */
817 if ((blocksize >> 10) == 0)
818 return -BLKID_ERR_PARAM;
819
820 /* If the superblock is inside the journal, we have the wrong one */
821 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
822 return -BLKID_ERR_BIG;
823
824 /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
825 if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') {
826 if (strlen(rs->rs_label))
827 label = rs->rs_label;
828 set_uuid(probe->dev, rs->rs_uuid, 0);
829 }
830 blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label));
831
832 return 0;
833 }
834
probe_reiserfs4(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)835 static int probe_reiserfs4(struct blkid_probe *probe,
836 struct blkid_magic *id __BLKID_ATTR((unused)),
837 unsigned char *buf)
838 {
839 struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf;
840 const unsigned char *label = 0;
841
842 if (strlen((char *) rs4->rs4_label))
843 label = rs4->rs4_label;
844 set_uuid(probe->dev, rs4->rs4_uuid, 0);
845 blkid_set_tag(probe->dev, "LABEL", (const char *) label,
846 sizeof(rs4->rs4_label));
847
848 return 0;
849 }
850
probe_jfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)851 static int probe_jfs(struct blkid_probe *probe,
852 struct blkid_magic *id __BLKID_ATTR((unused)),
853 unsigned char *buf)
854 {
855 struct jfs_super_block *js;
856 const char *label = 0;
857
858 js = (struct jfs_super_block *)buf;
859
860 if (blkid_le32(js->js_bsize) != (1U << blkid_le16(js->js_l2bsize)))
861 return 1;
862
863 if (blkid_le32(js->js_pbsize) != (1U << blkid_le16(js->js_l2pbsize)))
864 return 1;
865
866 if ((blkid_le16(js->js_l2bsize) - blkid_le16(js->js_l2pbsize)) !=
867 blkid_le16(js->js_l2bfactor))
868 return 1;
869
870 if (strlen((char *) js->js_label))
871 label = (char *) js->js_label;
872 blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label));
873 set_uuid(probe->dev, js->js_uuid, 0);
874 return 0;
875 }
876
probe_zfs(struct blkid_probe * probe __BLKID_ATTR ((unused)),struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf __BLKID_ATTR ((unused)))877 static int probe_zfs(struct blkid_probe *probe __BLKID_ATTR((unused)),
878 struct blkid_magic *id __BLKID_ATTR((unused)),
879 unsigned char *buf __BLKID_ATTR((unused)))
880 {
881 #if 0
882 char *vdev_label;
883 const char *pool_name = 0;
884
885 /* read nvpair data for pool name, pool GUID (complex) */
886 blkid_set_tag(probe->dev, "LABEL", pool_name, sizeof(pool_name));
887 set_uuid(probe->dev, pool_guid, 0);
888 #endif
889 return 0;
890 }
891
probe_luks(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)892 static int probe_luks(struct blkid_probe *probe,
893 struct blkid_magic *id __BLKID_ATTR((unused)),
894 unsigned char *buf)
895 {
896 char uuid[41];
897
898 /* 168 is the offset to the 40 character uuid:
899 * http://luks.endorphin.org/LUKS-on-disk-format.pdf */
900 strncpy(uuid, (char *) buf+168, 40);
901 uuid[40] = 0;
902 blkid_set_tag(probe->dev, "UUID", uuid, 40);
903 return 0;
904 }
905
probe_romfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)906 static int probe_romfs(struct blkid_probe *probe,
907 struct blkid_magic *id __BLKID_ATTR((unused)),
908 unsigned char *buf)
909 {
910 struct romfs_super_block *ros;
911 const char *label = 0;
912
913 ros = (struct romfs_super_block *)buf;
914
915 if (strlen((char *) ros->ros_volume))
916 label = (char *) ros->ros_volume;
917 blkid_set_tag(probe->dev, "LABEL", label, 0);
918 return 0;
919 }
920
probe_cramfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)921 static int probe_cramfs(struct blkid_probe *probe,
922 struct blkid_magic *id __BLKID_ATTR((unused)),
923 unsigned char *buf)
924 {
925 struct cramfs_super_block *csb;
926 const char *label = 0;
927
928 csb = (struct cramfs_super_block *)buf;
929
930 if (strlen((char *) csb->name))
931 label = (char *) csb->name;
932 blkid_set_tag(probe->dev, "LABEL", label, 0);
933 return 0;
934 }
935
probe_swap0(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf __BLKID_ATTR ((unused)))936 static int probe_swap0(struct blkid_probe *probe,
937 struct blkid_magic *id __BLKID_ATTR((unused)),
938 unsigned char *buf __BLKID_ATTR((unused)))
939 {
940 blkid_set_tag(probe->dev, "UUID", 0, 0);
941 blkid_set_tag(probe->dev, "LABEL", 0, 0);
942 return 0;
943 }
944
probe_swap1(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf __BLKID_ATTR ((unused)))945 static int probe_swap1(struct blkid_probe *probe,
946 struct blkid_magic *id,
947 unsigned char *buf __BLKID_ATTR((unused)))
948 {
949 struct swap_id_block *sws;
950
951 probe_swap0(probe, id, buf);
952 /*
953 * Version 1 swap headers are always located at offset of 1024
954 * bytes, although the swap signature itself is located at the
955 * end of the page (which may vary depending on hardware
956 * pagesize).
957 */
958 sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024);
959 if (!sws)
960 return 1;
961
962 /* check for wrong version or zeroed pagecount, for sanity */
963 if (!memcmp(id->bim_magic, "SWAPSPACE2", id->bim_len) &&
964 (sws->sws_version != 1 || sws->sws_lastpage == 0))
965 return 1;
966
967 /* arbitrary sanity check.. is there any garbage down there? */
968 if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) {
969 if (sws->sws_volume[0])
970 blkid_set_tag(probe->dev, "LABEL", sws->sws_volume,
971 sizeof(sws->sws_volume));
972 if (sws->sws_uuid[0])
973 set_uuid(probe->dev, sws->sws_uuid, 0);
974 }
975 return 0;
976 }
977
probe_iso9660(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)978 static int probe_iso9660(struct blkid_probe *probe,
979 struct blkid_magic *id __BLKID_ATTR((unused)),
980 unsigned char *buf)
981 {
982 struct iso_volume_descriptor *iso;
983 const unsigned char *label;
984
985 iso = (struct iso_volume_descriptor *) buf;
986 label = iso->volume_id;
987
988 blkid_set_tag(probe->dev, "LABEL", (const char *) label,
989 figure_label_len(label, 32));
990 return 0;
991 }
992
993
994 static const char
995 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
996 "NSR03", "TEA01", 0 };
997
probe_udf(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf __BLKID_ATTR ((unused)))998 static int probe_udf(struct blkid_probe *probe,
999 struct blkid_magic *id __BLKID_ATTR((unused)),
1000 unsigned char *buf __BLKID_ATTR((unused)))
1001 {
1002 int j, bs;
1003 struct iso_volume_descriptor *isosb;
1004 const char ** m;
1005
1006 /* determine the block size by scanning in 2K increments
1007 (block sizes larger than 2K will be null padded) */
1008 for (bs = 1; bs < 16; bs++) {
1009 isosb = (struct iso_volume_descriptor *)
1010 get_buffer(probe, (blkid_loff_t) bs*2048+32768,
1011 sizeof(*isosb));
1012 if (!isosb)
1013 return 1;
1014 if (isosb->vd_id[0])
1015 break;
1016 }
1017
1018 /* Scan up to another 64 blocks looking for additional VSD's */
1019 for (j = 1; j < 64; j++) {
1020 if (j > 1) {
1021 isosb = (struct iso_volume_descriptor *)
1022 get_buffer(probe, j*bs*2048+32768,
1023 sizeof(*isosb));
1024 if (!isosb)
1025 return 1;
1026 }
1027 /* If we find NSR0x then call it udf:
1028 NSR01 for UDF 1.00
1029 NSR02 for UDF 1.50
1030 NSR03 for UDF 2.00 */
1031 if (!memcmp(isosb->vd_id, "NSR0", 4))
1032 return probe_iso9660(probe, id, buf);
1033 for (m = udf_magic; *m; m++)
1034 if (!memcmp(*m, isosb->vd_id, 5))
1035 break;
1036 if (*m == 0)
1037 return 1;
1038 }
1039 return 1;
1040 }
1041
probe_ocfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1042 static int probe_ocfs(struct blkid_probe *probe,
1043 struct blkid_magic *id __BLKID_ATTR((unused)),
1044 unsigned char *buf)
1045 {
1046 struct ocfs_volume_header ovh;
1047 struct ocfs_volume_label ovl;
1048 __u32 major;
1049
1050 memcpy(&ovh, buf, sizeof(ovh));
1051 memcpy(&ovl, buf+512, sizeof(ovl));
1052
1053 major = ocfsmajor(ovh);
1054 if (major == 1)
1055 blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
1056 else if (major >= 9)
1057 blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
1058
1059 blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl));
1060 blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
1061 set_uuid(probe->dev, ovl.vol_id, 0);
1062 return 0;
1063 }
1064
probe_ocfs2(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1065 static int probe_ocfs2(struct blkid_probe *probe,
1066 struct blkid_magic *id __BLKID_ATTR((unused)),
1067 unsigned char *buf)
1068 {
1069 struct ocfs2_super_block *osb;
1070
1071 osb = (struct ocfs2_super_block *)buf;
1072
1073 blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
1074 set_uuid(probe->dev, osb->s_uuid, 0);
1075 return 0;
1076 }
1077
probe_oracleasm(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1078 static int probe_oracleasm(struct blkid_probe *probe,
1079 struct blkid_magic *id __BLKID_ATTR((unused)),
1080 unsigned char *buf)
1081 {
1082 struct oracle_asm_disk_label *dl;
1083
1084 dl = (struct oracle_asm_disk_label *)buf;
1085
1086 blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
1087 return 0;
1088 }
1089
probe_gfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1090 static int probe_gfs(struct blkid_probe *probe,
1091 struct blkid_magic *id __BLKID_ATTR((unused)),
1092 unsigned char *buf)
1093 {
1094 struct gfs2_sb *sbd;
1095 const char *label = 0;
1096
1097 sbd = (struct gfs2_sb *)buf;
1098
1099 if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS &&
1100 blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI)
1101 {
1102 blkid_set_tag(probe->dev, "UUID", 0, 0);
1103
1104 if (strlen(sbd->sb_locktable))
1105 label = sbd->sb_locktable;
1106 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1107 return 0;
1108 }
1109 return 1;
1110 }
1111
probe_gfs2(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1112 static int probe_gfs2(struct blkid_probe *probe,
1113 struct blkid_magic *id __BLKID_ATTR((unused)),
1114 unsigned char *buf)
1115 {
1116 struct gfs2_sb *sbd;
1117 const char *label = 0;
1118
1119 sbd = (struct gfs2_sb *)buf;
1120
1121 if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS &&
1122 blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI)
1123 {
1124 blkid_set_tag(probe->dev, "UUID", 0, 0);
1125
1126 if (strlen(sbd->sb_locktable))
1127 label = sbd->sb_locktable;
1128 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1129 return 0;
1130 }
1131 return 1;
1132 }
1133
unicode_16be_to_utf8(unsigned char * str,int out_len,const unsigned char * buf,int in_len)1134 static void unicode_16be_to_utf8(unsigned char *str, int out_len,
1135 const unsigned char *buf, int in_len)
1136 {
1137 int i, j;
1138 unsigned int c;
1139
1140 for (i = j = 0; i + 2 <= in_len; i += 2) {
1141 c = (buf[i] << 8) | buf[i+1];
1142 if (c == 0) {
1143 str[j] = '\0';
1144 break;
1145 } else if (c < 0x80) {
1146 if (j+1 >= out_len)
1147 break;
1148 str[j++] = (unsigned char) c;
1149 } else if (c < 0x800) {
1150 if (j+2 >= out_len)
1151 break;
1152 str[j++] = (unsigned char) (0xc0 | (c >> 6));
1153 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1154 } else {
1155 if (j+3 >= out_len)
1156 break;
1157 str[j++] = (unsigned char) (0xe0 | (c >> 12));
1158 str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f));
1159 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1160 }
1161 }
1162 str[j] = '\0';
1163 }
1164
unicode_16le_to_utf8(unsigned char * str,int out_len,const unsigned char * buf,int in_len)1165 static void unicode_16le_to_utf8(unsigned char *str, int out_len,
1166 const unsigned char *buf, int in_len)
1167 {
1168 int i, j;
1169 unsigned int c;
1170
1171 for (i = j = 0; i + 2 <= in_len; i += 2) {
1172 c = (buf[i+1] << 8) | buf[i];
1173 if (c == 0) {
1174 str[j] = '\0';
1175 break;
1176 } else if (c < 0x80) {
1177 if (j+1 >= out_len)
1178 break;
1179 str[j++] = (unsigned char) c;
1180 } else if (c < 0x800) {
1181 if (j+2 >= out_len)
1182 break;
1183 str[j++] = (unsigned char) (0xc0 | (c >> 6));
1184 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1185 } else {
1186 if (j+3 >= out_len)
1187 break;
1188 str[j++] = (unsigned char) (0xe0 | (c >> 12));
1189 str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f));
1190 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1191 }
1192 }
1193 str[j] = '\0';
1194 }
1195
probe_hfs(struct blkid_probe * probe __BLKID_ATTR ((unused)),struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1196 static int probe_hfs(struct blkid_probe *probe __BLKID_ATTR((unused)),
1197 struct blkid_magic *id __BLKID_ATTR((unused)),
1198 unsigned char *buf)
1199 {
1200 struct hfs_mdb *hfs = (struct hfs_mdb *)buf;
1201 unsigned long long *uuid_ptr;
1202 char uuid_str[17];
1203 __u64 uuid;
1204
1205 if ((memcmp(hfs->embed_sig, "H+", 2) == 0) ||
1206 (memcmp(hfs->embed_sig, "HX", 2) == 0))
1207 return 1; /* Not hfs, but an embedded HFS+ */
1208
1209 uuid_ptr = (unsigned long long *)hfs->finder_info.id;
1210 uuid = blkid_le64(*uuid_ptr);
1211 if (uuid) {
1212 sprintf(uuid_str, "%016llX", uuid);
1213 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1214 }
1215 blkid_set_tag(probe->dev, "LABEL", (char *)hfs->label, hfs->label_len);
1216 return 0;
1217 }
1218
1219
1220 #define HFSPLUS_SECTOR_SIZE 512
1221
probe_hfsplus(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)1222 static int probe_hfsplus(struct blkid_probe *probe,
1223 struct blkid_magic *id,
1224 unsigned char *buf)
1225 {
1226 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
1227 struct hfsplus_bnode_descriptor *descr;
1228 struct hfsplus_bheader_record *bnode;
1229 struct hfsplus_catalog_key *key;
1230 struct hfsplus_vol_header *hfsplus;
1231 struct hfs_mdb *sbd = (struct hfs_mdb *) buf;
1232 unsigned int alloc_block_size;
1233 unsigned int alloc_first_block;
1234 unsigned int embed_first_block;
1235 unsigned int off = 0;
1236 unsigned int blocksize;
1237 unsigned int cat_block;
1238 unsigned int ext_block_start;
1239 unsigned int ext_block_count;
1240 unsigned int record_count;
1241 unsigned int leaf_node_head;
1242 unsigned int leaf_node_count;
1243 unsigned int leaf_node_size;
1244 unsigned int leaf_block;
1245 unsigned int label_len;
1246 unsigned long long *uuid_ptr;
1247 __u64 leaf_off, uuid;
1248 char uuid_str[17], label[512];
1249 int ext;
1250
1251 /* Check for a HFS+ volume embedded in a HFS volume */
1252 if (memcmp(sbd->signature, "BD", 2) == 0) {
1253 if ((memcmp(sbd->embed_sig, "H+", 2) != 0) &&
1254 (memcmp(sbd->embed_sig, "HX", 2) != 0))
1255 /* This must be an HFS volume, so fail */
1256 return 1;
1257
1258 alloc_block_size = blkid_be32(sbd->al_blk_size);
1259 alloc_first_block = blkid_be16(sbd->al_bl_st);
1260 embed_first_block = blkid_be16(sbd->embed_startblock);
1261 off = (alloc_first_block * 512) +
1262 (embed_first_block * alloc_block_size);
1263 buf = get_buffer(probe, off + (id->bim_kboff * 1024),
1264 sizeof(*sbd));
1265 if (!buf)
1266 return 1;
1267
1268 hfsplus = (struct hfsplus_vol_header *) buf;
1269 }
1270
1271 hfsplus = (struct hfsplus_vol_header *) buf;
1272
1273 if ((memcmp(hfsplus->signature, "H+", 2) != 0) &&
1274 (memcmp(hfsplus->signature, "HX", 2) != 0))
1275 return 1;
1276
1277 uuid_ptr = (unsigned long long *)hfsplus->finder_info.id;
1278 uuid = blkid_le64(*uuid_ptr);
1279 if (uuid) {
1280 sprintf(uuid_str, "%016llX", uuid);
1281 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1282 }
1283
1284 blocksize = blkid_be32(hfsplus->blocksize);
1285 if (blocksize < HFSPLUS_SECTOR_SIZE)
1286 return 1;
1287
1288 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
1289 cat_block = blkid_be32(extents[0].start_block);
1290
1291 buf = get_buffer(probe, off + ((__u64) cat_block * blocksize), 0x2000);
1292 if (!buf)
1293 return 0;
1294
1295 bnode = (struct hfsplus_bheader_record *)
1296 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1297
1298 leaf_node_head = blkid_be32(bnode->leaf_head);
1299 leaf_node_size = blkid_be16(bnode->node_size);
1300 leaf_node_count = blkid_be32(bnode->leaf_count);
1301 if (leaf_node_count == 0)
1302 return 0;
1303
1304 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
1305
1306 /* get physical location */
1307 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
1308 ext_block_start = blkid_be32(extents[ext].start_block);
1309 ext_block_count = blkid_be32(extents[ext].block_count);
1310 if (ext_block_count == 0)
1311 return 0;
1312
1313 /* this is our extent */
1314 if (leaf_block < ext_block_count)
1315 break;
1316
1317 leaf_block -= ext_block_count;
1318 }
1319 if (ext == HFSPLUS_EXTENT_COUNT)
1320 return 0;
1321
1322 leaf_off = (__u64) (ext_block_start + leaf_block) * blocksize;
1323
1324 buf = get_buffer(probe, off + leaf_off, leaf_node_size);
1325 if (!buf)
1326 return 0;
1327
1328 descr = (struct hfsplus_bnode_descriptor *) buf;
1329 record_count = blkid_be16(descr->num_recs);
1330 if (record_count == 0)
1331 return 0;
1332
1333 if (descr->type != HFS_NODE_LEAF)
1334 return 0;
1335
1336 key = (struct hfsplus_catalog_key *)
1337 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1338
1339 if (blkid_be32(key->parent_id) != HFSPLUS_POR_CNID)
1340 return 0;
1341
1342 label_len = blkid_be16(key->unicode_len) * 2;
1343 unicode_16be_to_utf8((unsigned char *)label, sizeof(label),
1344 key->unicode, label_len);
1345 blkid_set_tag(probe->dev, "LABEL", label, 0);
1346 return 0;
1347 }
1348
1349 #define LVM2_LABEL_SIZE 512
lvm2_calc_crc(const void * buf,unsigned int size)1350 static unsigned int lvm2_calc_crc(const void *buf, unsigned int size)
1351 {
1352 static const unsigned int crctab[] = {
1353 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1354 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1355 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1356 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1357 };
1358 unsigned int i, crc = 0xf597a6cf;
1359 const __u8 *data = (const __u8 *) buf;
1360
1361 for (i = 0; i < size; i++) {
1362 crc ^= *data++;
1363 crc = (crc >> 4) ^ crctab[crc & 0xf];
1364 crc = (crc >> 4) ^ crctab[crc & 0xf];
1365 }
1366 return crc;
1367 }
1368
probe_lvm2(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)1369 static int probe_lvm2(struct blkid_probe *probe,
1370 struct blkid_magic *id,
1371 unsigned char *buf)
1372 {
1373 int sector = (id->bim_kboff) << 1;
1374 struct lvm2_pv_label_header *label= (struct lvm2_pv_label_header *)buf;
1375 char *p, *q, uuid[40];
1376 unsigned int i, b;
1377
1378 /* buf is at 0k or 1k offset; find label inside */
1379 if (memcmp(buf, "LABELONE", 8) == 0) {
1380 label = (struct lvm2_pv_label_header *)buf;
1381 } else if (memcmp(buf + 512, "LABELONE", 8) == 0) {
1382 label = (struct lvm2_pv_label_header *)(buf + 512);
1383 sector++;
1384 } else {
1385 return 1;
1386 }
1387
1388 if (blkid_le64(label->sector_xl) != (unsigned) sector) {
1389 DBG(DEBUG_PROBE,
1390 printf("LVM2: label for sector %llu found at sector %d\n",
1391 blkid_le64(label->sector_xl), sector));
1392 return 1;
1393 }
1394
1395 if (lvm2_calc_crc(&label->offset_xl, LVM2_LABEL_SIZE -
1396 ((char *)&label->offset_xl - (char *)label)) !=
1397 blkid_le32(label->crc_xl)) {
1398 DBG(DEBUG_PROBE,
1399 printf("LVM2: label checksum incorrect at sector %d\n",
1400 sector));
1401 return 1;
1402 }
1403
1404 for (i=0, b=1, p=uuid, q= (char *) label->pv_uuid; i < LVM2_ID_LEN;
1405 i++, b <<= 1) {
1406 if (b & 0x4444440)
1407 *p++ = '-';
1408 *p++ = *q++;
1409 }
1410
1411 blkid_set_tag(probe->dev, "UUID", uuid, LVM2_ID_LEN+6);
1412
1413 return 0;
1414 }
1415
probe_btrfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1416 static int probe_btrfs(struct blkid_probe *probe,
1417 struct blkid_magic *id __BLKID_ATTR((unused)),
1418 unsigned char *buf)
1419 {
1420 struct btrfs_super_block *bs;
1421 const char *label = 0;
1422
1423 bs = (struct btrfs_super_block *)buf;
1424
1425 if (strlen(bs->label))
1426 label = bs->label;
1427 blkid_set_tag(probe->dev, "LABEL", label, sizeof(bs->label));
1428 set_uuid(probe->dev, bs->fsid, 0);
1429 return 0;
1430 }
1431
probe_f2fs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1432 static int probe_f2fs(struct blkid_probe *probe,
1433 struct blkid_magic *id __BLKID_ATTR((unused)),
1434 unsigned char *buf)
1435 {
1436 struct f2fs_super_block *bs;
1437
1438 bs = (struct f2fs_super_block *)buf;
1439 set_uuid(probe->dev, bs->uuid, 0);
1440 return 0;
1441 }
1442
exfat_block_to_offset(const struct exfat_super_block * sb,uint64_t block)1443 static uint64_t exfat_block_to_offset(const struct exfat_super_block *sb,
1444 uint64_t block)
1445 {
1446 return block << sb->block_bits;
1447 }
1448
exfat_cluster_to_block(const struct exfat_super_block * sb,uint32_t cluster)1449 static uint64_t exfat_cluster_to_block(const struct exfat_super_block *sb,
1450 uint32_t cluster)
1451 {
1452 return sb->cluster_block_start +
1453 ((uint64_t)(cluster - EXFAT_FIRST_DATA_CLUSTER) << sb->bpc_bits);
1454 }
1455
exfat_cluster_to_offset(const struct exfat_super_block * sb,uint32_t cluster)1456 static uint64_t exfat_cluster_to_offset(const struct exfat_super_block *sb,
1457 uint32_t cluster)
1458 {
1459 return exfat_block_to_offset(sb, exfat_cluster_to_block(sb, cluster));
1460 }
1461
exfat_next_cluster(struct blkid_probe * probe,const struct exfat_super_block * sb,uint32_t cluster)1462 static uint32_t exfat_next_cluster(struct blkid_probe *probe,
1463 const struct exfat_super_block *sb,
1464 uint32_t cluster)
1465 {
1466 uint32_t *next;
1467 uint64_t offset;
1468
1469 offset = exfat_block_to_offset(sb, sb->fat_block_start)
1470 + (uint64_t) cluster * sizeof (cluster);
1471 next = (uint32_t *)get_buffer(probe, offset, sizeof (uint32_t));
1472
1473 return next ? *next : 0;
1474 }
1475
find_exfat_entry_label(struct blkid_probe * probe,const struct exfat_super_block * sb)1476 static struct exfat_entry_label *find_exfat_entry_label(
1477 struct blkid_probe *probe, const struct exfat_super_block *sb)
1478 {
1479 uint32_t cluster = sb->rootdir_cluster;
1480 uint64_t offset = exfat_cluster_to_offset(sb, cluster);
1481 uint8_t *entry;
1482 const size_t max_iter = 10000;
1483 size_t i = 0;
1484
1485 for (; i < max_iter; ++i) {
1486 entry = (uint8_t *)get_buffer(probe, offset, EXFAT_ENTRY_SIZE);
1487 if (!entry)
1488 return NULL;
1489 if (entry[0] == EXFAT_ENTRY_EOD)
1490 return NULL;
1491 if (entry[0] == EXFAT_ENTRY_LABEL)
1492 return (struct exfat_entry_label*) entry;
1493
1494 offset += EXFAT_ENTRY_SIZE;
1495 if (offset % CLUSTER_SIZE(sb) == 0) {
1496 cluster = exfat_next_cluster(probe, sb, cluster);
1497 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
1498 return NULL;
1499 if (cluster > EXFAT_LAST_DATA_CLUSTER)
1500 return NULL;
1501 offset = exfat_cluster_to_offset(sb, cluster);
1502 }
1503 }
1504
1505 return NULL;
1506 }
1507
probe_exfat(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1508 static int probe_exfat(struct blkid_probe *probe,
1509 struct blkid_magic *id __BLKID_ATTR((unused)),
1510 unsigned char *buf)
1511 {
1512 struct exfat_super_block *sb;
1513 struct exfat_entry_label *label;
1514 char uuid[40];
1515
1516 sb = (struct exfat_super_block *)buf;
1517 if (!sb || CLUSTER_SIZE(sb) == 0) {
1518 DBG(DEBUG_PROBE, printf("bad exfat superblock.\n"));
1519 return errno ? - errno : 1;
1520 }
1521
1522 label = find_exfat_entry_label(probe, sb);
1523 if (label) {
1524 unsigned char utf8_label[128];
1525 unicode_16le_to_utf8(utf8_label, sizeof(utf8_label), label->name, label->length * 2);
1526 blkid_set_tag(probe->dev, "LABEL", (char *) utf8_label, 0);
1527 } else {
1528 blkid_set_tag(probe->dev, "LABEL", "disk", 4);
1529 }
1530
1531 memset(uuid, 0, sizeof (uuid));
1532 snprintf(uuid, sizeof (uuid), "%02hhX%02hhX-%02hhX%02hhX",
1533 sb->volume_serial[3], sb->volume_serial[2],
1534 sb->volume_serial[1], sb->volume_serial[0]);
1535 blkid_set_tag(probe->dev, "UUID", uuid, strlen(uuid));
1536
1537 return 0;
1538 }
1539
1540 /*
1541 * Various filesystem magics that we can check for. Note that kboff and
1542 * sboff are in kilobytes and bytes respectively. All magics are in
1543 * byte strings so we don't worry about endian issues.
1544 */
1545 static struct blkid_magic type_array[] = {
1546 /* type kboff sboff len magic probe */
1547 { "oracleasm", 0, 32, 8, "ORCLDISK", probe_oracleasm },
1548 { "ntfs", 0, 3, 8, "NTFS ", probe_ntfs },
1549 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd },
1550 { "ext4dev", 1, 0x38, 2, "\123\357", probe_ext4dev },
1551 { "ext4", 1, 0x38, 2, "\123\357", probe_ext4 },
1552 { "ext3", 1, 0x38, 2, "\123\357", probe_ext3 },
1553 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 },
1554 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs },
1555 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs },
1556 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs },
1557 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs },
1558 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs },
1559 { "reiser4", 64, 0, 7, "ReIsEr4", probe_reiserfs4 },
1560 { "gfs2", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs2 },
1561 { "gfs", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs },
1562 { "vfat", 0, 0x52, 5, "MSWIN", probe_fat },
1563 { "vfat", 0, 0x52, 8, "FAT32 ", probe_fat },
1564 { "vfat", 0, 0x36, 5, "MSDOS", probe_fat },
1565 { "vfat", 0, 0x36, 8, "FAT16 ", probe_fat },
1566 { "vfat", 0, 0x36, 8, "FAT12 ", probe_fat },
1567 { "vfat", 0, 0, 1, "\353", probe_fat_nomagic },
1568 { "vfat", 0, 0, 1, "\351", probe_fat_nomagic },
1569 { "vfat", 0, 0x1fe, 2, "\125\252", probe_fat_nomagic },
1570 { "minix", 1, 0x10, 2, "\177\023", 0 },
1571 { "minix", 1, 0x10, 2, "\217\023", 0 },
1572 { "minix", 1, 0x10, 2, "\150\044", 0 },
1573 { "minix", 1, 0x10, 2, "\170\044", 0 },
1574 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 },
1575 { "xfs", 0, 0, 4, "XFSB", probe_xfs },
1576 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs },
1577 { "bfs", 0, 0, 4, "\316\372\173\033", 0 },
1578 { "cramfs", 0, 0, 4, "E=\315\050", probe_cramfs },
1579 { "qnx4", 0, 4, 6, "QNX4FS", 0 },
1580 { "udf", 32, 1, 5, "BEA01", probe_udf },
1581 { "udf", 32, 1, 5, "BOOT2", probe_udf },
1582 { "udf", 32, 1, 5, "CD001", probe_udf },
1583 { "udf", 32, 1, 5, "CDW02", probe_udf },
1584 { "udf", 32, 1, 5, "NSR02", probe_udf },
1585 { "udf", 32, 1, 5, "NSR03", probe_udf },
1586 { "udf", 32, 1, 5, "TEA01", probe_udf },
1587 { "iso9660", 32, 1, 5, "CD001", probe_iso9660 },
1588 { "iso9660", 32, 9, 5, "CDROM", probe_iso9660 },
1589 { "jfs", 32, 0, 4, "JFS1", probe_jfs },
1590 /* ZFS has 128 root blocks (#4 is the first used), check only 6 of them */
1591 { "zfs", 128, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1592 { "zfs", 128, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1593 { "zfs", 132, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1594 { "zfs", 132, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1595 { "zfs", 136, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1596 { "zfs", 136, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1597 { "zfs", 384, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1598 { "zfs", 384, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1599 { "zfs", 388, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1600 { "zfs", 388, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1601 { "zfs", 392, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1602 { "zfs", 392, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1603 { "hfsplus", 1, 0, 2, "BD", probe_hfsplus },
1604 { "hfsplus", 1, 0, 2, "H+", probe_hfsplus },
1605 { "hfsplus", 1, 0, 2, "HX", probe_hfsplus },
1606 { "hfs", 1, 0, 2, "BD", probe_hfs },
1607 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 },
1608 { "hpfs", 8, 0, 4, "I\350\225\371", 0 },
1609 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 },
1610 { "swap", 0, 0xff6, 10, "SWAP-SPACE", probe_swap0 },
1611 { "swap", 0, 0xff6, 10, "SWAPSPACE2", probe_swap1 },
1612 { "swsuspend", 0, 0xff6, 9, "S1SUSPEND", probe_swap1 },
1613 { "swsuspend", 0, 0xff6, 9, "S2SUSPEND", probe_swap1 },
1614 { "swsuspend", 0, 0xff6, 9, "ULSUSPEND", probe_swap1 },
1615 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", probe_swap0 },
1616 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", probe_swap1 },
1617 { "swsuspend", 0, 0x1ff6, 9, "S1SUSPEND", probe_swap1 },
1618 { "swsuspend", 0, 0x1ff6, 9, "S2SUSPEND", probe_swap1 },
1619 { "swsuspend", 0, 0x1ff6, 9, "ULSUSPEND", probe_swap1 },
1620 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", probe_swap0 },
1621 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", probe_swap1 },
1622 { "swsuspend", 0, 0x3ff6, 9, "S1SUSPEND", probe_swap1 },
1623 { "swsuspend", 0, 0x3ff6, 9, "S2SUSPEND", probe_swap1 },
1624 { "swsuspend", 0, 0x3ff6, 9, "ULSUSPEND", probe_swap1 },
1625 { "swap", 0, 0x7ff6, 10, "SWAP-SPACE", probe_swap0 },
1626 { "swap", 0, 0x7ff6, 10, "SWAPSPACE2", probe_swap1 },
1627 { "swsuspend", 0, 0x7ff6, 9, "S1SUSPEND", probe_swap1 },
1628 { "swsuspend", 0, 0x7ff6, 9, "S2SUSPEND", probe_swap1 },
1629 { "swsuspend", 0, 0x7ff6, 9, "ULSUSPEND", probe_swap1 },
1630 { "swap", 0, 0xfff6, 10, "SWAP-SPACE", probe_swap0 },
1631 { "swap", 0, 0xfff6, 10, "SWAPSPACE2", probe_swap1 },
1632 { "swsuspend", 0, 0xfff6, 9, "S1SUSPEND", probe_swap1 },
1633 { "swsuspend", 0, 0xfff6, 9, "S2SUSPEND", probe_swap1 },
1634 { "swsuspend", 0, 0xfff6, 9, "ULSUSPEND", probe_swap1 },
1635 { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs },
1636 { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 },
1637 { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 },
1638 { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 },
1639 { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 },
1640 { "crypt_LUKS", 0, 0, 6, "LUKS\xba\xbe", probe_luks },
1641 { "squashfs", 0, 0, 4, "sqsh", 0 },
1642 { "squashfs", 0, 0, 4, "hsqs", 0 },
1643 { "lvm2pv", 0, 0x218, 8, "LVM2 001", probe_lvm2 },
1644 { "lvm2pv", 0, 0x018, 8, "LVM2 001", probe_lvm2 },
1645 { "lvm2pv", 1, 0x018, 8, "LVM2 001", probe_lvm2 },
1646 { "lvm2pv", 1, 0x218, 8, "LVM2 001", probe_lvm2 },
1647 { "btrfs", 64, 0x40, 8, "_BHRfS_M", probe_btrfs },
1648 { "f2fs", 1, 0, 4, "\x10\x20\xf5\xf2", probe_f2fs },
1649 { "exfat", 0, 3, 8, "EXFAT ", probe_exfat },
1650 { NULL, 0, 0, 0, NULL, NULL }
1651 };
1652
1653 /*
1654 * Verify that the data in dev is consistent with what is on the actual
1655 * block device (using the devname field only). Normally this will be
1656 * called when finding items in the cache, but for long running processes
1657 * is also desirable to revalidate an item before use.
1658 *
1659 * If we are unable to revalidate the data, we return the old data and
1660 * do not set the BLKID_BID_FL_VERIFIED flag on it.
1661 */
blkid_verify(blkid_cache cache,blkid_dev dev)1662 blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
1663 {
1664 struct blkid_magic *id;
1665 struct blkid_probe probe;
1666 blkid_tag_iterate iter;
1667 unsigned char *buf;
1668 const char *type, *value;
1669 struct stat st;
1670 time_t now;
1671 double diff;
1672 int idx;
1673
1674 if (!dev)
1675 return NULL;
1676
1677 now = time(0);
1678 diff = difftime(now, dev->bid_time);
1679
1680 if (stat(dev->bid_name, &st) < 0) {
1681 DBG(DEBUG_PROBE,
1682 printf("blkid_verify: error %s (%d) while "
1683 "trying to stat %s\n", strerror(errno), errno,
1684 dev->bid_name));
1685 open_err:
1686 if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) {
1687 /* We don't have read permission, just return cache data. */
1688 DBG(DEBUG_PROBE, printf("returning unverified data for %s\n",
1689 dev->bid_name));
1690 return dev;
1691 }
1692 blkid_free_dev(dev);
1693 return NULL;
1694 }
1695
1696 if ((now >= dev->bid_time) &&
1697 (st.st_mtime <= dev->bid_time) &&
1698 ((diff < BLKID_PROBE_MIN) ||
1699 (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
1700 diff < BLKID_PROBE_INTERVAL)))
1701 return dev;
1702
1703 DBG(DEBUG_PROBE,
1704 printf("need to revalidate %s (cache time %lu, stat time %lu,\n\t"
1705 "time since last check %lu)\n",
1706 dev->bid_name, (unsigned long)dev->bid_time,
1707 (unsigned long)st.st_mtime, (unsigned long)diff));
1708
1709 if ((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) {
1710 DBG(DEBUG_PROBE, printf("blkid_verify: error %s (%d) while "
1711 "opening %s\n", strerror(errno), errno,
1712 dev->bid_name));
1713 goto open_err;
1714 }
1715
1716 probe.cache = cache;
1717 probe.dev = dev;
1718 probe.sbbuf = 0;
1719 probe.buf = 0;
1720 probe.buf_max = 0;
1721
1722 /*
1723 * Iterate over the type array. If we already know the type,
1724 * then try that first. If it doesn't work, then blow away
1725 * the type information, and try again.
1726 *
1727 */
1728 try_again:
1729 type = 0;
1730 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
1731 uuid_t uuid;
1732
1733 if (check_mdraid(probe.fd, uuid) == 0) {
1734 set_uuid(dev, uuid, 0);
1735 type = "mdraid";
1736 goto found_type;
1737 }
1738 }
1739 for (id = type_array; id->bim_type; id++) {
1740 if (dev->bid_type &&
1741 strcmp(id->bim_type, dev->bid_type))
1742 continue;
1743
1744 idx = id->bim_kboff + (id->bim_sboff >> 10);
1745 buf = get_buffer(&probe, (__u64) idx << 10, 1024);
1746 if (!buf)
1747 continue;
1748
1749 if (memcmp(id->bim_magic, buf + (id->bim_sboff & 0x3ff),
1750 id->bim_len))
1751 continue;
1752
1753 if ((id->bim_probe == NULL) ||
1754 (id->bim_probe(&probe, id, buf) == 0)) {
1755 type = id->bim_type;
1756 goto found_type;
1757 }
1758 }
1759
1760 if (!id->bim_type && dev->bid_type) {
1761 /*
1762 * Zap the device filesystem information and try again
1763 */
1764 DBG(DEBUG_PROBE,
1765 printf("previous fs type %s not valid, "
1766 "trying full probe\n", dev->bid_type));
1767 iter = blkid_tag_iterate_begin(dev);
1768 while (blkid_tag_next(iter, &type, &value) == 0)
1769 blkid_set_tag(dev, type, 0, 0);
1770 blkid_tag_iterate_end(iter);
1771 goto try_again;
1772 }
1773
1774 if (!dev->bid_type) {
1775 blkid_free_dev(dev);
1776 dev = 0;
1777 goto found_type;
1778 }
1779
1780 found_type:
1781 if (dev && type) {
1782 dev->bid_devno = st.st_rdev;
1783 dev->bid_time = time(0);
1784 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
1785 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
1786
1787 blkid_set_tag(dev, "TYPE", type, 0);
1788
1789 DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
1790 dev->bid_name, (long long)st.st_rdev, type));
1791 }
1792
1793 free(probe.sbbuf);
1794 free(probe.buf);
1795 if (probe.fd >= 0)
1796 close(probe.fd);
1797
1798 return dev;
1799 }
1800
blkid_known_fstype(const char * fstype)1801 int blkid_known_fstype(const char *fstype)
1802 {
1803 struct blkid_magic *id;
1804
1805 for (id = type_array; id->bim_type; id++) {
1806 if (strcmp(fstype, id->bim_type) == 0)
1807 return 1;
1808 }
1809 return 0;
1810 }
1811
1812 #ifdef TEST_PROGRAM
main(int argc,char ** argv)1813 int main(int argc, char **argv)
1814 {
1815 blkid_dev dev;
1816 blkid_cache cache;
1817 int ret;
1818
1819 if (argc != 2) {
1820 fprintf(stderr, "Usage: %s device\n"
1821 "Probe a single device to determine type\n", argv[0]);
1822 exit(1);
1823 }
1824 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
1825 fprintf(stderr, "%s: error creating cache (%d)\n",
1826 argv[0], ret);
1827 exit(1);
1828 }
1829 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
1830 if (!dev) {
1831 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
1832 return (1);
1833 }
1834 printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
1835 if (dev->bid_label)
1836 printf("LABEL='%s'\n", dev->bid_label);
1837 if (dev->bid_uuid)
1838 printf("UUID='%s'\n", dev->bid_uuid);
1839
1840 blkid_free_dev(dev);
1841 return (0);
1842 }
1843 #endif
1844