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.
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_STAT_H
25 #ifdef HAVE_SYS_MKDEV_H
26 #include <sys/mkdev.h>
32 #include "uuid/uuid.h"
35 static int figure_label_len(const unsigned char *label, int len)
37 const unsigned char *end = label + len - 1;
39 while ((*end == ' ' || *end == 0) && end >= label)
43 return end - label + 1;
48 static unsigned char *get_buffer(struct blkid_probe *pr,
49 blkid_loff_t off, size_t len)
52 unsigned char *newbuf;
54 if (off + len <= SB_BUFFER_SIZE) {
56 pr->sbbuf = malloc(SB_BUFFER_SIZE);
59 if (lseek(pr->fd, 0, SEEK_SET) < 0)
61 ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE);
64 pr->sb_valid = ret_read;
66 if (off+len > pr->sb_valid)
68 return pr->sbbuf + off;
70 if (len > pr->buf_max) {
71 newbuf = realloc(pr->buf, len);
77 if (blkid_llseek(pr->fd, off, SEEK_SET) < 0)
79 ret_read = read(pr->fd, pr->buf, len);
80 if (ret_read != (ssize_t) len)
88 * This is a special case code to check for an MDRAID device. We do
89 * this special since it requires checking for a superblock at the end
92 static int check_mdraid(int fd, unsigned char *ret_uuid)
94 struct mdp_superblock_s *md;
99 return -BLKID_ERR_PARAM;
101 offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
103 if (blkid_llseek(fd, offset, 0) < 0 ||
104 read(fd, buf, 4096) != 4096)
105 return -BLKID_ERR_IO;
107 /* Check for magic number */
108 if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4))
109 return -BLKID_ERR_PARAM;
115 /* The MD UUID is not contiguous in the superblock, make it so */
116 md = (struct mdp_superblock_s *)buf;
117 if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
118 memcpy(ret_uuid, &md->set_uuid0, 4);
119 memcpy(ret_uuid + 4, &md->set_uuid1, 12);
124 static void set_uuid(blkid_dev dev, uuid_t uuid, const char *tag)
128 if (!uuid_is_null(uuid)) {
129 uuid_unparse(uuid, str);
130 blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
134 static void get_ext2_info(blkid_dev dev, unsigned char *buf)
136 struct ext2_super_block *es = (struct ext2_super_block *) buf;
137 const char *label = 0;
139 DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
140 blkid_le32(es->s_feature_compat),
141 blkid_le32(es->s_feature_incompat),
142 blkid_le32(es->s_feature_ro_compat)));
144 if (strlen(es->s_volume_name))
145 label = es->s_volume_name;
146 blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
148 set_uuid(dev, es->s_uuid, 0);
151 static int probe_ext3(struct blkid_probe *probe,
152 struct blkid_magic *id __BLKID_ATTR((unused)),
155 struct ext2_super_block *es;
156 es = (struct ext2_super_block *)buf;
158 /* Distinguish between jbd and ext2/3 fs */
159 if (blkid_le32(es->s_feature_incompat) &
160 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
161 return -BLKID_ERR_PARAM;
163 /* Distinguish between ext3 and ext2 */
164 if (!(blkid_le32(es->s_feature_compat) &
165 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
166 return -BLKID_ERR_PARAM;
168 get_ext2_info(probe->dev, buf);
170 if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
171 !uuid_is_null(es->s_journal_uuid))
172 set_uuid(probe->dev, es->s_journal_uuid, "EXT_JOURNAL");
174 blkid_set_tag(probe->dev, "SEC_TYPE", "ext2", sizeof("ext2"));
179 static int probe_ext2(struct blkid_probe *probe,
180 struct blkid_magic *id __BLKID_ATTR((unused)),
183 struct ext2_super_block *es;
185 es = (struct ext2_super_block *)buf;
187 /* Distinguish between jbd and ext2/3 fs */
188 if (blkid_le32(es->s_feature_incompat) &
189 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
190 return -BLKID_ERR_PARAM;
192 /* Distinguish between ext3 and ext2 */
193 if ((blkid_le32(es->s_feature_compat) &
194 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
195 return -BLKID_ERR_PARAM;
197 get_ext2_info(probe->dev, buf);
202 static int probe_jbd(struct blkid_probe *probe,
203 struct blkid_magic *id __BLKID_ATTR((unused)),
206 struct ext2_super_block *es = (struct ext2_super_block *) buf;
208 if (!(blkid_le32(es->s_feature_incompat) &
209 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
210 return -BLKID_ERR_PARAM;
212 get_ext2_info(probe->dev, buf);
217 #define FAT_ATTR_VOLUME_ID 0x08
218 #define FAT_ATTR_DIR 0x10
219 #define FAT_ATTR_LONG_NAME 0x0f
220 #define FAT_ATTR_MASK 0x3f
221 #define FAT_ENTRY_FREE 0xe5
223 static const char *no_name = "NO NAME ";
225 static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
229 for (i = 0; i < count; i++) {
230 if (dir[i].name[0] == 0x00)
233 if ((dir[i].name[0] == FAT_ENTRY_FREE) ||
234 (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) ||
235 ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
238 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
239 FAT_ATTR_VOLUME_ID) {
246 /* FAT label extraction from the root directory taken from Kay
247 * Sievers's volume_id library */
248 static int probe_fat(struct blkid_probe *probe,
249 struct blkid_magic *id __BLKID_ATTR((unused)),
252 struct vfat_super_block *vs = (struct vfat_super_block *) buf;
253 struct msdos_super_block *ms = (struct msdos_super_block *) buf;
254 struct vfat_dir_entry *dir;
256 const unsigned char *label = 0, *vol_label = 0, *tmp;
257 unsigned char *vol_serno;
258 int label_len = 0, maxloop = 100;
259 __u16 sector_size, dir_entries, reserved;
260 __u32 sect_count, fat_size, dir_size, cluster_count, fat_length;
261 __u32 buf_size, start_data_sect, next, root_start, root_dir_entries;
263 /* sector size check */
264 tmp = (unsigned char *)&ms->ms_sector_size;
265 sector_size = tmp[0] + (tmp[1] << 8);
266 if (sector_size != 0x200 && sector_size != 0x400 &&
267 sector_size != 0x800 && sector_size != 0x1000)
270 tmp = (unsigned char *)&ms->ms_dir_entries;
271 dir_entries = tmp[0] + (tmp[1] << 8);
272 reserved = blkid_le16(ms->ms_reserved);
273 tmp = (unsigned char *)&ms->ms_sectors;
274 sect_count = tmp[0] + (tmp[1] << 8);
276 sect_count = blkid_le32(ms->ms_total_sect);
278 fat_length = blkid_le16(ms->ms_fat_length);
280 fat_length = blkid_le32(vs->vs_fat32_length);
282 fat_size = fat_length * ms->ms_fats;
283 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
284 (sector_size-1)) / sector_size;
286 cluster_count = sect_count - (reserved + fat_size + dir_size);
287 cluster_count /= ms->ms_cluster_size;
289 if (cluster_count > FAT32_MAX)
292 if (ms->ms_fat_length) {
293 /* the label may be an attribute in the root directory */
294 root_start = (reserved + fat_size) * sector_size;
295 root_dir_entries = vs->vs_dir_entries[0] +
296 (vs->vs_dir_entries[1] << 8);
298 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
299 dir = (struct vfat_dir_entry *) get_buffer(probe, root_start,
302 vol_label = search_fat_label(dir, root_dir_entries);
304 if (!vol_label || !memcmp(vol_label, no_name, 11))
305 vol_label = ms->ms_label;
306 vol_serno = ms->ms_serno;
308 blkid_set_tag(probe->dev, "SEC_TYPE", "msdos",
311 /* Search the FAT32 root dir for the label attribute */
312 buf_size = vs->vs_cluster_size * sector_size;
313 start_data_sect = reserved + fat_size;
315 next = blkid_le32(vs->vs_root_cluster);
316 while (next && --maxloop) {
318 __u64 next_off, fat_entry_off;
321 next_sect_off = (next - 2) * vs->vs_cluster_size;
322 next_off = (start_data_sect + next_sect_off) *
325 dir = (struct vfat_dir_entry *)
326 get_buffer(probe, next_off, buf_size);
330 count = buf_size / sizeof(struct vfat_dir_entry);
332 vol_label = search_fat_label(dir, count);
337 fat_entry_off = (reserved * sector_size) +
338 (next * sizeof(__u32));
339 buf = get_buffer(probe, fat_entry_off, buf_size);
343 /* set next cluster */
344 next = blkid_le32(*((__u32 *) buf) & 0x0fffffff);
347 if (!vol_label || !memcmp(vol_label, no_name, 11))
348 vol_label = vs->vs_label;
349 vol_serno = vs->vs_serno;
352 if (vol_label && memcmp(vol_label, no_name, 11)) {
353 if ((label_len = figure_label_len(vol_label, 11)))
357 /* We can't just print them as %04X, because they are unaligned */
358 sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
359 vol_serno[1], vol_serno[0]);
361 blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len);
362 blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1);
368 * The FAT filesystem could be without a magic string in superblock
369 * (e.g. old floppies). This heuristic for FAT detection is inspired
370 * by http://vrfy.org/projects/volume_id/ and Linux kernel.
371 * [7-Jul-2005, Karel Zak <kzak@redhat.com>]
373 static int probe_fat_nomagic(struct blkid_probe *probe,
374 struct blkid_magic *id __BLKID_ATTR((unused)),
377 struct vfat_super_block *vs;
379 vs = (struct vfat_super_block *)buf;
382 if (vs->vs_heads == 0)
385 /* cluster size check*/
386 if (vs->vs_cluster_size == 0 ||
387 (vs->vs_cluster_size & (vs->vs_cluster_size-1)))
391 if (vs->vs_media < 0xf8 && vs->vs_media != 0xf0)
394 /* fat counts(Linux kernel expects at least 1 FAT table) */
398 return probe_fat(probe, id, buf);
401 static int probe_ntfs(struct blkid_probe *probe,
402 struct blkid_magic *id __BLKID_ATTR((unused)),
405 struct ntfs_super_block *ns;
406 struct master_file_table_record *mft;
407 struct file_attribute *attr;
408 char uuid_str[17], label_str[129], *cp;
409 int bytes_per_sector, sectors_per_cluster;
410 int mft_record_size, attr_off, attr_len;
411 unsigned int i, attr_type, val_len;
415 unsigned char *buf_mft, *val;
417 ns = (struct ntfs_super_block *) buf;
419 bytes_per_sector = ns->bios_parameter_block[0] +
420 (ns->bios_parameter_block[1] << 8);
421 sectors_per_cluster = ns->bios_parameter_block[2];
423 if ((bytes_per_sector < 512) || (sectors_per_cluster == 0))
426 if (ns->cluster_per_mft_record < 0)
427 mft_record_size = 1 << (0-ns->cluster_per_mft_record);
429 mft_record_size = ns->cluster_per_mft_record *
430 sectors_per_cluster * bytes_per_sector;
431 nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster;
433 if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) ||
434 (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters))
437 off = blkid_le64(ns->mft_mirror_cluster_location) *
438 bytes_per_sector * sectors_per_cluster;
440 buf_mft = get_buffer(probe, off, mft_record_size);
444 if (memcmp(buf_mft, "FILE", 4))
447 off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector *
450 buf_mft = get_buffer(probe, off, mft_record_size);
454 if (memcmp(buf_mft, "FILE", 4))
457 off += MFT_RECORD_VOLUME * mft_record_size;
459 buf_mft = get_buffer(probe, off, mft_record_size);
463 if (memcmp(buf_mft, "FILE", 4))
466 mft = (struct master_file_table_record *) buf_mft;
468 attr_off = blkid_le16(mft->attrs_offset);
472 attr = (struct file_attribute *) (buf_mft + attr_off);
473 attr_len = blkid_le16(attr->len);
474 attr_type = blkid_le32(attr->type);
475 val_off = blkid_le16(attr->value_offset);
476 val_len = blkid_le32(attr->value_len);
478 attr_off += attr_len;
480 if ((attr_off > mft_record_size) ||
484 if (attr_type == MFT_RECORD_ATTR_END)
487 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
488 if (val_len > sizeof(label_str))
489 val_len = sizeof(label_str)-1;
491 for (i=0, cp=label_str; i < val_len; i+=2,cp++) {
492 val = ((__u8 *) attr) + val_off + i;
501 sprintf(uuid_str, "%llX", blkid_le64(ns->volume_serial));
502 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
504 blkid_set_tag(probe->dev, "LABEL", label_str, 0);
509 static int probe_xfs(struct blkid_probe *probe,
510 struct blkid_magic *id __BLKID_ATTR((unused)),
513 struct xfs_super_block *xs;
514 const char *label = 0;
516 xs = (struct xfs_super_block *)buf;
518 if (strlen(xs->xs_fname))
519 label = xs->xs_fname;
520 blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname));
521 set_uuid(probe->dev, xs->xs_uuid, 0);
525 static int probe_reiserfs(struct blkid_probe *probe,
526 struct blkid_magic *id, unsigned char *buf)
528 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
529 unsigned int blocksize;
530 const char *label = 0;
532 blocksize = blkid_le16(rs->rs_blocksize);
534 /* If the superblock is inside the journal, we have the wrong one */
535 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
536 return -BLKID_ERR_BIG;
538 /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
539 if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') {
540 if (strlen(rs->rs_label))
541 label = rs->rs_label;
542 set_uuid(probe->dev, rs->rs_uuid, 0);
544 blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label));
549 static int probe_reiserfs4(struct blkid_probe *probe,
550 struct blkid_magic *id __BLKID_ATTR((unused)),
553 struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf;
554 const unsigned char *label = 0;
556 if (strlen((char *) rs4->rs4_label))
557 label = rs4->rs4_label;
558 set_uuid(probe->dev, rs4->rs4_uuid, 0);
559 blkid_set_tag(probe->dev, "LABEL", (const char *) label,
560 sizeof(rs4->rs4_label));
565 static int probe_jfs(struct blkid_probe *probe,
566 struct blkid_magic *id __BLKID_ATTR((unused)),
569 struct jfs_super_block *js;
570 const char *label = 0;
572 js = (struct jfs_super_block *)buf;
574 if (strlen((char *) js->js_label))
575 label = (char *) js->js_label;
576 blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label));
577 set_uuid(probe->dev, js->js_uuid, 0);
581 static int probe_luks(struct blkid_probe *probe,
582 struct blkid_magic *id __BLKID_ATTR((unused)),
587 /* 168 is the offset to the 40 character uuid:
588 * http://luks.endorphin.org/LUKS-on-disk-format.pdf */
589 strncpy(uuid, (char *) buf+168, 40);
590 blkid_set_tag(probe->dev, "UUID", uuid, sizeof(uuid));
594 static int probe_romfs(struct blkid_probe *probe,
595 struct blkid_magic *id __BLKID_ATTR((unused)),
598 struct romfs_super_block *ros;
599 const char *label = 0;
601 ros = (struct romfs_super_block *)buf;
603 if (strlen((char *) ros->ros_volume))
604 label = (char *) ros->ros_volume;
605 blkid_set_tag(probe->dev, "LABEL", label, 0);
609 static int probe_cramfs(struct blkid_probe *probe,
610 struct blkid_magic *id __BLKID_ATTR((unused)),
613 struct cramfs_super_block *csb;
614 const char *label = 0;
616 csb = (struct cramfs_super_block *)buf;
618 if (strlen((char *) csb->name))
619 label = (char *) csb->name;
620 blkid_set_tag(probe->dev, "LABEL", label, 0);
624 static int probe_swap0(struct blkid_probe *probe,
625 struct blkid_magic *id __BLKID_ATTR((unused)),
626 unsigned char *buf __BLKID_ATTR((unused)))
628 blkid_set_tag(probe->dev, "UUID", 0, 0);
629 blkid_set_tag(probe->dev, "LABEL", 0, 0);
633 static int probe_swap1(struct blkid_probe *probe,
634 struct blkid_magic *id __BLKID_ATTR((unused)),
635 unsigned char *buf __BLKID_ATTR((unused)))
637 struct swap_id_block *sws;
639 probe_swap0(probe, id, buf);
641 * Version 1 swap headers are always located at offset of 1024
642 * bytes, although the swap signature itself is located at the
643 * end of the page (which may vary depending on hardware
646 sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024);
650 /* arbitrary sanity check.. is there any garbage down there? */
651 if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) {
652 if (sws->sws_volume[0])
653 blkid_set_tag(probe->dev, "LABEL", sws->sws_volume,
654 sizeof(sws->sws_volume));
655 if (sws->sws_uuid[0])
656 set_uuid(probe->dev, sws->sws_uuid, 0);
661 static int probe_iso9660(struct blkid_probe *probe,
662 struct blkid_magic *id __BLKID_ATTR((unused)),
665 struct iso_volume_descriptor *iso;
666 const unsigned char *label;
668 iso = (struct iso_volume_descriptor *) buf;
669 label = iso->volume_id;
671 blkid_set_tag(probe->dev, "LABEL", (const char *) label,
672 figure_label_len(label, 32));
678 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
679 "NSR03", "TEA01", 0 };
681 static int probe_udf(struct blkid_probe *probe,
682 struct blkid_magic *id __BLKID_ATTR((unused)),
683 unsigned char *buf __BLKID_ATTR((unused)))
686 struct iso_volume_descriptor *isosb;
689 /* determine the block size by scanning in 2K increments
690 (block sizes larger than 2K will be null padded) */
691 for (bs = 1; bs < 16; bs++) {
692 isosb = (struct iso_volume_descriptor *)
693 get_buffer(probe, bs*2048+32768, sizeof(isosb));
700 /* Scan up to another 64 blocks looking for additional VSD's */
701 for (j = 1; j < 64; j++) {
703 isosb = (struct iso_volume_descriptor *)
704 get_buffer(probe, j*bs*2048+32768,
709 /* If we find NSR0x then call it udf:
712 NSR03 for UDF 2.00 */
713 if (!memcmp(isosb->vd_id, "NSR0", 4))
715 for (m = udf_magic; *m; m++)
716 if (!memcmp(*m, isosb->vd_id, 5))
724 static int probe_ocfs(struct blkid_probe *probe,
725 struct blkid_magic *id __BLKID_ATTR((unused)),
728 struct ocfs_volume_header ovh;
729 struct ocfs_volume_label ovl;
732 memcpy(&ovh, buf, sizeof(ovh));
733 memcpy(&ovl, buf+512, sizeof(ovl));
735 major = ocfsmajor(ovh);
737 blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
739 blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
741 blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl));
742 blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
743 set_uuid(probe->dev, ovl.vol_id, 0);
747 static int probe_ocfs2(struct blkid_probe *probe,
748 struct blkid_magic *id __BLKID_ATTR((unused)),
751 struct ocfs2_super_block *osb;
753 osb = (struct ocfs2_super_block *)buf;
755 blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
756 set_uuid(probe->dev, osb->s_uuid, 0);
760 static int probe_oracleasm(struct blkid_probe *probe,
761 struct blkid_magic *id __BLKID_ATTR((unused)),
764 struct oracle_asm_disk_label *dl;
766 dl = (struct oracle_asm_disk_label *)buf;
768 blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
772 static int probe_gfs(struct blkid_probe *probe,
773 struct blkid_magic *id __BLKID_ATTR((unused)),
777 const char *label = 0;
779 sbd = (struct gfs2_sb *)buf;
781 if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS &&
782 blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI)
784 blkid_set_tag(probe->dev, "UUID", 0, 0);
786 if (strlen(sbd->sb_locktable))
787 label = sbd->sb_locktable;
788 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
794 static int probe_gfs2(struct blkid_probe *probe,
795 struct blkid_magic *id __BLKID_ATTR((unused)),
799 const char *label = 0;
801 sbd = (struct gfs2_sb *)buf;
803 if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS &&
804 blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI)
806 blkid_set_tag(probe->dev, "UUID", 0, 0);
808 if (strlen(sbd->sb_locktable))
809 label = sbd->sb_locktable;
810 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
817 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
818 * in the type_array table below + bim_kbalign.
820 * When probing for a lot of magics, we handle everything in 1kB buffers so
821 * that we don't have to worry about reading each combination of block sizes.
823 #define BLKID_BLK_OFFS 64 /* currently reiserfs */
826 * Various filesystem magics that we can check for. Note that kboff and
827 * sboff are in kilobytes and bytes respectively. All magics are in
828 * byte strings so we don't worry about endian issues.
830 static struct blkid_magic type_array[] = {
831 /* type kboff sboff len magic probe */
832 { "oracleasm", 0, 32, 8, "ORCLDISK", probe_oracleasm },
833 { "ntfs", 0, 3, 8, "NTFS ", probe_ntfs },
834 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd },
835 { "ext3", 1, 0x38, 2, "\123\357", probe_ext3 },
836 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 },
837 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs },
838 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs },
839 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs },
840 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs },
841 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs },
842 { "reiser4", 64, 0, 7, "ReIsEr4", probe_reiserfs4 },
843 { "gfs2", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs2 },
844 { "gfs", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs },
845 { "vfat", 0, 0x52, 5, "MSWIN", probe_fat },
846 { "vfat", 0, 0x52, 8, "FAT32 ", probe_fat },
847 { "vfat", 0, 0x36, 5, "MSDOS", probe_fat },
848 { "vfat", 0, 0x36, 8, "FAT16 ", probe_fat },
849 { "vfat", 0, 0x36, 8, "FAT12 ", probe_fat },
850 { "vfat", 0, 0, 1, "\353", probe_fat_nomagic },
851 { "vfat", 0, 0, 1, "\351", probe_fat_nomagic },
852 { "vfat", 0, 0x1fe, 2, "\125\252", probe_fat_nomagic },
853 { "minix", 1, 0x10, 2, "\177\023", 0 },
854 { "minix", 1, 0x10, 2, "\217\023", 0 },
855 { "minix", 1, 0x10, 2, "\150\044", 0 },
856 { "minix", 1, 0x10, 2, "\170\044", 0 },
857 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 },
858 { "xfs", 0, 0, 4, "XFSB", probe_xfs },
859 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs },
860 { "bfs", 0, 0, 4, "\316\372\173\033", 0 },
861 { "cramfs", 0, 0, 4, "E=\315\050", probe_cramfs },
862 { "qnx4", 0, 4, 6, "QNX4FS", 0 },
863 { "udf", 32, 1, 5, "BEA01", probe_udf },
864 { "udf", 32, 1, 5, "BOOT2", probe_udf },
865 { "udf", 32, 1, 5, "CD001", probe_udf },
866 { "udf", 32, 1, 5, "CDW02", probe_udf },
867 { "udf", 32, 1, 5, "NSR02", probe_udf },
868 { "udf", 32, 1, 5, "NSR03", probe_udf },
869 { "udf", 32, 1, 5, "TEA01", probe_udf },
870 { "iso9660", 32, 1, 5, "CD001", probe_iso9660 },
871 { "iso9660", 32, 9, 5, "CDROM", probe_iso9660 },
872 { "jfs", 32, 0, 4, "JFS1", probe_jfs },
873 { "hfs", 1, 0, 2, "BD", 0 },
874 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 },
875 { "hpfs", 8, 0, 4, "I\350\225\371", 0 },
876 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 },
877 { "swap", 0, 0xff6, 10, "SWAP-SPACE", probe_swap0 },
878 { "swap", 0, 0xff6, 10, "SWAPSPACE2", probe_swap1 },
879 { "swsuspend", 0, 0xff6, 9, "S1SUSPEND", probe_swap1 },
880 { "swsuspend", 0, 0xff6, 9, "S2SUSPEND", probe_swap1 },
881 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", probe_swap0 },
882 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", probe_swap1 },
883 { "swsuspend", 0, 0x1ff6, 9, "S1SUSPEND", probe_swap1 },
884 { "swsuspend", 0, 0x1ff6, 9, "S2SUSPEND", probe_swap1 },
885 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", probe_swap0 },
886 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", probe_swap1 },
887 { "swsuspend", 0, 0x3ff6, 9, "S1SUSPEND", probe_swap1 },
888 { "swsuspend", 0, 0x3ff6, 9, "S2SUSPEND", probe_swap1 },
889 { "swap", 0, 0x7ff6, 10, "SWAP-SPACE", probe_swap0 },
890 { "swap", 0, 0x7ff6, 10, "SWAPSPACE2", probe_swap1 },
891 { "swsuspend", 0, 0x7ff6, 9, "S1SUSPEND", probe_swap1 },
892 { "swsuspend", 0, 0x7ff6, 9, "S2SUSPEND", probe_swap1 },
893 { "swap", 0, 0xfff6, 10, "SWAP-SPACE", probe_swap0 },
894 { "swap", 0, 0xfff6, 10, "SWAPSPACE2", probe_swap1 },
895 { "swsuspend", 0, 0xfff6, 9, "S1SUSPEND", probe_swap1 },
896 { "swsuspend", 0, 0xfff6, 9, "S2SUSPEND", probe_swap1 },
897 { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs },
898 { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 },
899 { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 },
900 { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 },
901 { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 },
902 { "crypt_LUKS", 0, 0, 6, "LUKS\xba\xbe", probe_luks },
903 { "squashfs", 0, 0, 4, "sqsh", 0 },
904 { "squashfs", 0, 0, 4, "hsqs", 0 },
905 { NULL, 0, 0, 0, NULL, NULL }
909 * Verify that the data in dev is consistent with what is on the actual
910 * block device (using the devname field only). Normally this will be
911 * called when finding items in the cache, but for long running processes
912 * is also desirable to revalidate an item before use.
914 * If we are unable to revalidate the data, we return the old data and
915 * do not set the BLKID_BID_FL_VERIFIED flag on it.
917 blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
919 struct blkid_magic *id;
920 struct blkid_probe probe;
921 blkid_tag_iterate iter;
923 const char *type, *value;
932 diff = now - dev->bid_time;
934 if ((now > dev->bid_time) && (diff > 0) &&
935 ((diff < BLKID_PROBE_MIN) ||
936 (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
937 diff < BLKID_PROBE_INTERVAL)))
941 printf("need to revalidate %s (time since last check %llu)\n",
942 dev->bid_name, (unsigned long long)diff));
944 if (((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) ||
945 (fstat(probe.fd, &st) < 0)) {
946 if (probe.fd >= 0) close(probe.fd);
947 if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
951 /* We don't have read permission, just return cache data. */
953 printf("returning unverified data for %s\n",
965 * Iterate over the type array. If we already know the type,
966 * then try that first. If it doesn't work, then blow away
967 * the type information, and try again.
972 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
975 if (check_mdraid(probe.fd, uuid) == 0) {
976 set_uuid(dev, uuid, 0);
981 for (id = type_array; id->bim_type; id++) {
983 strcmp(id->bim_type, dev->bid_type))
986 idx = id->bim_kboff + (id->bim_sboff >> 10);
987 buf = get_buffer(&probe, idx << 10, 1024);
991 if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
995 if ((id->bim_probe == NULL) ||
996 (id->bim_probe(&probe, id, buf) == 0)) {
1002 if (!id->bim_type && dev->bid_type) {
1004 * Zap the device filesystem information and try again
1007 printf("previous fs type %s not valid, "
1008 "trying full probe\n", dev->bid_type));
1009 iter = blkid_tag_iterate_begin(dev);
1010 while (blkid_tag_next(iter, &type, &value) == 0)
1011 blkid_set_tag(dev, type, 0, 0);
1012 blkid_tag_iterate_end(iter);
1016 if (!dev->bid_type) {
1017 blkid_free_dev(dev);
1024 dev->bid_devno = st.st_rdev;
1025 dev->bid_time = time(0);
1026 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
1027 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
1029 blkid_set_tag(dev, "TYPE", type, 0);
1031 DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
1032 dev->bid_name, (long long)st.st_rdev, type));
1045 int blkid_known_fstype(const char *fstype)
1047 struct blkid_magic *id;
1049 for (id = type_array; id->bim_type; id++) {
1050 if (strcmp(fstype, id->bim_type) == 0)
1057 int main(int argc, char **argv)
1064 fprintf(stderr, "Usage: %s device\n"
1065 "Probe a single device to determine type\n", argv[0]);
1068 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
1069 fprintf(stderr, "%s: error creating cache (%d)\n",
1073 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
1075 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
1078 printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
1080 printf("LABEL='%s'\n", dev->bid_label);
1082 printf("UUID='%s'\n", dev->bid_uuid);
1084 blkid_free_dev(dev);