2 * probe.c - identify a block device by its contents, and return a dev
3 * struct with the details
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>
11 * This file may be redistributed under the terms of the
12 * GNU Lesser General Public License.
23 #include <sys/types.h>
24 #ifdef HAVE_SYS_STAT_H
27 #ifdef HAVE_SYS_MKDEV_H
28 #include <sys/mkdev.h>
31 #include <sys/utsname.h>
37 #include "uuid/uuid.h"
40 static int figure_label_len(const unsigned char *label, int len)
42 const unsigned char *end = label + len - 1;
44 while ((*end == ' ' || *end == 0) && end >= label)
47 return end - label + 1;
51 static unsigned char *get_buffer(struct blkid_probe *pr,
52 blkid_loff_t off, size_t len)
55 unsigned char *newbuf;
57 if (off + len <= SB_BUFFER_SIZE) {
59 pr->sbbuf = malloc(SB_BUFFER_SIZE);
62 if (lseek(pr->fd, 0, SEEK_SET) < 0)
64 ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE);
67 pr->sb_valid = ret_read;
69 if (off+len > pr->sb_valid)
71 return pr->sbbuf + off;
73 if (len > pr->buf_max) {
74 newbuf = realloc(pr->buf, len);
80 if (blkid_llseek(pr->fd, off, SEEK_SET) < 0)
82 ret_read = read(pr->fd, pr->buf, len);
83 if (ret_read != (ssize_t) len)
91 * This is a special case code to check for an MDRAID device. We do
92 * this special since it requires checking for a superblock at the end
95 static int check_mdraid(int fd, unsigned char *ret_uuid)
97 struct mdp_superblock_s *md;
102 return -BLKID_ERR_PARAM;
104 offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
106 if (blkid_llseek(fd, offset, 0) < 0 ||
107 read(fd, buf, 4096) != 4096)
108 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;
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);
127 static void set_uuid(blkid_dev dev, uuid_t uuid, const char *tag)
131 if (!uuid_is_null(uuid)) {
132 uuid_unparse(uuid, str);
133 blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
137 static void get_ext2_info(blkid_dev dev, struct blkid_magic *id,
140 struct ext2_super_block *es = (struct ext2_super_block *) buf;
141 const char *label = 0;
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)));
148 if (strlen(es->s_volume_name))
149 label = es->s_volume_name;
150 blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
152 set_uuid(dev, es->s_uuid, 0);
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");
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"));
165 * Check to see if a filesystem is in /proc/filesystems.
166 * Returns 1 if found, 0 if not
168 static int fs_proc_check(const char *fs_name)
171 char buf[80], *cp, *t;
173 f = fopen("/proc/filesystems", "r");
177 if (!fgets(buf, sizeof(buf), f))
181 while (*cp && !isspace(*cp))
184 while (*cp && isspace(*cp))
186 if ((t = strchr(cp, '\n')) != NULL)
188 if ((t = strchr(cp, '\t')) != NULL)
190 if ((t = strchr(cp, ' ')) != NULL)
192 if (!strcmp(fs_name, cp)) {
202 * Check to see if a filesystem is available as a module
203 * Returns 1 if found, 0 if not
205 static int check_for_modules(const char *fs_name)
215 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
221 namesz = strlen(fs_name);
224 if (!fgets(buf, sizeof(buf), f))
226 if ((cp = strchr(buf, ':')) != NULL)
230 if ((cp = strrchr(buf, '/')) != NULL)
234 if (!strncmp(cp, fs_name, namesz) &&
235 (!strcmp(cp + namesz, ".ko") ||
236 !strcmp(cp + namesz, ".ko.gz"))) {
246 static int linux_version_code()
250 static int version_code = -1;
251 int major, minor, rev;
255 if (version_code > 0)
262 major = strtol(cp, &endptr, 10);
263 if (cp == endptr || *endptr != '.')
266 minor = strtol(cp, &endptr, 10);
267 if (cp == endptr || *endptr != '.')
270 rev = strtol(cp, &endptr, 10);
273 version_code = (((major * 256) + minor) * 256) + rev;
280 #define EXT4_SUPPORTS_EXT2 (2 * 65536 + 6*256 + 29)
282 static int system_supports_ext2(void)
284 static time_t last_check = 0;
286 time_t now = time(0);
288 if (ret != -1 || (now - last_check) < 5)
291 ret = (fs_proc_check("ext2") || check_for_modules("ext2"));
295 static int system_supports_ext4(void)
297 static time_t last_check = 0;
299 time_t now = time(0);
301 if (ret != -1 || (now - last_check) < 5)
304 ret = (fs_proc_check("ext4") || check_for_modules("ext4"));
308 static int system_supports_ext4dev(void)
310 static time_t last_check = 0;
312 time_t now = time(0);
314 if (ret != -1 || (now - last_check) < 5)
317 ret = (fs_proc_check("ext4dev") || check_for_modules("ext4dev"));
321 static int probe_ext4dev(struct blkid_probe *probe,
322 struct blkid_magic *id,
325 struct ext2_super_block *es;
326 es = (struct ext2_super_block *)buf;
328 /* Distinguish from jbd */
329 if (blkid_le32(es->s_feature_incompat) &
330 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
331 return -BLKID_ERR_PARAM;
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.
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)
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.
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.
355 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
356 if (!system_supports_ext4dev() && system_supports_ext4())
357 return -BLKID_ERR_PARAM;
359 return -BLKID_ERR_PARAM;
362 get_ext2_info(probe->dev, id, buf);
366 static int probe_ext4(struct blkid_probe *probe, struct blkid_magic *id,
369 struct ext2_super_block *es;
370 es = (struct ext2_super_block *)buf;
372 /* Distinguish from jbd */
373 if (blkid_le32(es->s_feature_incompat) &
374 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
375 return -BLKID_ERR_PARAM;
378 * If the filesystem does not have a journal and ext2 is not
379 * present, then force this to be detected as an ext2
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)
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;
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.
402 * If the filesystem is marked as in use by production
403 * filesystem, then it can only be used by ext4 and NOT by
406 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
407 if (system_supports_ext4dev() || !system_supports_ext4())
408 return -BLKID_ERR_PARAM;
410 get_ext2_info(probe->dev, id, buf);
414 static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id,
417 struct ext2_super_block *es;
418 es = (struct ext2_super_block *)buf;
420 /* ext3 requires journal */
421 if (!(blkid_le32(es->s_feature_compat) &
422 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
423 return -BLKID_ERR_PARAM;
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;
432 get_ext2_info(probe->dev, id, buf);
436 static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id,
439 struct ext2_super_block *es;
441 es = (struct ext2_super_block *)buf;
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;
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;
456 * If ext2 is not present, but ext4 or ext4dev are, then
457 * disclaim we are ext2
459 if (!system_supports_ext2() &&
460 (system_supports_ext4() || system_supports_ext4dev()) &&
461 linux_version_code() >= EXT4_SUPPORTS_EXT2)
462 return -BLKID_ERR_PARAM;
464 get_ext2_info(probe->dev, id, buf);
468 static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id,
471 struct ext2_super_block *es = (struct ext2_super_block *) buf;
473 if (!(blkid_le32(es->s_feature_incompat) &
474 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
475 return -BLKID_ERR_PARAM;
477 get_ext2_info(probe->dev, id, buf);
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
488 static const char *no_name = "NO NAME ";
490 static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
494 for (i = 0; i < count; i++) {
495 if (dir[i].name[0] == 0x00)
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))
503 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
504 FAT_ATTR_VOLUME_ID) {
511 /* FAT label extraction from the root directory taken from Kay
512 * Sievers's volume_id library */
513 static int probe_fat(struct blkid_probe *probe,
514 struct blkid_magic *id __BLKID_ATTR((unused)),
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;
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;
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)
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);
541 sect_count = blkid_le32(ms->ms_total_sect);
543 fat_length = blkid_le16(ms->ms_fat_length);
545 fat_length = blkid_le32(vs->vs_fat32_length);
547 fat_size = fat_length * ms->ms_fats;
548 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
549 (sector_size-1)) / sector_size;
551 cluster_count = sect_count - (reserved + fat_size + dir_size);
552 if (ms->ms_cluster_size == 0)
554 cluster_count /= ms->ms_cluster_size;
556 if (cluster_count > FAT32_MAX)
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);
565 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
566 dir = (struct vfat_dir_entry *) get_buffer(probe, root_start,
569 vol_label = search_fat_label(dir, root_dir_entries);
571 if (!vol_label || !memcmp(vol_label, no_name, 11))
572 vol_label = ms->ms_label;
573 vol_serno = ms->ms_serno;
575 blkid_set_tag(probe->dev, "SEC_TYPE", "msdos",
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;
582 next = blkid_le32(vs->vs_root_cluster);
583 while (next && --maxloop) {
585 __u64 next_off, fat_entry_off;
588 next_sect_off = (next - 2) * vs->vs_cluster_size;
589 next_off = (start_data_sect + next_sect_off) *
592 dir = (struct vfat_dir_entry *)
593 get_buffer(probe, next_off, buf_size);
597 count = buf_size / sizeof(struct vfat_dir_entry);
599 vol_label = search_fat_label(dir, count);
604 fat_entry_off = (reserved * sector_size) +
605 (next * sizeof(__u32));
606 buf = get_buffer(probe, fat_entry_off, buf_size);
610 /* set next cluster */
611 next = blkid_le32(*((__u32 *) buf) & 0x0fffffff);
614 if (!vol_label || !memcmp(vol_label, no_name, 11))
615 vol_label = vs->vs_label;
616 vol_serno = vs->vs_serno;
619 if (vol_label && memcmp(vol_label, no_name, 11)) {
620 if ((label_len = figure_label_len(vol_label, 11)))
624 /* We can't just print them as %04X, because they are unaligned */
625 sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
626 vol_serno[1], vol_serno[0]);
628 blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len);
629 blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1);
635 * The FAT filesystem could be without a magic string in superblock
636 * (e.g. old floppies). This heuristic for FAT detection is inspired
637 * by http://vrfy.org/projects/volume_id/ and Linux kernel.
638 * [7-Jul-2005, Karel Zak <kzak@redhat.com>]
640 static int probe_fat_nomagic(struct blkid_probe *probe,
641 struct blkid_magic *id __BLKID_ATTR((unused)),
644 struct msdos_super_block *ms;
646 ms = (struct msdos_super_block *)buf;
649 if (ms->ms_heads == 0)
652 /* cluster size check*/
653 if (ms->ms_cluster_size == 0 ||
654 (ms->ms_cluster_size & (ms->ms_cluster_size-1)))
658 if (ms->ms_media < 0xf8 && ms->ms_media != 0xf0)
661 /* fat counts(Linux kernel expects at least 1 FAT table) */
666 * OS/2 and apparently DFSee will place a FAT12/16-like
667 * pseudo-superblock in the first 512 bytes of non-FAT
668 * filesystems --- at least JFS and HPFS, and possibly others.
669 * So we explicitly check for those filesystems at the
670 * FAT12/16 filesystem magic field identifier, and if they are
671 * present, we rule this out as a FAT filesystem, despite the
672 * FAT-like pseudo-header.
674 if ((memcmp(ms->ms_magic, "JFS ", 8) == 0) ||
675 (memcmp(ms->ms_magic, "HPFS ", 8) == 0))
678 return probe_fat(probe, id, buf);
681 static int probe_ntfs(struct blkid_probe *probe,
682 struct blkid_magic *id __BLKID_ATTR((unused)),
685 struct ntfs_super_block *ns;
686 struct master_file_table_record *mft;
687 struct file_attribute *attr;
688 char uuid_str[17], label_str[129], *cp;
689 int bytes_per_sector, sectors_per_cluster;
690 int mft_record_size, attr_off, attr_len;
691 unsigned int i, attr_type, val_len;
695 unsigned char *buf_mft, *val;
697 ns = (struct ntfs_super_block *) buf;
699 bytes_per_sector = ns->bios_parameter_block[0] +
700 (ns->bios_parameter_block[1] << 8);
701 sectors_per_cluster = ns->bios_parameter_block[2];
703 if ((bytes_per_sector < 512) || (sectors_per_cluster == 0))
706 if (ns->cluster_per_mft_record < 0)
707 mft_record_size = 1 << (0-ns->cluster_per_mft_record);
709 mft_record_size = ns->cluster_per_mft_record *
710 sectors_per_cluster * bytes_per_sector;
711 nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster;
713 if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) ||
714 (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters))
717 off = blkid_le64(ns->mft_mirror_cluster_location) *
718 bytes_per_sector * sectors_per_cluster;
720 buf_mft = get_buffer(probe, off, mft_record_size);
724 if (memcmp(buf_mft, "FILE", 4))
727 off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector *
730 buf_mft = get_buffer(probe, off, mft_record_size);
734 if (memcmp(buf_mft, "FILE", 4))
737 off += MFT_RECORD_VOLUME * mft_record_size;
739 buf_mft = get_buffer(probe, off, mft_record_size);
743 if (memcmp(buf_mft, "FILE", 4))
746 mft = (struct master_file_table_record *) buf_mft;
748 attr_off = blkid_le16(mft->attrs_offset);
752 attr = (struct file_attribute *) (buf_mft + attr_off);
753 attr_len = blkid_le16(attr->len);
754 attr_type = blkid_le32(attr->type);
755 val_off = blkid_le16(attr->value_offset);
756 val_len = blkid_le32(attr->value_len);
758 attr_off += attr_len;
760 if ((attr_off > mft_record_size) ||
764 if (attr_type == MFT_RECORD_ATTR_END)
767 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
768 if (val_len > sizeof(label_str))
769 val_len = sizeof(label_str)-1;
771 for (i=0, cp=label_str; i < val_len; i+=2,cp++) {
772 val = ((__u8 *) attr) + val_off + i;
781 sprintf(uuid_str, "%016llX", blkid_le64(ns->volume_serial));
782 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
784 blkid_set_tag(probe->dev, "LABEL", label_str, 0);
789 static int probe_xfs(struct blkid_probe *probe,
790 struct blkid_magic *id __BLKID_ATTR((unused)),
793 struct xfs_super_block *xs;
794 const char *label = 0;
796 xs = (struct xfs_super_block *)buf;
798 if (strlen(xs->xs_fname))
799 label = xs->xs_fname;
800 blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname));
801 set_uuid(probe->dev, xs->xs_uuid, 0);
805 static int probe_reiserfs(struct blkid_probe *probe,
806 struct blkid_magic *id, unsigned char *buf)
808 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
809 unsigned int blocksize;
810 const char *label = 0;
812 blocksize = blkid_le16(rs->rs_blocksize);
814 /* The blocksize must be at least 1k */
815 if ((blocksize >> 10) == 0)
816 return -BLKID_ERR_PARAM;
818 /* If the superblock is inside the journal, we have the wrong one */
819 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
820 return -BLKID_ERR_BIG;
822 /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
823 if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') {
824 if (strlen(rs->rs_label))
825 label = rs->rs_label;
826 set_uuid(probe->dev, rs->rs_uuid, 0);
828 blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label));
833 static int probe_reiserfs4(struct blkid_probe *probe,
834 struct blkid_magic *id __BLKID_ATTR((unused)),
837 struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf;
838 const unsigned char *label = 0;
840 if (strlen((char *) rs4->rs4_label))
841 label = rs4->rs4_label;
842 set_uuid(probe->dev, rs4->rs4_uuid, 0);
843 blkid_set_tag(probe->dev, "LABEL", (const char *) label,
844 sizeof(rs4->rs4_label));
849 static int probe_jfs(struct blkid_probe *probe,
850 struct blkid_magic *id __BLKID_ATTR((unused)),
853 struct jfs_super_block *js;
854 const char *label = 0;
856 js = (struct jfs_super_block *)buf;
858 if (blkid_le32(js->js_bsize) != (1 << blkid_le16(js->js_l2bsize)))
861 if (blkid_le32(js->js_pbsize) != (1 << blkid_le16(js->js_l2pbsize)))
864 if ((blkid_le16(js->js_l2bsize) - blkid_le16(js->js_l2pbsize)) !=
865 blkid_le16(js->js_l2bfactor))
868 if (strlen((char *) js->js_label))
869 label = (char *) js->js_label;
870 blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label));
871 set_uuid(probe->dev, js->js_uuid, 0);
875 static int probe_zfs(struct blkid_probe *probe, struct blkid_magic *id,
880 const char *pool_name = 0;
882 /* read nvpair data for pool name, pool GUID (complex) */
883 blkid_set_tag(probe->dev, "LABEL", pool_name, sizeof(pool_name));
884 set_uuid(probe->dev, pool_guid, 0);
889 static int probe_luks(struct blkid_probe *probe,
890 struct blkid_magic *id __BLKID_ATTR((unused)),
895 /* 168 is the offset to the 40 character uuid:
896 * http://luks.endorphin.org/LUKS-on-disk-format.pdf */
897 strncpy(uuid, (char *) buf+168, 40);
898 blkid_set_tag(probe->dev, "UUID", uuid, sizeof(uuid));
902 static int probe_romfs(struct blkid_probe *probe,
903 struct blkid_magic *id __BLKID_ATTR((unused)),
906 struct romfs_super_block *ros;
907 const char *label = 0;
909 ros = (struct romfs_super_block *)buf;
911 if (strlen((char *) ros->ros_volume))
912 label = (char *) ros->ros_volume;
913 blkid_set_tag(probe->dev, "LABEL", label, 0);
917 static int probe_cramfs(struct blkid_probe *probe,
918 struct blkid_magic *id __BLKID_ATTR((unused)),
921 struct cramfs_super_block *csb;
922 const char *label = 0;
924 csb = (struct cramfs_super_block *)buf;
926 if (strlen((char *) csb->name))
927 label = (char *) csb->name;
928 blkid_set_tag(probe->dev, "LABEL", label, 0);
932 static int probe_swap0(struct blkid_probe *probe,
933 struct blkid_magic *id __BLKID_ATTR((unused)),
934 unsigned char *buf __BLKID_ATTR((unused)))
936 blkid_set_tag(probe->dev, "UUID", 0, 0);
937 blkid_set_tag(probe->dev, "LABEL", 0, 0);
941 static int probe_swap1(struct blkid_probe *probe,
942 struct blkid_magic *id,
943 unsigned char *buf __BLKID_ATTR((unused)))
945 struct swap_id_block *sws;
947 probe_swap0(probe, id, buf);
949 * Version 1 swap headers are always located at offset of 1024
950 * bytes, although the swap signature itself is located at the
951 * end of the page (which may vary depending on hardware
954 sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024);
958 /* check for wrong version or zeroed pagecount, for sanity */
959 if (!memcmp(id->bim_magic, "SWAPSPACE2", id->bim_len) &&
960 (sws->sws_version != 1 || sws->sws_lastpage == 0))
963 /* arbitrary sanity check.. is there any garbage down there? */
964 if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) {
965 if (sws->sws_volume[0])
966 blkid_set_tag(probe->dev, "LABEL", sws->sws_volume,
967 sizeof(sws->sws_volume));
968 if (sws->sws_uuid[0])
969 set_uuid(probe->dev, sws->sws_uuid, 0);
974 static int probe_iso9660(struct blkid_probe *probe,
975 struct blkid_magic *id __BLKID_ATTR((unused)),
978 struct iso_volume_descriptor *iso;
979 const unsigned char *label;
981 iso = (struct iso_volume_descriptor *) buf;
982 label = iso->volume_id;
984 blkid_set_tag(probe->dev, "LABEL", (const char *) label,
985 figure_label_len(label, 32));
991 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
992 "NSR03", "TEA01", 0 };
994 static int probe_udf(struct blkid_probe *probe,
995 struct blkid_magic *id __BLKID_ATTR((unused)),
996 unsigned char *buf __BLKID_ATTR((unused)))
999 struct iso_volume_descriptor *isosb;
1002 /* determine the block size by scanning in 2K increments
1003 (block sizes larger than 2K will be null padded) */
1004 for (bs = 1; bs < 16; bs++) {
1005 isosb = (struct iso_volume_descriptor *)
1006 get_buffer(probe, bs*2048+32768, sizeof(*isosb));
1009 if (isosb->vd_id[0])
1013 /* Scan up to another 64 blocks looking for additional VSD's */
1014 for (j = 1; j < 64; j++) {
1016 isosb = (struct iso_volume_descriptor *)
1017 get_buffer(probe, j*bs*2048+32768,
1022 /* If we find NSR0x then call it udf:
1025 NSR03 for UDF 2.00 */
1026 if (!memcmp(isosb->vd_id, "NSR0", 4))
1027 return probe_iso9660(probe, id, buf);
1028 for (m = udf_magic; *m; m++)
1029 if (!memcmp(*m, isosb->vd_id, 5))
1037 static int probe_ocfs(struct blkid_probe *probe,
1038 struct blkid_magic *id __BLKID_ATTR((unused)),
1041 struct ocfs_volume_header ovh;
1042 struct ocfs_volume_label ovl;
1045 memcpy(&ovh, buf, sizeof(ovh));
1046 memcpy(&ovl, buf+512, sizeof(ovl));
1048 major = ocfsmajor(ovh);
1050 blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
1051 else if (major >= 9)
1052 blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
1054 blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl));
1055 blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
1056 set_uuid(probe->dev, ovl.vol_id, 0);
1060 static int probe_ocfs2(struct blkid_probe *probe,
1061 struct blkid_magic *id __BLKID_ATTR((unused)),
1064 struct ocfs2_super_block *osb;
1066 osb = (struct ocfs2_super_block *)buf;
1068 blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
1069 set_uuid(probe->dev, osb->s_uuid, 0);
1073 static int probe_oracleasm(struct blkid_probe *probe,
1074 struct blkid_magic *id __BLKID_ATTR((unused)),
1077 struct oracle_asm_disk_label *dl;
1079 dl = (struct oracle_asm_disk_label *)buf;
1081 blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
1085 static int probe_gfs(struct blkid_probe *probe,
1086 struct blkid_magic *id __BLKID_ATTR((unused)),
1089 struct gfs2_sb *sbd;
1090 const char *label = 0;
1092 sbd = (struct gfs2_sb *)buf;
1094 if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS &&
1095 blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI)
1097 blkid_set_tag(probe->dev, "UUID", 0, 0);
1099 if (strlen(sbd->sb_locktable))
1100 label = sbd->sb_locktable;
1101 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1107 static int probe_gfs2(struct blkid_probe *probe,
1108 struct blkid_magic *id __BLKID_ATTR((unused)),
1111 struct gfs2_sb *sbd;
1112 const char *label = 0;
1114 sbd = (struct gfs2_sb *)buf;
1116 if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS &&
1117 blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI)
1119 blkid_set_tag(probe->dev, "UUID", 0, 0);
1121 if (strlen(sbd->sb_locktable))
1122 label = sbd->sb_locktable;
1123 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1129 static void unicode_16be_to_utf8(unsigned char *str, int out_len,
1130 const unsigned char *buf, int in_len)
1135 for (i = j = 0; i + 2 <= in_len; i += 2) {
1136 c = (buf[i] << 8) | buf[i+1];
1140 } else if (c < 0x80) {
1143 str[j++] = (unsigned char) c;
1144 } else if (c < 0x800) {
1147 str[j++] = (unsigned char) (0xc0 | (c >> 6));
1148 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1152 str[j++] = (unsigned char) (0xe0 | (c >> 12));
1153 str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f));
1154 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1160 static int probe_hfs(struct blkid_probe *probe __BLKID_ATTR((unused)),
1161 struct blkid_magic *id __BLKID_ATTR((unused)),
1164 struct hfs_mdb *hfs = (struct hfs_mdb *)buf;
1165 unsigned long long *uuid_ptr;
1169 if ((memcmp(hfs->embed_sig, "H+", 2) == 0) ||
1170 (memcmp(hfs->embed_sig, "HX", 2) == 0))
1171 return 1; /* Not hfs, but an embedded HFS+ */
1173 uuid_ptr = (unsigned long long *)hfs->finder_info.id;
1174 uuid = blkid_le64(*uuid_ptr);
1176 sprintf(uuid_str, "%016llX", uuid);
1177 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1179 blkid_set_tag(probe->dev, "LABEL", (char *)hfs->label, hfs->label_len);
1184 static int probe_hfsplus(struct blkid_probe *probe,
1185 struct blkid_magic *id,
1188 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
1189 struct hfsplus_bnode_descriptor *descr;
1190 struct hfsplus_bheader_record *bnode;
1191 struct hfsplus_catalog_key *key;
1192 struct hfsplus_vol_header *hfsplus;
1193 struct hfs_mdb *sbd = (struct hfs_mdb *) buf;
1194 unsigned int alloc_block_size;
1195 unsigned int alloc_first_block;
1196 unsigned int embed_first_block;
1197 unsigned int off = 0;
1198 unsigned int blocksize;
1199 unsigned int cat_block;
1200 unsigned int ext_block_start;
1201 unsigned int ext_block_count;
1202 unsigned int record_count;
1203 unsigned int leaf_node_head;
1204 unsigned int leaf_node_count;
1205 unsigned int leaf_node_size;
1206 unsigned int leaf_block;
1207 unsigned int label_len;
1208 unsigned long long *uuid_ptr;
1209 __u64 leaf_off, uuid;
1210 char uuid_str[17], label[512];
1213 /* Check for a HFS+ volume embedded in a HFS volume */
1214 if (memcmp(sbd->signature, "BD", 2) == 0) {
1215 if ((memcmp(sbd->embed_sig, "H+", 2) != 0) &&
1216 (memcmp(sbd->embed_sig, "HX", 2) != 0))
1217 /* This must be an HFS volume, so fail */
1220 alloc_block_size = blkid_be32(sbd->al_blk_size);
1221 alloc_first_block = blkid_be16(sbd->al_bl_st);
1222 embed_first_block = blkid_be16(sbd->embed_startblock);
1223 off = (alloc_first_block * 512) +
1224 (embed_first_block * alloc_block_size);
1225 buf = get_buffer(probe, off + (id->bim_kboff * 1024),
1230 hfsplus = (struct hfsplus_vol_header *) buf;
1233 hfsplus = (struct hfsplus_vol_header *) buf;
1235 if ((memcmp(hfsplus->signature, "H+", 2) != 0) &&
1236 (memcmp(hfsplus->signature, "HX", 2) != 0))
1239 uuid_ptr = (unsigned long long *)hfsplus->finder_info.id;
1240 uuid = blkid_le64(*uuid_ptr);
1242 sprintf(uuid_str, "%016llX", uuid);
1243 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1246 blocksize = blkid_be32(hfsplus->blocksize);
1247 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
1248 cat_block = blkid_be32(extents[0].start_block);
1250 buf = get_buffer(probe, off + (cat_block * blocksize), 0x2000);
1254 bnode = (struct hfsplus_bheader_record *)
1255 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1257 leaf_node_head = blkid_be32(bnode->leaf_head);
1258 leaf_node_size = blkid_be16(bnode->node_size);
1259 leaf_node_count = blkid_be32(bnode->leaf_count);
1260 if (leaf_node_count == 0)
1263 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
1265 /* get physical location */
1266 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
1267 ext_block_start = blkid_be32(extents[ext].start_block);
1268 ext_block_count = blkid_be32(extents[ext].block_count);
1269 if (ext_block_count == 0)
1272 /* this is our extent */
1273 if (leaf_block < ext_block_count)
1276 leaf_block -= ext_block_count;
1278 if (ext == HFSPLUS_EXTENT_COUNT)
1281 leaf_off = (ext_block_start + leaf_block) * blocksize;
1283 buf = get_buffer(probe, off + leaf_off, leaf_node_size);
1287 descr = (struct hfsplus_bnode_descriptor *) buf;
1288 record_count = blkid_be16(descr->num_recs);
1289 if (record_count == 0)
1292 if (descr->type != HFS_NODE_LEAF)
1295 key = (struct hfsplus_catalog_key *)
1296 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1298 if (blkid_be32(key->parent_id) != HFSPLUS_POR_CNID)
1301 label_len = blkid_be16(key->unicode_len) * 2;
1302 unicode_16be_to_utf8((unsigned char *)label, sizeof(label),
1303 key->unicode, label_len);
1304 blkid_set_tag(probe->dev, "LABEL", label, 0);
1308 #define LVM2_LABEL_SIZE 512
1309 static unsigned int lvm2_calc_crc(const void *buf, unsigned int size)
1311 static const unsigned int crctab[] = {
1312 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1313 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1314 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1315 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1317 unsigned int i, crc = 0xf597a6cf;
1318 const __u8 *data = (const __u8 *) buf;
1320 for (i = 0; i < size; i++) {
1322 crc = (crc >> 4) ^ crctab[crc & 0xf];
1323 crc = (crc >> 4) ^ crctab[crc & 0xf];
1328 static int probe_lvm2(struct blkid_probe *probe,
1329 struct blkid_magic *id,
1332 int sector = (id->bim_kboff) << 1;
1333 struct lvm2_pv_label_header *label= (struct lvm2_pv_label_header *)buf;
1334 char *p, *q, uuid[40];
1337 /* buf is at 0k or 1k offset; find label inside */
1338 if (memcmp(buf, "LABELONE", 8) == 0) {
1339 label = (struct lvm2_pv_label_header *)buf;
1340 } else if (memcmp(buf + 512, "LABELONE", 8) == 0) {
1341 label = (struct lvm2_pv_label_header *)(buf + 512);
1347 if (blkid_le64(label->sector_xl) != (unsigned) sector) {
1349 printf("LVM2: label for sector %llu found at sector %d\n",
1350 blkid_le64(label->sector_xl), sector));
1354 if (lvm2_calc_crc(&label->offset_xl, LVM2_LABEL_SIZE -
1355 ((char *)&label->offset_xl - (char *)label)) !=
1356 blkid_le32(label->crc_xl)) {
1358 printf("LVM2: label checksum incorrect at sector %d\n",
1363 for (i=0, b=1, p=uuid, q= (char *) label->pv_uuid; i < LVM2_ID_LEN;
1370 blkid_set_tag(probe->dev, "UUID", uuid, LVM2_ID_LEN+6);
1375 static int probe_btrfs(struct blkid_probe *probe,
1376 struct blkid_magic *id,
1379 struct btrfs_super_block *bs;
1380 const char *label = 0;
1382 bs = (struct btrfs_super_block *)buf;
1384 if (strlen(bs->label))
1386 blkid_set_tag(probe->dev, "LABEL", label, sizeof(bs->label));
1387 set_uuid(probe->dev, bs->fsid, 0);
1391 * Various filesystem magics that we can check for. Note that kboff and
1392 * sboff are in kilobytes and bytes respectively. All magics are in
1393 * byte strings so we don't worry about endian issues.
1395 static struct blkid_magic type_array[] = {
1396 /* type kboff sboff len magic probe */
1397 { "oracleasm", 0, 32, 8, "ORCLDISK", probe_oracleasm },
1398 { "ntfs", 0, 3, 8, "NTFS ", probe_ntfs },
1399 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd },
1400 { "ext4dev", 1, 0x38, 2, "\123\357", probe_ext4dev },
1401 { "ext4", 1, 0x38, 2, "\123\357", probe_ext4 },
1402 { "ext3", 1, 0x38, 2, "\123\357", probe_ext3 },
1403 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 },
1404 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs },
1405 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs },
1406 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs },
1407 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs },
1408 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs },
1409 { "reiser4", 64, 0, 7, "ReIsEr4", probe_reiserfs4 },
1410 { "gfs2", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs2 },
1411 { "gfs", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs },
1412 { "vfat", 0, 0x52, 5, "MSWIN", probe_fat },
1413 { "vfat", 0, 0x52, 8, "FAT32 ", probe_fat },
1414 { "vfat", 0, 0x36, 5, "MSDOS", probe_fat },
1415 { "vfat", 0, 0x36, 8, "FAT16 ", probe_fat },
1416 { "vfat", 0, 0x36, 8, "FAT12 ", probe_fat },
1417 { "vfat", 0, 0, 1, "\353", probe_fat_nomagic },
1418 { "vfat", 0, 0, 1, "\351", probe_fat_nomagic },
1419 { "vfat", 0, 0x1fe, 2, "\125\252", probe_fat_nomagic },
1420 { "minix", 1, 0x10, 2, "\177\023", 0 },
1421 { "minix", 1, 0x10, 2, "\217\023", 0 },
1422 { "minix", 1, 0x10, 2, "\150\044", 0 },
1423 { "minix", 1, 0x10, 2, "\170\044", 0 },
1424 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 },
1425 { "xfs", 0, 0, 4, "XFSB", probe_xfs },
1426 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs },
1427 { "bfs", 0, 0, 4, "\316\372\173\033", 0 },
1428 { "cramfs", 0, 0, 4, "E=\315\050", probe_cramfs },
1429 { "qnx4", 0, 4, 6, "QNX4FS", 0 },
1430 { "udf", 32, 1, 5, "BEA01", probe_udf },
1431 { "udf", 32, 1, 5, "BOOT2", probe_udf },
1432 { "udf", 32, 1, 5, "CD001", probe_udf },
1433 { "udf", 32, 1, 5, "CDW02", probe_udf },
1434 { "udf", 32, 1, 5, "NSR02", probe_udf },
1435 { "udf", 32, 1, 5, "NSR03", probe_udf },
1436 { "udf", 32, 1, 5, "TEA01", probe_udf },
1437 { "iso9660", 32, 1, 5, "CD001", probe_iso9660 },
1438 { "iso9660", 32, 9, 5, "CDROM", probe_iso9660 },
1439 { "jfs", 32, 0, 4, "JFS1", probe_jfs },
1440 { "zfs", 8, 0, 8, "\0\0\x02\xf5\xb0\x07\xb1\x0c", probe_zfs },
1441 { "zfs", 8, 0, 8, "\x0c\xb1\x07\xb0\xf5\x02\0\0", probe_zfs },
1442 { "zfs", 264, 0, 8, "\0\0\x02\xf5\xb0\x07\xb1\x0c", probe_zfs },
1443 { "zfs", 264, 0, 8, "\x0c\xb1\x07\xb0\xf5\x02\0\0", probe_zfs },
1444 { "hfsplus", 1, 0, 2, "BD", probe_hfsplus },
1445 { "hfsplus", 1, 0, 2, "H+", probe_hfsplus },
1446 { "hfsplus", 1, 0, 2, "HX", probe_hfsplus },
1447 { "hfs", 1, 0, 2, "BD", probe_hfs },
1448 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 },
1449 { "hpfs", 8, 0, 4, "I\350\225\371", 0 },
1450 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 },
1451 { "swap", 0, 0xff6, 10, "SWAP-SPACE", probe_swap0 },
1452 { "swap", 0, 0xff6, 10, "SWAPSPACE2", probe_swap1 },
1453 { "swsuspend", 0, 0xff6, 9, "S1SUSPEND", probe_swap1 },
1454 { "swsuspend", 0, 0xff6, 9, "S2SUSPEND", probe_swap1 },
1455 { "swsuspend", 0, 0xff6, 9, "ULSUSPEND", probe_swap1 },
1456 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", probe_swap0 },
1457 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", probe_swap1 },
1458 { "swsuspend", 0, 0x1ff6, 9, "S1SUSPEND", probe_swap1 },
1459 { "swsuspend", 0, 0x1ff6, 9, "S2SUSPEND", probe_swap1 },
1460 { "swsuspend", 0, 0x1ff6, 9, "ULSUSPEND", probe_swap1 },
1461 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", probe_swap0 },
1462 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", probe_swap1 },
1463 { "swsuspend", 0, 0x3ff6, 9, "S1SUSPEND", probe_swap1 },
1464 { "swsuspend", 0, 0x3ff6, 9, "S2SUSPEND", probe_swap1 },
1465 { "swsuspend", 0, 0x3ff6, 9, "ULSUSPEND", probe_swap1 },
1466 { "swap", 0, 0x7ff6, 10, "SWAP-SPACE", probe_swap0 },
1467 { "swap", 0, 0x7ff6, 10, "SWAPSPACE2", probe_swap1 },
1468 { "swsuspend", 0, 0x7ff6, 9, "S1SUSPEND", probe_swap1 },
1469 { "swsuspend", 0, 0x7ff6, 9, "S2SUSPEND", probe_swap1 },
1470 { "swsuspend", 0, 0x7ff6, 9, "ULSUSPEND", probe_swap1 },
1471 { "swap", 0, 0xfff6, 10, "SWAP-SPACE", probe_swap0 },
1472 { "swap", 0, 0xfff6, 10, "SWAPSPACE2", probe_swap1 },
1473 { "swsuspend", 0, 0xfff6, 9, "S1SUSPEND", probe_swap1 },
1474 { "swsuspend", 0, 0xfff6, 9, "S2SUSPEND", probe_swap1 },
1475 { "swsuspend", 0, 0xfff6, 9, "ULSUSPEND", probe_swap1 },
1476 { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs },
1477 { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 },
1478 { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 },
1479 { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 },
1480 { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 },
1481 { "crypt_LUKS", 0, 0, 6, "LUKS\xba\xbe", probe_luks },
1482 { "squashfs", 0, 0, 4, "sqsh", 0 },
1483 { "squashfs", 0, 0, 4, "hsqs", 0 },
1484 { "lvm2pv", 0, 0x218, 8, "LVM2 001", probe_lvm2 },
1485 { "lvm2pv", 0, 0x018, 8, "LVM2 001", probe_lvm2 },
1486 { "lvm2pv", 1, 0x018, 8, "LVM2 001", probe_lvm2 },
1487 { "lvm2pv", 1, 0x218, 8, "LVM2 001", probe_lvm2 },
1488 { "btrfs", 64, 0x40, 8, "_BHRfS_M", probe_btrfs },
1489 { NULL, 0, 0, 0, NULL, NULL }
1493 * Verify that the data in dev is consistent with what is on the actual
1494 * block device (using the devname field only). Normally this will be
1495 * called when finding items in the cache, but for long running processes
1496 * is also desirable to revalidate an item before use.
1498 * If we are unable to revalidate the data, we return the old data and
1499 * do not set the BLKID_BID_FL_VERIFIED flag on it.
1501 blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
1503 struct blkid_magic *id;
1504 struct blkid_probe probe;
1505 blkid_tag_iterate iter;
1507 const char *type, *value;
1516 diff = now - dev->bid_time;
1518 if (stat(dev->bid_name, &st) < 0) {
1520 printf("blkid_verify: error %s (%d) while "
1521 "trying to stat %s\n", strerror(errno), errno,
1524 if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) {
1525 /* We don't have read permission, just return cache data. */
1526 DBG(DEBUG_PROBE, printf("returning unverified data for %s\n",
1530 blkid_free_dev(dev);
1534 if ((now >= dev->bid_time) &&
1535 (st.st_mtime <= dev->bid_time) &&
1536 ((diff < BLKID_PROBE_MIN) ||
1537 (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
1538 diff < BLKID_PROBE_INTERVAL)))
1542 printf("need to revalidate %s (cache time %lu, stat time %lu,\n\t"
1543 "time since last check %lu)\n",
1544 dev->bid_name, (unsigned long)dev->bid_time,
1545 (unsigned long)st.st_mtime, (unsigned long)diff));
1547 if ((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) {
1548 DBG(DEBUG_PROBE, printf("blkid_verify: error %s (%d) while "
1549 "opening %s\n", strerror(errno), errno,
1554 probe.cache = cache;
1561 * Iterate over the type array. If we already know the type,
1562 * then try that first. If it doesn't work, then blow away
1563 * the type information, and try again.
1568 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
1571 if (check_mdraid(probe.fd, uuid) == 0) {
1572 set_uuid(dev, uuid, 0);
1577 for (id = type_array; id->bim_type; id++) {
1578 if (dev->bid_type &&
1579 strcmp(id->bim_type, dev->bid_type))
1582 idx = id->bim_kboff + (id->bim_sboff >> 10);
1583 buf = get_buffer(&probe, idx << 10, 1024);
1587 if (memcmp(id->bim_magic, buf + (id->bim_sboff & 0x3ff),
1591 if ((id->bim_probe == NULL) ||
1592 (id->bim_probe(&probe, id, buf) == 0)) {
1593 type = id->bim_type;
1598 if (!id->bim_type && dev->bid_type) {
1600 * Zap the device filesystem information and try again
1603 printf("previous fs type %s not valid, "
1604 "trying full probe\n", dev->bid_type));
1605 iter = blkid_tag_iterate_begin(dev);
1606 while (blkid_tag_next(iter, &type, &value) == 0)
1607 blkid_set_tag(dev, type, 0, 0);
1608 blkid_tag_iterate_end(iter);
1612 if (!dev->bid_type) {
1613 blkid_free_dev(dev);
1620 dev->bid_devno = st.st_rdev;
1621 dev->bid_time = time(0);
1622 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
1623 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
1625 blkid_set_tag(dev, "TYPE", type, 0);
1627 DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
1628 dev->bid_name, (long long)st.st_rdev, type));
1639 int blkid_known_fstype(const char *fstype)
1641 struct blkid_magic *id;
1643 for (id = type_array; id->bim_type; id++) {
1644 if (strcmp(fstype, id->bim_type) == 0)
1651 int main(int argc, char **argv)
1658 fprintf(stderr, "Usage: %s device\n"
1659 "Probe a single device to determine type\n", argv[0]);
1662 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
1663 fprintf(stderr, "%s: error creating cache (%d)\n",
1667 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
1669 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
1672 printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
1674 printf("LABEL='%s'\n", dev->bid_label);
1676 printf("UUID='%s'\n", dev->bid_uuid);
1678 blkid_free_dev(dev);