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), 1);
81 static int probe_ext2(int fd, blkid_cache cache, blkid_dev dev,
82 struct blkid_magic *id, unsigned char *buf,
83 const char **ret_sectype)
85 struct ext2_super_block *es;
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 /* 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;
102 if (strlen(es->s_volume_name))
103 blkid_set_tag(dev, "LABEL", es->s_volume_name,
104 sizeof(es->s_volume_name), 1);
106 set_uuid(dev, es->s_uuid);
111 static int probe_jbd(int fd, blkid_cache cache, blkid_dev dev,
112 struct blkid_magic *id, unsigned char *buf,
113 const char **ret_sectype)
115 struct ext2_super_block *es = (struct ext2_super_block *) buf;
117 if (!(blkid_le32(es->s_feature_incompat) &
118 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
119 return -BLKID_ERR_PARAM;
121 return (probe_ext2(fd, cache, dev, id, buf, ret_sectype));
124 static int probe_ext3(int fd, blkid_cache cache, blkid_dev dev,
125 struct blkid_magic *id, unsigned char *buf,
126 const char **ret_sectype)
128 struct ext2_super_block *es = (struct ext2_super_block *) buf;
131 if (!(blkid_le32(es->s_feature_compat) &
132 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
133 return -BLKID_ERR_PARAM;
135 if ((ret = probe_ext2(fd, cache, dev, id, buf, ret_sectype)) < 0)
138 if (!(blkid_le32(es->s_feature_incompat) &
139 EXT3_FEATURE_INCOMPAT_RECOVER))
140 *ret_sectype = "ext2";
145 static int probe_vfat(int fd, blkid_cache cache, blkid_dev dev,
146 struct blkid_magic *id, unsigned char *buf,
147 const char **ret_sectype)
149 struct vfat_super_block *vs;
152 vs = (struct vfat_super_block *)buf;
154 if (strncmp(vs->vs_label, "NO NAME", 7)) {
155 char *end = vs->vs_label + sizeof(vs->vs_label) - 1;
157 while (*end == ' ' && end >= vs->vs_label)
159 if (end >= vs->vs_label)
160 blkid_set_tag(dev, "LABEL", vs->vs_label,
161 end - vs->vs_label + 1, 1);
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);
172 static int probe_msdos(int fd, blkid_cache cache, blkid_dev dev,
173 struct blkid_magic *id, unsigned char *buf,
174 const char **ret_sectype)
176 struct msdos_super_block *ms = (struct msdos_super_block *) buf;
179 if (strncmp(ms->ms_label, "NO NAME", 7)) {
180 char *end = ms->ms_label + sizeof(ms->ms_label) - 1;
182 while (*end == ' ' && end >= ms->ms_label)
184 if (end >= ms->ms_label)
185 blkid_set_tag(dev, "LABEL", ms->ms_label,
186 end - ms->ms_label + 1, 1);
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);
197 static int probe_xfs(int fd, blkid_cache cache, blkid_dev dev,
198 struct blkid_magic *id, unsigned char *buf,
199 const char **ret_sectype)
201 struct xfs_super_block *xs;
203 xs = (struct xfs_super_block *)buf;
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);
212 static int probe_reiserfs(int fd, blkid_cache cache, blkid_dev dev,
213 struct blkid_magic *id, unsigned char *buf,
214 const char **ret_sectype)
216 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
217 unsigned int blocksize;
219 blocksize = blkid_le16(rs->rs_blocksize);
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;
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);
233 set_uuid(dev, rs->rs_uuid);
240 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
241 * in the type_array table below + bim_kbalign.
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.
246 #define BLKID_BLK_OFFS 64 /* currently reiserfs */
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.
253 static 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 }
303 * If a device's filesystem no longer checks out, we need to nuke
304 * information about it from the entry.
306 static void blkid_invalidate_fs(blkid_dev dev)
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);
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.
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.
322 blkid_dev blkid_verify_devname(blkid_cache cache, blkid_dev dev)
324 struct blkid_magic *id;
325 unsigned char *bufs[BLKID_BLK_OFFS + 1], *buf;
326 const char *sec_type, *type;
334 diff = time(0) - dev->bid_time;
336 if (diff < BLKID_PROBE_MIN || (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
337 diff < BLKID_PROBE_INTERVAL))
341 printf("need to revalidate %s (time since last check %lu)\n",
342 dev->bid_name, diff));
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) {
350 /* We don't have read permission, just return cache data. */
352 printf("returning unverified data for %s\n",
357 memset(bufs, 0, sizeof(bufs));
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.
368 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
371 if (check_mdraid(fd, uuid) == 0) {
377 for (id = type_array; id->bim_type; id++) {
379 strcmp(id->bim_type, dev->bid_type))
382 idx = id->bim_kboff + (id->bim_sboff >> 10);
383 if (idx > BLKID_BLK_OFFS || idx < 0)
387 if (lseek(fd, idx << 10, SEEK_SET) < 0)
390 if (!(buf = (unsigned char *)malloc(1024)))
393 if (read(fd, buf, 1024) != 1024) {
400 if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
404 if ((id->bim_probe == NULL) ||
405 (id->bim_probe(fd, cache, dev, id, buf, &sec_type) == 0)) {
411 if (!id->bim_type && dev->bid_type) {
413 * Zap the device filesystem type and try again
415 blkid_invalidate_fs(dev);
419 if (!dev->bid_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;
431 blkid_set_tag(dev, "TYPE", type, 0, 1);
433 blkid_set_tag(dev, "TYPE", sec_type, 0, 0);
435 DBG(DEBUG_PROBE, printf("%s: devno 0x%04Lx, type %s\n",
436 dev->bid_name, st.st_rdev, type));
445 int main(int argc, char **argv)
450 blkid_debug_mask = DEBUG_ALL;
452 fprintf(stderr, "Usage: %s device\n"
453 "Probe a single device to determine type\n", argv[0]);
456 cache = blkid_new_cache();
457 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
459 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
462 printf("%s is type %s\n", argv[1], dev->bid_type ?
463 dev->bid_type : "(null)");
465 printf("\tlabel is '%s'\n", dev->bid_label);
467 printf("\tuuid is %s\n", dev->bid_uuid);