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_cache cache, blkid_dev dev,
82 struct blkid_magic *id, unsigned char *buf)
84 struct ext2_super_block *es;
85 const char *sec_type = 0, *label = 0;
87 es = (struct ext2_super_block *)buf;
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)));
94 /* Distinguish between jbd and ext2/3 fs */
95 if (id && (blkid_le32(es->s_feature_incompat) &
96 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
97 return -BLKID_ERR_PARAM;
99 if (strlen(es->s_volume_name))
100 label = es->s_volume_name;
101 blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
103 set_uuid(dev, es->s_uuid);
105 if (blkid_le32(es->s_feature_compat) &
106 EXT3_FEATURE_COMPAT_HAS_JOURNAL)
109 blkid_set_tag(dev, "SEC_TYPE", sec_type, 0);
114 static int probe_jbd(int fd, blkid_cache cache, blkid_dev dev,
115 struct blkid_magic *id, unsigned char *buf)
117 struct ext2_super_block *es = (struct ext2_super_block *) buf;
119 if (!(blkid_le32(es->s_feature_incompat) &
120 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
121 return -BLKID_ERR_PARAM;
123 return (probe_ext2(fd, cache, dev, 0, buf));
126 static int probe_vfat(int fd, blkid_cache cache, blkid_dev dev,
127 struct blkid_magic *id, unsigned char *buf)
129 struct vfat_super_block *vs;
131 const char *label = 0;
133 vs = (struct vfat_super_block *)buf;
135 if (strncmp(vs->vs_label, "NO NAME", 7)) {
136 char *end = vs->vs_label + sizeof(vs->vs_label) - 1;
138 while (*end == ' ' && end >= vs->vs_label)
140 if (end >= vs->vs_label)
141 label = vs->vs_label;
142 blkid_set_tag(dev, "LABEL", label, end - vs->vs_label + 1);
145 /* We can't just print them as %04X, because they are unaligned */
146 sprintf(serno, "%02X%02X-%02X%02X", vs->vs_serno[3], vs->vs_serno[2],
147 vs->vs_serno[1], vs->vs_serno[0]);
148 blkid_set_tag(dev, "UUID", serno, sizeof(serno));
153 static int probe_msdos(int fd, blkid_cache cache, blkid_dev dev,
154 struct blkid_magic *id, unsigned char *buf)
156 struct msdos_super_block *ms = (struct msdos_super_block *) buf;
158 const char *label = 0;
160 if (strncmp(ms->ms_label, "NO NAME", 7)) {
161 char *end = ms->ms_label + sizeof(ms->ms_label) - 1;
163 while (*end == ' ' && end >= ms->ms_label)
165 if (end >= ms->ms_label)
166 label = ms->ms_label;
167 blkid_set_tag(dev, "LABEL", label, end - ms->ms_label + 1);
170 /* We can't just print them as %04X, because they are unaligned */
171 sprintf(serno, "%02X%02X-%02X%02X", ms->ms_serno[3], ms->ms_serno[2],
172 ms->ms_serno[1], ms->ms_serno[0]);
173 blkid_set_tag(dev, "UUID", serno, 0);
178 static int probe_xfs(int fd, blkid_cache cache, blkid_dev dev,
179 struct blkid_magic *id, unsigned char *buf)
181 struct xfs_super_block *xs;
182 const char *label = 0;
184 xs = (struct xfs_super_block *)buf;
186 if (strlen(xs->xs_fname))
187 label = xs->xs_fname;
188 blkid_set_tag(dev, "LABEL", label, sizeof(xs->xs_fname));
189 set_uuid(dev, xs->xs_uuid);
193 static int probe_reiserfs(int fd, blkid_cache cache, blkid_dev dev,
194 struct blkid_magic *id, unsigned char *buf)
196 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
197 unsigned int blocksize;
198 const char *label = 0;
200 blocksize = blkid_le16(rs->rs_blocksize);
202 /* If the superblock is inside the journal, we have the wrong one */
203 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
204 return -BLKID_ERR_BIG;
206 /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
207 if (!strcmp(id->bim_magic, "ReIsEr2Fs") ||
208 !strcmp(id->bim_magic, "ReIsEr3Fs")) {
209 if (strlen(rs->rs_label))
210 label = rs->rs_label;
211 blkid_set_tag(dev, "LABEL", label, sizeof(rs->rs_label));
212 set_uuid(dev, rs->rs_uuid);
218 static int probe_jfs(int fd, blkid_cache cache, blkid_dev dev,
219 struct blkid_magic *id, unsigned char *buf)
221 struct jfs_super_block *js;
222 const char *label = 0;
224 js = (struct jfs_super_block *)buf;
226 if (strlen((char *) js->js_label))
227 label = (char *) js->js_label;
228 blkid_set_tag(dev, "LABEL", label, sizeof(js->js_label));
229 set_uuid(dev, js->js_uuid);
233 static int probe_romfs(int fd, blkid_cache cache, blkid_dev dev,
234 struct blkid_magic *id, unsigned char *buf)
236 struct romfs_super_block *ros;
237 const char *label = 0;
239 ros = (struct romfs_super_block *)buf;
241 if (strlen((char *) ros->ros_volume))
242 label = (char *) ros->ros_volume;
243 blkid_set_tag(dev, "LABEL", label, strlen(label));
248 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
249 "NSR03", "TEA01", 0 };
251 static int probe_udf(int fd, blkid_cache cache, blkid_dev dev,
252 struct blkid_magic *id, unsigned char *buf)
255 struct iso_volume_descriptor isosb;
258 /* determine the block size by scanning in 2K increments
259 (block sizes larger than 2K will be null padded) */
260 for (bs = 1; bs < 16; bs++) {
261 lseek(fd, bs*2048+32768, SEEK_SET);
262 if (read(fd, (char *)&isosb, sizeof(isosb)) != sizeof(isosb))
268 /* Scan up to another 64 blocks looking for additional VSD's */
269 for (j = 1; j < 64; j++) {
271 lseek(fd, j*bs*2048+32768, SEEK_SET);
272 if (read(fd, (char *)&isosb, sizeof(isosb))
276 /* If we find NSR0x then call it udf:
279 NSR03 for UDF 2.00 */
280 if (!strncmp(isosb.id, "NSR0", 4))
282 for (m = udf_magic; *m; m++)
283 if (!strncmp(*m, isosb.id, 5))
292 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
293 * in the type_array table below + bim_kbalign.
295 * When probing for a lot of magics, we handle everything in 1kB buffers so
296 * that we don't have to worry about reading each combination of block sizes.
298 #define BLKID_BLK_OFFS 64 /* currently reiserfs */
301 * Various filesystem magics that we can check for. Note that kboff and
302 * sboff are in kilobytes and bytes respectively. All magics are in
303 * byte strings so we don't worry about endian issues.
305 static struct blkid_magic type_array[] = {
306 /* type kboff sboff len magic probe */
307 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd },
308 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 },
309 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs },
310 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs },
311 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs },
312 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs },
313 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs },
314 { "ntfs", 0, 3, 8, "NTFS ", 0 },
315 { "vfat", 0, 0x52, 5, "MSWIN", probe_vfat },
316 { "vfat", 0, 0x52, 8, "FAT32 ", probe_vfat },
317 { "msdos", 0, 0x36, 5, "MSDOS", probe_msdos },
318 { "msdos", 0, 0x36, 8, "FAT16 ", probe_msdos },
319 { "msdos", 0, 0x36, 8, "FAT12 ", probe_msdos },
320 { "minix", 1, 0x10, 2, "\177\023", 0 },
321 { "minix", 1, 0x10, 2, "\217\023", 0 },
322 { "minix", 1, 0x10, 2, "\150\044", 0 },
323 { "minix", 1, 0x10, 2, "\170\044", 0 },
324 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 },
325 { "xfs", 0, 0, 4, "XFSB", probe_xfs },
326 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs },
327 { "bfs", 0, 0, 4, "\316\372\173\033", 0 },
328 { "cramfs", 0, 0, 4, "E=\315\034", 0 },
329 { "qnx4", 0, 4, 6, "QNX4FS", 0 },
330 { "udf", 32, 1, 5, "BEA01", probe_udf },
331 { "udf", 32, 1, 5, "BOOT2", probe_udf },
332 { "udf", 32, 1, 5, "CD001", probe_udf },
333 { "udf", 32, 1, 5, "CDW02", probe_udf },
334 { "udf", 32, 1, 5, "NSR02", probe_udf },
335 { "udf", 32, 1, 5, "NSR03", probe_udf },
336 { "udf", 32, 1, 5, "TEA01", probe_udf },
337 { "iso9660", 32, 1, 5, "CD001", 0 },
338 { "iso9660", 32, 9, 5, "CDROM", 0 },
339 { "jfs", 32, 0, 4, "JFS1", probe_jfs },
340 { "hfs", 1, 0, 2, "BD", 0 },
341 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 },
342 { "hpfs", 8, 0, 4, "I\350\225\371", 0 },
343 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 },
344 { "swap", 0, 0xff6, 10, "SWAP-SPACE", 0 },
345 { "swap", 0, 0xff6, 10, "SWAPSPACE2", 0 },
346 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", 0 },
347 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", 0 },
348 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", 0 },
349 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", 0 },
350 { NULL, 0, 0, 0, NULL, NULL }
354 * Verify that the data in dev is consistent with what is on the actual
355 * block device (using the devname field only). Normally this will be
356 * called when finding items in the cache, but for long running processes
357 * is also desirable to revalidate an item before use.
359 * If we are unable to revalidate the data, we return the old data and
360 * do not set the BLKID_BID_FL_VERIFIED flag on it.
362 blkid_dev blkid_verify_devname(blkid_cache cache, blkid_dev dev)
364 struct blkid_magic *id;
365 unsigned char *bufs[BLKID_BLK_OFFS + 1], *buf;
374 diff = time(0) - dev->bid_time;
376 if (diff < BLKID_PROBE_MIN || (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
377 diff < BLKID_PROBE_INTERVAL))
381 printf("need to revalidate %s (time since last check %lu)\n",
382 dev->bid_name, diff));
384 if (((fd = open(dev->bid_name, O_RDONLY)) < 0) ||
385 (fstat(fd, &st) < 0) || !S_ISBLK(st.st_mode)) {
386 if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
390 /* We don't have read permission, just return cache data. */
392 printf("returning unverified data for %s\n",
397 memset(bufs, 0, sizeof(bufs));
400 * Iterate over the type array. If we already know the type,
401 * then try that first. If it doesn't work, then blow away
402 * the type information, and try again.
407 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
410 if (check_mdraid(fd, uuid) == 0) {
416 for (id = type_array; id->bim_type; id++) {
418 strcmp(id->bim_type, dev->bid_type))
421 idx = id->bim_kboff + (id->bim_sboff >> 10);
422 if (idx > BLKID_BLK_OFFS || idx < 0)
426 if (lseek(fd, idx << 10, SEEK_SET) < 0)
429 if (!(buf = (unsigned char *)malloc(1024)))
432 if (read(fd, buf, 1024) != 1024) {
439 if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
443 if ((id->bim_probe == NULL) ||
444 (id->bim_probe(fd, cache, dev, id, buf) == 0)) {
450 if (!id->bim_type && dev->bid_type) {
452 * Zap the device filesystem type and try again
454 blkid_set_tag(dev, "TYPE", 0, 0);
455 blkid_set_tag(dev, "SEC_TYPE", 0, 0);
456 blkid_set_tag(dev, "LABEL", 0, 0);
457 blkid_set_tag(dev, "UUID", 0, 0);
461 if (!dev->bid_type) {
468 dev->bid_devno = st.st_rdev;
469 dev->bid_time = time(0);
470 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
471 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
473 blkid_set_tag(dev, "TYPE", type, 0);
475 DBG(DEBUG_PROBE, printf("%s: devno 0x%04Lx, type %s\n",
476 dev->bid_name, st.st_rdev, type));
484 int blkid_known_fstype(const char *fstype)
486 struct blkid_magic *id;
488 for (id = type_array; id->bim_type; id++) {
489 if (strcmp(fstype, id->bim_type) == 0)
496 int main(int argc, char **argv)
502 blkid_debug_mask = DEBUG_ALL;
504 fprintf(stderr, "Usage: %s device\n"
505 "Probe a single device to determine type\n", argv[0]);
508 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
509 fprintf(stderr, "%s: error creating cache (%d)\n",
513 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
515 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
518 printf("%s is type %s\n", argv[1], dev->bid_type ?
519 dev->bid_type : "(null)");
521 printf("\tlabel is '%s'\n", dev->bid_label);
523 printf("\tuuid is %s\n", dev->bid_uuid);