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 (at) 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 <ctype.h> 22 #include <sys/types.h> 23 #ifdef HAVE_SYS_STAT_H 24 #include <sys/stat.h> 25 #endif 26 #ifdef HAVE_SYS_MKDEV_H 27 #include <sys/mkdev.h> 28 #endif 29 #ifdef __linux__ 30 #include <sys/utsname.h> 31 #endif 32 #ifdef HAVE_ERRNO_H 33 #include <errno.h> 34 #endif 35 #include "blkidP.h" 36 #include "uuid/uuid.h" 37 #include "probe.h" 38 39 static int figure_label_len(const unsigned char *label, int len) 40 { 41 const unsigned char *end = label + len - 1; 42 43 while ((*end == ' ' || *end == 0) && end >= label) 44 --end; 45 if (end >= label) { 46 label = label; 47 return end - label + 1; 48 } 49 return 0; 50 } 51 52 static unsigned char *get_buffer(struct blkid_probe *pr, 53 blkid_loff_t off, size_t len) 54 { 55 ssize_t ret_read; 56 unsigned char *newbuf; 57 58 if (off + len <= SB_BUFFER_SIZE) { 59 if (!pr->sbbuf) { 60 pr->sbbuf = malloc(SB_BUFFER_SIZE); 61 if (!pr->sbbuf) 62 return NULL; 63 if (lseek(pr->fd, 0, SEEK_SET) < 0) 64 return NULL; 65 ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE); 66 if (ret_read < 0) 67 ret_read = 0; 68 pr->sb_valid = ret_read; 69 } 70 if (off+len > pr->sb_valid) 71 return NULL; 72 return pr->sbbuf + off; 73 } else { 74 if (len > pr->buf_max) { 75 newbuf = realloc(pr->buf, len); 76 if (newbuf == NULL) 77 return NULL; 78 pr->buf = newbuf; 79 pr->buf_max = len; 80 } 81 if (blkid_llseek(pr->fd, off, SEEK_SET) < 0) 82 return NULL; 83 ret_read = read(pr->fd, pr->buf, len); 84 if (ret_read != (ssize_t) len) 85 return NULL; 86 return pr->buf; 87 } 88 } 89 90 91 /* 92 * This is a special case code to check for an MDRAID device. We do 93 * this special since it requires checking for a superblock at the end 94 * of the device. 95 */ 96 static int check_mdraid(int fd, unsigned char *ret_uuid) 97 { 98 struct mdp_superblock_s *md; 99 blkid_loff_t offset; 100 char buf[4096]; 101 102 if (fd < 0) 103 return -BLKID_ERR_PARAM; 104 105 offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536; 106 107 if (blkid_llseek(fd, offset, 0) < 0 || 108 read(fd, buf, 4096) != 4096) 109 return -BLKID_ERR_IO; 110 111 /* Check for magic number */ 112 if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4)) 113 return -BLKID_ERR_PARAM; 114 115 if (!ret_uuid) 116 return 0; 117 *ret_uuid = 0; 118 119 /* The MD UUID is not contiguous in the superblock, make it so */ 120 md = (struct mdp_superblock_s *)buf; 121 if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) { 122 memcpy(ret_uuid, &md->set_uuid0, 4); 123 memcpy(ret_uuid + 4, &md->set_uuid1, 12); 124 } 125 return 0; 126 } 127 128 static void set_uuid(blkid_dev dev, uuid_t uuid, const char *tag) 129 { 130 char str[37]; 131 132 if (!uuid_is_null(uuid)) { 133 uuid_unparse(uuid, str); 134 blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str)); 135 } 136 } 137 138 static void get_ext2_info(blkid_dev dev, struct blkid_magic *id, 139 unsigned char *buf) 140 { 141 struct ext2_super_block *es = (struct ext2_super_block *) buf; 142 const char *label = 0; 143 144 DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n", 145 blkid_le32(es->s_feature_compat), 146 blkid_le32(es->s_feature_incompat), 147 blkid_le32(es->s_feature_ro_compat))); 148 149 if (strlen(es->s_volume_name)) 150 label = es->s_volume_name; 151 blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name)); 152 153 set_uuid(dev, es->s_uuid, 0); 154 155 if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && 156 !uuid_is_null(es->s_journal_uuid)) 157 set_uuid(dev, es->s_journal_uuid, "EXT_JOURNAL"); 158 159 if (strcmp(id->bim_type, "ext2") && 160 ((blkid_le32(es->s_feature_incompat) & 161 EXT2_FEATURE_INCOMPAT_UNSUPPORTED) == 0)) 162 blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2")); 163 } 164 165 /* 166 * Check to see if a filesystem is in /proc/filesystems. 167 * Returns 1 if found, 0 if not 168 */ 169 static int fs_proc_check(const char *fs_name) 170 { 171 FILE *f; 172 char buf[80], *cp, *t; 173 174 f = fopen("/proc/filesystems", "r"); 175 if (!f) 176 return (0); 177 while (!feof(f)) { 178 if (!fgets(buf, sizeof(buf), f)) 179 break; 180 cp = buf; 181 if (!isspace(*cp)) { 182 while (*cp && !isspace(*cp)) 183 cp++; 184 } 185 while (*cp && isspace(*cp)) 186 cp++; 187 if ((t = strchr(cp, '\n')) != NULL) 188 *t = 0; 189 if ((t = strchr(cp, '\t')) != NULL) 190 *t = 0; 191 if ((t = strchr(cp, ' ')) != NULL) 192 *t = 0; 193 if (!strcmp(fs_name, cp)) { 194 fclose(f); 195 return (1); 196 } 197 } 198 fclose(f); 199 return (0); 200 } 201 202 /* 203 * Check to see if a filesystem is available as a module 204 * Returns 1 if found, 0 if not 205 */ 206 static int check_for_modules(const char *fs_name) 207 { 208 #ifdef __linux__ 209 struct utsname uts; 210 FILE *f; 211 char buf[1024], *cp; 212 int namesz; 213 214 if (uname(&uts)) 215 return (0); 216 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release); 217 218 f = fopen(buf, "r"); 219 if (!f) 220 return (0); 221 222 namesz = strlen(fs_name); 223 224 while (!feof(f)) { 225 if (!fgets(buf, sizeof(buf), f)) 226 break; 227 if ((cp = strchr(buf, ':')) != NULL) 228 *cp = 0; 229 else 230 continue; 231 if ((cp = strrchr(buf, '/')) != NULL) 232 cp++; 233 else 234 cp = buf; 235 if (!strncmp(cp, fs_name, namesz) && 236 (!strcmp(cp + namesz, ".ko") || 237 !strcmp(cp + namesz, ".ko.gz"))) { 238 fclose(f); 239 return (1); 240 } 241 } 242 fclose(f); 243 #endif 244 return (0); 245 } 246 247 static int linux_version_code() 248 { 249 #ifdef __linux__ 250 struct utsname ut; 251 static version_code = -1; 252 int major, minor, rev; 253 char *endptr; 254 const char *cp; 255 256 if (version_code > 0) 257 return version_code; 258 259 if (uname(&ut)) 260 return 0; 261 cp = ut.release; 262 263 major = strtol(cp, &endptr, 10); 264 if (cp == endptr || *endptr != '.') 265 return 0; 266 cp = endptr + 1; 267 minor = strtol(cp, &endptr, 10); 268 if (cp == endptr || *endptr != '.') 269 return 0; 270 cp = endptr + 1; 271 rev = strtol(cp, &endptr, 10); 272 if (cp == endptr) 273 return 0; 274 version_code = (((major * 256) + minor) * 256) + rev; 275 return version_code; 276 #else 277 return 0; 278 #endif 279 } 280 281 #define EXT4_SUPPORTS_EXT2 (2 * 65536 + 6*256 + 29) 282 283 static int system_supports_ext2(void) 284 { 285 static time_t last_check = 0; 286 static int ret = -1; 287 time_t now = time(0); 288 289 if (ret != -1 || (now - last_check) < 5) 290 return ret; 291 last_check = now; 292 ret = (fs_proc_check("ext2") || check_for_modules("ext2")); 293 return ret; 294 } 295 296 static int system_supports_ext4(void) 297 { 298 static time_t last_check = 0; 299 static int ret = -1; 300 time_t now = time(0); 301 302 if (ret != -1 || (now - last_check) < 5) 303 return ret; 304 last_check = now; 305 ret = (fs_proc_check("ext4") || check_for_modules("ext4")); 306 return ret; 307 } 308 309 static int system_supports_ext4dev(void) 310 { 311 static time_t last_check = 0; 312 static int ret = -1; 313 time_t now = time(0); 314 315 if (ret != -1 || (now - last_check) < 5) 316 return ret; 317 last_check = now; 318 ret = (fs_proc_check("ext4dev") || check_for_modules("ext4dev")); 319 return ret; 320 } 321 322 static int probe_ext4dev(struct blkid_probe *probe, 323 struct blkid_magic *id, 324 unsigned char *buf) 325 { 326 struct ext2_super_block *es; 327 es = (struct ext2_super_block *)buf; 328 329 /* Distinguish from jbd */ 330 if (blkid_le32(es->s_feature_incompat) & 331 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) 332 return -BLKID_ERR_PARAM; 333 334 /* 335 * If the filesystem does not have a journal and ext2 and ext4 336 * is not present, then force this to be detected as an 337 * ext4dev filesystem. 338 */ 339 if (!(blkid_le32(es->s_feature_compat) & 340 EXT3_FEATURE_COMPAT_HAS_JOURNAL) && 341 !system_supports_ext2() && !system_supports_ext4() && 342 system_supports_ext4dev() && 343 linux_version_code() >= EXT4_SUPPORTS_EXT2) 344 goto force_ext4dev; 345 346 /* 347 * If the filesystem is marked as OK for use by in-development 348 * filesystem code, but ext4dev is not supported, and ext4 is, 349 * then don't call ourselves ext4dev, since we should be 350 * detected as ext4 in that case. 351 * 352 * If the filesystem is marked as in use by production 353 * filesystem, then it can only be used by ext4 and NOT by 354 * ext4dev, so always disclaim we are ext4dev in that case. 355 */ 356 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) { 357 if (!system_supports_ext4dev() && system_supports_ext4()) 358 return -BLKID_ERR_PARAM; 359 } else 360 return -BLKID_ERR_PARAM; 361 362 force_ext4dev: 363 get_ext2_info(probe->dev, id, buf); 364 return 0; 365 } 366 367 static int probe_ext4(struct blkid_probe *probe, struct blkid_magic *id, 368 unsigned char *buf) 369 { 370 struct ext2_super_block *es; 371 es = (struct ext2_super_block *)buf; 372 373 /* Distinguish from jbd */ 374 if (blkid_le32(es->s_feature_incompat) & 375 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) 376 return -BLKID_ERR_PARAM; 377 378 /* 379 * If the filesystem does not have a journal and ext2 is not 380 * present, then force this to be detected as an ext2 381 * filesystem. 382 */ 383 if (!(blkid_le32(es->s_feature_compat) & 384 EXT3_FEATURE_COMPAT_HAS_JOURNAL) && 385 !system_supports_ext2() && system_supports_ext4() && 386 linux_version_code() >= EXT4_SUPPORTS_EXT2) 387 goto force_ext4; 388 389 /* Ext4 has at least one feature which ext3 doesn't understand */ 390 if (!(blkid_le32(es->s_feature_ro_compat) & 391 EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) && 392 !(blkid_le32(es->s_feature_incompat) & 393 EXT3_FEATURE_INCOMPAT_UNSUPPORTED)) 394 return -BLKID_ERR_PARAM; 395 396 force_ext4: 397 /* 398 * If the filesystem is a OK for use by in-development 399 * filesystem code, and ext4dev is supported or ext4 is not 400 * supported, then don't call ourselves ext4, so we can redo 401 * the detection and mark the filesystem as ext4dev. 402 * 403 * If the filesystem is marked as in use by production 404 * filesystem, then it can only be used by ext4 and NOT by 405 * ext4dev. 406 */ 407 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) { 408 if (system_supports_ext4dev() || !system_supports_ext4()) 409 return -BLKID_ERR_PARAM; 410 } 411 get_ext2_info(probe->dev, id, buf); 412 return 0; 413 } 414 415 static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id, 416 unsigned char *buf) 417 { 418 struct ext2_super_block *es; 419 es = (struct ext2_super_block *)buf; 420 421 /* ext3 requires journal */ 422 if (!(blkid_le32(es->s_feature_compat) & 423 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) 424 return -BLKID_ERR_PARAM; 425 426 /* Any features which ext3 doesn't understand */ 427 if ((blkid_le32(es->s_feature_ro_compat) & 428 EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) || 429 (blkid_le32(es->s_feature_incompat) & 430 EXT3_FEATURE_INCOMPAT_UNSUPPORTED)) 431 return -BLKID_ERR_PARAM; 432 433 get_ext2_info(probe->dev, id, buf); 434 return 0; 435 } 436 437 static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id, 438 unsigned char *buf) 439 { 440 struct ext2_super_block *es; 441 442 es = (struct ext2_super_block *)buf; 443 444 /* Distinguish between ext3 and ext2 */ 445 if ((blkid_le32(es->s_feature_compat) & 446 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) 447 return -BLKID_ERR_PARAM; 448 449 /* Any features which ext2 doesn't understand */ 450 if ((blkid_le32(es->s_feature_ro_compat) & 451 EXT2_FEATURE_RO_COMPAT_UNSUPPORTED) || 452 (blkid_le32(es->s_feature_incompat) & 453 EXT2_FEATURE_INCOMPAT_UNSUPPORTED)) 454 return -BLKID_ERR_PARAM; 455 456 /* 457 * If ext2 is not present, but ext4 or ext4dev are, then 458 * disclaim we are ext2 459 */ 460 if (!system_supports_ext2() && 461 (system_supports_ext4() || system_supports_ext4dev()) && 462 linux_version_code() >= EXT4_SUPPORTS_EXT2) 463 return -BLKID_ERR_PARAM; 464 465 get_ext2_info(probe->dev, id, buf); 466 return 0; 467 } 468 469 static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id, 470 unsigned char *buf) 471 { 472 struct ext2_super_block *es = (struct ext2_super_block *) buf; 473 474 if (!(blkid_le32(es->s_feature_incompat) & 475 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) 476 return -BLKID_ERR_PARAM; 477 478 get_ext2_info(probe->dev, id, buf); 479 480 return 0; 481 } 482 483 #define FAT_ATTR_VOLUME_ID 0x08 484 #define FAT_ATTR_DIR 0x10 485 #define FAT_ATTR_LONG_NAME 0x0f 486 #define FAT_ATTR_MASK 0x3f 487 #define FAT_ENTRY_FREE 0xe5 488 489 static const char *no_name = "NO NAME "; 490 491 static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count) 492 { 493 int i; 494 495 for (i = 0; i < count; i++) { 496 if (dir[i].name[0] == 0x00) 497 break; 498 499 if ((dir[i].name[0] == FAT_ENTRY_FREE) || 500 (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) || 501 ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)) 502 continue; 503 504 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == 505 FAT_ATTR_VOLUME_ID) { 506 return dir[i].name; 507 } 508 } 509 return 0; 510 } 511 512 /* FAT label extraction from the root directory taken from Kay 513 * Sievers's volume_id library */ 514 static int probe_fat(struct blkid_probe *probe, 515 struct blkid_magic *id __BLKID_ATTR((unused)), 516 unsigned char *buf) 517 { 518 struct vfat_super_block *vs = (struct vfat_super_block *) buf; 519 struct msdos_super_block *ms = (struct msdos_super_block *) buf; 520 struct vfat_dir_entry *dir; 521 char serno[10]; 522 const unsigned char *label = 0, *vol_label = 0, *tmp; 523 unsigned char *vol_serno; 524 int label_len = 0, maxloop = 100; 525 __u16 sector_size, dir_entries, reserved; 526 __u32 sect_count, fat_size, dir_size, cluster_count, fat_length; 527 __u32 buf_size, start_data_sect, next, root_start, root_dir_entries; 528 529 /* sector size check */ 530 tmp = (unsigned char *)&ms->ms_sector_size; 531 sector_size = tmp[0] + (tmp[1] << 8); 532 if (sector_size != 0x200 && sector_size != 0x400 && 533 sector_size != 0x800 && sector_size != 0x1000) 534 return 1; 535 536 tmp = (unsigned char *)&ms->ms_dir_entries; 537 dir_entries = tmp[0] + (tmp[1] << 8); 538 reserved = blkid_le16(ms->ms_reserved); 539 tmp = (unsigned char *)&ms->ms_sectors; 540 sect_count = tmp[0] + (tmp[1] << 8); 541 if (sect_count == 0) 542 sect_count = blkid_le32(ms->ms_total_sect); 543 544 fat_length = blkid_le16(ms->ms_fat_length); 545 if (fat_length == 0) 546 fat_length = blkid_le32(vs->vs_fat32_length); 547 548 fat_size = fat_length * ms->ms_fats; 549 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) + 550 (sector_size-1)) / sector_size; 551 552 cluster_count = sect_count - (reserved + fat_size + dir_size); 553 if (ms->ms_cluster_size == 0) 554 return 1; 555 cluster_count /= ms->ms_cluster_size; 556 557 if (cluster_count > FAT32_MAX) 558 return 1; 559 560 if (ms->ms_fat_length) { 561 /* the label may be an attribute in the root directory */ 562 root_start = (reserved + fat_size) * sector_size; 563 root_dir_entries = vs->vs_dir_entries[0] + 564 (vs->vs_dir_entries[1] << 8); 565 566 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry); 567 dir = (struct vfat_dir_entry *) get_buffer(probe, root_start, 568 buf_size); 569 if (dir) 570 vol_label = search_fat_label(dir, root_dir_entries); 571 572 if (!vol_label || !memcmp(vol_label, no_name, 11)) 573 vol_label = ms->ms_label; 574 vol_serno = ms->ms_serno; 575 576 blkid_set_tag(probe->dev, "SEC_TYPE", "msdos", 577 sizeof("msdos")); 578 } else { 579 /* Search the FAT32 root dir for the label attribute */ 580 buf_size = vs->vs_cluster_size * sector_size; 581 start_data_sect = reserved + fat_size; 582 583 next = blkid_le32(vs->vs_root_cluster); 584 while (next && --maxloop) { 585 __u32 next_sect_off; 586 __u64 next_off, fat_entry_off; 587 int count; 588 589 next_sect_off = (next - 2) * vs->vs_cluster_size; 590 next_off = (start_data_sect + next_sect_off) * 591 sector_size; 592 593 dir = (struct vfat_dir_entry *) 594 get_buffer(probe, next_off, buf_size); 595 if (dir == NULL) 596 break; 597 598 count = buf_size / sizeof(struct vfat_dir_entry); 599 600 vol_label = search_fat_label(dir, count); 601 if (vol_label) 602 break; 603 604 /* get FAT entry */ 605 fat_entry_off = (reserved * sector_size) + 606 (next * sizeof(__u32)); 607 buf = get_buffer(probe, fat_entry_off, buf_size); 608 if (buf == NULL) 609 break; 610 611 /* set next cluster */ 612 next = blkid_le32(*((__u32 *) buf) & 0x0fffffff); 613 } 614 615 if (!vol_label || !memcmp(vol_label, no_name, 11)) 616 vol_label = vs->vs_label; 617 vol_serno = vs->vs_serno; 618 } 619 620 if (vol_label && memcmp(vol_label, no_name, 11)) { 621 if ((label_len = figure_label_len(vol_label, 11))) 622 label = vol_label; 623 } 624 625 /* We can't just print them as %04X, because they are unaligned */ 626 sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2], 627 vol_serno[1], vol_serno[0]); 628 629 blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len); 630 blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1); 631 632 return 0; 633 } 634 635 /* 636 * The FAT filesystem could be without a magic string in superblock 637 * (e.g. old floppies). This heuristic for FAT detection is inspired 638 * by http://vrfy.org/projects/volume_id/ and Linux kernel. 639 * [7-Jul-2005, Karel Zak <kzak (at) redhat.com>] 640 */ 641 static int probe_fat_nomagic(struct blkid_probe *probe, 642 struct blkid_magic *id __BLKID_ATTR((unused)), 643 unsigned char *buf) 644 { 645 struct msdos_super_block *ms; 646 647 ms = (struct msdos_super_block *)buf; 648 649 /* heads check */ 650 if (ms->ms_heads == 0) 651 return 1; 652 653 /* cluster size check*/ 654 if (ms->ms_cluster_size == 0 || 655 (ms->ms_cluster_size & (ms->ms_cluster_size-1))) 656 return 1; 657 658 /* media check */ 659 if (ms->ms_media < 0xf8 && ms->ms_media != 0xf0) 660 return 1; 661 662 /* fat counts(Linux kernel expects at least 1 FAT table) */ 663 if (!ms->ms_fats) 664 return 1; 665 666 /* 667 * OS/2 and apparently DFSee will place a FAT12/16-like 668 * pseudo-superblock in the first 512 bytes of non-FAT 669 * filesystems --- at least JFS and HPFS, and possibly others. 670 * So we explicitly check for those filesystems at the 671 * FAT12/16 filesystem magic field identifier, and if they are 672 * present, we rule this out as a FAT filesystem, despite the 673 * FAT-like pseudo-header. 674 */ 675 if ((memcmp(ms->ms_magic, "JFS ", 8) == 0) || 676 (memcmp(ms->ms_magic, "HPFS ", 8) == 0)) 677 return 1; 678 679 return probe_fat(probe, id, buf); 680 } 681 682 static int probe_ntfs(struct blkid_probe *probe, 683 struct blkid_magic *id __BLKID_ATTR((unused)), 684 unsigned char *buf) 685 { 686 struct ntfs_super_block *ns; 687 struct master_file_table_record *mft; 688 struct file_attribute *attr; 689 char uuid_str[17], label_str[129], *cp; 690 int bytes_per_sector, sectors_per_cluster; 691 int mft_record_size, attr_off, attr_len; 692 unsigned int i, attr_type, val_len; 693 int val_off; 694 __u64 nr_clusters; 695 blkid_loff_t off; 696 unsigned char *buf_mft, *val; 697 698 ns = (struct ntfs_super_block *) buf; 699 700 bytes_per_sector = ns->bios_parameter_block[0] + 701 (ns->bios_parameter_block[1] << 8); 702 sectors_per_cluster = ns->bios_parameter_block[2]; 703 704 if ((bytes_per_sector < 512) || (sectors_per_cluster == 0)) 705 return 1; 706 707 if (ns->cluster_per_mft_record < 0) 708 mft_record_size = 1 << (0-ns->cluster_per_mft_record); 709 else 710 mft_record_size = ns->cluster_per_mft_record * 711 sectors_per_cluster * bytes_per_sector; 712 nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster; 713 714 if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) || 715 (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters)) 716 return 1; 717 718 off = blkid_le64(ns->mft_mirror_cluster_location) * 719 bytes_per_sector * sectors_per_cluster; 720 721 buf_mft = get_buffer(probe, off, mft_record_size); 722 if (!buf_mft) 723 return 1; 724 725 if (memcmp(buf_mft, "FILE", 4)) 726 return 1; 727 728 off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector * 729 sectors_per_cluster; 730 731 buf_mft = get_buffer(probe, off, mft_record_size); 732 if (!buf_mft) 733 return 1; 734 735 if (memcmp(buf_mft, "FILE", 4)) 736 return 1; 737 738 off += MFT_RECORD_VOLUME * mft_record_size; 739 740 buf_mft = get_buffer(probe, off, mft_record_size); 741 if (!buf_mft) 742 return 1; 743 744 if (memcmp(buf_mft, "FILE", 4)) 745 return 1; 746 747 mft = (struct master_file_table_record *) buf_mft; 748 749 attr_off = blkid_le16(mft->attrs_offset); 750 label_str[0] = 0; 751 752 while (1) { 753 attr = (struct file_attribute *) (buf_mft + attr_off); 754 attr_len = blkid_le16(attr->len); 755 attr_type = blkid_le32(attr->type); 756 val_off = blkid_le16(attr->value_offset); 757 val_len = blkid_le32(attr->value_len); 758 759 attr_off += attr_len; 760 761 if ((attr_off > mft_record_size) || 762 (attr_len == 0)) 763 break; 764 765 if (attr_type == MFT_RECORD_ATTR_END) 766 break; 767 768 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) { 769 if (val_len > sizeof(label_str)) 770 val_len = sizeof(label_str)-1; 771 772 for (i=0, cp=label_str; i < val_len; i+=2,cp++) { 773 val = ((__u8 *) attr) + val_off + i; 774 *cp = val[0]; 775 if (val[1]) 776 *cp = '?'; 777 } 778 *cp = 0; 779 } 780 } 781 782 sprintf(uuid_str, "%016llX", blkid_le64(ns->volume_serial)); 783 blkid_set_tag(probe->dev, "UUID", uuid_str, 0); 784 if (label_str[0]) 785 blkid_set_tag(probe->dev, "LABEL", label_str, 0); 786 return 0; 787 } 788 789 790 static int probe_xfs(struct blkid_probe *probe, 791 struct blkid_magic *id __BLKID_ATTR((unused)), 792 unsigned char *buf) 793 { 794 struct xfs_super_block *xs; 795 const char *label = 0; 796 797 xs = (struct xfs_super_block *)buf; 798 799 if (strlen(xs->xs_fname)) 800 label = xs->xs_fname; 801 blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname)); 802 set_uuid(probe->dev, xs->xs_uuid, 0); 803 return 0; 804 } 805 806 static int probe_reiserfs(struct blkid_probe *probe, 807 struct blkid_magic *id, unsigned char *buf) 808 { 809 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf; 810 unsigned int blocksize; 811 const char *label = 0; 812 813 blocksize = blkid_le16(rs->rs_blocksize); 814 815 /* The blocksize must be at least 1k */ 816 if ((blocksize >> 10) == 0) 817 return -BLKID_ERR_PARAM; 818 819 /* If the superblock is inside the journal, we have the wrong one */ 820 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block)) 821 return -BLKID_ERR_BIG; 822 823 /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */ 824 if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') { 825 if (strlen(rs->rs_label)) 826 label = rs->rs_label; 827 set_uuid(probe->dev, rs->rs_uuid, 0); 828 } 829 blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label)); 830 831 return 0; 832 } 833 834 static int probe_reiserfs4(struct blkid_probe *probe, 835 struct blkid_magic *id __BLKID_ATTR((unused)), 836 unsigned char *buf) 837 { 838 struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf; 839 const unsigned char *label = 0; 840 841 if (strlen((char *) rs4->rs4_label)) 842 label = rs4->rs4_label; 843 set_uuid(probe->dev, rs4->rs4_uuid, 0); 844 blkid_set_tag(probe->dev, "LABEL", (const char *) label, 845 sizeof(rs4->rs4_label)); 846 847 return 0; 848 } 849 850 static int probe_jfs(struct blkid_probe *probe, 851 struct blkid_magic *id __BLKID_ATTR((unused)), 852 unsigned char *buf) 853 { 854 struct jfs_super_block *js; 855 const char *label = 0; 856 857 js = (struct jfs_super_block *)buf; 858 859 if (blkid_le32(js->js_bsize) != (1 << blkid_le16(js->js_l2bsize))) 860 return 1; 861 862 if (blkid_le32(js->js_pbsize) != (1 << blkid_le16(js->js_l2pbsize))) 863 return 1; 864 865 if ((blkid_le16(js->js_l2bsize) - blkid_le16(js->js_l2pbsize)) != 866 blkid_le16(js->js_l2bfactor)) 867 return 1; 868 869 if (strlen((char *) js->js_label)) 870 label = (char *) js->js_label; 871 blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label)); 872 set_uuid(probe->dev, js->js_uuid, 0); 873 return 0; 874 } 875 876 static int probe_zfs(struct blkid_probe *probe, struct blkid_magic *id, 877 unsigned char *buf) 878 { 879 #if 0 880 char *vdev_label; 881 const char *pool_name = 0; 882 883 /* read nvpair data for pool name, pool GUID (complex) */ 884 blkid_set_tag(probe->dev, "LABEL", pool_name, sizeof(pool_name)); 885 set_uuid(probe->dev, pool_guid, 0); 886 #endif 887 return 0; 888 } 889 890 static int probe_luks(struct blkid_probe *probe, 891 struct blkid_magic *id __BLKID_ATTR((unused)), 892 unsigned char *buf) 893 { 894 char uuid[40]; 895 896 /* 168 is the offset to the 40 character uuid: 897 * http://luks.endorphin.org/LUKS-on-disk-format.pdf */ 898 strncpy(uuid, (char *) buf+168, 40); 899 blkid_set_tag(probe->dev, "UUID", uuid, sizeof(uuid)); 900 return 0; 901 } 902 903 static int probe_romfs(struct blkid_probe *probe, 904 struct blkid_magic *id __BLKID_ATTR((unused)), 905 unsigned char *buf) 906 { 907 struct romfs_super_block *ros; 908 const char *label = 0; 909 910 ros = (struct romfs_super_block *)buf; 911 912 if (strlen((char *) ros->ros_volume)) 913 label = (char *) ros->ros_volume; 914 blkid_set_tag(probe->dev, "LABEL", label, 0); 915 return 0; 916 } 917 918 static int probe_cramfs(struct blkid_probe *probe, 919 struct blkid_magic *id __BLKID_ATTR((unused)), 920 unsigned char *buf) 921 { 922 struct cramfs_super_block *csb; 923 const char *label = 0; 924 925 csb = (struct cramfs_super_block *)buf; 926 927 if (strlen((char *) csb->name)) 928 label = (char *) csb->name; 929 blkid_set_tag(probe->dev, "LABEL", label, 0); 930 return 0; 931 } 932 933 static int probe_swap0(struct blkid_probe *probe, 934 struct blkid_magic *id __BLKID_ATTR((unused)), 935 unsigned char *buf __BLKID_ATTR((unused))) 936 { 937 blkid_set_tag(probe->dev, "UUID", 0, 0); 938 blkid_set_tag(probe->dev, "LABEL", 0, 0); 939 return 0; 940 } 941 942 static int probe_swap1(struct blkid_probe *probe, 943 struct blkid_magic *id, 944 unsigned char *buf __BLKID_ATTR((unused))) 945 { 946 struct swap_id_block *sws; 947 948 probe_swap0(probe, id, buf); 949 /* 950 * Version 1 swap headers are always located at offset of 1024 951 * bytes, although the swap signature itself is located at the 952 * end of the page (which may vary depending on hardware 953 * pagesize). 954 */ 955 sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024); 956 if (!sws) 957 return 1; 958 959 /* check for wrong version or zeroed pagecount, for sanity */ 960 if (!memcmp(id->bim_magic, "SWAPSPACE2", id->bim_len) && 961 (sws->sws_version != 1 || sws->sws_lastpage == 0)) 962 return 1; 963 964 /* arbitrary sanity check.. is there any garbage down there? */ 965 if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) { 966 if (sws->sws_volume[0]) 967 blkid_set_tag(probe->dev, "LABEL", sws->sws_volume, 968 sizeof(sws->sws_volume)); 969 if (sws->sws_uuid[0]) 970 set_uuid(probe->dev, sws->sws_uuid, 0); 971 } 972 return 0; 973 } 974 975 static int probe_iso9660(struct blkid_probe *probe, 976 struct blkid_magic *id __BLKID_ATTR((unused)), 977 unsigned char *buf) 978 { 979 struct iso_volume_descriptor *iso; 980 const unsigned char *label; 981 982 iso = (struct iso_volume_descriptor *) buf; 983 label = iso->volume_id; 984 985 blkid_set_tag(probe->dev, "LABEL", (const char *) label, 986 figure_label_len(label, 32)); 987 return 0; 988 } 989 990 991 static const char 992 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02", 993 "NSR03", "TEA01", 0 }; 994 995 static int probe_udf(struct blkid_probe *probe, 996 struct blkid_magic *id __BLKID_ATTR((unused)), 997 unsigned char *buf __BLKID_ATTR((unused))) 998 { 999 int j, bs; 1000 struct iso_volume_descriptor *isosb; 1001 const char ** m; 1002 1003 /* determine the block size by scanning in 2K increments 1004 (block sizes larger than 2K will be null padded) */ 1005 for (bs = 1; bs < 16; bs++) { 1006 isosb = (struct iso_volume_descriptor *) 1007 get_buffer(probe, bs*2048+32768, sizeof(isosb)); 1008 if (!isosb) 1009 return 1; 1010 if (isosb->vd_id[0]) 1011 break; 1012 } 1013 1014 /* Scan up to another 64 blocks looking for additional VSD's */ 1015 for (j = 1; j < 64; j++) { 1016 if (j > 1) { 1017 isosb = (struct iso_volume_descriptor *) 1018 get_buffer(probe, j*bs*2048+32768, 1019 sizeof(isosb)); 1020 if (!isosb) 1021 return 1; 1022 } 1023 /* If we find NSR0x then call it udf: 1024 NSR01 for UDF 1.00 1025 NSR02 for UDF 1.50 1026 NSR03 for UDF 2.00 */ 1027 if (!memcmp(isosb->vd_id, "NSR0", 4)) 1028 return probe_iso9660(probe, id, buf); 1029 for (m = udf_magic; *m; m++) 1030 if (!memcmp(*m, isosb->vd_id, 5)) 1031 break; 1032 if (*m == 0) 1033 return 1; 1034 } 1035 return 1; 1036 } 1037 1038 static int probe_ocfs(struct blkid_probe *probe, 1039 struct blkid_magic *id __BLKID_ATTR((unused)), 1040 unsigned char *buf) 1041 { 1042 struct ocfs_volume_header ovh; 1043 struct ocfs_volume_label ovl; 1044 __u32 major; 1045 1046 memcpy(&ovh, buf, sizeof(ovh)); 1047 memcpy(&ovl, buf+512, sizeof(ovl)); 1048 1049 major = ocfsmajor(ovh); 1050 if (major == 1) 1051 blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1")); 1052 else if (major >= 9) 1053 blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs")); 1054 1055 blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl)); 1056 blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh)); 1057 set_uuid(probe->dev, ovl.vol_id, 0); 1058 return 0; 1059 } 1060 1061 static int probe_ocfs2(struct blkid_probe *probe, 1062 struct blkid_magic *id __BLKID_ATTR((unused)), 1063 unsigned char *buf) 1064 { 1065 struct ocfs2_super_block *osb; 1066 1067 osb = (struct ocfs2_super_block *)buf; 1068 1069 blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label)); 1070 set_uuid(probe->dev, osb->s_uuid, 0); 1071 return 0; 1072 } 1073 1074 static int probe_oracleasm(struct blkid_probe *probe, 1075 struct blkid_magic *id __BLKID_ATTR((unused)), 1076 unsigned char *buf) 1077 { 1078 struct oracle_asm_disk_label *dl; 1079 1080 dl = (struct oracle_asm_disk_label *)buf; 1081 1082 blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id)); 1083 return 0; 1084 } 1085 1086 static int probe_gfs(struct blkid_probe *probe, 1087 struct blkid_magic *id __BLKID_ATTR((unused)), 1088 unsigned char *buf) 1089 { 1090 struct gfs2_sb *sbd; 1091 const char *label = 0; 1092 1093 sbd = (struct gfs2_sb *)buf; 1094 1095 if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS && 1096 blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI) 1097 { 1098 blkid_set_tag(probe->dev, "UUID", 0, 0); 1099 1100 if (strlen(sbd->sb_locktable)) 1101 label = sbd->sb_locktable; 1102 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable)); 1103 return 0; 1104 } 1105 return 1; 1106 } 1107 1108 static int probe_gfs2(struct blkid_probe *probe, 1109 struct blkid_magic *id __BLKID_ATTR((unused)), 1110 unsigned char *buf) 1111 { 1112 struct gfs2_sb *sbd; 1113 const char *label = 0; 1114 1115 sbd = (struct gfs2_sb *)buf; 1116 1117 if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS && 1118 blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI) 1119 { 1120 blkid_set_tag(probe->dev, "UUID", 0, 0); 1121 1122 if (strlen(sbd->sb_locktable)) 1123 label = sbd->sb_locktable; 1124 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable)); 1125 return 0; 1126 } 1127 return 1; 1128 } 1129 1130 static void unicode_16be_to_utf8(unsigned char *str, int out_len, 1131 const unsigned char *buf, int in_len) 1132 { 1133 int i, j; 1134 unsigned int c; 1135 1136 for (i = j = 0; i + 2 <= in_len; i += 2) { 1137 c = (buf[i] << 8) | buf[i+1]; 1138 if (c == 0) { 1139 str[j] = '\0'; 1140 break; 1141 } else if (c < 0x80) { 1142 if (j+1 >= out_len) 1143 break; 1144 str[j++] = (unsigned char) c; 1145 } else if (c < 0x800) { 1146 if (j+2 >= out_len) 1147 break; 1148 str[j++] = (unsigned char) (0xc0 | (c >> 6)); 1149 str[j++] = (unsigned char) (0x80 | (c & 0x3f)); 1150 } else { 1151 if (j+3 >= out_len) 1152 break; 1153 str[j++] = (unsigned char) (0xe0 | (c >> 12)); 1154 str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f)); 1155 str[j++] = (unsigned char) (0x80 | (c & 0x3f)); 1156 } 1157 } 1158 str[j] = '\0'; 1159 } 1160 1161 static int probe_hfs(struct blkid_probe *probe __BLKID_ATTR((unused)), 1162 struct blkid_magic *id __BLKID_ATTR((unused)), 1163 unsigned char *buf) 1164 { 1165 struct hfs_mdb *hfs = (struct hfs_mdb *) buf; 1166 char uuid_str[17]; 1167 __u64 uuid; 1168 1169 if ((memcmp(hfs->embed_sig, "H+", 2) == 0) || 1170 (memcmp(hfs->embed_sig, "HX", 2) == 0)) 1171 return 1; /* Not hfs, but an embedded HFS+ */ 1172 1173 uuid = blkid_le64(*((unsigned long long *) hfs->finder_info.id)); 1174 if (uuid) { 1175 sprintf(uuid_str, "%016llX", uuid); 1176 blkid_set_tag(probe->dev, "UUID", uuid_str, 0); 1177 } 1178 blkid_set_tag(probe->dev, "LABEL", hfs->label, hfs->label_len); 1179 return 0; 1180 } 1181 1182 1183 static int probe_hfsplus(struct blkid_probe *probe, 1184 struct blkid_magic *id, 1185 unsigned char *buf) 1186 { 1187 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT]; 1188 struct hfsplus_bnode_descriptor *descr; 1189 struct hfsplus_bheader_record *bnode; 1190 struct hfsplus_catalog_key *key; 1191 struct hfsplus_vol_header *hfsplus; 1192 struct hfs_mdb *sbd = (struct hfs_mdb *) buf; 1193 unsigned int alloc_block_size; 1194 unsigned int alloc_first_block; 1195 unsigned int embed_first_block; 1196 unsigned int off = 0; 1197 unsigned int blocksize; 1198 unsigned int cat_block; 1199 unsigned int ext_block_start; 1200 unsigned int ext_block_count; 1201 unsigned int record_count; 1202 unsigned int leaf_node_head; 1203 unsigned int leaf_node_count; 1204 unsigned int leaf_node_size; 1205 unsigned int leaf_block; 1206 unsigned int label_len; 1207 int ext; 1208 __u64 leaf_off, uuid; 1209 char uuid_str[17], label[512]; 1210 1211 /* Check for a HFS+ volume embedded in a HFS volume */ 1212 if (memcmp(sbd->signature, "BD", 2) == 0) { 1213 if ((memcmp(sbd->embed_sig, "H+", 2) != 0) && 1214 (memcmp(sbd->embed_sig, "HX", 2) != 0)) 1215 /* This must be an HFS volume, so fail */ 1216 return 1; 1217 1218 alloc_block_size = blkid_be32(sbd->al_blk_size); 1219 alloc_first_block = blkid_be16(sbd->al_bl_st); 1220 embed_first_block = blkid_be16(sbd->embed_startblock); 1221 off = (alloc_first_block * 512) + 1222 (embed_first_block * alloc_block_size); 1223 buf = get_buffer(probe, off + (id->bim_kboff * 1024), 1224 sizeof(sbd)); 1225 if (!buf) 1226 return 1; 1227 1228 hfsplus = (struct hfsplus_vol_header *) buf; 1229 } 1230 1231 hfsplus = (struct hfsplus_vol_header *) buf; 1232 1233 if ((memcmp(hfsplus->signature, "H+", 2) != 0) && 1234 (memcmp(hfsplus->signature, "HX", 2) != 0)) 1235 return 1; 1236 1237 uuid = blkid_le64(*((unsigned long long *) hfsplus->finder_info.id)); 1238 if (uuid) { 1239 sprintf(uuid_str, "%016llX", uuid); 1240 blkid_set_tag(probe->dev, "UUID", uuid_str, 0); 1241 } 1242 1243 blocksize = blkid_be32(hfsplus->blocksize); 1244 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents)); 1245 cat_block = blkid_be32(extents[0].start_block); 1246 1247 buf = get_buffer(probe, off + (cat_block * blocksize), 0x2000); 1248 if (!buf) 1249 return 0; 1250 1251 bnode = (struct hfsplus_bheader_record *) 1252 &buf[sizeof(struct hfsplus_bnode_descriptor)]; 1253 1254 leaf_node_head = blkid_be32(bnode->leaf_head); 1255 leaf_node_size = blkid_be16(bnode->node_size); 1256 leaf_node_count = blkid_be32(bnode->leaf_count); 1257 if (leaf_node_count == 0) 1258 return 0; 1259 1260 leaf_block = (leaf_node_head * leaf_node_size) / blocksize; 1261 1262 /* get physical location */ 1263 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) { 1264 ext_block_start = blkid_be32(extents[ext].start_block); 1265 ext_block_count = blkid_be32(extents[ext].block_count); 1266 if (ext_block_count == 0) 1267 return 0; 1268 1269 /* this is our extent */ 1270 if (leaf_block < ext_block_count) 1271 break; 1272 1273 leaf_block -= ext_block_count; 1274 } 1275 if (ext == HFSPLUS_EXTENT_COUNT) 1276 return 0; 1277 1278 leaf_off = (ext_block_start + leaf_block) * blocksize; 1279 1280 buf = get_buffer(probe, off + leaf_off, leaf_node_size); 1281 if (!buf) 1282 return 0; 1283 1284 descr = (struct hfsplus_bnode_descriptor *) buf; 1285 record_count = blkid_be16(descr->num_recs); 1286 if (record_count == 0) 1287 return 0; 1288 1289 if (descr->type != HFS_NODE_LEAF) 1290 return 0; 1291 1292 key = (struct hfsplus_catalog_key *) 1293 &buf[sizeof(struct hfsplus_bnode_descriptor)]; 1294 1295 if (blkid_be32(key->parent_id) != HFSPLUS_POR_CNID) 1296 return 0; 1297 1298 label_len = blkid_be16(key->unicode_len) * 2; 1299 unicode_16be_to_utf8(label, sizeof(label), key->unicode, label_len); 1300 blkid_set_tag(probe->dev, "LABEL", label, 0); 1301 return 0; 1302 } 1303 1304 #define LVM2_LABEL_SIZE 512 1305 static unsigned int lvm2_calc_crc(const void *buf, unsigned int size) 1306 { 1307 static const unsigned int crctab[] = { 1308 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1309 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1310 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1311 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1312 }; 1313 unsigned int i, crc = 0xf597a6cf; 1314 const __u8 *data = (const __u8 *) buf; 1315 1316 for (i = 0; i < size; i++) { 1317 crc ^= *data++; 1318 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1319 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1320 } 1321 return crc; 1322 } 1323 1324 static int probe_lvm2(struct blkid_probe *probe, 1325 struct blkid_magic *id, 1326 unsigned char *buf) 1327 { 1328 int sector = (id->bim_kboff) << 1; 1329 struct lvm2_pv_label_header *label= (struct lvm2_pv_label_header *)buf; 1330 char *p, *q, uuid[40]; 1331 unsigned int i, b; 1332 1333 /* buf is at 0k or 1k offset; find label inside */ 1334 if (memcmp(buf, "LABELONE", 8) == 0) { 1335 label = (struct lvm2_pv_label_header *)buf; 1336 } else if (memcmp(buf + 512, "LABELONE", 8) == 0) { 1337 label = (struct lvm2_pv_label_header *)(buf + 512); 1338 sector++; 1339 } else { 1340 return 1; 1341 } 1342 1343 if (blkid_le64(label->sector_xl) != (unsigned) sector) { 1344 DBG(DEBUG_PROBE, 1345 printf("LVM2: label for sector %llu found at sector %d\n", 1346 blkid_le64(label->sector_xl), sector)); 1347 return 1; 1348 } 1349 1350 if (lvm2_calc_crc(&label->offset_xl, LVM2_LABEL_SIZE - 1351 ((char *)&label->offset_xl - (char *)label)) != 1352 blkid_le32(label->crc_xl)) { 1353 DBG(DEBUG_PROBE, 1354 printf("LVM2: label checksum incorrect at sector %d\n", 1355 sector)); 1356 return 1; 1357 } 1358 1359 for (i=0, b=1, p=uuid, q= (char *) label->pv_uuid; i <= 32; 1360 i++, b <<= 1) { 1361 if (b & 0x4444440) 1362 *p++ = '-'; 1363 *p++ = *q++; 1364 } 1365 1366 blkid_set_tag(probe->dev, "UUID", uuid, LVM2_ID_LEN+6); 1367 1368 return 0; 1369 } 1370 1371 static int probe_btrfs(struct blkid_probe *probe, 1372 struct blkid_magic *id, 1373 unsigned char *buf) 1374 { 1375 struct btrfs_super_block *bs; 1376 const char *label = 0; 1377 1378 bs = (struct btrfs_super_block *)buf; 1379 1380 if (strlen(bs->label)) 1381 label = bs->label; 1382 blkid_set_tag(probe->dev, "LABEL", label, sizeof(bs->label)); 1383 set_uuid(probe->dev, bs->fsid, 0); 1384 return 0; 1385 } 1386 /* 1387 * Various filesystem magics that we can check for. Note that kboff and 1388 * sboff are in kilobytes and bytes respectively. All magics are in 1389 * byte strings so we don't worry about endian issues. 1390 */ 1391 static struct blkid_magic type_array[] = { 1392 /* type kboff sboff len magic probe */ 1393 { "oracleasm", 0, 32, 8, "ORCLDISK", probe_oracleasm }, 1394 { "ntfs", 0, 3, 8, "NTFS ", probe_ntfs }, 1395 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd }, 1396 { "ext4dev", 1, 0x38, 2, "\123\357", probe_ext4dev }, 1397 { "ext4", 1, 0x38, 2, "\123\357", probe_ext4 }, 1398 { "ext3", 1, 0x38, 2, "\123\357", probe_ext3 }, 1399 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 }, 1400 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs }, 1401 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs }, 1402 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs }, 1403 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs }, 1404 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs }, 1405 { "reiser4", 64, 0, 7, "ReIsEr4", probe_reiserfs4 }, 1406 { "gfs2", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs2 }, 1407 { "gfs", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs }, 1408 { "vfat", 0, 0x52, 5, "MSWIN", probe_fat }, 1409 { "vfat", 0, 0x52, 8, "FAT32 ", probe_fat }, 1410 { "vfat", 0, 0x36, 5, "MSDOS", probe_fat }, 1411 { "vfat", 0, 0x36, 8, "FAT16 ", probe_fat }, 1412 { "vfat", 0, 0x36, 8, "FAT12 ", probe_fat }, 1413 { "vfat", 0, 0, 1, "\353", probe_fat_nomagic }, 1414 { "vfat", 0, 0, 1, "\351", probe_fat_nomagic }, 1415 { "vfat", 0, 0x1fe, 2, "\125\252", probe_fat_nomagic }, 1416 { "minix", 1, 0x10, 2, "\177\023", 0 }, 1417 { "minix", 1, 0x10, 2, "\217\023", 0 }, 1418 { "minix", 1, 0x10, 2, "\150\044", 0 }, 1419 { "minix", 1, 0x10, 2, "\170\044", 0 }, 1420 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 }, 1421 { "xfs", 0, 0, 4, "XFSB", probe_xfs }, 1422 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs }, 1423 { "bfs", 0, 0, 4, "\316\372\173\033", 0 }, 1424 { "cramfs", 0, 0, 4, "E=\315\050", probe_cramfs }, 1425 { "qnx4", 0, 4, 6, "QNX4FS", 0 }, 1426 { "udf", 32, 1, 5, "BEA01", probe_udf }, 1427 { "udf", 32, 1, 5, "BOOT2", probe_udf }, 1428 { "udf", 32, 1, 5, "CD001", probe_udf }, 1429 { "udf", 32, 1, 5, "CDW02", probe_udf }, 1430 { "udf", 32, 1, 5, "NSR02", probe_udf }, 1431 { "udf", 32, 1, 5, "NSR03", probe_udf }, 1432 { "udf", 32, 1, 5, "TEA01", probe_udf }, 1433 { "iso9660", 32, 1, 5, "CD001", probe_iso9660 }, 1434 { "iso9660", 32, 9, 5, "CDROM", probe_iso9660 }, 1435 { "jfs", 32, 0, 4, "JFS1", probe_jfs }, 1436 { "zfs", 8, 0, 8, "\0\0\x02\xf5\xb0\x07\xb1\x0c", probe_zfs }, 1437 { "zfs", 8, 0, 8, "\x0c\xb1\x07\xb0\xf5\x02\0\0", probe_zfs }, 1438 { "zfs", 264, 0, 8, "\0\0\x02\xf5\xb0\x07\xb1\x0c", probe_zfs }, 1439 { "zfs", 264, 0, 8, "\x0c\xb1\x07\xb0\xf5\x02\0\0", probe_zfs }, 1440 { "hfsplus", 1, 0, 2, "BD", probe_hfsplus }, 1441 { "hfsplus", 1, 0, 2, "H+", probe_hfsplus }, 1442 { "hfsplus", 1, 0, 2, "HX", probe_hfsplus }, 1443 { "hfs", 1, 0, 2, "BD", probe_hfs }, 1444 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 }, 1445 { "hpfs", 8, 0, 4, "I\350\225\371", 0 }, 1446 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 }, 1447 { "swap", 0, 0xff6, 10, "SWAP-SPACE", probe_swap0 }, 1448 { "swap", 0, 0xff6, 10, "SWAPSPACE2", probe_swap1 }, 1449 { "swsuspend", 0, 0xff6, 9, "S1SUSPEND", probe_swap1 }, 1450 { "swsuspend", 0, 0xff6, 9, "S2SUSPEND", probe_swap1 }, 1451 { "swsuspend", 0, 0xff6, 9, "ULSUSPEND", probe_swap1 }, 1452 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", probe_swap0 }, 1453 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", probe_swap1 }, 1454 { "swsuspend", 0, 0x1ff6, 9, "S1SUSPEND", probe_swap1 }, 1455 { "swsuspend", 0, 0x1ff6, 9, "S2SUSPEND", probe_swap1 }, 1456 { "swsuspend", 0, 0x1ff6, 9, "ULSUSPEND", probe_swap1 }, 1457 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", probe_swap0 }, 1458 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", probe_swap1 }, 1459 { "swsuspend", 0, 0x3ff6, 9, "S1SUSPEND", probe_swap1 }, 1460 { "swsuspend", 0, 0x3ff6, 9, "S2SUSPEND", probe_swap1 }, 1461 { "swsuspend", 0, 0x3ff6, 9, "ULSUSPEND", probe_swap1 }, 1462 { "swap", 0, 0x7ff6, 10, "SWAP-SPACE", probe_swap0 }, 1463 { "swap", 0, 0x7ff6, 10, "SWAPSPACE2", probe_swap1 }, 1464 { "swsuspend", 0, 0x7ff6, 9, "S1SUSPEND", probe_swap1 }, 1465 { "swsuspend", 0, 0x7ff6, 9, "S2SUSPEND", probe_swap1 }, 1466 { "swsuspend", 0, 0x7ff6, 9, "ULSUSPEND", probe_swap1 }, 1467 { "swap", 0, 0xfff6, 10, "SWAP-SPACE", probe_swap0 }, 1468 { "swap", 0, 0xfff6, 10, "SWAPSPACE2", probe_swap1 }, 1469 { "swsuspend", 0, 0xfff6, 9, "S1SUSPEND", probe_swap1 }, 1470 { "swsuspend", 0, 0xfff6, 9, "S2SUSPEND", probe_swap1 }, 1471 { "swsuspend", 0, 0xfff6, 9, "ULSUSPEND", probe_swap1 }, 1472 { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs }, 1473 { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 }, 1474 { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 }, 1475 { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 }, 1476 { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 }, 1477 { "crypt_LUKS", 0, 0, 6, "LUKS\xba\xbe", probe_luks }, 1478 { "squashfs", 0, 0, 4, "sqsh", 0 }, 1479 { "squashfs", 0, 0, 4, "hsqs", 0 }, 1480 { "lvm2pv", 0, 0x218, 8, "LVM2 001", probe_lvm2 }, 1481 { "lvm2pv", 0, 0x018, 8, "LVM2 001", probe_lvm2 }, 1482 { "lvm2pv", 1, 0x018, 8, "LVM2 001", probe_lvm2 }, 1483 { "lvm2pv", 1, 0x218, 8, "LVM2 001", probe_lvm2 }, 1484 { "btrfs", 64, 0x40, 8, "_BHRfS_M", probe_btrfs }, 1485 { NULL, 0, 0, 0, NULL, NULL } 1486 }; 1487 1488 /* 1489 * Verify that the data in dev is consistent with what is on the actual 1490 * block device (using the devname field only). Normally this will be 1491 * called when finding items in the cache, but for long running processes 1492 * is also desirable to revalidate an item before use. 1493 * 1494 * If we are unable to revalidate the data, we return the old data and 1495 * do not set the BLKID_BID_FL_VERIFIED flag on it. 1496 */ 1497 blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev) 1498 { 1499 struct blkid_magic *id; 1500 struct blkid_probe probe; 1501 blkid_tag_iterate iter; 1502 unsigned char *buf; 1503 const char *type, *value; 1504 struct stat st; 1505 time_t now; 1506 double diff; 1507 int idx; 1508 1509 if (!dev) 1510 return NULL; 1511 1512 now = time(0); 1513 diff = difftime(now, dev->bid_time); 1514 1515 if (stat(dev->bid_name, &st) < 0) { 1516 DBG(DEBUG_PROBE, 1517 printf("blkid_verify: error %s (%d) while " 1518 "trying to stat %s\n", strerror(errno), errno, 1519 dev->bid_name)); 1520 open_err: 1521 if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) { 1522 /* We don't have read permission, just return cache data. */ 1523 DBG(DEBUG_PROBE, printf("returning unverified data for %s\n", 1524 dev->bid_name)); 1525 return dev; 1526 } 1527 blkid_free_dev(dev); 1528 return NULL; 1529 } 1530 1531 if ((now >= dev->bid_time) && 1532 (st.st_mtime <= dev->bid_time) && 1533 ((diff < BLKID_PROBE_MIN) || 1534 (dev->bid_flags & BLKID_BID_FL_VERIFIED && 1535 diff < BLKID_PROBE_INTERVAL))) 1536 return dev; 1537 1538 DBG(DEBUG_PROBE, 1539 printf("need to revalidate %s (cache time %lu, stat time %lu,\n\t" 1540 "time since last check %lu)\n", 1541 dev->bid_name, (unsigned long)dev->bid_time, 1542 (unsigned long)st.st_mtime, (unsigned long)diff)); 1543 1544 if ((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) { 1545 DBG(DEBUG_PROBE, printf("blkid_verify: error %s (%d) while " 1546 "opening %s\n", strerror(errno), errno, 1547 dev->bid_name)); 1548 goto open_err; 1549 } 1550 1551 probe.cache = cache; 1552 probe.dev = dev; 1553 probe.sbbuf = 0; 1554 probe.buf = 0; 1555 probe.buf_max = 0; 1556 1557 /* 1558 * Iterate over the type array. If we already know the type, 1559 * then try that first. If it doesn't work, then blow away 1560 * the type information, and try again. 1561 * 1562 */ 1563 try_again: 1564 type = 0; 1565 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) { 1566 uuid_t uuid; 1567 1568 if (check_mdraid(probe.fd, uuid) == 0) { 1569 set_uuid(dev, uuid, 0); 1570 type = "mdraid"; 1571 goto found_type; 1572 } 1573 } 1574 for (id = type_array; id->bim_type; id++) { 1575 if (dev->bid_type && 1576 strcmp(id->bim_type, dev->bid_type)) 1577 continue; 1578 1579 idx = id->bim_kboff + (id->bim_sboff >> 10); 1580 buf = get_buffer(&probe, idx << 10, 1024); 1581 if (!buf) 1582 continue; 1583 1584 if (memcmp(id->bim_magic, buf + (id->bim_sboff & 0x3ff), 1585 id->bim_len)) 1586 continue; 1587 1588 if ((id->bim_probe == NULL) || 1589 (id->bim_probe(&probe, id, buf) == 0)) { 1590 type = id->bim_type; 1591 goto found_type; 1592 } 1593 } 1594 1595 if (!id->bim_type && dev->bid_type) { 1596 /* 1597 * Zap the device filesystem information and try again 1598 */ 1599 DBG(DEBUG_PROBE, 1600 printf("previous fs type %s not valid, " 1601 "trying full probe\n", dev->bid_type)); 1602 iter = blkid_tag_iterate_begin(dev); 1603 while (blkid_tag_next(iter, &type, &value) == 0) 1604 blkid_set_tag(dev, type, 0, 0); 1605 blkid_tag_iterate_end(iter); 1606 goto try_again; 1607 } 1608 1609 if (!dev->bid_type) { 1610 blkid_free_dev(dev); 1611 dev = 0; 1612 goto found_type; 1613 } 1614 1615 found_type: 1616 if (dev && type) { 1617 dev->bid_devno = st.st_rdev; 1618 dev->bid_time = time(0); 1619 dev->bid_flags |= BLKID_BID_FL_VERIFIED; 1620 cache->bic_flags |= BLKID_BIC_FL_CHANGED; 1621 1622 blkid_set_tag(dev, "TYPE", type, 0); 1623 1624 DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n", 1625 dev->bid_name, (long long)st.st_rdev, type)); 1626 } 1627 1628 free(probe.sbbuf); 1629 free(probe.buf); 1630 if (probe.fd >= 0) 1631 close(probe.fd); 1632 1633 return dev; 1634 } 1635 1636 int blkid_known_fstype(const char *fstype) 1637 { 1638 struct blkid_magic *id; 1639 1640 for (id = type_array; id->bim_type; id++) { 1641 if (strcmp(fstype, id->bim_type) == 0) 1642 return 1; 1643 } 1644 return 0; 1645 } 1646 1647 #ifdef TEST_PROGRAM 1648 int main(int argc, char **argv) 1649 { 1650 blkid_dev dev; 1651 blkid_cache cache; 1652 int ret; 1653 1654 if (argc != 2) { 1655 fprintf(stderr, "Usage: %s device\n" 1656 "Probe a single device to determine type\n", argv[0]); 1657 exit(1); 1658 } 1659 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) { 1660 fprintf(stderr, "%s: error creating cache (%d)\n", 1661 argv[0], ret); 1662 exit(1); 1663 } 1664 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL); 1665 if (!dev) { 1666 printf("%s: %s has an unsupported type\n", argv[0], argv[1]); 1667 return (1); 1668 } 1669 printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)"); 1670 if (dev->bid_label) 1671 printf("LABEL='%s'\n", dev->bid_label); 1672 if (dev->bid_uuid) 1673 printf("UUID='%s'\n", dev->bid_uuid); 1674 1675 blkid_free_dev(dev); 1676 return (0); 1677 } 1678 #endif 1679