probe.c revision 66352fe20013fb17fc3602a8549c008d7bd68309
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#ifdef HAVE_ERRNO_H
29#include <errno.h>
30#endif
31#include "blkidP.h"
32#include "uuid/uuid.h"
33#include "probe.h"
34
35static int figure_label_len(const unsigned char *label, int len)
36{
37	const unsigned char *end = label + len - 1;
38
39	while ((*end == ' ' || *end == 0) && end >= label)
40		--end;
41	if (end >= label) {
42		label = label;
43		return end - label + 1;
44	}
45	return 0;
46}
47
48static unsigned char *get_buffer(struct blkid_probe *pr,
49			  unsigned off, size_t len)
50{
51	ssize_t		ret_read;
52	unsigned char	*newbuf;
53
54	if (off + len <= SB_BUFFER_SIZE) {
55		if (!pr->sbbuf) {
56			pr->sbbuf = malloc(SB_BUFFER_SIZE);
57			if (!pr->sbbuf)
58				return NULL;
59			if (lseek(pr->fd, 0, SEEK_SET) < 0)
60				return NULL;
61			ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE);
62			if (ret_read < 0)
63				ret_read = 0;
64			pr->sb_valid = ret_read;
65		}
66		if (off+len > pr->sb_valid)
67			return NULL;
68		return pr->sbbuf + off;
69	} else {
70		if (len > pr->buf_max) {
71			newbuf = realloc(pr->buf, len);
72			if (newbuf == NULL)
73				return NULL;
74			pr->buf = newbuf;
75			pr->buf_max = len;
76		}
77		if (lseek(pr->fd, off, SEEK_SET) < 0)
78			return NULL;
79		ret_read = read(pr->fd, pr->buf, len);
80		if (ret_read != (ssize_t) len)
81			return NULL;
82		return pr->buf;
83	}
84}
85
86
87/*
88 * This is a special case code to check for an MDRAID device.  We do
89 * this special since it requires checking for a superblock at the end
90 * of the device.
91 */
92static int check_mdraid(int fd, unsigned char *ret_uuid)
93{
94	struct mdp_superblock_s *md;
95	blkid_loff_t		offset;
96	char			buf[4096];
97
98	if (fd < 0)
99		return -BLKID_ERR_PARAM;
100
101	offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
102
103	if (blkid_llseek(fd, offset, 0) < 0 ||
104	    read(fd, buf, 4096) != 4096)
105		return -BLKID_ERR_IO;
106
107	/* Check for magic number */
108	if (memcmp("\251+N\374", buf, 4))
109		return -BLKID_ERR_PARAM;
110
111	if (!ret_uuid)
112		return 0;
113	*ret_uuid = 0;
114
115	/* The MD UUID is not contiguous in the superblock, make it so */
116	md = (struct mdp_superblock_s *)buf;
117	if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
118		memcpy(ret_uuid, &md->set_uuid0, 4);
119		memcpy(ret_uuid, &md->set_uuid1, 12);
120	}
121	return 0;
122}
123
124static void set_uuid(blkid_dev dev, uuid_t uuid, char *tag)
125{
126	char	str[37];
127
128	if (!uuid_is_null(uuid)) {
129		uuid_unparse(uuid, str);
130		blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
131	}
132}
133
134static void get_ext2_info(blkid_dev dev, unsigned char *buf)
135{
136	struct ext2_super_block *es = (struct ext2_super_block *) buf;
137	const char *label = 0;
138
139	DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
140		   blkid_le32(es->s_feature_compat),
141		   blkid_le32(es->s_feature_incompat),
142		   blkid_le32(es->s_feature_ro_compat)));
143
144	if (strlen(es->s_volume_name))
145		label = es->s_volume_name;
146	blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
147
148	set_uuid(dev, es->s_uuid, 0);
149}
150
151static int probe_ext3(struct blkid_probe *probe,
152		      struct blkid_magic *id __BLKID_ATTR((unused)),
153		      unsigned char *buf)
154{
155	struct ext2_super_block *es;
156	es = (struct ext2_super_block *)buf;
157
158	/* Distinguish between jbd and ext2/3 fs */
159	if (blkid_le32(es->s_feature_incompat) &
160	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
161		return -BLKID_ERR_PARAM;
162
163	/* Distinguish between ext3 and ext2 */
164	if (!(blkid_le32(es->s_feature_compat) &
165	      EXT3_FEATURE_COMPAT_HAS_JOURNAL))
166		return -BLKID_ERR_PARAM;
167
168	get_ext2_info(probe->dev, buf);
169
170	if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
171	    !uuid_is_null(es->s_journal_uuid))
172		set_uuid(probe->dev, es->s_journal_uuid, "EXT_JOURNAL");
173
174	blkid_set_tag(probe->dev, "SEC_TYPE", "ext2", sizeof("ext2"));
175
176	return 0;
177}
178
179static int probe_ext2(struct blkid_probe *probe,
180		      struct blkid_magic *id __BLKID_ATTR((unused)),
181		      unsigned char *buf)
182{
183	struct ext2_super_block *es;
184
185	es = (struct ext2_super_block *)buf;
186
187	/* Distinguish between jbd and ext2/3 fs */
188	if (blkid_le32(es->s_feature_incompat) &
189	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
190		return -BLKID_ERR_PARAM;
191
192	/* Distinguish between ext3 and ext2 */
193	if ((blkid_le32(es->s_feature_compat) &
194	      EXT3_FEATURE_COMPAT_HAS_JOURNAL))
195		return -BLKID_ERR_PARAM;
196
197	get_ext2_info(probe->dev, buf);
198
199	return 0;
200}
201
202static int probe_jbd(struct blkid_probe *probe,
203		     struct blkid_magic *id __BLKID_ATTR((unused)),
204		     unsigned char *buf)
205{
206	struct ext2_super_block *es = (struct ext2_super_block *) buf;
207
208	if (!(blkid_le32(es->s_feature_incompat) &
209	      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
210		return -BLKID_ERR_PARAM;
211
212	get_ext2_info(probe->dev, buf);
213
214	return 0;
215}
216
217#define FAT_ATTR_VOLUME_ID		0x08
218#define FAT_ATTR_DIR			0x10
219#define FAT_ATTR_LONG_NAME		0x0f
220#define FAT_ATTR_MASK			0x3f
221#define FAT_ENTRY_FREE			0xe5
222
223static char *no_name = "NO NAME    ";
224
225static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
226{
227	unsigned int i;
228
229	for (i = 0; i < count; i++) {
230		if (dir[i].name[0] == 0x00)
231			break;
232
233		if ((dir[i].name[0] == FAT_ENTRY_FREE) ||
234		    (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) ||
235		    ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
236			continue;
237
238		if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
239		    FAT_ATTR_VOLUME_ID) {
240			return dir[i].name;
241		}
242	}
243	return 0;
244}
245
246/* FAT label extraction from the root directory taken from Kay
247 * Sievers's volume_id library */
248static int probe_fat(struct blkid_probe *probe,
249		      struct blkid_magic *id __BLKID_ATTR((unused)),
250		      unsigned char *buf)
251{
252	struct vfat_super_block *vs = (struct vfat_super_block *) buf;
253	struct msdos_super_block *ms = (struct msdos_super_block *) buf;
254	struct vfat_dir_entry *dir;
255	char serno[10];
256	const unsigned char *label = 0, *vol_label = 0;
257	unsigned char	*vol_serno;
258	int label_len = 0, maxloop = 100;
259	__u16 sector_size, dir_entries, reserved;
260	__u32 sect_count, fat_size, dir_size, cluster_count, fat_length;
261	__u32 buf_size, start_data_sect, next, root_start, root_dir_entries;
262
263	/* sector size check */
264	sector_size = blkid_le16(*((__u16 *) &ms->ms_sector_size));
265	if (sector_size != 0x200 && sector_size != 0x400 &&
266	    sector_size != 0x800 && sector_size != 0x1000)
267		return 1;
268
269	dir_entries = blkid_le16(*((__u16 *) &ms->ms_dir_entries));
270	reserved =  blkid_le16(ms->ms_reserved);
271	sect_count = blkid_le16(*((__u16 *) &ms->ms_sectors));
272	if (sect_count == 0)
273		sect_count = blkid_le32(ms->ms_total_sect);
274
275	fat_length = blkid_le16(ms->ms_fat_length);
276	if (fat_length == 0)
277		fat_length = blkid_le32(vs->vs_fat32_length);
278
279	fat_size = fat_length * ms->ms_fats;
280	dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
281			(sector_size-1)) / sector_size;
282
283	cluster_count = sect_count - (reserved + fat_size + dir_size);
284	cluster_count /= ms->ms_cluster_size;
285
286	if (cluster_count > FAT32_MAX)
287		return 1;
288
289	if (ms->ms_fat_length) {
290		/* the label may be an attribute in the root directory */
291		root_start = (reserved + fat_size) * sector_size;
292		root_dir_entries = vs->vs_dir_entries[0] +
293			(vs->vs_dir_entries[1] << 8);
294
295		buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
296		dir = (struct vfat_dir_entry *) get_buffer(probe, root_start,
297							   buf_size);
298		if (dir)
299			vol_label = search_fat_label(dir, root_dir_entries);
300
301		if (!vol_label || !memcmp(vol_label, no_name, 11))
302			vol_label = ms->ms_label;
303		vol_serno = ms->ms_serno;
304
305		blkid_set_tag(probe->dev, "SEC_TYPE", "msdos",
306			      sizeof("msdos"));
307	} else {
308		/* Search the FAT32 root dir for the label attribute */
309		buf_size = vs->vs_cluster_size * sector_size;
310		start_data_sect = reserved + fat_size;
311
312		next = blkid_le32(vs->vs_root_cluster);
313		while (next && --maxloop) {
314			__u32 next_sect_off;
315			__u64 next_off, fat_entry_off;
316			int count;
317
318			next_sect_off = (next - 2) * vs->vs_cluster_size;
319			next_off = (start_data_sect + next_sect_off) *
320				sector_size;
321
322			dir = (struct vfat_dir_entry *)
323				get_buffer(probe, next_off, buf_size);
324			if (dir == NULL)
325				break;
326
327			count = buf_size / sizeof(struct vfat_dir_entry);
328
329			vol_label = search_fat_label(dir, count);
330			if (vol_label)
331				break;
332
333			/* get FAT entry */
334			fat_entry_off = (reserved * sector_size) +
335				(next * sizeof(__u32));
336			buf = get_buffer(probe, fat_entry_off, buf_size);
337			if (buf == NULL)
338				break;
339
340			/* set next cluster */
341			next = blkid_le32(*((__u32 *) buf) & 0x0fffffff);
342		}
343
344		if (!vol_label || !memcmp(vol_label, no_name, 11))
345			vol_label = vs->vs_label;
346		vol_serno = vs->vs_serno;
347	}
348
349	if (vol_label && memcmp(vol_label, no_name, 11)) {
350		label = vol_label;
351		label_len = figure_label_len(vol_label, 11);
352	}
353
354	/* We can't just print them as %04X, because they are unaligned */
355	sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
356		vol_serno[1], vol_serno[0]);
357
358	blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len);
359	blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1);
360
361	return 0;
362}
363
364/*
365 * The FAT filesystem could be without a magic string in superblock
366 * (e.g. old floppies).  This heuristic for FAT detection is inspired
367 * by http://vrfy.org/projects/volume_id/ and Linux kernel.
368 * [7-Jul-2005, Karel Zak <kzak@redhat.com>]
369 */
370static int probe_fat_nomagic(struct blkid_probe *probe,
371			     struct blkid_magic *id __BLKID_ATTR((unused)),
372			     unsigned char *buf)
373{
374	struct vfat_super_block *vs;
375
376	vs = (struct vfat_super_block *)buf;
377
378	/* heads check */
379	if (vs->vs_heads == 0)
380		return 1;
381
382	/* cluster size check*/
383	if (vs->vs_cluster_size == 0 ||
384	    (vs->vs_cluster_size & (vs->vs_cluster_size-1)))
385		return 1;
386
387	/* media check */
388	if (vs->vs_media < 0xf8 && vs->vs_media != 0xf0)
389		return 1;
390
391	/* fat counts(Linux kernel expects at least 1 FAT table) */
392	if (!vs->vs_fats)
393		return 1;
394
395	return probe_fat(probe, id, buf);
396}
397
398static int probe_xfs(struct blkid_probe *probe,
399		     struct blkid_magic *id __BLKID_ATTR((unused)),
400		     unsigned char *buf)
401{
402	struct xfs_super_block *xs;
403	const char *label = 0;
404
405	xs = (struct xfs_super_block *)buf;
406
407	if (strlen(xs->xs_fname))
408		label = xs->xs_fname;
409	blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname));
410	set_uuid(probe->dev, xs->xs_uuid, 0);
411	return 0;
412}
413
414static int probe_reiserfs(struct blkid_probe *probe,
415			  struct blkid_magic *id, unsigned char *buf)
416{
417	struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
418	unsigned int blocksize;
419	const char *label = 0;
420
421	blocksize = blkid_le16(rs->rs_blocksize);
422
423	/* If the superblock is inside the journal, we have the wrong one */
424	if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
425		return -BLKID_ERR_BIG;
426
427	/* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
428	if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') {
429		if (strlen(rs->rs_label))
430			label = rs->rs_label;
431		set_uuid(probe->dev, rs->rs_uuid, 0);
432	}
433	blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label));
434
435	return 0;
436}
437
438static int probe_reiserfs4(struct blkid_probe *probe,
439			   struct blkid_magic *id __BLKID_ATTR((unused)),
440			   unsigned char *buf)
441{
442	struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf;
443	const unsigned char *label = 0;
444
445	if (strlen((char *) rs4->rs4_label))
446		label = rs4->rs4_label;
447	set_uuid(probe->dev, rs4->rs4_uuid, 0);
448	blkid_set_tag(probe->dev, "LABEL", (const char *) label,
449		      sizeof(rs4->rs4_label));
450
451	return 0;
452}
453
454static int probe_jfs(struct blkid_probe *probe,
455		     struct blkid_magic *id __BLKID_ATTR((unused)),
456		     unsigned char *buf)
457{
458	struct jfs_super_block *js;
459	const char *label = 0;
460
461	js = (struct jfs_super_block *)buf;
462
463	if (strlen((char *) js->js_label))
464		label = (char *) js->js_label;
465	blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label));
466	set_uuid(probe->dev, js->js_uuid, 0);
467	return 0;
468}
469
470static int probe_romfs(struct blkid_probe *probe,
471		       struct blkid_magic *id __BLKID_ATTR((unused)),
472		       unsigned char *buf)
473{
474	struct romfs_super_block *ros;
475	const char *label = 0;
476
477	ros = (struct romfs_super_block *)buf;
478
479	if (strlen((char *) ros->ros_volume))
480		label = (char *) ros->ros_volume;
481	blkid_set_tag(probe->dev, "LABEL", label, 0);
482	return 0;
483}
484
485static int probe_cramfs(struct blkid_probe *probe,
486			struct blkid_magic *id __BLKID_ATTR((unused)),
487			unsigned char *buf)
488{
489	struct cramfs_super_block *csb;
490	const char *label = 0;
491
492	csb = (struct cramfs_super_block *)buf;
493
494	if (strlen((char *) csb->name))
495		label = (char *) csb->name;
496	blkid_set_tag(probe->dev, "LABEL", label, 0);
497	return 0;
498}
499
500static int probe_swap0(struct blkid_probe *probe,
501		       struct blkid_magic *id __BLKID_ATTR((unused)),
502		       unsigned char *buf __BLKID_ATTR((unused)))
503{
504	blkid_set_tag(probe->dev, "UUID", 0, 0);
505	blkid_set_tag(probe->dev, "LABEL", 0, 0);
506	return 0;
507}
508
509static int probe_swap1(struct blkid_probe *probe,
510		       struct blkid_magic *id __BLKID_ATTR((unused)),
511		       unsigned char *buf __BLKID_ATTR((unused)))
512{
513	struct swap_id_block *sws;
514
515	probe_swap0(probe, id, buf);
516	/*
517	 * Version 1 swap headers are always located at offset of 1024
518	 * bytes, although the swap signature itself is located at the
519	 * end of the page (which may vary depending on hardware
520	 * pagesize).
521	 */
522	sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024);
523	if (!sws)
524		return 1;
525
526	/* arbitrary sanity check.. is there any garbage down there? */
527	if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0)  {
528		if (sws->sws_volume[0])
529			blkid_set_tag(probe->dev, "LABEL", sws->sws_volume,
530				      sizeof(sws->sws_volume));
531		if (sws->sws_uuid[0])
532			set_uuid(probe->dev, sws->sws_uuid, 0);
533	}
534	return 0;
535}
536
537static int probe_iso9660(struct blkid_probe *probe,
538			 struct blkid_magic *id __BLKID_ATTR((unused)),
539			 unsigned char *buf)
540{
541	struct iso_volume_descriptor *iso;
542	const unsigned char *label;
543
544	iso = (struct iso_volume_descriptor *) buf;
545	label = iso->volume_id;
546
547	blkid_set_tag(probe->dev, "LABEL", (const char *) label,
548		      figure_label_len(label, 32));
549	return 0;
550}
551
552
553static const char
554*udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
555		 "NSR03", "TEA01", 0 };
556
557static int probe_udf(struct blkid_probe *probe,
558		     struct blkid_magic *id __BLKID_ATTR((unused)),
559		     unsigned char *buf __BLKID_ATTR((unused)))
560{
561	int j, bs;
562	struct iso_volume_descriptor *isosb;
563	const char ** m;
564
565	/* determine the block size by scanning in 2K increments
566	   (block sizes larger than 2K will be null padded) */
567	for (bs = 1; bs < 16; bs++) {
568		isosb = (struct iso_volume_descriptor *)
569			get_buffer(probe, bs*2048+32768, sizeof(isosb));
570		if (!isosb)
571			return 1;
572		if (isosb->vd_id[0])
573			break;
574	}
575
576	/* Scan up to another 64 blocks looking for additional VSD's */
577	for (j = 1; j < 64; j++) {
578		if (j > 1) {
579			isosb = (struct iso_volume_descriptor *)
580				get_buffer(probe, j*bs*2048+32768,
581					   sizeof(isosb));
582			if (!isosb)
583				return 1;
584		}
585		/* If we find NSR0x then call it udf:
586		   NSR01 for UDF 1.00
587		   NSR02 for UDF 1.50
588		   NSR03 for UDF 2.00 */
589		if (!memcmp(isosb->vd_id, "NSR0", 4))
590			return 0;
591		for (m = udf_magic; *m; m++)
592			if (!memcmp(*m, isosb->vd_id, 5))
593				break;
594		if (*m == 0)
595			return 1;
596	}
597	return 1;
598}
599
600static int probe_ocfs(struct blkid_probe *probe,
601		      struct blkid_magic *id __BLKID_ATTR((unused)),
602		      unsigned char *buf)
603{
604	struct ocfs_volume_header ovh;
605	struct ocfs_volume_label ovl;
606	__u32 major;
607
608	memcpy(&ovh, buf, sizeof(ovh));
609	memcpy(&ovl, buf+512, sizeof(ovl));
610
611	major = ocfsmajor(ovh);
612	if (major == 1)
613		blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
614	else if (major >= 9)
615		blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
616
617	blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl));
618	blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
619	set_uuid(probe->dev, ovl.vol_id, 0);
620	return 0;
621}
622
623static int probe_ocfs2(struct blkid_probe *probe,
624		       struct blkid_magic *id __BLKID_ATTR((unused)),
625		       unsigned char *buf)
626{
627	struct ocfs2_super_block *osb;
628
629	osb = (struct ocfs2_super_block *)buf;
630
631	blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
632	set_uuid(probe->dev, osb->s_uuid, 0);
633	return 0;
634}
635
636static int probe_oracleasm(struct blkid_probe *probe,
637			   struct blkid_magic *id __BLKID_ATTR((unused)),
638			   unsigned char *buf)
639{
640	struct oracle_asm_disk_label *dl;
641
642	dl = (struct oracle_asm_disk_label *)buf;
643
644	blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
645	return 0;
646}
647
648/*
649 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
650 * in the type_array table below + bim_kbalign.
651 *
652 * When probing for a lot of magics, we handle everything in 1kB buffers so
653 * that we don't have to worry about reading each combination of block sizes.
654 */
655#define BLKID_BLK_OFFS	64	/* currently reiserfs */
656
657/*
658 * Various filesystem magics that we can check for.  Note that kboff and
659 * sboff are in kilobytes and bytes respectively.  All magics are in
660 * byte strings so we don't worry about endian issues.
661 */
662static struct blkid_magic type_array[] = {
663/*  type     kboff   sboff len  magic			probe */
664  { "oracleasm", 0,	32,  8, "ORCLDISK",		probe_oracleasm },
665  { "ntfs",      0,      3,  8, "NTFS    ",             0 },
666  { "jbd",	 1,   0x38,  2, "\123\357",		probe_jbd },
667  { "ext3",	 1,   0x38,  2, "\123\357",		probe_ext3 },
668  { "ext2",	 1,   0x38,  2, "\123\357",		probe_ext2 },
669  { "reiserfs",	 8,   0x34,  8, "ReIsErFs",		probe_reiserfs },
670  { "reiserfs", 64,   0x34,  9, "ReIsEr2Fs",		probe_reiserfs },
671  { "reiserfs", 64,   0x34,  9, "ReIsEr3Fs",		probe_reiserfs },
672  { "reiserfs", 64,   0x34,  8, "ReIsErFs",		probe_reiserfs },
673  { "reiserfs",	 8,	20,  8, "ReIsErFs",		probe_reiserfs },
674  { "reiser4",  64,	 0,  7, "ReIsEr4",		probe_reiserfs4 },
675  { "vfat",      0,   0x52,  5, "MSWIN",                probe_fat },
676  { "vfat",      0,   0x52,  8, "FAT32   ",             probe_fat },
677  { "vfat",      0,   0x36,  5, "MSDOS",                probe_fat },
678  { "vfat",      0,   0x36,  8, "FAT16   ",             probe_fat },
679  { "vfat",      0,   0x36,  8, "FAT12   ",             probe_fat },
680  { "vfat",      0,      0,  2, "\353\220",             probe_fat_nomagic },
681  { "vfat",      0,      0,  1, "\351",                 probe_fat_nomagic },
682  { "minix",     1,   0x10,  2, "\177\023",             0 },
683  { "minix",     1,   0x10,  2, "\217\023",             0 },
684  { "minix",	 1,   0x10,  2, "\150\044",		0 },
685  { "minix",	 1,   0x10,  2, "\170\044",		0 },
686  { "vxfs",	 1,	 0,  4, "\365\374\001\245",	0 },
687  { "xfs",	 0,	 0,  4, "XFSB",			probe_xfs },
688  { "romfs",	 0,	 0,  8, "-rom1fs-",		probe_romfs },
689  { "bfs",	 0,	 0,  4, "\316\372\173\033",	0 },
690  { "cramfs",	 0,	 0,  4, "E=\315\050",		probe_cramfs },
691  { "qnx4",	 0,	 4,  6, "QNX4FS",		0 },
692  { "udf",	32,	 1,  5, "BEA01",		probe_udf },
693  { "udf",	32,	 1,  5, "BOOT2",		probe_udf },
694  { "udf",	32,	 1,  5, "CD001",		probe_udf },
695  { "udf",	32,	 1,  5, "CDW02",		probe_udf },
696  { "udf",	32,	 1,  5, "NSR02",		probe_udf },
697  { "udf",	32,	 1,  5, "NSR03",		probe_udf },
698  { "udf",	32,	 1,  5, "TEA01",		probe_udf },
699  { "iso9660",	32,	 1,  5, "CD001",		probe_iso9660 },
700  { "iso9660",	32,	 9,  5, "CDROM",		probe_iso9660 },
701  { "jfs",	32,	 0,  4, "JFS1",			probe_jfs },
702  { "hfs",	 1,	 0,  2, "BD",			0 },
703  { "ufs",	 8,  0x55c,  4, "T\031\001\000",	0 },
704  { "hpfs",	 8,	 0,  4, "I\350\225\371",	0 },
705  { "sysv",	 0,  0x3f8,  4, "\020~\030\375",	0 },
706  { "swap",	 0,  0xff6, 10, "SWAP-SPACE",		probe_swap0 },
707  { "swap",	 0,  0xff6, 10, "SWAPSPACE2",		probe_swap1 },
708  { "swsuspend", 0,  0xff6,  9, "S1SUSPEND",		probe_swap1 },
709  { "swsuspend", 0,  0xff6,  9, "S2SUSPEND",		probe_swap1 },
710  { "swap",	 0, 0x1ff6, 10, "SWAP-SPACE",		probe_swap0 },
711  { "swap",	 0, 0x1ff6, 10, "SWAPSPACE2",		probe_swap1 },
712  { "swsuspend", 0, 0x1ff6,  9, "S1SUSPEND",		probe_swap1 },
713  { "swsuspend", 0, 0x1ff6,  9, "S2SUSPEND",		probe_swap1 },
714  { "swap",	 0, 0x3ff6, 10, "SWAP-SPACE",		probe_swap0 },
715  { "swap",	 0, 0x3ff6, 10, "SWAPSPACE2",		probe_swap1 },
716  { "swsuspend", 0, 0x3ff6,  9, "S1SUSPEND",		probe_swap1 },
717  { "swsuspend", 0, 0x3ff6,  9, "S2SUSPEND",		probe_swap1 },
718  { "swap",	 0, 0x7ff6, 10, "SWAP-SPACE",		probe_swap0 },
719  { "swap",	 0, 0x7ff6, 10, "SWAPSPACE2",		probe_swap1 },
720  { "swsuspend", 0, 0x7ff6,  9, "S1SUSPEND",		probe_swap1 },
721  { "swsuspend", 0, 0x7ff6,  9, "S2SUSPEND",		probe_swap1 },
722  { "swap",	 0, 0xfff6, 10, "SWAP-SPACE",		probe_swap0 },
723  { "swap",	 0, 0xfff6, 10, "SWAPSPACE2",		probe_swap1 },
724  { "swsuspend", 0, 0xfff6,  9, "S1SUSPEND",		probe_swap1 },
725  { "swsuspend", 0, 0xfff6,  9, "S2SUSPEND",		probe_swap1 },
726  { "ocfs",	 0,	 8,  9,	"OracleCFS",		probe_ocfs },
727  { "ocfs2",	 1,	 0,  6,	"OCFSV2",		probe_ocfs2 },
728  { "ocfs2",	 2,	 0,  6,	"OCFSV2",		probe_ocfs2 },
729  { "ocfs2",	 4,	 0,  6,	"OCFSV2",		probe_ocfs2 },
730  { "ocfs2",	 8,	 0,  6,	"OCFSV2",		probe_ocfs2 },
731  {   NULL,	 0,	 0,  0, NULL,			NULL }
732};
733
734/*
735 * Verify that the data in dev is consistent with what is on the actual
736 * block device (using the devname field only).  Normally this will be
737 * called when finding items in the cache, but for long running processes
738 * is also desirable to revalidate an item before use.
739 *
740 * If we are unable to revalidate the data, we return the old data and
741 * do not set the BLKID_BID_FL_VERIFIED flag on it.
742 */
743blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
744{
745	struct blkid_magic *id;
746	struct blkid_probe probe;
747	blkid_tag_iterate iter;
748	unsigned char *buf;
749	const char *type, *value;
750	struct stat st;
751	time_t diff, now;
752	int idx;
753
754	if (!dev)
755		return NULL;
756
757	now = time(0);
758	diff = now - dev->bid_time;
759
760	if ((now > dev->bid_time) && (diff > 0) &&
761	    ((diff < BLKID_PROBE_MIN) ||
762	     (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
763	      diff < BLKID_PROBE_INTERVAL)))
764		return dev;
765
766	DBG(DEBUG_PROBE,
767	    printf("need to revalidate %s (time since last check %lu)\n",
768		   dev->bid_name, diff));
769
770	if (((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) ||
771	    (fstat(probe.fd, &st) < 0)) {
772		if (probe.fd >= 0) close(probe.fd);
773		if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
774			blkid_free_dev(dev);
775			return NULL;
776		}
777		/* We don't have read permission, just return cache data. */
778		DBG(DEBUG_PROBE,
779		    printf("returning unverified data for %s\n",
780			   dev->bid_name));
781		return dev;
782	}
783
784	probe.cache = cache;
785	probe.dev = dev;
786	probe.sbbuf = 0;
787	probe.buf = 0;
788	probe.buf_max = 0;
789
790	/*
791	 * Iterate over the type array.  If we already know the type,
792	 * then try that first.  If it doesn't work, then blow away
793	 * the type information, and try again.
794	 *
795	 */
796try_again:
797	type = 0;
798	if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
799		uuid_t	uuid;
800
801		if (check_mdraid(probe.fd, uuid) == 0) {
802			set_uuid(dev, uuid, 0);
803			type = "mdraid";
804			goto found_type;
805		}
806	}
807	for (id = type_array; id->bim_type; id++) {
808		if (dev->bid_type &&
809		    strcmp(id->bim_type, dev->bid_type))
810			continue;
811
812		idx = id->bim_kboff + (id->bim_sboff >> 10);
813		buf = get_buffer(&probe, idx << 10, 1024);
814		if (!buf)
815			continue;
816
817		if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
818			   id->bim_len))
819			continue;
820
821		if ((id->bim_probe == NULL) ||
822		    (id->bim_probe(&probe, id, buf) == 0)) {
823			type = id->bim_type;
824			goto found_type;
825		}
826	}
827
828	if (!id->bim_type && dev->bid_type) {
829		/*
830		 * Zap the device filesystem information and try again
831		 */
832		iter = blkid_tag_iterate_begin(dev);
833		while (blkid_tag_next(iter, &type, &value) == 0)
834			blkid_set_tag(dev, type, 0, 0);
835		blkid_tag_iterate_end(iter);
836		goto try_again;
837	}
838
839	if (!dev->bid_type) {
840		if (probe.fd >= 0) close(probe.fd);
841		blkid_free_dev(dev);
842		return NULL;
843	}
844
845found_type:
846	if (dev && type) {
847		dev->bid_devno = st.st_rdev;
848		dev->bid_time = time(0);
849		dev->bid_flags |= BLKID_BID_FL_VERIFIED;
850		cache->bic_flags |= BLKID_BIC_FL_CHANGED;
851
852		blkid_set_tag(dev, "TYPE", type, 0);
853
854		DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
855			   dev->bid_name, st.st_rdev, type));
856	}
857
858	if (probe.sbbuf)
859		free(probe.sbbuf);
860	if (probe.buf)
861		free(probe.buf);
862	close(probe.fd);
863
864	return dev;
865}
866
867int blkid_known_fstype(const char *fstype)
868{
869	struct blkid_magic *id;
870
871	for (id = type_array; id->bim_type; id++) {
872		if (strcmp(fstype, id->bim_type) == 0)
873			return 1;
874	}
875	return 0;
876}
877
878#ifdef TEST_PROGRAM
879int main(int argc, char **argv)
880{
881	blkid_dev dev;
882	blkid_cache cache;
883	int ret;
884
885	if (argc != 2) {
886		fprintf(stderr, "Usage: %s device\n"
887			"Probe a single device to determine type\n", argv[0]);
888		exit(1);
889	}
890	if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
891		fprintf(stderr, "%s: error creating cache (%d)\n",
892			argv[0], ret);
893		exit(1);
894	}
895	dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
896	if (!dev) {
897		printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
898		return (1);
899	}
900	printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
901	if (dev->bid_label)
902		printf("LABEL='%s'\n", dev->bid_label);
903	if (dev->bid_uuid)
904		printf("UUID='%s'\n", dev->bid_uuid);
905
906	blkid_free_dev(dev);
907	return (0);
908}
909#endif
910