probe.c revision e382a7eaded3ec6b38114988ae9b0723bb48002a
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 blkid_loff_t 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 (blkid_llseek(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) && memcmp("\374N+\251", 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 + 4, &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 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, *tmp; 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 tmp = (unsigned char *)&ms->ms_sector_size; 265 sector_size = tmp[0] + (tmp[1] << 8); 266 if (sector_size != 0x200 && sector_size != 0x400 && 267 sector_size != 0x800 && sector_size != 0x1000) 268 return 1; 269 270 tmp = (unsigned char *)&ms->ms_dir_entries; 271 dir_entries = tmp[0] + (tmp[1] << 8); 272 reserved = blkid_le16(ms->ms_reserved); 273 tmp = (unsigned char *)&ms->ms_sectors; 274 sect_count = tmp[0] + (tmp[1] << 8); 275 if (sect_count == 0) 276 sect_count = blkid_le32(ms->ms_total_sect); 277 278 fat_length = blkid_le16(ms->ms_fat_length); 279 if (fat_length == 0) 280 fat_length = blkid_le32(vs->vs_fat32_length); 281 282 fat_size = fat_length * ms->ms_fats; 283 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) + 284 (sector_size-1)) / sector_size; 285 286 cluster_count = sect_count - (reserved + fat_size + dir_size); 287 cluster_count /= ms->ms_cluster_size; 288 289 if (cluster_count > FAT32_MAX) 290 return 1; 291 292 if (ms->ms_fat_length) { 293 /* the label may be an attribute in the root directory */ 294 root_start = (reserved + fat_size) * sector_size; 295 root_dir_entries = vs->vs_dir_entries[0] + 296 (vs->vs_dir_entries[1] << 8); 297 298 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry); 299 dir = (struct vfat_dir_entry *) get_buffer(probe, root_start, 300 buf_size); 301 if (dir) 302 vol_label = search_fat_label(dir, root_dir_entries); 303 304 if (!vol_label || !memcmp(vol_label, no_name, 11)) 305 vol_label = ms->ms_label; 306 vol_serno = ms->ms_serno; 307 308 blkid_set_tag(probe->dev, "SEC_TYPE", "msdos", 309 sizeof("msdos")); 310 } else { 311 /* Search the FAT32 root dir for the label attribute */ 312 buf_size = vs->vs_cluster_size * sector_size; 313 start_data_sect = reserved + fat_size; 314 315 next = blkid_le32(vs->vs_root_cluster); 316 while (next && --maxloop) { 317 __u32 next_sect_off; 318 __u64 next_off, fat_entry_off; 319 int count; 320 321 next_sect_off = (next - 2) * vs->vs_cluster_size; 322 next_off = (start_data_sect + next_sect_off) * 323 sector_size; 324 325 dir = (struct vfat_dir_entry *) 326 get_buffer(probe, next_off, buf_size); 327 if (dir == NULL) 328 break; 329 330 count = buf_size / sizeof(struct vfat_dir_entry); 331 332 vol_label = search_fat_label(dir, count); 333 if (vol_label) 334 break; 335 336 /* get FAT entry */ 337 fat_entry_off = (reserved * sector_size) + 338 (next * sizeof(__u32)); 339 buf = get_buffer(probe, fat_entry_off, buf_size); 340 if (buf == NULL) 341 break; 342 343 /* set next cluster */ 344 next = blkid_le32(*((__u32 *) buf) & 0x0fffffff); 345 } 346 347 if (!vol_label || !memcmp(vol_label, no_name, 11)) 348 vol_label = vs->vs_label; 349 vol_serno = vs->vs_serno; 350 } 351 352 if (vol_label && memcmp(vol_label, no_name, 11)) { 353 if ((label_len = figure_label_len(vol_label, 11))) 354 label = vol_label; 355 } 356 357 /* We can't just print them as %04X, because they are unaligned */ 358 sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2], 359 vol_serno[1], vol_serno[0]); 360 361 blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len); 362 blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1); 363 364 return 0; 365} 366 367/* 368 * The FAT filesystem could be without a magic string in superblock 369 * (e.g. old floppies). This heuristic for FAT detection is inspired 370 * by http://vrfy.org/projects/volume_id/ and Linux kernel. 371 * [7-Jul-2005, Karel Zak <kzak@redhat.com>] 372 */ 373static int probe_fat_nomagic(struct blkid_probe *probe, 374 struct blkid_magic *id __BLKID_ATTR((unused)), 375 unsigned char *buf) 376{ 377 struct vfat_super_block *vs; 378 379 vs = (struct vfat_super_block *)buf; 380 381 /* heads check */ 382 if (vs->vs_heads == 0) 383 return 1; 384 385 /* cluster size check*/ 386 if (vs->vs_cluster_size == 0 || 387 (vs->vs_cluster_size & (vs->vs_cluster_size-1))) 388 return 1; 389 390 /* media check */ 391 if (vs->vs_media < 0xf8 && vs->vs_media != 0xf0) 392 return 1; 393 394 /* fat counts(Linux kernel expects at least 1 FAT table) */ 395 if (!vs->vs_fats) 396 return 1; 397 398 return probe_fat(probe, id, buf); 399} 400 401static int probe_ntfs(struct blkid_probe *probe, 402 struct blkid_magic *id __BLKID_ATTR((unused)), 403 unsigned char *buf) 404{ 405 struct ntfs_super_block *ns; 406 struct master_file_table_record *mft; 407 struct file_attribute *attr; 408 char uuid_str[17], label_str[129], *cp; 409 int bytes_per_sector, sectors_per_cluster; 410 int mft_record_size, attr_off, attr_len; 411 unsigned int i, attr_type, val_len; 412 int val_off; 413 __u64 nr_clusters; 414 blkid_loff_t off; 415 unsigned char *buf_mft, *val; 416 417 ns = (struct ntfs_super_block *) buf; 418 419 bytes_per_sector = ns->bios_parameter_block[0] + 420 (ns->bios_parameter_block[1] << 8); 421 sectors_per_cluster = ns->bios_parameter_block[2]; 422 423 if (ns->cluster_per_mft_record < 0) 424 mft_record_size = 1 << - ns->cluster_per_mft_record; 425 else 426 mft_record_size = ns->cluster_per_mft_record * 427 sectors_per_cluster * bytes_per_sector; 428 nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster; 429 430 if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) || 431 (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters)) 432 return 1; 433 434 off = blkid_le64(ns->mft_mirror_cluster_location) * 435 bytes_per_sector * sectors_per_cluster; 436 437 buf_mft = get_buffer(probe, off, mft_record_size); 438 if (!buf_mft) 439 return 1; 440 441 if (memcmp(buf_mft, "FILE", 4)) 442 return 1; 443 444 off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector * 445 sectors_per_cluster; 446 447 buf_mft = get_buffer(probe, off, mft_record_size); 448 if (!buf_mft) 449 return 1; 450 451 if (memcmp(buf_mft, "FILE", 4)) 452 return 1; 453 454 off += MFT_RECORD_VOLUME * mft_record_size; 455 456 buf_mft = get_buffer(probe, off, mft_record_size); 457 if (!buf_mft) 458 return 1; 459 460 if (memcmp(buf_mft, "FILE", 4)) 461 return 1; 462 463 mft = (struct master_file_table_record *) buf_mft; 464 465 attr_off = blkid_le16(mft->attrs_offset); 466 label_str[0] = 0; 467 468 while (1) { 469 attr = (struct file_attribute *) (buf_mft + attr_off); 470 attr_len = blkid_le16(attr->len); 471 attr_type = blkid_le32(attr->type); 472 val_off = blkid_le16(attr->value_offset); 473 val_len = blkid_le32(attr->value_len); 474 475 attr_off += attr_len; 476 477 if ((attr_off > mft_record_size) || 478 (attr_len == 0)) 479 break; 480 481 if (attr_type == MFT_RECORD_ATTR_END) 482 break; 483 484 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) { 485 if (val_len > sizeof(label_str)) 486 val_len = sizeof(label_str)-1; 487 488 for (i=0, cp=label_str; i < val_len; i+=2,cp++) { 489 val = ((__u8 *) attr) + val_off + i; 490 *cp = val[0]; 491 if (val[1]) 492 *cp = '?'; 493 } 494 *cp = 0; 495 } 496 } 497 498 sprintf(uuid_str, "%llX", blkid_le64(ns->volume_serial)); 499 blkid_set_tag(probe->dev, "UUID", uuid_str, 0); 500 if (label_str[0]) 501 blkid_set_tag(probe->dev, "LABEL", label_str, 0); 502 return 0; 503} 504 505 506static int probe_xfs(struct blkid_probe *probe, 507 struct blkid_magic *id __BLKID_ATTR((unused)), 508 unsigned char *buf) 509{ 510 struct xfs_super_block *xs; 511 const char *label = 0; 512 513 xs = (struct xfs_super_block *)buf; 514 515 if (strlen(xs->xs_fname)) 516 label = xs->xs_fname; 517 blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname)); 518 set_uuid(probe->dev, xs->xs_uuid, 0); 519 return 0; 520} 521 522static int probe_reiserfs(struct blkid_probe *probe, 523 struct blkid_magic *id, unsigned char *buf) 524{ 525 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf; 526 unsigned int blocksize; 527 const char *label = 0; 528 529 blocksize = blkid_le16(rs->rs_blocksize); 530 531 /* If the superblock is inside the journal, we have the wrong one */ 532 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block)) 533 return -BLKID_ERR_BIG; 534 535 /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */ 536 if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') { 537 if (strlen(rs->rs_label)) 538 label = rs->rs_label; 539 set_uuid(probe->dev, rs->rs_uuid, 0); 540 } 541 blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label)); 542 543 return 0; 544} 545 546static int probe_reiserfs4(struct blkid_probe *probe, 547 struct blkid_magic *id __BLKID_ATTR((unused)), 548 unsigned char *buf) 549{ 550 struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf; 551 const unsigned char *label = 0; 552 553 if (strlen((char *) rs4->rs4_label)) 554 label = rs4->rs4_label; 555 set_uuid(probe->dev, rs4->rs4_uuid, 0); 556 blkid_set_tag(probe->dev, "LABEL", (const char *) label, 557 sizeof(rs4->rs4_label)); 558 559 return 0; 560} 561 562static int probe_jfs(struct blkid_probe *probe, 563 struct blkid_magic *id __BLKID_ATTR((unused)), 564 unsigned char *buf) 565{ 566 struct jfs_super_block *js; 567 const char *label = 0; 568 569 js = (struct jfs_super_block *)buf; 570 571 if (strlen((char *) js->js_label)) 572 label = (char *) js->js_label; 573 blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label)); 574 set_uuid(probe->dev, js->js_uuid, 0); 575 return 0; 576} 577 578static int probe_luks(struct blkid_probe *probe, 579 struct blkid_magic *id __BLKID_ATTR((unused)), 580 unsigned char *buf) 581{ 582 unsigned char uuid[40]; 583 /* 168 is the offset to the 40 character uuid: 584 * http://luks.endorphin.org/LUKS-on-disk-format.pdf */ 585 strncpy(uuid, buf+168, 40); 586 blkid_set_tag(probe->dev, "UUID", uuid, sizeof(uuid)); 587 return 0; 588} 589 590static int probe_romfs(struct blkid_probe *probe, 591 struct blkid_magic *id __BLKID_ATTR((unused)), 592 unsigned char *buf) 593{ 594 struct romfs_super_block *ros; 595 const char *label = 0; 596 597 ros = (struct romfs_super_block *)buf; 598 599 if (strlen((char *) ros->ros_volume)) 600 label = (char *) ros->ros_volume; 601 blkid_set_tag(probe->dev, "LABEL", label, 0); 602 return 0; 603} 604 605static int probe_cramfs(struct blkid_probe *probe, 606 struct blkid_magic *id __BLKID_ATTR((unused)), 607 unsigned char *buf) 608{ 609 struct cramfs_super_block *csb; 610 const char *label = 0; 611 612 csb = (struct cramfs_super_block *)buf; 613 614 if (strlen((char *) csb->name)) 615 label = (char *) csb->name; 616 blkid_set_tag(probe->dev, "LABEL", label, 0); 617 return 0; 618} 619 620static int probe_swap0(struct blkid_probe *probe, 621 struct blkid_magic *id __BLKID_ATTR((unused)), 622 unsigned char *buf __BLKID_ATTR((unused))) 623{ 624 blkid_set_tag(probe->dev, "UUID", 0, 0); 625 blkid_set_tag(probe->dev, "LABEL", 0, 0); 626 return 0; 627} 628 629static int probe_swap1(struct blkid_probe *probe, 630 struct blkid_magic *id __BLKID_ATTR((unused)), 631 unsigned char *buf __BLKID_ATTR((unused))) 632{ 633 struct swap_id_block *sws; 634 635 probe_swap0(probe, id, buf); 636 /* 637 * Version 1 swap headers are always located at offset of 1024 638 * bytes, although the swap signature itself is located at the 639 * end of the page (which may vary depending on hardware 640 * pagesize). 641 */ 642 sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024); 643 if (!sws) 644 return 1; 645 646 /* arbitrary sanity check.. is there any garbage down there? */ 647 if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) { 648 if (sws->sws_volume[0]) 649 blkid_set_tag(probe->dev, "LABEL", sws->sws_volume, 650 sizeof(sws->sws_volume)); 651 if (sws->sws_uuid[0]) 652 set_uuid(probe->dev, sws->sws_uuid, 0); 653 } 654 return 0; 655} 656 657static int probe_iso9660(struct blkid_probe *probe, 658 struct blkid_magic *id __BLKID_ATTR((unused)), 659 unsigned char *buf) 660{ 661 struct iso_volume_descriptor *iso; 662 const unsigned char *label; 663 664 iso = (struct iso_volume_descriptor *) buf; 665 label = iso->volume_id; 666 667 blkid_set_tag(probe->dev, "LABEL", (const char *) label, 668 figure_label_len(label, 32)); 669 return 0; 670} 671 672 673static const char 674*udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02", 675 "NSR03", "TEA01", 0 }; 676 677static int probe_udf(struct blkid_probe *probe, 678 struct blkid_magic *id __BLKID_ATTR((unused)), 679 unsigned char *buf __BLKID_ATTR((unused))) 680{ 681 int j, bs; 682 struct iso_volume_descriptor *isosb; 683 const char ** m; 684 685 /* determine the block size by scanning in 2K increments 686 (block sizes larger than 2K will be null padded) */ 687 for (bs = 1; bs < 16; bs++) { 688 isosb = (struct iso_volume_descriptor *) 689 get_buffer(probe, bs*2048+32768, sizeof(isosb)); 690 if (!isosb) 691 return 1; 692 if (isosb->vd_id[0]) 693 break; 694 } 695 696 /* Scan up to another 64 blocks looking for additional VSD's */ 697 for (j = 1; j < 64; j++) { 698 if (j > 1) { 699 isosb = (struct iso_volume_descriptor *) 700 get_buffer(probe, j*bs*2048+32768, 701 sizeof(isosb)); 702 if (!isosb) 703 return 1; 704 } 705 /* If we find NSR0x then call it udf: 706 NSR01 for UDF 1.00 707 NSR02 for UDF 1.50 708 NSR03 for UDF 2.00 */ 709 if (!memcmp(isosb->vd_id, "NSR0", 4)) 710 return 0; 711 for (m = udf_magic; *m; m++) 712 if (!memcmp(*m, isosb->vd_id, 5)) 713 break; 714 if (*m == 0) 715 return 1; 716 } 717 return 1; 718} 719 720static int probe_ocfs(struct blkid_probe *probe, 721 struct blkid_magic *id __BLKID_ATTR((unused)), 722 unsigned char *buf) 723{ 724 struct ocfs_volume_header ovh; 725 struct ocfs_volume_label ovl; 726 __u32 major; 727 728 memcpy(&ovh, buf, sizeof(ovh)); 729 memcpy(&ovl, buf+512, sizeof(ovl)); 730 731 major = ocfsmajor(ovh); 732 if (major == 1) 733 blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1")); 734 else if (major >= 9) 735 blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs")); 736 737 blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl)); 738 blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh)); 739 set_uuid(probe->dev, ovl.vol_id, 0); 740 return 0; 741} 742 743static int probe_ocfs2(struct blkid_probe *probe, 744 struct blkid_magic *id __BLKID_ATTR((unused)), 745 unsigned char *buf) 746{ 747 struct ocfs2_super_block *osb; 748 749 osb = (struct ocfs2_super_block *)buf; 750 751 blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label)); 752 set_uuid(probe->dev, osb->s_uuid, 0); 753 return 0; 754} 755 756static int probe_oracleasm(struct blkid_probe *probe, 757 struct blkid_magic *id __BLKID_ATTR((unused)), 758 unsigned char *buf) 759{ 760 struct oracle_asm_disk_label *dl; 761 762 dl = (struct oracle_asm_disk_label *)buf; 763 764 blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id)); 765 return 0; 766} 767 768static int probe_gfs(struct blkid_probe *probe, 769 struct blkid_magic *id __BLKID_ATTR((unused)), 770 unsigned char *buf) 771{ 772 struct gfs2_sb *sbd; 773 const char *label = 0; 774 775 sbd = (struct gfs2_sb *)buf; 776 777 if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS && 778 blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI) 779 { 780 blkid_set_tag(probe->dev, "UUID", 0, 0); 781 782 if (strlen(sbd->sb_locktable)) 783 label = sbd->sb_locktable; 784 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable)); 785 return 0; 786 } 787 return 1; 788} 789 790static int probe_gfs2(struct blkid_probe *probe, 791 struct blkid_magic *id __BLKID_ATTR((unused)), 792 unsigned char *buf) 793{ 794 struct gfs2_sb *sbd; 795 const char *label = 0; 796 797 sbd = (struct gfs2_sb *)buf; 798 799 if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS && 800 blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI) 801 { 802 blkid_set_tag(probe->dev, "UUID", 0, 0); 803 804 if (strlen(sbd->sb_locktable)) 805 label = sbd->sb_locktable; 806 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable)); 807 return 0; 808 } 809 return 1; 810} 811 812/* 813 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined 814 * in the type_array table below + bim_kbalign. 815 * 816 * When probing for a lot of magics, we handle everything in 1kB buffers so 817 * that we don't have to worry about reading each combination of block sizes. 818 */ 819#define BLKID_BLK_OFFS 64 /* currently reiserfs */ 820 821/* 822 * Various filesystem magics that we can check for. Note that kboff and 823 * sboff are in kilobytes and bytes respectively. All magics are in 824 * byte strings so we don't worry about endian issues. 825 */ 826static struct blkid_magic type_array[] = { 827/* type kboff sboff len magic probe */ 828 { "oracleasm", 0, 32, 8, "ORCLDISK", probe_oracleasm }, 829 { "ntfs", 0, 3, 8, "NTFS ", probe_ntfs }, 830 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd }, 831 { "ext3", 1, 0x38, 2, "\123\357", probe_ext3 }, 832 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 }, 833 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs }, 834 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs }, 835 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs }, 836 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs }, 837 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs }, 838 { "reiser4", 64, 0, 7, "ReIsEr4", probe_reiserfs4 }, 839 { "gfs2", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs2 }, 840 { "gfs", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs }, 841 { "vfat", 0, 0x52, 5, "MSWIN", probe_fat }, 842 { "vfat", 0, 0x52, 8, "FAT32 ", probe_fat }, 843 { "vfat", 0, 0x36, 5, "MSDOS", probe_fat }, 844 { "vfat", 0, 0x36, 8, "FAT16 ", probe_fat }, 845 { "vfat", 0, 0x36, 8, "FAT12 ", probe_fat }, 846 { "vfat", 0, 0, 2, "\353\220", probe_fat_nomagic }, 847 { "vfat", 0, 0, 1, "\351", probe_fat_nomagic }, 848 { "minix", 1, 0x10, 2, "\177\023", 0 }, 849 { "minix", 1, 0x10, 2, "\217\023", 0 }, 850 { "minix", 1, 0x10, 2, "\150\044", 0 }, 851 { "minix", 1, 0x10, 2, "\170\044", 0 }, 852 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 }, 853 { "xfs", 0, 0, 4, "XFSB", probe_xfs }, 854 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs }, 855 { "bfs", 0, 0, 4, "\316\372\173\033", 0 }, 856 { "cramfs", 0, 0, 4, "E=\315\050", probe_cramfs }, 857 { "qnx4", 0, 4, 6, "QNX4FS", 0 }, 858 { "udf", 32, 1, 5, "BEA01", probe_udf }, 859 { "udf", 32, 1, 5, "BOOT2", probe_udf }, 860 { "udf", 32, 1, 5, "CD001", probe_udf }, 861 { "udf", 32, 1, 5, "CDW02", probe_udf }, 862 { "udf", 32, 1, 5, "NSR02", probe_udf }, 863 { "udf", 32, 1, 5, "NSR03", probe_udf }, 864 { "udf", 32, 1, 5, "TEA01", probe_udf }, 865 { "iso9660", 32, 1, 5, "CD001", probe_iso9660 }, 866 { "iso9660", 32, 9, 5, "CDROM", probe_iso9660 }, 867 { "jfs", 32, 0, 4, "JFS1", probe_jfs }, 868 { "hfs", 1, 0, 2, "BD", 0 }, 869 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 }, 870 { "hpfs", 8, 0, 4, "I\350\225\371", 0 }, 871 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 }, 872 { "swap", 0, 0xff6, 10, "SWAP-SPACE", probe_swap0 }, 873 { "swap", 0, 0xff6, 10, "SWAPSPACE2", probe_swap1 }, 874 { "swsuspend", 0, 0xff6, 9, "S1SUSPEND", probe_swap1 }, 875 { "swsuspend", 0, 0xff6, 9, "S2SUSPEND", probe_swap1 }, 876 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", probe_swap0 }, 877 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", probe_swap1 }, 878 { "swsuspend", 0, 0x1ff6, 9, "S1SUSPEND", probe_swap1 }, 879 { "swsuspend", 0, 0x1ff6, 9, "S2SUSPEND", probe_swap1 }, 880 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", probe_swap0 }, 881 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", probe_swap1 }, 882 { "swsuspend", 0, 0x3ff6, 9, "S1SUSPEND", probe_swap1 }, 883 { "swsuspend", 0, 0x3ff6, 9, "S2SUSPEND", probe_swap1 }, 884 { "swap", 0, 0x7ff6, 10, "SWAP-SPACE", probe_swap0 }, 885 { "swap", 0, 0x7ff6, 10, "SWAPSPACE2", probe_swap1 }, 886 { "swsuspend", 0, 0x7ff6, 9, "S1SUSPEND", probe_swap1 }, 887 { "swsuspend", 0, 0x7ff6, 9, "S2SUSPEND", probe_swap1 }, 888 { "swap", 0, 0xfff6, 10, "SWAP-SPACE", probe_swap0 }, 889 { "swap", 0, 0xfff6, 10, "SWAPSPACE2", probe_swap1 }, 890 { "swsuspend", 0, 0xfff6, 9, "S1SUSPEND", probe_swap1 }, 891 { "swsuspend", 0, 0xfff6, 9, "S2SUSPEND", probe_swap1 }, 892 { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs }, 893 { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 }, 894 { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 }, 895 { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 }, 896 { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 }, 897 { "crypt_LUKS", 0, 0, 6, "LUKS\xba\xbe", probe_luks }, 898 { NULL, 0, 0, 0, NULL, NULL } 899}; 900 901/* 902 * Verify that the data in dev is consistent with what is on the actual 903 * block device (using the devname field only). Normally this will be 904 * called when finding items in the cache, but for long running processes 905 * is also desirable to revalidate an item before use. 906 * 907 * If we are unable to revalidate the data, we return the old data and 908 * do not set the BLKID_BID_FL_VERIFIED flag on it. 909 */ 910blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev) 911{ 912 struct blkid_magic *id; 913 struct blkid_probe probe; 914 blkid_tag_iterate iter; 915 unsigned char *buf; 916 const char *type, *value; 917 struct stat st; 918 time_t diff, now; 919 int idx; 920 921 if (!dev) 922 return NULL; 923 924 now = time(0); 925 diff = now - dev->bid_time; 926 927 if ((now > dev->bid_time) && (diff > 0) && 928 ((diff < BLKID_PROBE_MIN) || 929 (dev->bid_flags & BLKID_BID_FL_VERIFIED && 930 diff < BLKID_PROBE_INTERVAL))) 931 return dev; 932 933 DBG(DEBUG_PROBE, 934 printf("need to revalidate %s (time since last check %llu)\n", 935 dev->bid_name, (unsigned long long)diff)); 936 937 if (((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) || 938 (fstat(probe.fd, &st) < 0)) { 939 if (probe.fd >= 0) close(probe.fd); 940 if (errno == ENXIO || errno == ENODEV || errno == ENOENT) { 941 blkid_free_dev(dev); 942 return NULL; 943 } 944 /* We don't have read permission, just return cache data. */ 945 DBG(DEBUG_PROBE, 946 printf("returning unverified data for %s\n", 947 dev->bid_name)); 948 return dev; 949 } 950 951 probe.cache = cache; 952 probe.dev = dev; 953 probe.sbbuf = 0; 954 probe.buf = 0; 955 probe.buf_max = 0; 956 957 /* 958 * Iterate over the type array. If we already know the type, 959 * then try that first. If it doesn't work, then blow away 960 * the type information, and try again. 961 * 962 */ 963try_again: 964 type = 0; 965 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) { 966 uuid_t uuid; 967 968 if (check_mdraid(probe.fd, uuid) == 0) { 969 set_uuid(dev, uuid, 0); 970 type = "mdraid"; 971 goto found_type; 972 } 973 } 974 for (id = type_array; id->bim_type; id++) { 975 if (dev->bid_type && 976 strcmp(id->bim_type, dev->bid_type)) 977 continue; 978 979 idx = id->bim_kboff + (id->bim_sboff >> 10); 980 buf = get_buffer(&probe, idx << 10, 1024); 981 if (!buf) 982 continue; 983 984 if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff), 985 id->bim_len)) 986 continue; 987 988 if ((id->bim_probe == NULL) || 989 (id->bim_probe(&probe, id, buf) == 0)) { 990 type = id->bim_type; 991 goto found_type; 992 } 993 } 994 995 if (!id->bim_type && dev->bid_type) { 996 /* 997 * Zap the device filesystem information and try again 998 */ 999 iter = blkid_tag_iterate_begin(dev); 1000 while (blkid_tag_next(iter, &type, &value) == 0) 1001 blkid_set_tag(dev, type, 0, 0); 1002 blkid_tag_iterate_end(iter); 1003 goto try_again; 1004 } 1005 1006 if (!dev->bid_type) { 1007 blkid_free_dev(dev); 1008 dev = 0; 1009 goto found_type; 1010 } 1011 1012found_type: 1013 if (dev && type) { 1014 dev->bid_devno = st.st_rdev; 1015 dev->bid_time = time(0); 1016 dev->bid_flags |= BLKID_BID_FL_VERIFIED; 1017 cache->bic_flags |= BLKID_BIC_FL_CHANGED; 1018 1019 blkid_set_tag(dev, "TYPE", type, 0); 1020 1021 DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n", 1022 dev->bid_name, (long long)st.st_rdev, type)); 1023 } 1024 1025 if (probe.sbbuf) 1026 free(probe.sbbuf); 1027 if (probe.buf) 1028 free(probe.buf); 1029 if (probe.fd >= 0) 1030 close(probe.fd); 1031 1032 return dev; 1033} 1034 1035int blkid_known_fstype(const char *fstype) 1036{ 1037 struct blkid_magic *id; 1038 1039 for (id = type_array; id->bim_type; id++) { 1040 if (strcmp(fstype, id->bim_type) == 0) 1041 return 1; 1042 } 1043 return 0; 1044} 1045 1046#ifdef TEST_PROGRAM 1047int main(int argc, char **argv) 1048{ 1049 blkid_dev dev; 1050 blkid_cache cache; 1051 int ret; 1052 1053 if (argc != 2) { 1054 fprintf(stderr, "Usage: %s device\n" 1055 "Probe a single device to determine type\n", argv[0]); 1056 exit(1); 1057 } 1058 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) { 1059 fprintf(stderr, "%s: error creating cache (%d)\n", 1060 argv[0], ret); 1061 exit(1); 1062 } 1063 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL); 1064 if (!dev) { 1065 printf("%s: %s has an unsupported type\n", argv[0], argv[1]); 1066 return (1); 1067 } 1068 printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)"); 1069 if (dev->bid_label) 1070 printf("LABEL='%s'\n", dev->bid_label); 1071 if (dev->bid_uuid) 1072 printf("UUID='%s'\n", dev->bid_uuid); 1073 1074 blkid_free_dev(dev); 1075 return (0); 1076} 1077#endif 1078