probe.c revision f0a22d0fd3ec3f45b562af5afba8811f72b94a28
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 *
9 * %Begin-Header%
10 * This file may be redistributed under the terms of the
11 * GNU Lesser General Public License.
12 * %End-Header%
13 */
14
15#include <stdio.h>
16#include <string.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <fcntl.h>
20#include <sys/types.h>
21#ifdef HAVE_SYS_STAT_H
22#include <sys/stat.h>
23#endif
24#ifdef HAVE_SYS_MKDEV_H
25#include <sys/mkdev.h>
26#endif
27#ifdef HAVE_ERRNO_H
28#include <errno.h>
29#endif
30#include "blkidP.h"
31#include "uuid/uuid.h"
32#include "probe.h"
33
34/*
35 * This is a special case code to check for an MDRAID device.  We do
36 * this special since it requires checking for a superblock at the end
37 * of the device.
38 */
39static int check_mdraid(int fd, unsigned char *ret_uuid)
40{
41	struct mdp_superblock_s *md;
42	blkid_loff_t		offset;
43	char			buf[4096];
44
45	if (fd < 0)
46		return -BLKID_ERR_PARAM;
47
48	offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
49
50	if (blkid_llseek(fd, offset, 0) < 0 ||
51	    read(fd, buf, 4096) != 4096)
52		return -BLKID_ERR_IO;
53
54	/* Check for magic number */
55	if (memcmp("\251+N\374", buf, 4))
56		return -BLKID_ERR_PARAM;
57
58	if (!ret_uuid)
59		return 0;
60	*ret_uuid = 0;
61
62	/* The MD UUID is not contiguous in the superblock, make it so */
63	md = (struct mdp_superblock_s *)buf;
64	if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
65		memcpy(ret_uuid, &md->set_uuid0, 4);
66		memcpy(ret_uuid, &md->set_uuid1, 12);
67	}
68	return 0;
69}
70
71static void set_uuid(blkid_dev dev, uuid_t uuid)
72{
73	char	str[37];
74
75	if (!uuid_is_null(uuid)) {
76		uuid_unparse(uuid, str);
77		blkid_set_tag(dev, "UUID", str, sizeof(str), 1);
78	}
79}
80
81static int probe_ext2(int fd, blkid_cache cache, blkid_dev dev,
82		      struct blkid_magic *id, unsigned char *buf,
83		      const char **ret_sectype)
84{
85	struct ext2_super_block *es;
86
87	es = (struct ext2_super_block *)buf;
88
89	DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
90		   blkid_le32(es->s_feature_compat),
91		   blkid_le32(es->s_feature_incompat),
92		   blkid_le32(es->s_feature_ro_compat)));
93
94	/* Make sure we don't keep re-probing as ext2 for a journaled fs */
95	if (!strcmp(id->bim_type, "ext2") &&
96	    ((blkid_le32(es->s_feature_compat) &
97	      EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
98	     (blkid_le32(es->s_feature_incompat) &
99	      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)))
100		return -BLKID_ERR_PARAM;
101
102	if (strlen(es->s_volume_name))
103		blkid_set_tag(dev, "LABEL", es->s_volume_name,
104			      sizeof(es->s_volume_name), 1);
105
106	set_uuid(dev, es->s_uuid);
107
108	return 0;
109}
110
111static int probe_jbd(int fd, blkid_cache cache, blkid_dev dev,
112		     struct blkid_magic *id, unsigned char *buf,
113		     const char **ret_sectype)
114{
115	struct ext2_super_block *es = (struct ext2_super_block *) buf;
116
117	if (!(blkid_le32(es->s_feature_incompat) &
118	      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
119		return -BLKID_ERR_PARAM;
120
121	return (probe_ext2(fd, cache, dev, id, buf, ret_sectype));
122}
123
124static int probe_ext3(int fd, blkid_cache cache, blkid_dev dev,
125		      struct blkid_magic *id, unsigned char *buf,
126		      const char **ret_sectype)
127{
128	struct ext2_super_block *es = (struct ext2_super_block *) buf;
129	int ret;
130
131	if (!(blkid_le32(es->s_feature_compat) &
132	      EXT3_FEATURE_COMPAT_HAS_JOURNAL))
133		return -BLKID_ERR_PARAM;
134
135	if ((ret = probe_ext2(fd, cache, dev, id, buf, ret_sectype)) < 0)
136		return ret;
137
138	if (!(blkid_le32(es->s_feature_incompat) &
139	      EXT3_FEATURE_INCOMPAT_RECOVER))
140		*ret_sectype = "ext2";
141
142	return 0;
143}
144
145static int probe_vfat(int fd, blkid_cache cache, blkid_dev dev,
146		      struct blkid_magic *id, unsigned char *buf,
147		      const char **ret_sectype)
148{
149	struct vfat_super_block *vs;
150	char serno[10];
151
152	vs = (struct vfat_super_block *)buf;
153
154	if (strncmp(vs->vs_label, "NO NAME", 7)) {
155		char *end = vs->vs_label + sizeof(vs->vs_label) - 1;
156
157		while (*end == ' ' && end >= vs->vs_label)
158			--end;
159		if (end >= vs->vs_label)
160			blkid_set_tag(dev, "LABEL", vs->vs_label,
161				      end - vs->vs_label + 1, 1);
162	}
163
164	/* We can't just print them as %04X, because they are unaligned */
165	sprintf(serno, "%02X%02X-%02X%02X", vs->vs_serno[3], vs->vs_serno[2],
166		vs->vs_serno[1], vs->vs_serno[0]);
167	blkid_set_tag(dev, "UUID", serno, sizeof(serno), 1);
168
169	return 0;
170}
171
172static int probe_msdos(int fd, blkid_cache cache, blkid_dev dev,
173		       struct blkid_magic *id, unsigned char *buf,
174		       const char **ret_sectype)
175{
176	struct msdos_super_block *ms = (struct msdos_super_block *) buf;
177	char serno[10];
178
179	if (strncmp(ms->ms_label, "NO NAME", 7)) {
180		char *end = ms->ms_label + sizeof(ms->ms_label) - 1;
181
182		while (*end == ' ' && end >= ms->ms_label)
183			--end;
184		if (end >= ms->ms_label)
185			blkid_set_tag(dev, "LABEL", ms->ms_label,
186				      end - ms->ms_label + 1, 1);
187	}
188
189	/* We can't just print them as %04X, because they are unaligned */
190	sprintf(serno, "%02X%02X-%02X%02X", ms->ms_serno[3], ms->ms_serno[2],
191		ms->ms_serno[1], ms->ms_serno[0]);
192	blkid_set_tag(dev, "UUID", serno, 0, 1);
193
194	return 0;
195}
196
197static int probe_xfs(int fd, blkid_cache cache, blkid_dev dev,
198		     struct blkid_magic *id, unsigned char *buf,
199		     const char **ret_sectype)
200{
201	struct xfs_super_block *xs;
202
203	xs = (struct xfs_super_block *)buf;
204
205	if (strlen(xs->xs_fname))
206		blkid_set_tag(dev, "LABEL", xs->xs_fname,
207			      sizeof(xs->xs_fname), 1);
208	set_uuid(dev, xs->xs_uuid);
209	return 0;
210}
211
212static int probe_reiserfs(int fd, blkid_cache cache, blkid_dev dev,
213			  struct blkid_magic *id, unsigned char *buf,
214			  const char **ret_sectype)
215{
216	struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
217	unsigned int blocksize;
218
219	blocksize = blkid_le16(rs->rs_blocksize);
220
221	/* If the superblock is inside the journal, we have the wrong one */
222	if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
223		return -BLKID_ERR_BIG;
224
225	/* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
226	if (!strcmp(id->bim_magic, "ReIsEr2Fs") ||
227	    !strcmp(id->bim_magic, "ReIsEr3Fs")) {
228		if (strlen(rs->rs_label)) {
229			blkid_set_tag(dev, "LABEL", rs->rs_label,
230				      sizeof(rs->rs_label), 1);
231		}
232
233		set_uuid(dev, rs->rs_uuid);
234	}
235
236	return 0;
237}
238
239/*
240 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
241 * in the type_array table below + bim_kbalign.
242 *
243 * When probing for a lot of magics, we handle everything in 1kB buffers so
244 * that we don't have to worry about reading each combination of block sizes.
245 */
246#define BLKID_BLK_OFFS	64	/* currently reiserfs */
247
248/*
249 * Various filesystem magics that we can check for.  Note that kboff and
250 * sboff are in kilobytes and bytes respectively.  All magics are in
251 * byte strings so we don't worry about endian issues.
252 */
253static struct blkid_magic type_array[] = {
254/*  type     kboff   sboff len  magic			probe */
255  { "jbd",	 1,   0x38,  2, "\123\357",		probe_jbd },
256  { "ext3",	 1,   0x38,  2, "\123\357",		probe_ext3 },
257  { "ext2",	 1,   0x38,  2, "\123\357",		probe_ext2 },
258  { "reiserfs",	 8,   0x34,  8, "ReIsErFs",		probe_reiserfs },
259  { "reiserfs", 64,   0x34,  9, "ReIsEr2Fs",		probe_reiserfs },
260  { "reiserfs", 64,   0x34,  9, "ReIsEr3Fs",		probe_reiserfs },
261  { "reiserfs", 64,   0x34,  8, "ReIsErFs",		probe_reiserfs },
262  { "reiserfs",	 8,	20,  8, "ReIsErFs",		probe_reiserfs },
263  { "ntfs",      0,      3,  8, "NTFS    ",             0 },
264  { "vfat",      0,   0x52,  5, "MSWIN",                probe_vfat },
265  { "vfat",      0,   0x52,  8, "FAT32   ",             probe_vfat },
266  { "msdos",     0,   0x36,  5, "MSDOS",                probe_msdos },
267  { "msdos",     0,   0x36,  8, "FAT16   ",             probe_msdos },
268  { "msdos",     0,   0x36,  8, "FAT12   ",             probe_msdos },
269  { "minix",     1,   0x10,  2, "\177\023",             0 },
270  { "minix",     1,   0x10,  2, "\217\023",             0 },
271  { "minix",	 1,   0x10,  2, "\150\044",		0 },
272  { "minix",	 1,   0x10,  2, "\170\044",		0 },
273  { "vxfs",	 1,	 0,  4, "\365\374\001\245",	0 },
274  { "xfs",	 0,	 0,  4, "XFSB",			probe_xfs },
275  { "romfs",	 0,	 0,  8, "-rom1fs-",		0 },
276  { "bfs",	 0,	 0,  4, "\316\372\173\033",	0 },
277  { "cramfs",	 0,	 0,  4, "E=\315\034",		0 },
278  { "qnx4",	 0,	 4,  6, "QNX4FS",		0 },
279  { "iso9660",	32,	 1,  5, "CD001",		0 },
280  { "iso9660",	32,	 9,  5, "CDROM",		0 },
281  { "udf",	32,	 1,  5, "BEA01",		0 },
282  { "udf",	32,	 1,  5, "BOOT2",		0 },
283  { "udf",	32,	 1,  5, "CD001",		0 },
284  { "udf",	32,	 1,  5, "CDW02",		0 },
285  { "udf",	32,	 1,  5, "NSR02",		0 },
286  { "udf",	32,	 1,  5, "NSR03",		0 },
287  { "udf",	32,	 1,  5, "TEA01",		0 },
288  { "jfs",	32,	 0,  4, "JFS1",			0 },
289  { "hfs",	 1,	 0,  2, "BD",			0 },
290  { "ufs",	 8,  0x55c,  4, "T\031\001\000",	0 },
291  { "hpfs",	 8,	 0,  4, "I\350\225\371",	0 },
292  { "sysv",	 0,  0x3f8,  4, "\020~\030\375",	0 },
293  { "swap",	 0,  0xff6, 10, "SWAP-SPACE",		0 },
294  { "swap",	 0,  0xff6, 10, "SWAPSPACE2",		0 },
295  { "swap",	 0, 0x1ff6, 10, "SWAP-SPACE",		0 },
296  { "swap",	 0, 0x1ff6, 10, "SWAPSPACE2",		0 },
297  { "swap",	 0, 0x3ff6, 10, "SWAP-SPACE",		0 },
298  { "swap",	 0, 0x3ff6, 10, "SWAPSPACE2",		0 },
299  {   NULL,	 0,	 0,  0, NULL,			NULL }
300};
301
302/*
303 * If a device's filesystem no longer checks out, we need to nuke
304 * information about it from the entry.
305 */
306static void blkid_invalidate_fs(blkid_dev dev)
307{
308	blkid_set_tag(dev, "TYPE", 0, 0, 0);
309	blkid_set_tag(dev, "LABEL", 0, 0, 0);
310	blkid_set_tag(dev, "UUID", 0, 0, 0);
311}
312
313/*
314 * Verify that the data in dev is consistent with what is on the actual
315 * block device (using the devname field only).  Normally this will be
316 * called when finding items in the cache, but for long running processes
317 * is also desirable to revalidate an item before use.
318 *
319 * If we are unable to revalidate the data, we return the old data and
320 * do not set the BLKID_BID_FL_VERIFIED flag on it.
321 */
322blkid_dev blkid_verify_devname(blkid_cache cache, blkid_dev dev)
323{
324	struct blkid_magic *id;
325	unsigned char *bufs[BLKID_BLK_OFFS + 1], *buf;
326	const char *sec_type, *type;
327	struct stat st;
328	time_t diff;
329	int fd, idx;
330
331	if (!dev)
332		return NULL;
333
334	diff = time(0) - dev->bid_time;
335
336	if (diff < BLKID_PROBE_MIN || (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
337				       diff < BLKID_PROBE_INTERVAL))
338		return dev;
339
340	DBG(DEBUG_PROBE,
341	    printf("need to revalidate %s (time since last check %lu)\n",
342		   dev->bid_name, diff));
343
344	if (((fd = open(dev->bid_name, O_RDONLY)) < 0) ||
345	    (fstat(fd, &st) < 0) || !S_ISBLK(st.st_mode)) {
346		if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
347			blkid_free_dev(dev);
348			return NULL;
349		}
350		/* We don't have read permission, just return cache data. */
351		DBG(DEBUG_PROBE,
352		    printf("returning unverified data for %s\n",
353			   dev->bid_name));
354		return dev;
355	}
356
357	memset(bufs, 0, sizeof(bufs));
358
359	/*
360	 * Iterate over the type array.  If we already know the type,
361	 * then try that first.  If it doesn't work, then blow away
362	 * the type information, and try again.
363	 *
364	 */
365try_again:
366	type = 0;
367	sec_type = 0;
368	if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
369		uuid_t	uuid;
370
371		if (check_mdraid(fd, uuid) == 0) {
372			set_uuid(dev, uuid);
373			type = "mdraid";
374			goto found_type;
375		}
376	}
377	for (id = type_array; id->bim_type; id++) {
378		if (dev->bid_type &&
379		    strcmp(id->bim_type, dev->bid_type))
380			continue;
381
382		idx = id->bim_kboff + (id->bim_sboff >> 10);
383		if (idx > BLKID_BLK_OFFS || idx < 0)
384			continue;
385		buf = bufs[idx];
386		if (!buf) {
387			if (lseek(fd, idx << 10, SEEK_SET) < 0)
388				continue;
389
390			if (!(buf = (unsigned char *)malloc(1024)))
391				continue;
392
393			if (read(fd, buf, 1024) != 1024) {
394				free(buf);
395				continue;
396			}
397			bufs[idx] = buf;
398		}
399
400		if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
401			   id->bim_len))
402			continue;
403
404		if ((id->bim_probe == NULL) ||
405		    (id->bim_probe(fd, cache, dev, id, buf, &sec_type) == 0)) {
406			type = id->bim_type;
407			goto found_type;
408		}
409	}
410
411	if (!id->bim_type && dev->bid_type) {
412		/*
413		 * Zap the device filesystem type and try again
414		 */
415		blkid_invalidate_fs(dev);
416		goto try_again;
417	}
418
419	if (!dev->bid_type) {
420		blkid_free_dev(dev);
421		return NULL;
422	}
423
424found_type:
425	if (dev && type) {
426		dev->bid_devno = st.st_rdev;
427		dev->bid_time = time(0);
428		dev->bid_flags |= BLKID_BID_FL_VERIFIED;
429		cache->bic_flags |= BLKID_BIC_FL_CHANGED;
430
431		blkid_set_tag(dev, "TYPE", type, 0, 1);
432		if (sec_type)
433			blkid_set_tag(dev, "TYPE", sec_type, 0, 0);
434
435		DBG(DEBUG_PROBE, printf("%s: devno 0x%04Lx, type %s\n",
436			   dev->bid_name, st.st_rdev, type));
437	}
438
439	close(fd);
440
441	return dev;
442}
443
444#ifdef TEST_PROGRAM
445int main(int argc, char **argv)
446{
447	blkid_dev dev;
448	blkid_cache cache;
449
450	blkid_debug_mask = DEBUG_ALL;
451	if (argc != 2) {
452		fprintf(stderr, "Usage: %s device\n"
453			"Probe a single device to determine type\n", argv[0]);
454		exit(1);
455	}
456	cache = blkid_new_cache();
457	dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
458	if (!dev) {
459		printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
460		return (1);
461	}
462	printf("%s is type %s\n", argv[1], dev->bid_type ?
463		dev->bid_type : "(null)");
464	if (dev->bid_label)
465		printf("\tlabel is '%s'\n", dev->bid_label);
466	if (dev->bid_uuid)
467		printf("\tuuid is %s\n", dev->bid_uuid);
468
469	blkid_free_dev(dev);
470	return (0);
471}
472#endif
473