2 * probe.c - identify a block device by its contents, and return a dev
3 * struct with the details
5 * Copyright (C) 1999 by Andries Brouwer
6 * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
7 * Copyright (C) 2001 by Andreas Dilger
10 * This file may be redistributed under the terms of the
11 * GNU Lesser General Public License.
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_STAT_H
24 #ifdef HAVE_SYS_MKDEV_H
25 #include <sys/mkdev.h>
31 #include "uuid/uuid.h"
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
39 static int check_mdraid(int fd, unsigned char *ret_uuid)
41 struct mdp_superblock_s *md;
46 return -BLKID_ERR_PARAM;
48 offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
50 if (blkid_llseek(fd, offset, 0) < 0 ||
51 read(fd, buf, 4096) != 4096)
54 /* Check for magic number */
55 if (memcmp("\251+N\374", buf, 4))
56 return -BLKID_ERR_PARAM;
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);
71 static void set_uuid(blkid_dev dev, uuid_t uuid)
75 if (!uuid_is_null(uuid)) {
76 uuid_unparse(uuid, str);
77 blkid_set_tag(dev, "UUID", str, sizeof(str));
81 static int probe_ext2(int fd __BLKID_ATTR((unused)),
82 blkid_cache cache __BLKID_ATTR((unused)),
84 struct blkid_magic *id, unsigned char *buf)
86 struct ext2_super_block *es;
87 const char *sec_type = 0, *label = 0;
89 es = (struct ext2_super_block *)buf;
91 DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
92 blkid_le32(es->s_feature_compat),
93 blkid_le32(es->s_feature_incompat),
94 blkid_le32(es->s_feature_ro_compat)));
96 /* Distinguish between jbd and ext2/3 fs */
97 if (id && (blkid_le32(es->s_feature_incompat) &
98 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
99 return -BLKID_ERR_PARAM;
101 if (strlen(es->s_volume_name))
102 label = es->s_volume_name;
103 blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
105 set_uuid(dev, es->s_uuid);
107 if (blkid_le32(es->s_feature_compat) &
108 EXT3_FEATURE_COMPAT_HAS_JOURNAL)
111 blkid_set_tag(dev, "SEC_TYPE", sec_type, 0);
116 static int probe_jbd(int fd __BLKID_ATTR((unused)),
117 blkid_cache cache __BLKID_ATTR((unused)),
119 struct blkid_magic *id __BLKID_ATTR((unused)),
122 struct ext2_super_block *es = (struct ext2_super_block *) buf;
124 if (!(blkid_le32(es->s_feature_incompat) &
125 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
126 return -BLKID_ERR_PARAM;
128 return (probe_ext2(fd, cache, dev, 0, buf));
131 static int probe_vfat(int fd __BLKID_ATTR((unused)),
132 blkid_cache cache __BLKID_ATTR((unused)),
134 struct blkid_magic *id __BLKID_ATTR((unused)),
137 struct vfat_super_block *vs;
139 const char *label = 0;
141 vs = (struct vfat_super_block *)buf;
143 if (strncmp(vs->vs_label, "NO NAME", 7)) {
144 char *end = vs->vs_label + sizeof(vs->vs_label) - 1;
146 while (*end == ' ' && end >= vs->vs_label)
148 if (end >= vs->vs_label)
149 label = vs->vs_label;
150 blkid_set_tag(dev, "LABEL", label, end - vs->vs_label + 1);
153 /* We can't just print them as %04X, because they are unaligned */
154 sprintf(serno, "%02X%02X-%02X%02X", vs->vs_serno[3], vs->vs_serno[2],
155 vs->vs_serno[1], vs->vs_serno[0]);
156 blkid_set_tag(dev, "UUID", serno, sizeof(serno));
161 static int probe_msdos(int fd __BLKID_ATTR((unused)),
162 blkid_cache cache __BLKID_ATTR((unused)),
164 struct blkid_magic *id __BLKID_ATTR((unused)),
167 struct msdos_super_block *ms = (struct msdos_super_block *) buf;
169 const char *label = 0;
171 if (strncmp(ms->ms_label, "NO NAME", 7)) {
172 char *end = ms->ms_label + sizeof(ms->ms_label) - 1;
174 while (*end == ' ' && end >= ms->ms_label)
176 if (end >= ms->ms_label)
177 label = ms->ms_label;
178 blkid_set_tag(dev, "LABEL", label, end - ms->ms_label + 1);
181 /* We can't just print them as %04X, because they are unaligned */
182 sprintf(serno, "%02X%02X-%02X%02X", ms->ms_serno[3], ms->ms_serno[2],
183 ms->ms_serno[1], ms->ms_serno[0]);
184 blkid_set_tag(dev, "UUID", serno, 0);
189 static int probe_xfs(int fd __BLKID_ATTR((unused)),
190 blkid_cache cache __BLKID_ATTR((unused)),
192 struct blkid_magic *id __BLKID_ATTR((unused)),
195 struct xfs_super_block *xs;
196 const char *label = 0;
198 xs = (struct xfs_super_block *)buf;
200 if (strlen(xs->xs_fname))
201 label = xs->xs_fname;
202 blkid_set_tag(dev, "LABEL", label, sizeof(xs->xs_fname));
203 set_uuid(dev, xs->xs_uuid);
207 static int probe_reiserfs(int fd __BLKID_ATTR((unused)),
208 blkid_cache cache __BLKID_ATTR((unused)),
210 struct blkid_magic *id, unsigned char *buf)
212 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
213 unsigned int blocksize;
214 const char *label = 0;
216 blocksize = blkid_le16(rs->rs_blocksize);
218 /* If the superblock is inside the journal, we have the wrong one */
219 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
220 return -BLKID_ERR_BIG;
222 /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
223 if (!strcmp(id->bim_magic, "ReIsEr2Fs") ||
224 !strcmp(id->bim_magic, "ReIsEr3Fs")) {
225 if (strlen(rs->rs_label))
226 label = rs->rs_label;
227 blkid_set_tag(dev, "LABEL", label, sizeof(rs->rs_label));
228 set_uuid(dev, rs->rs_uuid);
234 static int probe_jfs(int fd __BLKID_ATTR((unused)),
235 blkid_cache cache __BLKID_ATTR((unused)),
237 struct blkid_magic *id __BLKID_ATTR((unused)),
240 struct jfs_super_block *js;
241 const char *label = 0;
243 js = (struct jfs_super_block *)buf;
245 if (strlen((char *) js->js_label))
246 label = (char *) js->js_label;
247 blkid_set_tag(dev, "LABEL", label, sizeof(js->js_label));
248 set_uuid(dev, js->js_uuid);
252 static int probe_romfs(int fd __BLKID_ATTR((unused)),
253 blkid_cache cache __BLKID_ATTR((unused)),
255 struct blkid_magic *id __BLKID_ATTR((unused)),
258 struct romfs_super_block *ros;
259 const char *label = 0;
261 ros = (struct romfs_super_block *)buf;
263 if (strlen((char *) ros->ros_volume))
264 label = (char *) ros->ros_volume;
265 blkid_set_tag(dev, "LABEL", label, strlen(label));
270 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
271 "NSR03", "TEA01", 0 };
273 static int probe_udf(int fd, blkid_cache cache __BLKID_ATTR((unused)),
274 blkid_dev dev __BLKID_ATTR((unused)),
275 struct blkid_magic *id __BLKID_ATTR((unused)),
276 unsigned char *buf __BLKID_ATTR((unused)))
279 struct iso_volume_descriptor isosb;
282 /* determine the block size by scanning in 2K increments
283 (block sizes larger than 2K will be null padded) */
284 for (bs = 1; bs < 16; bs++) {
285 lseek(fd, bs*2048+32768, SEEK_SET);
286 if (read(fd, (char *)&isosb, sizeof(isosb)) != sizeof(isosb))
292 /* Scan up to another 64 blocks looking for additional VSD's */
293 for (j = 1; j < 64; j++) {
295 lseek(fd, j*bs*2048+32768, SEEK_SET);
296 if (read(fd, (char *)&isosb, sizeof(isosb))
300 /* If we find NSR0x then call it udf:
303 NSR03 for UDF 2.00 */
304 if (!strncmp(isosb.id, "NSR0", 4))
306 for (m = udf_magic; *m; m++)
307 if (!strncmp(*m, isosb.id, 5))
315 static int probe_ocfs(int fd __BLKID_ATTR((unused)),
316 blkid_cache cache __BLKID_ATTR((unused)),
318 struct blkid_magic *id __BLKID_ATTR((unused)),
321 struct ocfs_volume_header ovh;
322 struct ocfs_volume_label ovl;
325 memcpy(&ovh, buf, sizeof(ovh));
326 memcpy(&ovl, buf+512, sizeof(ovl));
328 major = ocfsmajor(ovh);
330 blkid_set_tag(dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
332 blkid_set_tag(dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
334 blkid_set_tag(dev, "LABEL", ovl.label, ocfslabellen(ovl));
335 blkid_set_tag(dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
336 set_uuid(dev, ovl.vol_id);
340 static int probe_ocfs2(int fd __BLKID_ATTR((unused)),
341 blkid_cache cache __BLKID_ATTR((unused)),
343 struct blkid_magic *id __BLKID_ATTR((unused)),
346 struct ocfs2_super_block *osb;
348 osb = (struct ocfs2_super_block *)buf;
350 blkid_set_tag(dev, "LABEL", osb->s_label, sizeof(osb->s_label));
351 set_uuid(dev, osb->s_uuid);
356 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
357 * in the type_array table below + bim_kbalign.
359 * When probing for a lot of magics, we handle everything in 1kB buffers so
360 * that we don't have to worry about reading each combination of block sizes.
362 #define BLKID_BLK_OFFS 64 /* currently reiserfs */
365 * Various filesystem magics that we can check for. Note that kboff and
366 * sboff are in kilobytes and bytes respectively. All magics are in
367 * byte strings so we don't worry about endian issues.
369 static struct blkid_magic type_array[] = {
370 /* type kboff sboff len magic probe */
371 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd },
372 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 },
373 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs },
374 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs },
375 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs },
376 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs },
377 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs },
378 { "ntfs", 0, 3, 8, "NTFS ", 0 },
379 { "vfat", 0, 0x52, 5, "MSWIN", probe_vfat },
380 { "vfat", 0, 0x52, 8, "FAT32 ", probe_vfat },
381 { "msdos", 0, 0x36, 5, "MSDOS", probe_msdos },
382 { "msdos", 0, 0x36, 8, "FAT16 ", probe_msdos },
383 { "msdos", 0, 0x36, 8, "FAT12 ", probe_msdos },
384 { "minix", 1, 0x10, 2, "\177\023", 0 },
385 { "minix", 1, 0x10, 2, "\217\023", 0 },
386 { "minix", 1, 0x10, 2, "\150\044", 0 },
387 { "minix", 1, 0x10, 2, "\170\044", 0 },
388 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 },
389 { "xfs", 0, 0, 4, "XFSB", probe_xfs },
390 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs },
391 { "bfs", 0, 0, 4, "\316\372\173\033", 0 },
392 { "cramfs", 0, 0, 4, "E=\315\034", 0 },
393 { "qnx4", 0, 4, 6, "QNX4FS", 0 },
394 { "udf", 32, 1, 5, "BEA01", probe_udf },
395 { "udf", 32, 1, 5, "BOOT2", probe_udf },
396 { "udf", 32, 1, 5, "CD001", probe_udf },
397 { "udf", 32, 1, 5, "CDW02", probe_udf },
398 { "udf", 32, 1, 5, "NSR02", probe_udf },
399 { "udf", 32, 1, 5, "NSR03", probe_udf },
400 { "udf", 32, 1, 5, "TEA01", probe_udf },
401 { "iso9660", 32, 1, 5, "CD001", 0 },
402 { "iso9660", 32, 9, 5, "CDROM", 0 },
403 { "jfs", 32, 0, 4, "JFS1", probe_jfs },
404 { "hfs", 1, 0, 2, "BD", 0 },
405 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 },
406 { "hpfs", 8, 0, 4, "I\350\225\371", 0 },
407 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 },
408 { "swap", 0, 0xff6, 10, "SWAP-SPACE", 0 },
409 { "swap", 0, 0xff6, 10, "SWAPSPACE2", 0 },
410 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", 0 },
411 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", 0 },
412 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", 0 },
413 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", 0 },
414 { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs },
415 { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 },
416 { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 },
417 { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 },
418 { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 },
419 { NULL, 0, 0, 0, NULL, NULL }
423 * Verify that the data in dev is consistent with what is on the actual
424 * block device (using the devname field only). Normally this will be
425 * called when finding items in the cache, but for long running processes
426 * is also desirable to revalidate an item before use.
428 * If we are unable to revalidate the data, we return the old data and
429 * do not set the BLKID_BID_FL_VERIFIED flag on it.
431 blkid_dev blkid_verify_devname(blkid_cache cache, blkid_dev dev)
433 struct blkid_magic *id;
434 unsigned char *bufs[BLKID_BLK_OFFS + 1], *buf;
444 diff = now - dev->bid_time;
446 if ((now < dev->bid_time) ||
447 (diff < BLKID_PROBE_MIN) ||
448 (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
449 diff < BLKID_PROBE_INTERVAL))
453 printf("need to revalidate %s (time since last check %lu)\n",
454 dev->bid_name, diff));
456 if (((fd = open(dev->bid_name, O_RDONLY)) < 0) ||
457 (fstat(fd, &st) < 0) || !S_ISBLK(st.st_mode)) {
458 if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
462 /* We don't have read permission, just return cache data. */
464 printf("returning unverified data for %s\n",
469 memset(bufs, 0, sizeof(bufs));
472 * Iterate over the type array. If we already know the type,
473 * then try that first. If it doesn't work, then blow away
474 * the type information, and try again.
479 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
482 if (check_mdraid(fd, uuid) == 0) {
488 for (id = type_array; id->bim_type; id++) {
490 strcmp(id->bim_type, dev->bid_type))
493 idx = id->bim_kboff + (id->bim_sboff >> 10);
494 if (idx > BLKID_BLK_OFFS || idx < 0)
498 if (lseek(fd, idx << 10, SEEK_SET) < 0)
501 if (!(buf = (unsigned char *)malloc(1024)))
504 if (read(fd, buf, 1024) != 1024) {
511 if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
515 if ((id->bim_probe == NULL) ||
516 (id->bim_probe(fd, cache, dev, id, buf) == 0)) {
522 if (!id->bim_type && dev->bid_type) {
524 * Zap the device filesystem type and try again
526 blkid_set_tag(dev, "TYPE", 0, 0);
527 blkid_set_tag(dev, "SEC_TYPE", 0, 0);
528 blkid_set_tag(dev, "LABEL", 0, 0);
529 blkid_set_tag(dev, "UUID", 0, 0);
533 if (!dev->bid_type) {
540 dev->bid_devno = st.st_rdev;
541 dev->bid_time = time(0);
542 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
543 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
545 blkid_set_tag(dev, "TYPE", type, 0);
547 DBG(DEBUG_PROBE, printf("%s: devno 0x%04Lx, type %s\n",
548 dev->bid_name, st.st_rdev, type));
556 int blkid_known_fstype(const char *fstype)
558 struct blkid_magic *id;
560 for (id = type_array; id->bim_type; id++) {
561 if (strcmp(fstype, id->bim_type) == 0)
568 int main(int argc, char **argv)
574 blkid_debug_mask = DEBUG_ALL;
576 fprintf(stderr, "Usage: %s device\n"
577 "Probe a single device to determine type\n", argv[0]);
580 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
581 fprintf(stderr, "%s: error creating cache (%d)\n",
585 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
587 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
590 printf("%s is type %s\n", argv[1], dev->bid_type ?
591 dev->bid_type : "(null)");
593 printf("\tlabel is '%s'\n", dev->bid_label);
595 printf("\tuuid is %s\n", dev->bid_uuid);