probe.c revision 838f133c72a583eae67414368e46ee0303e0a51f
1/*
2 * probe.c - identify a block device by its contents, and return a dev
3 *           struct with the details
4 *
5 * Copyright (C) 1999 by Andries Brouwer
6 * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
7 * Copyright (C) 2001 by Andreas Dilger
8 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
9 *
10 * %Begin-Header%
11 * This file may be redistributed under the terms of the
12 * GNU Lesser General Public License.
13 * %End-Header%
14 */
15
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <fcntl.h>
21#include <sys/types.h>
22#ifdef HAVE_SYS_STAT_H
23#include <sys/stat.h>
24#endif
25#ifdef HAVE_SYS_MKDEV_H
26#include <sys/mkdev.h>
27#endif
28#include <sys/utsname.h>
29#ifdef HAVE_ERRNO_H
30#include <errno.h>
31#endif
32#include "blkidP.h"
33#include "uuid/uuid.h"
34#include "probe.h"
35
36static int figure_label_len(const unsigned char *label, int len)
37{
38	const unsigned char *end = label + len - 1;
39
40	while ((*end == ' ' || *end == 0) && end >= label)
41		--end;
42	if (end >= label) {
43		label = label;
44		return end - label + 1;
45	}
46	return 0;
47}
48
49static unsigned char *get_buffer(struct blkid_probe *pr,
50			  blkid_loff_t off, size_t len)
51{
52	ssize_t		ret_read;
53	unsigned char	*newbuf;
54
55	if (off + len <= SB_BUFFER_SIZE) {
56		if (!pr->sbbuf) {
57			pr->sbbuf = malloc(SB_BUFFER_SIZE);
58			if (!pr->sbbuf)
59				return NULL;
60			if (lseek(pr->fd, 0, SEEK_SET) < 0)
61				return NULL;
62			ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE);
63			if (ret_read < 0)
64				ret_read = 0;
65			pr->sb_valid = ret_read;
66		}
67		if (off+len > pr->sb_valid)
68			return NULL;
69		return pr->sbbuf + off;
70	} else {
71		if (len > pr->buf_max) {
72			newbuf = realloc(pr->buf, len);
73			if (newbuf == NULL)
74				return NULL;
75			pr->buf = newbuf;
76			pr->buf_max = len;
77		}
78		if (blkid_llseek(pr->fd, off, SEEK_SET) < 0)
79			return NULL;
80		ret_read = read(pr->fd, pr->buf, len);
81		if (ret_read != (ssize_t) len)
82			return NULL;
83		return pr->buf;
84	}
85}
86
87
88/*
89 * This is a special case code to check for an MDRAID device.  We do
90 * this special since it requires checking for a superblock at the end
91 * of the device.
92 */
93static int check_mdraid(int fd, unsigned char *ret_uuid)
94{
95	struct mdp_superblock_s *md;
96	blkid_loff_t		offset;
97	char			buf[4096];
98
99	if (fd < 0)
100		return -BLKID_ERR_PARAM;
101
102	offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
103
104	if (blkid_llseek(fd, offset, 0) < 0 ||
105	    read(fd, buf, 4096) != 4096)
106		return -BLKID_ERR_IO;
107
108	/* Check for magic number */
109	if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4))
110		return -BLKID_ERR_PARAM;
111
112	if (!ret_uuid)
113		return 0;
114	*ret_uuid = 0;
115
116	/* The MD UUID is not contiguous in the superblock, make it so */
117	md = (struct mdp_superblock_s *)buf;
118	if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
119		memcpy(ret_uuid, &md->set_uuid0, 4);
120		memcpy(ret_uuid + 4, &md->set_uuid1, 12);
121	}
122	return 0;
123}
124
125static void set_uuid(blkid_dev dev, uuid_t uuid, char *tag)
126{
127	char	str[37];
128
129	if (!uuid_is_null(uuid)) {
130		uuid_unparse(uuid, str);
131		blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
132	}
133}
134
135static void get_ext2_info(blkid_dev dev, struct blkid_magic *id,
136			  unsigned char *buf)
137{
138	struct ext2_super_block *es = (struct ext2_super_block *) buf;
139	const char *label = 0;
140
141	DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
142		   blkid_le32(es->s_feature_compat),
143		   blkid_le32(es->s_feature_incompat),
144		   blkid_le32(es->s_feature_ro_compat)));
145
146	if (strlen(es->s_volume_name))
147		label = es->s_volume_name;
148	blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
149
150	set_uuid(dev, es->s_uuid, 0);
151
152	if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
153	    !uuid_is_null(es->s_journal_uuid))
154		set_uuid(dev, es->s_journal_uuid, "EXT_JOURNAL");
155
156	if (strcmp(id->bim_type, "ext2") &&
157	    ((blkid_le32(es->s_feature_incompat) &
158	      EXT2_FEATURE_INCOMPAT_UNSUPPORTED) == 0))
159		blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2"));
160}
161
162/*
163 * Check to see if a filesystem is in /proc/filesystems.
164 * Returns 1 if found, 0 if not
165 */
166int fs_proc_check(const char *fs_name)
167{
168	FILE	*f;
169	char	buf[80], *cp, *t;
170
171	f = fopen("/proc/filesystems", "r");
172	if (!f)
173		return (0);
174	while (!feof(f)) {
175		if (!fgets(buf, sizeof(buf), f))
176			break;
177		cp = buf;
178		if (!isspace(*cp)) {
179			while (*cp && !isspace(*cp))
180				cp++;
181		}
182		while (*cp && isspace(*cp))
183			cp++;
184		if ((t = strchr(cp, '\n')) != NULL)
185			*t = 0;
186		if ((t = strchr(cp, '\t')) != NULL)
187			*t = 0;
188		if ((t = strchr(cp, ' ')) != NULL)
189			*t = 0;
190		if (!strcmp(fs_name, cp)) {
191			fclose(f);
192			return (1);
193		}
194	}
195	fclose(f);
196	return (0);
197}
198
199/*
200 * Check to see if a filesystem is available as a module
201 * Returns 1 if found, 0 if not
202 */
203int check_for_modules(const char *fs_name)
204{
205	struct utsname	uts;
206	FILE		*f;
207	char		buf[1024], *cp, *t;
208	int		i;
209
210	if (uname(&uts))
211		return (0);
212	snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
213
214	f = fopen(buf, "r");
215	if (!f)
216		return (0);
217	while (!feof(f)) {
218		if (!fgets(buf, sizeof(buf), f))
219			break;
220		if ((cp = strchr(buf, ':')) != NULL)
221			*cp = 0;
222		else
223			continue;
224		if ((cp = strrchr(buf, '/')) != NULL)
225			cp++;
226		i = strlen(cp);
227		if (i > 3) {
228			t = cp + i - 3;
229			if (!strcmp(t, ".ko"))
230				*t = 0;
231		}
232		if (!strcmp(cp, fs_name))
233			return (1);
234	}
235	fclose(f);
236	return (0);
237}
238
239static int system_supports_ext4()
240{
241	static time_t	last_check = 0;
242	static int	ret = -1;
243	time_t		now = time(0);
244
245	if (ret != -1 || (last_check - now) < 5)
246		return ret;
247	last_check = now;
248	ret = (fs_proc_check("ext4") || check_for_modules("ext4"));
249	return ret;
250}
251
252static int system_supports_ext4dev()
253{
254	static time_t	last_check = 0;
255	static int	ret = -1;
256	time_t		now = time(0);
257
258	if (ret != -1 || (last_check - now) < 5)
259		return ret;
260	last_check = now;
261	ret = (fs_proc_check("ext4dev") || check_for_modules("ext4dev"));
262	return ret;
263}
264
265static int probe_ext4dev(struct blkid_probe *probe,
266			 struct blkid_magic *id,
267			 unsigned char *buf)
268{
269	struct ext2_super_block *es;
270	es = (struct ext2_super_block *)buf;
271
272	/* Distinguish from jbd */
273	if (blkid_le32(es->s_feature_incompat) &
274	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
275		return -BLKID_ERR_PARAM;
276
277	/* ext4dev requires a journal */
278	if (!(blkid_le32(es->s_feature_compat) &
279	      EXT3_FEATURE_COMPAT_HAS_JOURNAL))
280		return -BLKID_ERR_PARAM;
281
282	/*
283	 * If the filesystem is marked as OK for use by in-development
284	 * filesystem code, but ext4dev is not supported, and ext4 is,
285	 * then don't call ourselves ext4dev, since we should be
286	 * detected as ext4 in that case.
287	 *
288	 * If the filesystem is marked as in use by production
289	 * filesystem, then it can only be used by ext4 and NOT by
290	 * ext4dev, so always disclaim we are ext4dev in that case.
291	 */
292	if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
293		if (!system_supports_ext4dev() && system_supports_ext4())
294			return -BLKID_ERR_PARAM;
295	} else
296		return -BLKID_ERR_PARAM;
297
298    	get_ext2_info(probe->dev, id, buf);
299	return 0;
300}
301
302static int probe_ext4(struct blkid_probe *probe, struct blkid_magic *id,
303		      unsigned char *buf)
304{
305	struct ext2_super_block *es;
306	es = (struct ext2_super_block *)buf;
307
308	/* Distinguish from jbd */
309	if (blkid_le32(es->s_feature_incompat) &
310	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
311		return -BLKID_ERR_PARAM;
312
313	/* ext4 requires journal */
314	if (!(blkid_le32(es->s_feature_compat) &
315	      EXT3_FEATURE_COMPAT_HAS_JOURNAL))
316		return -BLKID_ERR_PARAM;
317
318	/* Ext4 has at least one feature which ext3 doesn't understand */
319	if (!(blkid_le32(es->s_feature_ro_compat) &
320	      EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) &&
321	    !(blkid_le32(es->s_feature_incompat) &
322	      EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
323		return -BLKID_ERR_PARAM;
324
325	/*
326	 * If the filesystem is a OK for use by in-development
327	 * filesystem code, and ext4dev is supported or ext4 is not
328	 * supported, then don't call ourselves ext4, so we can redo
329	 * the detection and mark the filesystem as ext4dev.
330	 *
331	 * If the filesystem is marked as in use by production
332	 * filesystem, then it can only be used by ext4 and NOT by
333	 * ext4dev.
334	 */
335	if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
336		if (system_supports_ext4dev() || !system_supports_ext4())
337			return -BLKID_ERR_PARAM;
338	}
339    	get_ext2_info(probe->dev, id, buf);
340	return 0;
341}
342
343static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id,
344		      unsigned char *buf)
345{
346	struct ext2_super_block *es;
347	es = (struct ext2_super_block *)buf;
348
349	/* Distinguish from ext4dev */
350	if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS)
351		return -BLKID_ERR_PARAM;
352
353	/* ext3 requires journal */
354	if (!(blkid_le32(es->s_feature_compat) &
355	      EXT3_FEATURE_COMPAT_HAS_JOURNAL))
356		return -BLKID_ERR_PARAM;
357
358	/* Any features which ext3 doesn't understand */
359	if ((blkid_le32(es->s_feature_ro_compat) &
360	     EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) ||
361	    (blkid_le32(es->s_feature_incompat) &
362	     EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
363		return -BLKID_ERR_PARAM;
364
365    	get_ext2_info(probe->dev, id, buf);
366	return 0;
367}
368
369static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id,
370		      unsigned char *buf)
371{
372	struct ext2_super_block *es;
373
374	es = (struct ext2_super_block *)buf;
375
376	/* Distinguish between ext3 and ext2 */
377	if ((blkid_le32(es->s_feature_compat) &
378	      EXT3_FEATURE_COMPAT_HAS_JOURNAL))
379		return -BLKID_ERR_PARAM;
380
381	/* Any features which ext2 doesn't understand */
382	if ((blkid_le32(es->s_feature_ro_compat) &
383	     EXT2_FEATURE_RO_COMPAT_UNSUPPORTED) ||
384	    (blkid_le32(es->s_feature_incompat) &
385	     EXT2_FEATURE_INCOMPAT_UNSUPPORTED))
386		return -BLKID_ERR_PARAM;
387
388	get_ext2_info(probe->dev, id, buf);
389	return 0;
390}
391
392static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id,
393		     unsigned char *buf)
394{
395	struct ext2_super_block *es = (struct ext2_super_block *) buf;
396
397	if (!(blkid_le32(es->s_feature_incompat) &
398	      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
399		return -BLKID_ERR_PARAM;
400
401	get_ext2_info(probe->dev, id, buf);
402
403	return 0;
404}
405
406#define FAT_ATTR_VOLUME_ID		0x08
407#define FAT_ATTR_DIR			0x10
408#define FAT_ATTR_LONG_NAME		0x0f
409#define FAT_ATTR_MASK			0x3f
410#define FAT_ENTRY_FREE			0xe5
411
412static char *no_name = "NO NAME    ";
413
414static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
415{
416	int i;
417
418	for (i = 0; i < count; i++) {
419		if (dir[i].name[0] == 0x00)
420			break;
421
422		if ((dir[i].name[0] == FAT_ENTRY_FREE) ||
423		    (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) ||
424		    ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
425			continue;
426
427		if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
428		    FAT_ATTR_VOLUME_ID) {
429			return dir[i].name;
430		}
431	}
432	return 0;
433}
434
435/* FAT label extraction from the root directory taken from Kay
436 * Sievers's volume_id library */
437static int probe_fat(struct blkid_probe *probe,
438		      struct blkid_magic *id __BLKID_ATTR((unused)),
439		      unsigned char *buf)
440{
441	struct vfat_super_block *vs = (struct vfat_super_block *) buf;
442	struct msdos_super_block *ms = (struct msdos_super_block *) buf;
443	struct vfat_dir_entry *dir;
444	char serno[10];
445	const unsigned char *label = 0, *vol_label = 0, *tmp;
446	unsigned char	*vol_serno;
447	int label_len = 0, maxloop = 100;
448	__u16 sector_size, dir_entries, reserved;
449	__u32 sect_count, fat_size, dir_size, cluster_count, fat_length;
450	__u32 buf_size, start_data_sect, next, root_start, root_dir_entries;
451
452	/* sector size check */
453	tmp = (unsigned char *)&ms->ms_sector_size;
454	sector_size = tmp[0] + (tmp[1] << 8);
455	if (sector_size != 0x200 && sector_size != 0x400 &&
456	    sector_size != 0x800 && sector_size != 0x1000)
457		return 1;
458
459	tmp = (unsigned char *)&ms->ms_dir_entries;
460	dir_entries = tmp[0] + (tmp[1] << 8);
461	reserved =  blkid_le16(ms->ms_reserved);
462	tmp = (unsigned char *)&ms->ms_sectors;
463	sect_count = tmp[0] + (tmp[1] << 8);
464	if (sect_count == 0)
465		sect_count = blkid_le32(ms->ms_total_sect);
466
467	fat_length = blkid_le16(ms->ms_fat_length);
468	if (fat_length == 0)
469		fat_length = blkid_le32(vs->vs_fat32_length);
470
471	fat_size = fat_length * ms->ms_fats;
472	dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
473			(sector_size-1)) / sector_size;
474
475	cluster_count = sect_count - (reserved + fat_size + dir_size);
476	if (ms->ms_cluster_size == 0)
477		return 1;
478	cluster_count /= ms->ms_cluster_size;
479
480	if (cluster_count > FAT32_MAX)
481		return 1;
482
483	if (ms->ms_fat_length) {
484		/* the label may be an attribute in the root directory */
485		root_start = (reserved + fat_size) * sector_size;
486		root_dir_entries = vs->vs_dir_entries[0] +
487			(vs->vs_dir_entries[1] << 8);
488
489		buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
490		dir = (struct vfat_dir_entry *) get_buffer(probe, root_start,
491							   buf_size);
492		if (dir)
493			vol_label = search_fat_label(dir, root_dir_entries);
494
495		if (!vol_label || !memcmp(vol_label, no_name, 11))
496			vol_label = ms->ms_label;
497		vol_serno = ms->ms_serno;
498
499		blkid_set_tag(probe->dev, "SEC_TYPE", "msdos",
500			      sizeof("msdos"));
501	} else {
502		/* Search the FAT32 root dir for the label attribute */
503		buf_size = vs->vs_cluster_size * sector_size;
504		start_data_sect = reserved + fat_size;
505
506		next = blkid_le32(vs->vs_root_cluster);
507		while (next && --maxloop) {
508			__u32 next_sect_off;
509			__u64 next_off, fat_entry_off;
510			int count;
511
512			next_sect_off = (next - 2) * vs->vs_cluster_size;
513			next_off = (start_data_sect + next_sect_off) *
514				sector_size;
515
516			dir = (struct vfat_dir_entry *)
517				get_buffer(probe, next_off, buf_size);
518			if (dir == NULL)
519				break;
520
521			count = buf_size / sizeof(struct vfat_dir_entry);
522
523			vol_label = search_fat_label(dir, count);
524			if (vol_label)
525				break;
526
527			/* get FAT entry */
528			fat_entry_off = (reserved * sector_size) +
529				(next * sizeof(__u32));
530			buf = get_buffer(probe, fat_entry_off, buf_size);
531			if (buf == NULL)
532				break;
533
534			/* set next cluster */
535			next = blkid_le32(*((__u32 *) buf) & 0x0fffffff);
536		}
537
538		if (!vol_label || !memcmp(vol_label, no_name, 11))
539			vol_label = vs->vs_label;
540		vol_serno = vs->vs_serno;
541	}
542
543	if (vol_label && memcmp(vol_label, no_name, 11)) {
544		if ((label_len = figure_label_len(vol_label, 11)))
545			label = vol_label;
546	}
547
548	/* We can't just print them as %04X, because they are unaligned */
549	sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
550		vol_serno[1], vol_serno[0]);
551
552	blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len);
553	blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1);
554
555	return 0;
556}
557
558/*
559 * The FAT filesystem could be without a magic string in superblock
560 * (e.g. old floppies).  This heuristic for FAT detection is inspired
561 * by http://vrfy.org/projects/volume_id/ and Linux kernel.
562 * [7-Jul-2005, Karel Zak <kzak@redhat.com>]
563 */
564static int probe_fat_nomagic(struct blkid_probe *probe,
565			     struct blkid_magic *id __BLKID_ATTR((unused)),
566			     unsigned char *buf)
567{
568	struct vfat_super_block *vs;
569
570	vs = (struct vfat_super_block *)buf;
571
572	/* heads check */
573	if (vs->vs_heads == 0)
574		return 1;
575
576	/* cluster size check*/
577	if (vs->vs_cluster_size == 0 ||
578	    (vs->vs_cluster_size & (vs->vs_cluster_size-1)))
579		return 1;
580
581	/* media check */
582	if (vs->vs_media < 0xf8 && vs->vs_media != 0xf0)
583		return 1;
584
585	/* fat counts(Linux kernel expects at least 1 FAT table) */
586	if (!vs->vs_fats)
587		return 1;
588
589	return probe_fat(probe, id, buf);
590}
591
592static int probe_ntfs(struct blkid_probe *probe,
593		      struct blkid_magic *id __BLKID_ATTR((unused)),
594		      unsigned char *buf)
595{
596	struct ntfs_super_block *ns;
597	struct master_file_table_record *mft;
598	struct file_attribute *attr;
599	char		uuid_str[17], label_str[129], *cp;
600	int		bytes_per_sector, sectors_per_cluster;
601	int		mft_record_size, attr_off, attr_len;
602	unsigned int	i, attr_type, val_len;
603	int		val_off;
604	__u64		nr_clusters;
605	blkid_loff_t off;
606	unsigned char *buf_mft, *val;
607
608	ns = (struct ntfs_super_block *) buf;
609
610	bytes_per_sector = ns->bios_parameter_block[0] +
611		(ns->bios_parameter_block[1]  << 8);
612	sectors_per_cluster = ns->bios_parameter_block[2];
613
614	if ((bytes_per_sector < 512) || (sectors_per_cluster == 0))
615		return 1;
616
617	if (ns->cluster_per_mft_record < 0)
618		mft_record_size = 1 << (0-ns->cluster_per_mft_record);
619	else
620		mft_record_size = ns->cluster_per_mft_record *
621			sectors_per_cluster * bytes_per_sector;
622	nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster;
623
624	if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) ||
625	    (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters))
626		return 1;
627
628	off = blkid_le64(ns->mft_mirror_cluster_location) *
629		bytes_per_sector * sectors_per_cluster;
630
631	buf_mft = get_buffer(probe, off, mft_record_size);
632	if (!buf_mft)
633		return 1;
634
635	if (memcmp(buf_mft, "FILE", 4))
636		return 1;
637
638	off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector *
639		sectors_per_cluster;
640
641	buf_mft = get_buffer(probe, off, mft_record_size);
642	if (!buf_mft)
643		return 1;
644
645	if (memcmp(buf_mft, "FILE", 4))
646		return 1;
647
648	off += MFT_RECORD_VOLUME * mft_record_size;
649
650	buf_mft = get_buffer(probe, off, mft_record_size);
651	if (!buf_mft)
652		return 1;
653
654	if (memcmp(buf_mft, "FILE", 4))
655		return 1;
656
657	mft = (struct master_file_table_record *) buf_mft;
658
659	attr_off = blkid_le16(mft->attrs_offset);
660	label_str[0] = 0;
661
662	while (1) {
663		attr = (struct file_attribute *) (buf_mft + attr_off);
664		attr_len = blkid_le16(attr->len);
665		attr_type = blkid_le32(attr->type);
666		val_off = blkid_le16(attr->value_offset);
667		val_len = blkid_le32(attr->value_len);
668
669		attr_off += attr_len;
670
671		if ((attr_off > mft_record_size) ||
672		    (attr_len == 0))
673			break;
674
675		if (attr_type == MFT_RECORD_ATTR_END)
676			break;
677
678		if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
679			if (val_len > sizeof(label_str))
680				val_len = sizeof(label_str)-1;
681
682			for (i=0, cp=label_str; i < val_len; i+=2,cp++) {
683				val = ((__u8 *) attr) + val_off + i;
684				*cp = val[0];
685				if (val[1])
686					*cp = '?';
687			}
688			*cp = 0;
689		}
690	}
691
692	sprintf(uuid_str, "%016llX", blkid_le64(ns->volume_serial));
693	blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
694	if (label_str[0])
695		blkid_set_tag(probe->dev, "LABEL", label_str, 0);
696	return 0;
697}
698
699
700static int probe_xfs(struct blkid_probe *probe,
701		     struct blkid_magic *id __BLKID_ATTR((unused)),
702		     unsigned char *buf)
703{
704	struct xfs_super_block *xs;
705	const char *label = 0;
706
707	xs = (struct xfs_super_block *)buf;
708
709	if (strlen(xs->xs_fname))
710		label = xs->xs_fname;
711	blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname));
712	set_uuid(probe->dev, xs->xs_uuid, 0);
713	return 0;
714}
715
716static int probe_reiserfs(struct blkid_probe *probe,
717			  struct blkid_magic *id, unsigned char *buf)
718{
719	struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
720	unsigned int blocksize;
721	const char *label = 0;
722
723	blocksize = blkid_le16(rs->rs_blocksize);
724
725	/* The blocksize must be at least 1k */
726	if ((blocksize >> 10) == 0)
727		return -BLKID_ERR_PARAM;
728
729	/* If the superblock is inside the journal, we have the wrong one */
730	if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
731		return -BLKID_ERR_BIG;
732
733	/* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
734	if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') {
735		if (strlen(rs->rs_label))
736			label = rs->rs_label;
737		set_uuid(probe->dev, rs->rs_uuid, 0);
738	}
739	blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label));
740
741	return 0;
742}
743
744static int probe_reiserfs4(struct blkid_probe *probe,
745			   struct blkid_magic *id __BLKID_ATTR((unused)),
746			   unsigned char *buf)
747{
748	struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf;
749	const unsigned char *label = 0;
750
751	if (strlen((char *) rs4->rs4_label))
752		label = rs4->rs4_label;
753	set_uuid(probe->dev, rs4->rs4_uuid, 0);
754	blkid_set_tag(probe->dev, "LABEL", (const char *) label,
755		      sizeof(rs4->rs4_label));
756
757	return 0;
758}
759
760static int probe_jfs(struct blkid_probe *probe,
761		     struct blkid_magic *id __BLKID_ATTR((unused)),
762		     unsigned char *buf)
763{
764	struct jfs_super_block *js;
765	const char *label = 0;
766
767	js = (struct jfs_super_block *)buf;
768
769	if (strlen((char *) js->js_label))
770		label = (char *) js->js_label;
771	blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label));
772	set_uuid(probe->dev, js->js_uuid, 0);
773	return 0;
774}
775
776static int probe_luks(struct blkid_probe *probe,
777		       struct blkid_magic *id __BLKID_ATTR((unused)),
778		       unsigned char *buf)
779{
780	unsigned char uuid[40];
781	/* 168 is the offset to the 40 character uuid:
782	 * http://luks.endorphin.org/LUKS-on-disk-format.pdf */
783	strncpy(uuid, buf+168, 40);
784	blkid_set_tag(probe->dev, "UUID", uuid, sizeof(uuid));
785	return 0;
786}
787
788static int probe_romfs(struct blkid_probe *probe,
789		       struct blkid_magic *id __BLKID_ATTR((unused)),
790		       unsigned char *buf)
791{
792	struct romfs_super_block *ros;
793	const char *label = 0;
794
795	ros = (struct romfs_super_block *)buf;
796
797	if (strlen((char *) ros->ros_volume))
798		label = (char *) ros->ros_volume;
799	blkid_set_tag(probe->dev, "LABEL", label, 0);
800	return 0;
801}
802
803static int probe_cramfs(struct blkid_probe *probe,
804			struct blkid_magic *id __BLKID_ATTR((unused)),
805			unsigned char *buf)
806{
807	struct cramfs_super_block *csb;
808	const char *label = 0;
809
810	csb = (struct cramfs_super_block *)buf;
811
812	if (strlen((char *) csb->name))
813		label = (char *) csb->name;
814	blkid_set_tag(probe->dev, "LABEL", label, 0);
815	return 0;
816}
817
818static int probe_swap0(struct blkid_probe *probe,
819		       struct blkid_magic *id __BLKID_ATTR((unused)),
820		       unsigned char *buf __BLKID_ATTR((unused)))
821{
822	blkid_set_tag(probe->dev, "UUID", 0, 0);
823	blkid_set_tag(probe->dev, "LABEL", 0, 0);
824	return 0;
825}
826
827static int probe_swap1(struct blkid_probe *probe,
828		       struct blkid_magic *id __BLKID_ATTR((unused)),
829		       unsigned char *buf __BLKID_ATTR((unused)))
830{
831	struct swap_id_block *sws;
832
833	probe_swap0(probe, id, buf);
834	/*
835	 * Version 1 swap headers are always located at offset of 1024
836	 * bytes, although the swap signature itself is located at the
837	 * end of the page (which may vary depending on hardware
838	 * pagesize).
839	 */
840	sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024);
841	if (!sws)
842		return 1;
843
844	/* arbitrary sanity check.. is there any garbage down there? */
845	if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0)  {
846		if (sws->sws_volume[0])
847			blkid_set_tag(probe->dev, "LABEL", sws->sws_volume,
848				      sizeof(sws->sws_volume));
849		if (sws->sws_uuid[0])
850			set_uuid(probe->dev, sws->sws_uuid, 0);
851	}
852	return 0;
853}
854
855static int probe_iso9660(struct blkid_probe *probe,
856			 struct blkid_magic *id __BLKID_ATTR((unused)),
857			 unsigned char *buf)
858{
859	struct iso_volume_descriptor *iso;
860	const unsigned char *label;
861
862	iso = (struct iso_volume_descriptor *) buf;
863	label = iso->volume_id;
864
865	blkid_set_tag(probe->dev, "LABEL", (const char *) label,
866		      figure_label_len(label, 32));
867	return 0;
868}
869
870
871static const char
872*udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
873		 "NSR03", "TEA01", 0 };
874
875static int probe_udf(struct blkid_probe *probe,
876		     struct blkid_magic *id __BLKID_ATTR((unused)),
877		     unsigned char *buf __BLKID_ATTR((unused)))
878{
879	int j, bs;
880	struct iso_volume_descriptor *isosb;
881	const char ** m;
882
883	/* determine the block size by scanning in 2K increments
884	   (block sizes larger than 2K will be null padded) */
885	for (bs = 1; bs < 16; bs++) {
886		isosb = (struct iso_volume_descriptor *)
887			get_buffer(probe, bs*2048+32768, sizeof(isosb));
888		if (!isosb)
889			return 1;
890		if (isosb->vd_id[0])
891			break;
892	}
893
894	/* Scan up to another 64 blocks looking for additional VSD's */
895	for (j = 1; j < 64; j++) {
896		if (j > 1) {
897			isosb = (struct iso_volume_descriptor *)
898				get_buffer(probe, j*bs*2048+32768,
899					   sizeof(isosb));
900			if (!isosb)
901				return 1;
902		}
903		/* If we find NSR0x then call it udf:
904		   NSR01 for UDF 1.00
905		   NSR02 for UDF 1.50
906		   NSR03 for UDF 2.00 */
907		if (!memcmp(isosb->vd_id, "NSR0", 4))
908			return 0;
909		for (m = udf_magic; *m; m++)
910			if (!memcmp(*m, isosb->vd_id, 5))
911				break;
912		if (*m == 0)
913			return 1;
914	}
915	return 1;
916}
917
918static int probe_ocfs(struct blkid_probe *probe,
919		      struct blkid_magic *id __BLKID_ATTR((unused)),
920		      unsigned char *buf)
921{
922	struct ocfs_volume_header ovh;
923	struct ocfs_volume_label ovl;
924	__u32 major;
925
926	memcpy(&ovh, buf, sizeof(ovh));
927	memcpy(&ovl, buf+512, sizeof(ovl));
928
929	major = ocfsmajor(ovh);
930	if (major == 1)
931		blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
932	else if (major >= 9)
933		blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
934
935	blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl));
936	blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
937	set_uuid(probe->dev, ovl.vol_id, 0);
938	return 0;
939}
940
941static int probe_ocfs2(struct blkid_probe *probe,
942		       struct blkid_magic *id __BLKID_ATTR((unused)),
943		       unsigned char *buf)
944{
945	struct ocfs2_super_block *osb;
946
947	osb = (struct ocfs2_super_block *)buf;
948
949	blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
950	set_uuid(probe->dev, osb->s_uuid, 0);
951	return 0;
952}
953
954static int probe_oracleasm(struct blkid_probe *probe,
955			   struct blkid_magic *id __BLKID_ATTR((unused)),
956			   unsigned char *buf)
957{
958	struct oracle_asm_disk_label *dl;
959
960	dl = (struct oracle_asm_disk_label *)buf;
961
962	blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
963	return 0;
964}
965
966static int probe_gfs(struct blkid_probe *probe,
967		     struct blkid_magic *id __BLKID_ATTR((unused)),
968		     unsigned char *buf)
969{
970	struct gfs2_sb *sbd;
971	const char *label = 0;
972
973	sbd = (struct gfs2_sb *)buf;
974
975	if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS &&
976	    blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI)
977	{
978		blkid_set_tag(probe->dev, "UUID", 0, 0);
979
980		if (strlen(sbd->sb_locktable))
981			label = sbd->sb_locktable;
982		blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
983		return 0;
984	}
985	return 1;
986}
987
988static int probe_gfs2(struct blkid_probe *probe,
989		     struct blkid_magic *id __BLKID_ATTR((unused)),
990		     unsigned char *buf)
991{
992	struct gfs2_sb *sbd;
993	const char *label = 0;
994
995	sbd = (struct gfs2_sb *)buf;
996
997	if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS &&
998	    blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI)
999	{
1000		blkid_set_tag(probe->dev, "UUID", 0, 0);
1001
1002		if (strlen(sbd->sb_locktable))
1003			label = sbd->sb_locktable;
1004		blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1005		return 0;
1006	}
1007	return 1;
1008}
1009
1010static int probe_hfsplus(struct blkid_probe *probe,
1011			 struct blkid_magic *id __BLKID_ATTR((unused)),
1012			 unsigned char *buf)
1013{
1014	struct hfs_mdb *sbd = (struct hfs_mdb *)buf;
1015
1016	/* Check for a HFS+ volume embedded in a HFS volume */
1017	if (memcmp(sbd->embed_sig, "H+", 2) == 0)
1018		return 0;
1019
1020	return 1;
1021}
1022
1023/*
1024 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
1025 * in the type_array table below + bim_kbalign.
1026 *
1027 * When probing for a lot of magics, we handle everything in 1kB buffers so
1028 * that we don't have to worry about reading each combination of block sizes.
1029 */
1030#define BLKID_BLK_OFFS	64	/* currently reiserfs */
1031
1032/*
1033 * Various filesystem magics that we can check for.  Note that kboff and
1034 * sboff are in kilobytes and bytes respectively.  All magics are in
1035 * byte strings so we don't worry about endian issues.
1036 */
1037static struct blkid_magic type_array[] = {
1038/*  type     kboff   sboff len  magic			probe */
1039  { "oracleasm", 0,	32,  8, "ORCLDISK",		probe_oracleasm },
1040  { "ntfs",	 0,	 3,  8, "NTFS    ",		probe_ntfs },
1041  { "jbd",	 1,   0x38,  2, "\123\357",		probe_jbd },
1042  { "ext4dev",	 1,   0x38,  2, "\123\357",		probe_ext4dev },
1043  { "ext4",	 1,   0x38,  2, "\123\357",		probe_ext4 },
1044  { "ext3",	 1,   0x38,  2, "\123\357",		probe_ext3 },
1045  { "ext2",	 1,   0x38,  2, "\123\357",		probe_ext2 },
1046  { "reiserfs",	 8,   0x34,  8, "ReIsErFs",		probe_reiserfs },
1047  { "reiserfs", 64,   0x34,  9, "ReIsEr2Fs",		probe_reiserfs },
1048  { "reiserfs", 64,   0x34,  9, "ReIsEr3Fs",		probe_reiserfs },
1049  { "reiserfs", 64,   0x34,  8, "ReIsErFs",		probe_reiserfs },
1050  { "reiserfs",	 8,	20,  8, "ReIsErFs",		probe_reiserfs },
1051  { "reiser4",  64,	 0,  7, "ReIsEr4",		probe_reiserfs4 },
1052  { "gfs2",     64,      0,  4, "\x01\x16\x19\x70",     probe_gfs2 },
1053  { "gfs",      64,      0,  4, "\x01\x16\x19\x70",     probe_gfs },
1054  { "vfat",      0,   0x52,  5, "MSWIN",                probe_fat },
1055  { "vfat",      0,   0x52,  8, "FAT32   ",             probe_fat },
1056  { "vfat",      0,   0x36,  5, "MSDOS",                probe_fat },
1057  { "vfat",      0,   0x36,  8, "FAT16   ",             probe_fat },
1058  { "vfat",      0,   0x36,  8, "FAT12   ",             probe_fat },
1059  { "vfat",      0,      0,  1, "\353",                 probe_fat_nomagic },
1060  { "vfat",      0,      0,  1, "\351",                 probe_fat_nomagic },
1061  { "vfat",      0,  0x1fe,  2, "\125\252",             probe_fat_nomagic },
1062  { "minix",     1,   0x10,  2, "\177\023",             0 },
1063  { "minix",     1,   0x10,  2, "\217\023",             0 },
1064  { "minix",	 1,   0x10,  2, "\150\044",		0 },
1065  { "minix",	 1,   0x10,  2, "\170\044",		0 },
1066  { "vxfs",	 1,	 0,  4, "\365\374\001\245",	0 },
1067  { "xfs",	 0,	 0,  4, "XFSB",			probe_xfs },
1068  { "romfs",	 0,	 0,  8, "-rom1fs-",		probe_romfs },
1069  { "bfs",	 0,	 0,  4, "\316\372\173\033",	0 },
1070  { "cramfs",	 0,	 0,  4, "E=\315\050",		probe_cramfs },
1071  { "qnx4",	 0,	 4,  6, "QNX4FS",		0 },
1072  { "udf",	32,	 1,  5, "BEA01",		probe_udf },
1073  { "udf",	32,	 1,  5, "BOOT2",		probe_udf },
1074  { "udf",	32,	 1,  5, "CD001",		probe_udf },
1075  { "udf",	32,	 1,  5, "CDW02",		probe_udf },
1076  { "udf",	32,	 1,  5, "NSR02",		probe_udf },
1077  { "udf",	32,	 1,  5, "NSR03",		probe_udf },
1078  { "udf",	32,	 1,  5, "TEA01",		probe_udf },
1079  { "iso9660",	32,	 1,  5, "CD001",		probe_iso9660 },
1080  { "iso9660",	32,	 9,  5, "CDROM",		probe_iso9660 },
1081  { "jfs",	32,	 0,  4, "JFS1",			probe_jfs },
1082  { "hfsplus",	 1,	 0,  2, "BD",			probe_hfsplus },
1083  { "hfsplus",	 1,	 0,  2, "H+",			0 },
1084  { "hfs",	 1,	 0,  2, "BD",			0 },
1085  { "ufs",	 8,  0x55c,  4, "T\031\001\000",	0 },
1086  { "hpfs",	 8,	 0,  4, "I\350\225\371",	0 },
1087  { "sysv",	 0,  0x3f8,  4, "\020~\030\375",	0 },
1088  { "swap",	 0,  0xff6, 10, "SWAP-SPACE",		probe_swap0 },
1089  { "swap",	 0,  0xff6, 10, "SWAPSPACE2",		probe_swap1 },
1090  { "swsuspend", 0,  0xff6,  9, "S1SUSPEND",		probe_swap1 },
1091  { "swsuspend", 0,  0xff6,  9, "S2SUSPEND",		probe_swap1 },
1092  { "swap",	 0, 0x1ff6, 10, "SWAP-SPACE",		probe_swap0 },
1093  { "swap",	 0, 0x1ff6, 10, "SWAPSPACE2",		probe_swap1 },
1094  { "swsuspend", 0, 0x1ff6,  9, "S1SUSPEND",		probe_swap1 },
1095  { "swsuspend", 0, 0x1ff6,  9, "S2SUSPEND",		probe_swap1 },
1096  { "swap",	 0, 0x3ff6, 10, "SWAP-SPACE",		probe_swap0 },
1097  { "swap",	 0, 0x3ff6, 10, "SWAPSPACE2",		probe_swap1 },
1098  { "swsuspend", 0, 0x3ff6,  9, "S1SUSPEND",		probe_swap1 },
1099  { "swsuspend", 0, 0x3ff6,  9, "S2SUSPEND",		probe_swap1 },
1100  { "swap",	 0, 0x7ff6, 10, "SWAP-SPACE",		probe_swap0 },
1101  { "swap",	 0, 0x7ff6, 10, "SWAPSPACE2",		probe_swap1 },
1102  { "swsuspend", 0, 0x7ff6,  9, "S1SUSPEND",		probe_swap1 },
1103  { "swsuspend", 0, 0x7ff6,  9, "S2SUSPEND",		probe_swap1 },
1104  { "swap",	 0, 0xfff6, 10, "SWAP-SPACE",		probe_swap0 },
1105  { "swap",	 0, 0xfff6, 10, "SWAPSPACE2",		probe_swap1 },
1106  { "swsuspend", 0, 0xfff6,  9, "S1SUSPEND",		probe_swap1 },
1107  { "swsuspend", 0, 0xfff6,  9, "S2SUSPEND",		probe_swap1 },
1108  { "ocfs",	 0,	 8,  9,	"OracleCFS",		probe_ocfs },
1109  { "ocfs2",	 1,	 0,  6,	"OCFSV2",		probe_ocfs2 },
1110  { "ocfs2",	 2,	 0,  6,	"OCFSV2",		probe_ocfs2 },
1111  { "ocfs2",	 4,	 0,  6,	"OCFSV2",		probe_ocfs2 },
1112  { "ocfs2",	 8,	 0,  6,	"OCFSV2",		probe_ocfs2 },
1113  { "crypt_LUKS", 0,	 0,  6,	"LUKS\xba\xbe",		probe_luks },
1114  { "squashfs",	 0,	 0,  4,	"sqsh",			0 },
1115  { "squashfs",	 0,	 0,  4,	"hsqs",			0 },
1116  {   NULL,	 0,	 0,  0, NULL,			NULL }
1117};
1118
1119/*
1120 * Verify that the data in dev is consistent with what is on the actual
1121 * block device (using the devname field only).  Normally this will be
1122 * called when finding items in the cache, but for long running processes
1123 * is also desirable to revalidate an item before use.
1124 *
1125 * If we are unable to revalidate the data, we return the old data and
1126 * do not set the BLKID_BID_FL_VERIFIED flag on it.
1127 */
1128blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
1129{
1130	struct blkid_magic *id;
1131	struct blkid_probe probe;
1132	blkid_tag_iterate iter;
1133	unsigned char *buf;
1134	const char *type, *value;
1135	struct stat st;
1136	time_t diff, now;
1137	int idx;
1138
1139	if (!dev)
1140		return NULL;
1141
1142	now = time(0);
1143	diff = now - dev->bid_time;
1144
1145	if ((now > dev->bid_time) && (diff > 0) &&
1146	    ((diff < BLKID_PROBE_MIN) ||
1147	     (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
1148	      diff < BLKID_PROBE_INTERVAL)))
1149		return dev;
1150
1151	DBG(DEBUG_PROBE,
1152	    printf("need to revalidate %s (time since last check %llu)\n",
1153		   dev->bid_name, (unsigned long long)diff));
1154
1155	if (((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) ||
1156	    (fstat(probe.fd, &st) < 0)) {
1157		if (probe.fd >= 0) close(probe.fd);
1158		if (errno != EPERM) {
1159			blkid_free_dev(dev);
1160			return NULL;
1161		}
1162		/* We don't have read permission, just return cache data. */
1163		DBG(DEBUG_PROBE,
1164		    printf("returning unverified data for %s\n",
1165			   dev->bid_name));
1166		return dev;
1167	}
1168
1169	probe.cache = cache;
1170	probe.dev = dev;
1171	probe.sbbuf = 0;
1172	probe.buf = 0;
1173	probe.buf_max = 0;
1174
1175	/*
1176	 * Iterate over the type array.  If we already know the type,
1177	 * then try that first.  If it doesn't work, then blow away
1178	 * the type information, and try again.
1179	 *
1180	 */
1181try_again:
1182	type = 0;
1183	if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
1184		uuid_t	uuid;
1185
1186		if (check_mdraid(probe.fd, uuid) == 0) {
1187			set_uuid(dev, uuid, 0);
1188			type = "mdraid";
1189			goto found_type;
1190		}
1191	}
1192	for (id = type_array; id->bim_type; id++) {
1193		if (dev->bid_type &&
1194		    strcmp(id->bim_type, dev->bid_type))
1195			continue;
1196
1197		idx = id->bim_kboff + (id->bim_sboff >> 10);
1198		buf = get_buffer(&probe, idx << 10, 1024);
1199		if (!buf)
1200			continue;
1201
1202		if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
1203			   id->bim_len))
1204			continue;
1205
1206		if ((id->bim_probe == NULL) ||
1207		    (id->bim_probe(&probe, id, buf) == 0)) {
1208			type = id->bim_type;
1209			goto found_type;
1210		}
1211	}
1212
1213	if (!id->bim_type && dev->bid_type) {
1214		/*
1215		 * Zap the device filesystem information and try again
1216		 */
1217		DBG(DEBUG_PROBE,
1218		    printf("previous fs type %s not valid, "
1219			   "trying full probe\n", dev->bid_type));
1220		iter = blkid_tag_iterate_begin(dev);
1221		while (blkid_tag_next(iter, &type, &value) == 0)
1222			blkid_set_tag(dev, type, 0, 0);
1223		blkid_tag_iterate_end(iter);
1224		goto try_again;
1225	}
1226
1227	if (!dev->bid_type) {
1228		blkid_free_dev(dev);
1229		dev = 0;
1230		goto found_type;
1231	}
1232
1233found_type:
1234	if (dev && type) {
1235		dev->bid_devno = st.st_rdev;
1236		dev->bid_time = time(0);
1237		dev->bid_flags |= BLKID_BID_FL_VERIFIED;
1238		cache->bic_flags |= BLKID_BIC_FL_CHANGED;
1239
1240		blkid_set_tag(dev, "TYPE", type, 0);
1241
1242		DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
1243			   dev->bid_name, (long long)st.st_rdev, type));
1244	}
1245
1246	if (probe.sbbuf)
1247		free(probe.sbbuf);
1248	if (probe.buf)
1249		free(probe.buf);
1250	if (probe.fd >= 0)
1251		close(probe.fd);
1252
1253	return dev;
1254}
1255
1256int blkid_known_fstype(const char *fstype)
1257{
1258	struct blkid_magic *id;
1259
1260	for (id = type_array; id->bim_type; id++) {
1261		if (strcmp(fstype, id->bim_type) == 0)
1262			return 1;
1263	}
1264	return 0;
1265}
1266
1267#ifdef TEST_PROGRAM
1268int main(int argc, char **argv)
1269{
1270	blkid_dev dev;
1271	blkid_cache cache;
1272	int ret;
1273
1274	if (argc != 2) {
1275		fprintf(stderr, "Usage: %s device\n"
1276			"Probe a single device to determine type\n", argv[0]);
1277		exit(1);
1278	}
1279	if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
1280		fprintf(stderr, "%s: error creating cache (%d)\n",
1281			argv[0], ret);
1282		exit(1);
1283	}
1284	dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
1285	if (!dev) {
1286		printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
1287		return (1);
1288	}
1289	printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
1290	if (dev->bid_label)
1291		printf("LABEL='%s'\n", dev->bid_label);
1292	if (dev->bid_uuid)
1293		printf("UUID='%s'\n", dev->bid_uuid);
1294
1295	blkid_free_dev(dev);
1296	return (0);
1297}
1298#endif
1299