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