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 "config.h"
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <ctype.h>
23 #include <sys/types.h>
24 #ifdef HAVE_SYS_STAT_H
25 #include <sys/stat.h>
26 #endif
27 #ifdef HAVE_SYS_MKDEV_H
28 #include <sys/mkdev.h>
29 #endif
30 #ifdef __linux__
31 #include <sys/utsname.h>
32 #endif
33 #ifdef HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36 #include "blkidP.h"
37 #include "uuid/uuid.h"
38 #include "probe.h"
39
figure_label_len(const unsigned char * label,int len)40 static int figure_label_len(const unsigned char *label, int len)
41 {
42 const unsigned char *end = label + len - 1;
43
44 while (end >= label && (*end == ' ' || *end == 0))
45 --end;
46 if (end >= label)
47 return end - label + 1;
48 return 0;
49 }
50
get_buffer(struct blkid_probe * pr,blkid_loff_t off,size_t len)51 static unsigned char *get_buffer(struct blkid_probe *pr,
52 blkid_loff_t off, size_t len)
53 {
54 ssize_t ret_read;
55 unsigned char *newbuf;
56
57 if (off + len <= SB_BUFFER_SIZE) {
58 if (!pr->sbbuf) {
59 pr->sbbuf = malloc(SB_BUFFER_SIZE);
60 if (!pr->sbbuf)
61 return NULL;
62 if (lseek(pr->fd, 0, SEEK_SET) < 0)
63 return NULL;
64 ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE);
65 if (ret_read < 0)
66 ret_read = 0;
67 pr->sb_valid = ret_read;
68 }
69 if (off+len > pr->sb_valid)
70 return NULL;
71 return pr->sbbuf + off;
72 } else {
73 if (len > pr->buf_max) {
74 newbuf = realloc(pr->buf, len);
75 if (newbuf == NULL)
76 return NULL;
77 pr->buf = newbuf;
78 pr->buf_max = len;
79 }
80 if (blkid_llseek(pr->fd, off, SEEK_SET) < 0)
81 return NULL;
82 ret_read = read(pr->fd, pr->buf, len);
83 if (ret_read != (ssize_t) len)
84 return NULL;
85 return pr->buf;
86 }
87 }
88
89
90 /*
91 * This is a special case code to check for an MDRAID device. We do
92 * this special since it requires checking for a superblock at the end
93 * of the device.
94 */
check_mdraid(int fd,unsigned char * ret_uuid)95 static int check_mdraid(int fd, unsigned char *ret_uuid)
96 {
97 struct mdp_superblock_s *md;
98 blkid_loff_t offset;
99 char buf[4096];
100
101 if (fd < 0)
102 return -BLKID_ERR_PARAM;
103
104 offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
105
106 if (blkid_llseek(fd, offset, 0) < 0 ||
107 read(fd, buf, 4096) != 4096)
108 return -BLKID_ERR_IO;
109 /* Check for magic number */
110 if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4))
111 return -BLKID_ERR_PARAM;
112
113 if (!ret_uuid)
114 return 0;
115 *ret_uuid = 0;
116
117 /* The MD UUID is not contiguous in the superblock, make it so */
118 md = (struct mdp_superblock_s *)buf;
119 if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
120 memcpy(ret_uuid, &md->set_uuid0, 4);
121 memcpy(ret_uuid + 4, &md->set_uuid1, 12);
122 }
123 return 0;
124 }
125
set_uuid(blkid_dev dev,uuid_t uuid,const char * tag)126 static void set_uuid(blkid_dev dev, uuid_t uuid, const char *tag)
127 {
128 char str[37];
129
130 if (!uuid_is_null(uuid)) {
131 uuid_unparse(uuid, str);
132 blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
133 }
134 }
135
get_ext2_info(blkid_dev dev,struct blkid_magic * id,unsigned char * buf)136 static void get_ext2_info(blkid_dev dev, struct blkid_magic *id,
137 unsigned char *buf)
138 {
139 struct ext2_super_block *es = (struct ext2_super_block *) buf;
140 const char *label = 0;
141
142 DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
143 blkid_le32(es->s_feature_compat),
144 blkid_le32(es->s_feature_incompat),
145 blkid_le32(es->s_feature_ro_compat)));
146
147 if (strlen(es->s_volume_name))
148 label = es->s_volume_name;
149 blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
150
151 set_uuid(dev, es->s_uuid, 0);
152
153 if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
154 !uuid_is_null(es->s_journal_uuid))
155 set_uuid(dev, es->s_journal_uuid, "EXT_JOURNAL");
156
157 if (strcmp(id->bim_type, "ext2") &&
158 ((blkid_le32(es->s_feature_incompat) &
159 EXT2_FEATURE_INCOMPAT_UNSUPPORTED) == 0))
160 blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2"));
161 }
162
163 /*
164 * Check to see if a filesystem is in /proc/filesystems.
165 * Returns 1 if found, 0 if not
166 */
fs_proc_check(const char * fs_name)167 static int fs_proc_check(const char *fs_name)
168 {
169 FILE *f;
170 char buf[80], *cp, *t;
171
172 f = fopen("/proc/filesystems", "r");
173 if (!f)
174 return (0);
175 while (!feof(f)) {
176 if (!fgets(buf, sizeof(buf), f))
177 break;
178 cp = buf;
179 if (!isspace(*cp)) {
180 while (*cp && !isspace(*cp))
181 cp++;
182 }
183 while (*cp && isspace(*cp))
184 cp++;
185 if ((t = strchr(cp, '\n')) != NULL)
186 *t = 0;
187 if ((t = strchr(cp, '\t')) != NULL)
188 *t = 0;
189 if ((t = strchr(cp, ' ')) != NULL)
190 *t = 0;
191 if (!strcmp(fs_name, cp)) {
192 fclose(f);
193 return (1);
194 }
195 }
196 fclose(f);
197 return (0);
198 }
199
200 /*
201 * Check to see if a filesystem is available as a module
202 * Returns 1 if found, 0 if not
203 */
check_for_modules(const char * fs_name)204 static int check_for_modules(const char *fs_name)
205 {
206 #ifdef __linux__
207 struct utsname uts;
208 FILE *f;
209 char buf[1024], *cp;
210 int namesz;
211
212 if (uname(&uts))
213 return (0);
214 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
215
216 f = fopen(buf, "r");
217 if (!f)
218 return (0);
219
220 namesz = strlen(fs_name);
221
222 while (!feof(f)) {
223 if (!fgets(buf, sizeof(buf), f))
224 break;
225 if ((cp = strchr(buf, ':')) != NULL)
226 *cp = 0;
227 else
228 continue;
229 if ((cp = strrchr(buf, '/')) != NULL)
230 cp++;
231 else
232 cp = buf;
233 if (!strncmp(cp, fs_name, namesz) &&
234 (!strcmp(cp + namesz, ".ko") ||
235 !strcmp(cp + namesz, ".ko.gz"))) {
236 fclose(f);
237 return (1);
238 }
239 }
240 fclose(f);
241 #endif
242 return (0);
243 }
244
linux_version_code(void)245 static int linux_version_code(void)
246 {
247 #ifdef __linux__
248 struct utsname ut;
249 static int version_code = -1;
250 int major, minor, rev;
251 char *endptr;
252 const char *cp;
253
254 if (version_code > 0)
255 return version_code;
256
257 if (uname(&ut))
258 return 0;
259 cp = ut.release;
260
261 major = strtol(cp, &endptr, 10);
262 if (cp == endptr || *endptr != '.')
263 return 0;
264 cp = endptr + 1;
265 minor = strtol(cp, &endptr, 10);
266 if (cp == endptr || *endptr != '.')
267 return 0;
268 cp = endptr + 1;
269 rev = strtol(cp, &endptr, 10);
270 if (cp == endptr)
271 return 0;
272 version_code = (((major * 256) + minor) * 256) + rev;
273 return version_code;
274 #else
275 return 0;
276 #endif
277 }
278
279 #define EXT4_SUPPORTS_EXT2 (2 * 65536 + 6*256 + 29)
280
system_supports_ext2(void)281 static int system_supports_ext2(void)
282 {
283 static time_t last_check = 0;
284 static int ret = -1;
285 time_t now = time(0);
286
287 if (ret != -1 || (now - last_check) < 5)
288 return ret;
289 last_check = now;
290 ret = (fs_proc_check("ext2") || check_for_modules("ext2"));
291 return ret;
292 }
293
system_supports_ext4(void)294 static int system_supports_ext4(void)
295 {
296 static time_t last_check = 0;
297 static int ret = -1;
298 time_t now = time(0);
299
300 if (ret != -1 || (now - last_check) < 5)
301 return ret;
302 last_check = now;
303 ret = (fs_proc_check("ext4") || check_for_modules("ext4"));
304 return ret;
305 }
306
system_supports_ext4dev(void)307 static int system_supports_ext4dev(void)
308 {
309 static time_t last_check = 0;
310 static int ret = -1;
311 time_t now = time(0);
312
313 if (ret != -1 || (now - last_check) < 5)
314 return ret;
315 last_check = now;
316 ret = (fs_proc_check("ext4dev") || check_for_modules("ext4dev"));
317 return ret;
318 }
319
probe_ext4dev(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)320 static int probe_ext4dev(struct blkid_probe *probe,
321 struct blkid_magic *id,
322 unsigned char *buf)
323 {
324 struct ext2_super_block *es;
325 es = (struct ext2_super_block *)buf;
326
327 /* Distinguish from jbd */
328 if (blkid_le32(es->s_feature_incompat) &
329 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
330 return -BLKID_ERR_PARAM;
331
332 /*
333 * If the filesystem does not have a journal and ext2 and ext4
334 * is not present, then force this to be detected as an
335 * ext4dev filesystem.
336 */
337 if (!(blkid_le32(es->s_feature_compat) &
338 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
339 !system_supports_ext2() && !system_supports_ext4() &&
340 system_supports_ext4dev() &&
341 linux_version_code() >= EXT4_SUPPORTS_EXT2)
342 goto force_ext4dev;
343
344 /*
345 * If the filesystem is marked as OK for use by in-development
346 * filesystem code, but ext4dev is not supported, and ext4 is,
347 * then don't call ourselves ext4dev, since we should be
348 * detected as ext4 in that case.
349 *
350 * If the filesystem is marked as in use by production
351 * filesystem, then it can only be used by ext4 and NOT by
352 * ext4dev, so always disclaim we are ext4dev in that case.
353 */
354 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
355 if (!system_supports_ext4dev() && system_supports_ext4())
356 return -BLKID_ERR_PARAM;
357 } else
358 return -BLKID_ERR_PARAM;
359
360 force_ext4dev:
361 get_ext2_info(probe->dev, id, buf);
362 return 0;
363 }
364
probe_ext4(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)365 static int probe_ext4(struct blkid_probe *probe, struct blkid_magic *id,
366 unsigned char *buf)
367 {
368 struct ext2_super_block *es;
369 es = (struct ext2_super_block *)buf;
370
371 /* Distinguish from jbd */
372 if (blkid_le32(es->s_feature_incompat) &
373 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
374 return -BLKID_ERR_PARAM;
375
376 /*
377 * If the filesystem does not have a journal and ext2 is not
378 * present, then force this to be detected as an ext2
379 * filesystem.
380 */
381 if (!(blkid_le32(es->s_feature_compat) &
382 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
383 !system_supports_ext2() && system_supports_ext4() &&
384 linux_version_code() >= EXT4_SUPPORTS_EXT2)
385 goto force_ext4;
386
387 /* Ext4 has at least one feature which ext3 doesn't understand */
388 if (!(blkid_le32(es->s_feature_ro_compat) &
389 EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) &&
390 !(blkid_le32(es->s_feature_incompat) &
391 EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
392 return -BLKID_ERR_PARAM;
393
394 force_ext4:
395 /*
396 * If the filesystem is a OK for use by in-development
397 * filesystem code, and ext4dev is supported or ext4 is not
398 * supported, then don't call ourselves ext4, so we can redo
399 * the detection and mark the filesystem as ext4dev.
400 *
401 * If the filesystem is marked as in use by production
402 * filesystem, then it can only be used by ext4 and NOT by
403 * ext4dev.
404 */
405 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
406 if (system_supports_ext4dev() || !system_supports_ext4())
407 return -BLKID_ERR_PARAM;
408 }
409 get_ext2_info(probe->dev, id, buf);
410 return 0;
411 }
412
probe_ext3(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)413 static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id,
414 unsigned char *buf)
415 {
416 struct ext2_super_block *es;
417 es = (struct ext2_super_block *)buf;
418
419 /* ext3 requires journal */
420 if (!(blkid_le32(es->s_feature_compat) &
421 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
422 return -BLKID_ERR_PARAM;
423
424 /* Any features which ext3 doesn't understand */
425 if ((blkid_le32(es->s_feature_ro_compat) &
426 EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) ||
427 (blkid_le32(es->s_feature_incompat) &
428 EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
429 return -BLKID_ERR_PARAM;
430
431 get_ext2_info(probe->dev, id, buf);
432 return 0;
433 }
434
probe_ext2(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)435 static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id,
436 unsigned char *buf)
437 {
438 struct ext2_super_block *es;
439
440 es = (struct ext2_super_block *)buf;
441
442 /* Distinguish between ext3 and ext2 */
443 if ((blkid_le32(es->s_feature_compat) &
444 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
445 return -BLKID_ERR_PARAM;
446
447 /* Any features which ext2 doesn't understand */
448 if ((blkid_le32(es->s_feature_ro_compat) &
449 EXT2_FEATURE_RO_COMPAT_UNSUPPORTED) ||
450 (blkid_le32(es->s_feature_incompat) &
451 EXT2_FEATURE_INCOMPAT_UNSUPPORTED))
452 return -BLKID_ERR_PARAM;
453
454 /*
455 * If ext2 is not present, but ext4 or ext4dev are, then
456 * disclaim we are ext2
457 */
458 if (!system_supports_ext2() &&
459 (system_supports_ext4() || system_supports_ext4dev()) &&
460 linux_version_code() >= EXT4_SUPPORTS_EXT2)
461 return -BLKID_ERR_PARAM;
462
463 get_ext2_info(probe->dev, id, buf);
464 return 0;
465 }
466
probe_jbd(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)467 static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id,
468 unsigned char *buf)
469 {
470 struct ext2_super_block *es = (struct ext2_super_block *) buf;
471
472 if (!(blkid_le32(es->s_feature_incompat) &
473 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
474 return -BLKID_ERR_PARAM;
475
476 get_ext2_info(probe->dev, id, buf);
477
478 return 0;
479 }
480
481 #define FAT_ATTR_VOLUME_ID 0x08
482 #define FAT_ATTR_DIR 0x10
483 #define FAT_ATTR_LONG_NAME 0x0f
484 #define FAT_ATTR_MASK 0x3f
485 #define FAT_ENTRY_FREE 0xe5
486
487 static const char *no_name = "NO NAME ";
488
search_fat_label(struct vfat_dir_entry * dir,int count)489 static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
490 {
491 int i;
492
493 for (i = 0; i < count; i++) {
494 if (dir[i].name[0] == 0x00)
495 break;
496
497 if ((dir[i].name[0] == FAT_ENTRY_FREE) ||
498 (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) ||
499 ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
500 continue;
501
502 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
503 FAT_ATTR_VOLUME_ID) {
504 return dir[i].name;
505 }
506 }
507 return 0;
508 }
509
510 /* FAT label extraction from the root directory taken from Kay
511 * Sievers's volume_id library */
probe_fat(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)512 static int probe_fat(struct blkid_probe *probe,
513 struct blkid_magic *id __BLKID_ATTR((unused)),
514 unsigned char *buf)
515 {
516 struct vfat_super_block *vs = (struct vfat_super_block *) buf;
517 struct msdos_super_block *ms = (struct msdos_super_block *) buf;
518 struct vfat_dir_entry *dir;
519 char serno[10];
520 const unsigned char *label = 0, *vol_label = 0, *tmp;
521 unsigned char *vol_serno;
522 int label_len = 0, maxloop = 100;
523 __u16 sector_size, dir_entries, reserved;
524 __u32 sect_count, fat_size, dir_size, cluster_count, fat_length;
525 __u32 buf_size, start_data_sect, next, root_start, root_dir_entries;
526
527 /* sector size check */
528 tmp = (unsigned char *)&ms->ms_sector_size;
529 sector_size = tmp[0] + (tmp[1] << 8);
530 if (sector_size != 0x200 && sector_size != 0x400 &&
531 sector_size != 0x800 && sector_size != 0x1000)
532 return 1;
533
534 tmp = (unsigned char *)&ms->ms_dir_entries;
535 dir_entries = tmp[0] + (tmp[1] << 8);
536 reserved = blkid_le16(ms->ms_reserved);
537 tmp = (unsigned char *)&ms->ms_sectors;
538 sect_count = tmp[0] + (tmp[1] << 8);
539 if (sect_count == 0)
540 sect_count = blkid_le32(ms->ms_total_sect);
541
542 fat_length = blkid_le16(ms->ms_fat_length);
543 if (fat_length == 0)
544 fat_length = blkid_le32(vs->vs_fat32_length);
545
546 fat_size = fat_length * ms->ms_fats;
547 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
548 (sector_size-1)) / sector_size;
549
550 cluster_count = sect_count - (reserved + fat_size + dir_size);
551 if (ms->ms_cluster_size == 0)
552 return 1;
553 cluster_count /= ms->ms_cluster_size;
554
555 if (cluster_count > FAT32_MAX)
556 return 1;
557
558 if (ms->ms_fat_length) {
559 /* the label may be an attribute in the root directory */
560 root_start = (reserved + fat_size) * sector_size;
561 root_dir_entries = vs->vs_dir_entries[0] +
562 (vs->vs_dir_entries[1] << 8);
563
564 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
565 dir = (struct vfat_dir_entry *) get_buffer(probe, root_start,
566 buf_size);
567 if (dir)
568 vol_label = search_fat_label(dir, root_dir_entries);
569
570 if (!vol_label || !memcmp(vol_label, no_name, 11))
571 vol_label = ms->ms_label;
572 vol_serno = ms->ms_serno;
573
574 blkid_set_tag(probe->dev, "SEC_TYPE", "msdos",
575 sizeof("msdos"));
576 } else {
577 /* Search the FAT32 root dir for the label attribute */
578 buf_size = vs->vs_cluster_size * sector_size;
579 start_data_sect = reserved + fat_size;
580
581 next = blkid_le32(vs->vs_root_cluster);
582 while (next && --maxloop) {
583 __u32 next_sect_off;
584 __u64 next_off, fat_entry_off;
585 int count;
586
587 next_sect_off = (next - 2) * vs->vs_cluster_size;
588 next_off = (__u64) (start_data_sect + next_sect_off) *
589 sector_size;
590
591 dir = (struct vfat_dir_entry *)
592 get_buffer(probe, next_off, buf_size);
593 if (dir == NULL)
594 break;
595
596 count = buf_size / sizeof(struct vfat_dir_entry);
597
598 vol_label = search_fat_label(dir, count);
599 if (vol_label)
600 break;
601
602 /* get FAT entry */
603 fat_entry_off =
604 ((unsigned int) reserved *
605 (unsigned int) 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@redhat.com>]
640 */
probe_fat_nomagic(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)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
probe_ntfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)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
probe_xfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)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
probe_reiserfs(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)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
probe_reiserfs4(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)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
probe_jfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)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) != (1U << blkid_le16(js->js_l2bsize)))
860 return 1;
861
862 if (blkid_le32(js->js_pbsize) != (1U << 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
probe_zfs(struct blkid_probe * probe __BLKID_ATTR ((unused)),struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf __BLKID_ATTR ((unused)))876 static int probe_zfs(struct blkid_probe *probe __BLKID_ATTR((unused)),
877 struct blkid_magic *id __BLKID_ATTR((unused)),
878 unsigned char *buf __BLKID_ATTR((unused)))
879 {
880 #if 0
881 char *vdev_label;
882 const char *pool_name = 0;
883
884 /* read nvpair data for pool name, pool GUID (complex) */
885 blkid_set_tag(probe->dev, "LABEL", pool_name, sizeof(pool_name));
886 set_uuid(probe->dev, pool_guid, 0);
887 #endif
888 return 0;
889 }
890
probe_luks(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)891 static int probe_luks(struct blkid_probe *probe,
892 struct blkid_magic *id __BLKID_ATTR((unused)),
893 unsigned char *buf)
894 {
895 char uuid[41];
896
897 /* 168 is the offset to the 40 character uuid:
898 * http://luks.endorphin.org/LUKS-on-disk-format.pdf */
899 strncpy(uuid, (char *) buf+168, 40);
900 uuid[40] = 0;
901 blkid_set_tag(probe->dev, "UUID", uuid, 40);
902 return 0;
903 }
904
probe_romfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)905 static int probe_romfs(struct blkid_probe *probe,
906 struct blkid_magic *id __BLKID_ATTR((unused)),
907 unsigned char *buf)
908 {
909 struct romfs_super_block *ros;
910 const char *label = 0;
911
912 ros = (struct romfs_super_block *)buf;
913
914 if (strlen((char *) ros->ros_volume))
915 label = (char *) ros->ros_volume;
916 blkid_set_tag(probe->dev, "LABEL", label, 0);
917 return 0;
918 }
919
probe_cramfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)920 static int probe_cramfs(struct blkid_probe *probe,
921 struct blkid_magic *id __BLKID_ATTR((unused)),
922 unsigned char *buf)
923 {
924 struct cramfs_super_block *csb;
925 const char *label = 0;
926
927 csb = (struct cramfs_super_block *)buf;
928
929 if (strlen((char *) csb->name))
930 label = (char *) csb->name;
931 blkid_set_tag(probe->dev, "LABEL", label, 0);
932 return 0;
933 }
934
probe_swap0(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf __BLKID_ATTR ((unused)))935 static int probe_swap0(struct blkid_probe *probe,
936 struct blkid_magic *id __BLKID_ATTR((unused)),
937 unsigned char *buf __BLKID_ATTR((unused)))
938 {
939 blkid_set_tag(probe->dev, "UUID", 0, 0);
940 blkid_set_tag(probe->dev, "LABEL", 0, 0);
941 return 0;
942 }
943
probe_swap1(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf __BLKID_ATTR ((unused)))944 static int probe_swap1(struct blkid_probe *probe,
945 struct blkid_magic *id,
946 unsigned char *buf __BLKID_ATTR((unused)))
947 {
948 struct swap_id_block *sws;
949
950 probe_swap0(probe, id, buf);
951 /*
952 * Version 1 swap headers are always located at offset of 1024
953 * bytes, although the swap signature itself is located at the
954 * end of the page (which may vary depending on hardware
955 * pagesize).
956 */
957 sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024);
958 if (!sws)
959 return 1;
960
961 /* check for wrong version or zeroed pagecount, for sanity */
962 if (!memcmp(id->bim_magic, "SWAPSPACE2", id->bim_len) &&
963 (sws->sws_version != 1 || sws->sws_lastpage == 0))
964 return 1;
965
966 /* arbitrary sanity check.. is there any garbage down there? */
967 if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) {
968 if (sws->sws_volume[0])
969 blkid_set_tag(probe->dev, "LABEL", sws->sws_volume,
970 sizeof(sws->sws_volume));
971 if (sws->sws_uuid[0])
972 set_uuid(probe->dev, sws->sws_uuid, 0);
973 }
974 return 0;
975 }
976
probe_iso9660(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)977 static int probe_iso9660(struct blkid_probe *probe,
978 struct blkid_magic *id __BLKID_ATTR((unused)),
979 unsigned char *buf)
980 {
981 struct iso_volume_descriptor *iso;
982 const unsigned char *label;
983
984 iso = (struct iso_volume_descriptor *) buf;
985 label = iso->volume_id;
986
987 blkid_set_tag(probe->dev, "LABEL", (const char *) label,
988 figure_label_len(label, 32));
989 return 0;
990 }
991
992
993 static const char
994 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
995 "NSR03", "TEA01", 0 };
996
probe_udf(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf __BLKID_ATTR ((unused)))997 static int probe_udf(struct blkid_probe *probe,
998 struct blkid_magic *id __BLKID_ATTR((unused)),
999 unsigned char *buf __BLKID_ATTR((unused)))
1000 {
1001 int j, bs;
1002 struct iso_volume_descriptor *isosb;
1003 const char ** m;
1004
1005 /* determine the block size by scanning in 2K increments
1006 (block sizes larger than 2K will be null padded) */
1007 for (bs = 1; bs < 16; bs++) {
1008 isosb = (struct iso_volume_descriptor *)
1009 get_buffer(probe, (blkid_loff_t) bs*2048+32768,
1010 sizeof(*isosb));
1011 if (!isosb)
1012 return 1;
1013 if (isosb->vd_id[0])
1014 break;
1015 }
1016
1017 /* Scan up to another 64 blocks looking for additional VSD's */
1018 for (j = 1; j < 64; j++) {
1019 if (j > 1) {
1020 isosb = (struct iso_volume_descriptor *)
1021 get_buffer(probe, j*bs*2048+32768,
1022 sizeof(*isosb));
1023 if (!isosb)
1024 return 1;
1025 }
1026 /* If we find NSR0x then call it udf:
1027 NSR01 for UDF 1.00
1028 NSR02 for UDF 1.50
1029 NSR03 for UDF 2.00 */
1030 if (!memcmp(isosb->vd_id, "NSR0", 4))
1031 return probe_iso9660(probe, id, buf);
1032 for (m = udf_magic; *m; m++)
1033 if (!memcmp(*m, isosb->vd_id, 5))
1034 break;
1035 if (*m == 0)
1036 return 1;
1037 }
1038 return 1;
1039 }
1040
probe_ocfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1041 static int probe_ocfs(struct blkid_probe *probe,
1042 struct blkid_magic *id __BLKID_ATTR((unused)),
1043 unsigned char *buf)
1044 {
1045 struct ocfs_volume_header ovh;
1046 struct ocfs_volume_label ovl;
1047 __u32 major;
1048
1049 memcpy(&ovh, buf, sizeof(ovh));
1050 memcpy(&ovl, buf+512, sizeof(ovl));
1051
1052 major = ocfsmajor(ovh);
1053 if (major == 1)
1054 blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
1055 else if (major >= 9)
1056 blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
1057
1058 blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl));
1059 blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
1060 set_uuid(probe->dev, ovl.vol_id, 0);
1061 return 0;
1062 }
1063
probe_ocfs2(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1064 static int probe_ocfs2(struct blkid_probe *probe,
1065 struct blkid_magic *id __BLKID_ATTR((unused)),
1066 unsigned char *buf)
1067 {
1068 struct ocfs2_super_block *osb;
1069
1070 osb = (struct ocfs2_super_block *)buf;
1071
1072 blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
1073 set_uuid(probe->dev, osb->s_uuid, 0);
1074 return 0;
1075 }
1076
probe_oracleasm(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1077 static int probe_oracleasm(struct blkid_probe *probe,
1078 struct blkid_magic *id __BLKID_ATTR((unused)),
1079 unsigned char *buf)
1080 {
1081 struct oracle_asm_disk_label *dl;
1082
1083 dl = (struct oracle_asm_disk_label *)buf;
1084
1085 blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
1086 return 0;
1087 }
1088
probe_gfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1089 static int probe_gfs(struct blkid_probe *probe,
1090 struct blkid_magic *id __BLKID_ATTR((unused)),
1091 unsigned char *buf)
1092 {
1093 struct gfs2_sb *sbd;
1094 const char *label = 0;
1095
1096 sbd = (struct gfs2_sb *)buf;
1097
1098 if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS &&
1099 blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI)
1100 {
1101 blkid_set_tag(probe->dev, "UUID", 0, 0);
1102
1103 if (strlen(sbd->sb_locktable))
1104 label = sbd->sb_locktable;
1105 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1106 return 0;
1107 }
1108 return 1;
1109 }
1110
probe_gfs2(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1111 static int probe_gfs2(struct blkid_probe *probe,
1112 struct blkid_magic *id __BLKID_ATTR((unused)),
1113 unsigned char *buf)
1114 {
1115 struct gfs2_sb *sbd;
1116 const char *label = 0;
1117
1118 sbd = (struct gfs2_sb *)buf;
1119
1120 if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS &&
1121 blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI)
1122 {
1123 blkid_set_tag(probe->dev, "UUID", 0, 0);
1124
1125 if (strlen(sbd->sb_locktable))
1126 label = sbd->sb_locktable;
1127 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1128 return 0;
1129 }
1130 return 1;
1131 }
1132
unicode_16be_to_utf8(unsigned char * str,int out_len,const unsigned char * buf,int in_len)1133 static void unicode_16be_to_utf8(unsigned char *str, int out_len,
1134 const unsigned char *buf, int in_len)
1135 {
1136 int i, j;
1137 unsigned int c;
1138
1139 for (i = j = 0; i + 2 <= in_len; i += 2) {
1140 c = (buf[i] << 8) | buf[i+1];
1141 if (c == 0) {
1142 str[j] = '\0';
1143 break;
1144 } else if (c < 0x80) {
1145 if (j+1 >= out_len)
1146 break;
1147 str[j++] = (unsigned char) c;
1148 } else if (c < 0x800) {
1149 if (j+2 >= out_len)
1150 break;
1151 str[j++] = (unsigned char) (0xc0 | (c >> 6));
1152 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1153 } else {
1154 if (j+3 >= out_len)
1155 break;
1156 str[j++] = (unsigned char) (0xe0 | (c >> 12));
1157 str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f));
1158 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1159 }
1160 }
1161 str[j] = '\0';
1162 }
1163
unicode_16le_to_utf8(unsigned char * str,int out_len,const unsigned char * buf,int in_len)1164 static void unicode_16le_to_utf8(unsigned char *str, int out_len,
1165 const unsigned char *buf, int in_len)
1166 {
1167 int i, j;
1168 unsigned int c;
1169
1170 for (i = j = 0; i + 2 <= in_len; i += 2) {
1171 c = (buf[i+1] << 8) | buf[i];
1172 if (c == 0) {
1173 str[j] = '\0';
1174 break;
1175 } else if (c < 0x80) {
1176 if (j+1 >= out_len)
1177 break;
1178 str[j++] = (unsigned char) c;
1179 } else if (c < 0x800) {
1180 if (j+2 >= out_len)
1181 break;
1182 str[j++] = (unsigned char) (0xc0 | (c >> 6));
1183 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1184 } else {
1185 if (j+3 >= out_len)
1186 break;
1187 str[j++] = (unsigned char) (0xe0 | (c >> 12));
1188 str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f));
1189 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1190 }
1191 }
1192 str[j] = '\0';
1193 }
1194
probe_hfs(struct blkid_probe * probe __BLKID_ATTR ((unused)),struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1195 static int probe_hfs(struct blkid_probe *probe __BLKID_ATTR((unused)),
1196 struct blkid_magic *id __BLKID_ATTR((unused)),
1197 unsigned char *buf)
1198 {
1199 struct hfs_mdb *hfs = (struct hfs_mdb *)buf;
1200 unsigned long long *uuid_ptr;
1201 char uuid_str[17];
1202 __u64 uuid;
1203
1204 if ((memcmp(hfs->embed_sig, "H+", 2) == 0) ||
1205 (memcmp(hfs->embed_sig, "HX", 2) == 0))
1206 return 1; /* Not hfs, but an embedded HFS+ */
1207
1208 uuid_ptr = (unsigned long long *)hfs->finder_info.id;
1209 uuid = blkid_le64(*uuid_ptr);
1210 if (uuid) {
1211 sprintf(uuid_str, "%016llX", uuid);
1212 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1213 }
1214 blkid_set_tag(probe->dev, "LABEL", (char *)hfs->label, hfs->label_len);
1215 return 0;
1216 }
1217
1218
1219 #define HFSPLUS_SECTOR_SIZE 512
1220
probe_hfsplus(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)1221 static int probe_hfsplus(struct blkid_probe *probe,
1222 struct blkid_magic *id,
1223 unsigned char *buf)
1224 {
1225 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
1226 struct hfsplus_bnode_descriptor *descr;
1227 struct hfsplus_bheader_record *bnode;
1228 struct hfsplus_catalog_key *key;
1229 struct hfsplus_vol_header *hfsplus;
1230 struct hfs_mdb *sbd = (struct hfs_mdb *) buf;
1231 unsigned int alloc_block_size;
1232 unsigned int alloc_first_block;
1233 unsigned int embed_first_block;
1234 unsigned int off = 0;
1235 unsigned int blocksize;
1236 unsigned int cat_block;
1237 unsigned int ext_block_start;
1238 unsigned int ext_block_count;
1239 unsigned int record_count;
1240 unsigned int leaf_node_head;
1241 unsigned int leaf_node_count;
1242 unsigned int leaf_node_size;
1243 unsigned int leaf_block;
1244 unsigned int label_len;
1245 unsigned long long *uuid_ptr;
1246 __u64 leaf_off, uuid;
1247 char uuid_str[17], label[512];
1248 int ext;
1249
1250 /* Check for a HFS+ volume embedded in a HFS volume */
1251 if (memcmp(sbd->signature, "BD", 2) == 0) {
1252 if ((memcmp(sbd->embed_sig, "H+", 2) != 0) &&
1253 (memcmp(sbd->embed_sig, "HX", 2) != 0))
1254 /* This must be an HFS volume, so fail */
1255 return 1;
1256
1257 alloc_block_size = blkid_be32(sbd->al_blk_size);
1258 alloc_first_block = blkid_be16(sbd->al_bl_st);
1259 embed_first_block = blkid_be16(sbd->embed_startblock);
1260 off = (alloc_first_block * 512) +
1261 (embed_first_block * alloc_block_size);
1262 buf = get_buffer(probe, off + (id->bim_kboff * 1024),
1263 sizeof(*sbd));
1264 if (!buf)
1265 return 1;
1266
1267 hfsplus = (struct hfsplus_vol_header *) buf;
1268 }
1269
1270 hfsplus = (struct hfsplus_vol_header *) buf;
1271
1272 if ((memcmp(hfsplus->signature, "H+", 2) != 0) &&
1273 (memcmp(hfsplus->signature, "HX", 2) != 0))
1274 return 1;
1275
1276 uuid_ptr = (unsigned long long *)hfsplus->finder_info.id;
1277 uuid = blkid_le64(*uuid_ptr);
1278 if (uuid) {
1279 sprintf(uuid_str, "%016llX", uuid);
1280 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1281 }
1282
1283 blocksize = blkid_be32(hfsplus->blocksize);
1284 if (blocksize < HFSPLUS_SECTOR_SIZE)
1285 return 1;
1286
1287 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
1288 cat_block = blkid_be32(extents[0].start_block);
1289
1290 buf = get_buffer(probe, off + ((__u64) cat_block * blocksize), 0x2000);
1291 if (!buf)
1292 return 0;
1293
1294 bnode = (struct hfsplus_bheader_record *)
1295 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1296
1297 leaf_node_head = blkid_be32(bnode->leaf_head);
1298 leaf_node_size = blkid_be16(bnode->node_size);
1299 leaf_node_count = blkid_be32(bnode->leaf_count);
1300 if (leaf_node_count == 0)
1301 return 0;
1302
1303 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
1304
1305 /* get physical location */
1306 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
1307 ext_block_start = blkid_be32(extents[ext].start_block);
1308 ext_block_count = blkid_be32(extents[ext].block_count);
1309 if (ext_block_count == 0)
1310 return 0;
1311
1312 /* this is our extent */
1313 if (leaf_block < ext_block_count)
1314 break;
1315
1316 leaf_block -= ext_block_count;
1317 }
1318 if (ext == HFSPLUS_EXTENT_COUNT)
1319 return 0;
1320
1321 leaf_off = (__u64) (ext_block_start + leaf_block) * blocksize;
1322
1323 buf = get_buffer(probe, off + leaf_off, leaf_node_size);
1324 if (!buf)
1325 return 0;
1326
1327 descr = (struct hfsplus_bnode_descriptor *) buf;
1328 record_count = blkid_be16(descr->num_recs);
1329 if (record_count == 0)
1330 return 0;
1331
1332 if (descr->type != HFS_NODE_LEAF)
1333 return 0;
1334
1335 key = (struct hfsplus_catalog_key *)
1336 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1337
1338 if (blkid_be32(key->parent_id) != HFSPLUS_POR_CNID)
1339 return 0;
1340
1341 label_len = blkid_be16(key->unicode_len) * 2;
1342 unicode_16be_to_utf8((unsigned char *)label, sizeof(label),
1343 key->unicode, label_len);
1344 blkid_set_tag(probe->dev, "LABEL", label, 0);
1345 return 0;
1346 }
1347
1348 #define LVM2_LABEL_SIZE 512
lvm2_calc_crc(const void * buf,unsigned int size)1349 static unsigned int lvm2_calc_crc(const void *buf, unsigned int size)
1350 {
1351 static const unsigned int crctab[] = {
1352 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1353 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1354 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1355 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1356 };
1357 unsigned int i, crc = 0xf597a6cf;
1358 const __u8 *data = (const __u8 *) buf;
1359
1360 for (i = 0; i < size; i++) {
1361 crc ^= *data++;
1362 crc = (crc >> 4) ^ crctab[crc & 0xf];
1363 crc = (crc >> 4) ^ crctab[crc & 0xf];
1364 }
1365 return crc;
1366 }
1367
probe_lvm2(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)1368 static int probe_lvm2(struct blkid_probe *probe,
1369 struct blkid_magic *id,
1370 unsigned char *buf)
1371 {
1372 int sector = (id->bim_kboff) << 1;
1373 struct lvm2_pv_label_header *label= (struct lvm2_pv_label_header *)buf;
1374 char *p, *q, uuid[40];
1375 unsigned int i, b;
1376
1377 /* buf is at 0k or 1k offset; find label inside */
1378 if (memcmp(buf, "LABELONE", 8) == 0) {
1379 label = (struct lvm2_pv_label_header *)buf;
1380 } else if (memcmp(buf + 512, "LABELONE", 8) == 0) {
1381 label = (struct lvm2_pv_label_header *)(buf + 512);
1382 sector++;
1383 } else {
1384 return 1;
1385 }
1386
1387 if (blkid_le64(label->sector_xl) != (unsigned) sector) {
1388 DBG(DEBUG_PROBE,
1389 printf("LVM2: label for sector %llu found at sector %d\n",
1390 blkid_le64(label->sector_xl), sector));
1391 return 1;
1392 }
1393
1394 if (lvm2_calc_crc(&label->offset_xl, LVM2_LABEL_SIZE -
1395 ((char *)&label->offset_xl - (char *)label)) !=
1396 blkid_le32(label->crc_xl)) {
1397 DBG(DEBUG_PROBE,
1398 printf("LVM2: label checksum incorrect at sector %d\n",
1399 sector));
1400 return 1;
1401 }
1402
1403 for (i=0, b=1, p=uuid, q= (char *) label->pv_uuid; i < LVM2_ID_LEN;
1404 i++, b <<= 1) {
1405 if (b & 0x4444440)
1406 *p++ = '-';
1407 *p++ = *q++;
1408 }
1409
1410 blkid_set_tag(probe->dev, "UUID", uuid, LVM2_ID_LEN+6);
1411
1412 return 0;
1413 }
1414
probe_btrfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1415 static int probe_btrfs(struct blkid_probe *probe,
1416 struct blkid_magic *id __BLKID_ATTR((unused)),
1417 unsigned char *buf)
1418 {
1419 struct btrfs_super_block *bs;
1420 const char *label = 0;
1421
1422 bs = (struct btrfs_super_block *)buf;
1423
1424 if (strlen(bs->label))
1425 label = bs->label;
1426 blkid_set_tag(probe->dev, "LABEL", label, sizeof(bs->label));
1427 set_uuid(probe->dev, bs->fsid, 0);
1428 return 0;
1429 }
1430
probe_f2fs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1431 static int probe_f2fs(struct blkid_probe *probe,
1432 struct blkid_magic *id __BLKID_ATTR((unused)),
1433 unsigned char *buf)
1434 {
1435 struct f2fs_super_block *bs;
1436
1437 bs = (struct f2fs_super_block *)buf;
1438 set_uuid(probe->dev, bs->uuid, 0);
1439 return 0;
1440 }
1441
exfat_block_to_offset(const struct exfat_super_block * sb,uint64_t block)1442 static uint64_t exfat_block_to_offset(const struct exfat_super_block *sb,
1443 uint64_t block)
1444 {
1445 return block << sb->block_bits;
1446 }
1447
exfat_cluster_to_block(const struct exfat_super_block * sb,uint32_t cluster)1448 static uint64_t exfat_cluster_to_block(const struct exfat_super_block *sb,
1449 uint32_t cluster)
1450 {
1451 return sb->cluster_block_start +
1452 ((uint64_t)(cluster - EXFAT_FIRST_DATA_CLUSTER) << sb->bpc_bits);
1453 }
1454
exfat_cluster_to_offset(const struct exfat_super_block * sb,uint32_t cluster)1455 static uint64_t exfat_cluster_to_offset(const struct exfat_super_block *sb,
1456 uint32_t cluster)
1457 {
1458 return exfat_block_to_offset(sb, exfat_cluster_to_block(sb, cluster));
1459 }
1460
exfat_next_cluster(struct blkid_probe * probe,const struct exfat_super_block * sb,uint32_t cluster)1461 static uint32_t exfat_next_cluster(struct blkid_probe *probe,
1462 const struct exfat_super_block *sb,
1463 uint32_t cluster)
1464 {
1465 uint32_t *next;
1466 uint64_t offset;
1467
1468 offset = exfat_block_to_offset(sb, sb->fat_block_start)
1469 + (uint64_t) cluster * sizeof (cluster);
1470 next = (uint32_t *)get_buffer(probe, offset, sizeof (uint32_t));
1471
1472 return next ? *next : 0;
1473 }
1474
find_exfat_entry_label(struct blkid_probe * probe,const struct exfat_super_block * sb)1475 static struct exfat_entry_label *find_exfat_entry_label(
1476 struct blkid_probe *probe, const struct exfat_super_block *sb)
1477 {
1478 uint32_t cluster = sb->rootdir_cluster;
1479 uint64_t offset = exfat_cluster_to_offset(sb, cluster);
1480 uint8_t *entry;
1481 const size_t max_iter = 10000;
1482 size_t i = 0;
1483
1484 for (; i < max_iter; ++i) {
1485 entry = (uint8_t *)get_buffer(probe, offset, EXFAT_ENTRY_SIZE);
1486 if (!entry)
1487 return NULL;
1488 if (entry[0] == EXFAT_ENTRY_EOD)
1489 return NULL;
1490 if (entry[0] == EXFAT_ENTRY_LABEL)
1491 return (struct exfat_entry_label*) entry;
1492
1493 offset += EXFAT_ENTRY_SIZE;
1494 if (offset % CLUSTER_SIZE(sb) == 0) {
1495 cluster = exfat_next_cluster(probe, sb, cluster);
1496 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
1497 return NULL;
1498 if (cluster > EXFAT_LAST_DATA_CLUSTER)
1499 return NULL;
1500 offset = exfat_cluster_to_offset(sb, cluster);
1501 }
1502 }
1503
1504 return NULL;
1505 }
1506
probe_exfat(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1507 static int probe_exfat(struct blkid_probe *probe,
1508 struct blkid_magic *id __BLKID_ATTR((unused)),
1509 unsigned char *buf)
1510 {
1511 struct exfat_super_block *sb;
1512 struct exfat_entry_label *label;
1513 char uuid[40];
1514
1515 sb = (struct exfat_super_block *)buf;
1516 if (!sb || CLUSTER_SIZE(sb) == 0) {
1517 DBG(DEBUG_PROBE, printf("bad exfat superblock.\n"));
1518 return errno ? - errno : 1;
1519 }
1520
1521 label = find_exfat_entry_label(probe, sb);
1522 if (label) {
1523 unsigned char utf8_label[128];
1524 unicode_16le_to_utf8(utf8_label, sizeof(utf8_label), label->name, label->length * 2);
1525 blkid_set_tag(probe->dev, "LABEL", (char *) utf8_label, 0);
1526 } else {
1527 blkid_set_tag(probe->dev, "LABEL", "disk", 4);
1528 }
1529
1530 memset(uuid, 0, sizeof (uuid));
1531 snprintf(uuid, sizeof (uuid), "%02hhX%02hhX-%02hhX%02hhX",
1532 sb->volume_serial[3], sb->volume_serial[2],
1533 sb->volume_serial[1], sb->volume_serial[0]);
1534 blkid_set_tag(probe->dev, "UUID", uuid, strlen(uuid));
1535
1536 return 0;
1537 }
1538
1539 /*
1540 * Various filesystem magics that we can check for. Note that kboff and
1541 * sboff are in kilobytes and bytes respectively. All magics are in
1542 * byte strings so we don't worry about endian issues.
1543 */
1544 static struct blkid_magic type_array[] = {
1545 /* type kboff sboff len magic probe */
1546 { "oracleasm", 0, 32, 8, "ORCLDISK", probe_oracleasm },
1547 { "ntfs", 0, 3, 8, "NTFS ", probe_ntfs },
1548 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd },
1549 { "ext4dev", 1, 0x38, 2, "\123\357", probe_ext4dev },
1550 { "ext4", 1, 0x38, 2, "\123\357", probe_ext4 },
1551 { "ext3", 1, 0x38, 2, "\123\357", probe_ext3 },
1552 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 },
1553 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs },
1554 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs },
1555 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs },
1556 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs },
1557 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs },
1558 { "reiser4", 64, 0, 7, "ReIsEr4", probe_reiserfs4 },
1559 { "gfs2", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs2 },
1560 { "gfs", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs },
1561 { "vfat", 0, 0x52, 5, "MSWIN", probe_fat },
1562 { "vfat", 0, 0x52, 8, "FAT32 ", probe_fat },
1563 { "vfat", 0, 0x36, 5, "MSDOS", probe_fat },
1564 { "vfat", 0, 0x36, 8, "FAT16 ", probe_fat },
1565 { "vfat", 0, 0x36, 8, "FAT12 ", probe_fat },
1566 { "vfat", 0, 0, 1, "\353", probe_fat_nomagic },
1567 { "vfat", 0, 0, 1, "\351", probe_fat_nomagic },
1568 { "vfat", 0, 0x1fe, 2, "\125\252", probe_fat_nomagic },
1569 { "minix", 1, 0x10, 2, "\177\023", 0 },
1570 { "minix", 1, 0x10, 2, "\217\023", 0 },
1571 { "minix", 1, 0x10, 2, "\150\044", 0 },
1572 { "minix", 1, 0x10, 2, "\170\044", 0 },
1573 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 },
1574 { "xfs", 0, 0, 4, "XFSB", probe_xfs },
1575 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs },
1576 { "bfs", 0, 0, 4, "\316\372\173\033", 0 },
1577 { "cramfs", 0, 0, 4, "E=\315\050", probe_cramfs },
1578 { "qnx4", 0, 4, 6, "QNX4FS", 0 },
1579 { "udf", 32, 1, 5, "BEA01", probe_udf },
1580 { "udf", 32, 1, 5, "BOOT2", probe_udf },
1581 { "udf", 32, 1, 5, "CD001", probe_udf },
1582 { "udf", 32, 1, 5, "CDW02", probe_udf },
1583 { "udf", 32, 1, 5, "NSR02", probe_udf },
1584 { "udf", 32, 1, 5, "NSR03", probe_udf },
1585 { "udf", 32, 1, 5, "TEA01", probe_udf },
1586 { "iso9660", 32, 1, 5, "CD001", probe_iso9660 },
1587 { "iso9660", 32, 9, 5, "CDROM", probe_iso9660 },
1588 { "jfs", 32, 0, 4, "JFS1", probe_jfs },
1589 /* ZFS has 128 root blocks (#4 is the first used), check only 6 of them */
1590 { "zfs", 128, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1591 { "zfs", 128, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1592 { "zfs", 132, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1593 { "zfs", 132, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1594 { "zfs", 136, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1595 { "zfs", 136, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1596 { "zfs", 384, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1597 { "zfs", 384, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1598 { "zfs", 388, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1599 { "zfs", 388, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1600 { "zfs", 392, 0, 8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1601 { "zfs", 392, 0, 8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1602 { "hfsplus", 1, 0, 2, "BD", probe_hfsplus },
1603 { "hfsplus", 1, 0, 2, "H+", probe_hfsplus },
1604 { "hfsplus", 1, 0, 2, "HX", probe_hfsplus },
1605 { "hfs", 1, 0, 2, "BD", probe_hfs },
1606 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 },
1607 { "hpfs", 8, 0, 4, "I\350\225\371", 0 },
1608 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 },
1609 { "swap", 0, 0xff6, 10, "SWAP-SPACE", probe_swap0 },
1610 { "swap", 0, 0xff6, 10, "SWAPSPACE2", probe_swap1 },
1611 { "swsuspend", 0, 0xff6, 9, "S1SUSPEND", probe_swap1 },
1612 { "swsuspend", 0, 0xff6, 9, "S2SUSPEND", probe_swap1 },
1613 { "swsuspend", 0, 0xff6, 9, "ULSUSPEND", probe_swap1 },
1614 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", probe_swap0 },
1615 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", probe_swap1 },
1616 { "swsuspend", 0, 0x1ff6, 9, "S1SUSPEND", probe_swap1 },
1617 { "swsuspend", 0, 0x1ff6, 9, "S2SUSPEND", probe_swap1 },
1618 { "swsuspend", 0, 0x1ff6, 9, "ULSUSPEND", probe_swap1 },
1619 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", probe_swap0 },
1620 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", probe_swap1 },
1621 { "swsuspend", 0, 0x3ff6, 9, "S1SUSPEND", probe_swap1 },
1622 { "swsuspend", 0, 0x3ff6, 9, "S2SUSPEND", probe_swap1 },
1623 { "swsuspend", 0, 0x3ff6, 9, "ULSUSPEND", probe_swap1 },
1624 { "swap", 0, 0x7ff6, 10, "SWAP-SPACE", probe_swap0 },
1625 { "swap", 0, 0x7ff6, 10, "SWAPSPACE2", probe_swap1 },
1626 { "swsuspend", 0, 0x7ff6, 9, "S1SUSPEND", probe_swap1 },
1627 { "swsuspend", 0, 0x7ff6, 9, "S2SUSPEND", probe_swap1 },
1628 { "swsuspend", 0, 0x7ff6, 9, "ULSUSPEND", probe_swap1 },
1629 { "swap", 0, 0xfff6, 10, "SWAP-SPACE", probe_swap0 },
1630 { "swap", 0, 0xfff6, 10, "SWAPSPACE2", probe_swap1 },
1631 { "swsuspend", 0, 0xfff6, 9, "S1SUSPEND", probe_swap1 },
1632 { "swsuspend", 0, 0xfff6, 9, "S2SUSPEND", probe_swap1 },
1633 { "swsuspend", 0, 0xfff6, 9, "ULSUSPEND", probe_swap1 },
1634 { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs },
1635 { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 },
1636 { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 },
1637 { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 },
1638 { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 },
1639 { "crypt_LUKS", 0, 0, 6, "LUKS\xba\xbe", probe_luks },
1640 { "squashfs", 0, 0, 4, "sqsh", 0 },
1641 { "squashfs", 0, 0, 4, "hsqs", 0 },
1642 { "lvm2pv", 0, 0x218, 8, "LVM2 001", probe_lvm2 },
1643 { "lvm2pv", 0, 0x018, 8, "LVM2 001", probe_lvm2 },
1644 { "lvm2pv", 1, 0x018, 8, "LVM2 001", probe_lvm2 },
1645 { "lvm2pv", 1, 0x218, 8, "LVM2 001", probe_lvm2 },
1646 { "btrfs", 64, 0x40, 8, "_BHRfS_M", probe_btrfs },
1647 { "f2fs", 1, 0, 4, "\x10\x20\xf5\xf2", probe_f2fs },
1648 { "exfat", 0, 3, 8, "EXFAT ", probe_exfat },
1649 { NULL, 0, 0, 0, NULL, NULL }
1650 };
1651
1652 /*
1653 * Verify that the data in dev is consistent with what is on the actual
1654 * block device (using the devname field only). Normally this will be
1655 * called when finding items in the cache, but for long running processes
1656 * is also desirable to revalidate an item before use.
1657 *
1658 * If we are unable to revalidate the data, we return the old data and
1659 * do not set the BLKID_BID_FL_VERIFIED flag on it.
1660 */
blkid_verify(blkid_cache cache,blkid_dev dev)1661 blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
1662 {
1663 struct blkid_magic *id;
1664 struct blkid_probe probe;
1665 blkid_tag_iterate iter;
1666 unsigned char *buf;
1667 const char *type, *value;
1668 struct stat st;
1669 time_t now;
1670 double diff;
1671 int idx;
1672
1673 if (!dev)
1674 return NULL;
1675
1676 now = time(0);
1677 diff = difftime(now, dev->bid_time);
1678
1679 if (stat(dev->bid_name, &st) < 0) {
1680 DBG(DEBUG_PROBE,
1681 printf("blkid_verify: error %s (%d) while "
1682 "trying to stat %s\n", strerror(errno), errno,
1683 dev->bid_name));
1684 open_err:
1685 if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) {
1686 /* We don't have read permission, just return cache data. */
1687 DBG(DEBUG_PROBE, printf("returning unverified data for %s\n",
1688 dev->bid_name));
1689 return dev;
1690 }
1691 blkid_free_dev(dev);
1692 return NULL;
1693 }
1694
1695 if ((now >= dev->bid_time) &&
1696 (st.st_mtime <= dev->bid_time) &&
1697 ((diff < BLKID_PROBE_MIN) ||
1698 (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
1699 diff < BLKID_PROBE_INTERVAL)))
1700 return dev;
1701
1702 DBG(DEBUG_PROBE,
1703 printf("need to revalidate %s (cache time %lu, stat time %lu,\n\t"
1704 "time since last check %lu)\n",
1705 dev->bid_name, (unsigned long)dev->bid_time,
1706 (unsigned long)st.st_mtime, (unsigned long)diff));
1707
1708 if ((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) {
1709 DBG(DEBUG_PROBE, printf("blkid_verify: error %s (%d) while "
1710 "opening %s\n", strerror(errno), errno,
1711 dev->bid_name));
1712 goto open_err;
1713 }
1714
1715 probe.cache = cache;
1716 probe.dev = dev;
1717 probe.sbbuf = 0;
1718 probe.buf = 0;
1719 probe.buf_max = 0;
1720
1721 /*
1722 * Iterate over the type array. If we already know the type,
1723 * then try that first. If it doesn't work, then blow away
1724 * the type information, and try again.
1725 *
1726 */
1727 try_again:
1728 type = 0;
1729 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
1730 uuid_t uuid;
1731
1732 if (check_mdraid(probe.fd, uuid) == 0) {
1733 set_uuid(dev, uuid, 0);
1734 type = "mdraid";
1735 goto found_type;
1736 }
1737 }
1738 for (id = type_array; id->bim_type; id++) {
1739 if (dev->bid_type &&
1740 strcmp(id->bim_type, dev->bid_type))
1741 continue;
1742
1743 idx = id->bim_kboff + (id->bim_sboff >> 10);
1744 buf = get_buffer(&probe, (__u64) idx << 10, 1024);
1745 if (!buf)
1746 continue;
1747
1748 if (memcmp(id->bim_magic, buf + (id->bim_sboff & 0x3ff),
1749 id->bim_len))
1750 continue;
1751
1752 if ((id->bim_probe == NULL) ||
1753 (id->bim_probe(&probe, id, buf) == 0)) {
1754 type = id->bim_type;
1755 goto found_type;
1756 }
1757 }
1758
1759 if (!id->bim_type && dev->bid_type) {
1760 /*
1761 * Zap the device filesystem information and try again
1762 */
1763 DBG(DEBUG_PROBE,
1764 printf("previous fs type %s not valid, "
1765 "trying full probe\n", dev->bid_type));
1766 iter = blkid_tag_iterate_begin(dev);
1767 while (blkid_tag_next(iter, &type, &value) == 0)
1768 blkid_set_tag(dev, type, 0, 0);
1769 blkid_tag_iterate_end(iter);
1770 goto try_again;
1771 }
1772
1773 if (!dev->bid_type) {
1774 blkid_free_dev(dev);
1775 dev = 0;
1776 goto found_type;
1777 }
1778
1779 found_type:
1780 if (dev && type) {
1781 dev->bid_devno = st.st_rdev;
1782 dev->bid_time = time(0);
1783 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
1784 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
1785
1786 blkid_set_tag(dev, "TYPE", type, 0);
1787
1788 DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
1789 dev->bid_name, (long long)st.st_rdev, type));
1790 }
1791
1792 free(probe.sbbuf);
1793 free(probe.buf);
1794 if (probe.fd >= 0)
1795 close(probe.fd);
1796
1797 return dev;
1798 }
1799
blkid_known_fstype(const char * fstype)1800 int blkid_known_fstype(const char *fstype)
1801 {
1802 struct blkid_magic *id;
1803
1804 for (id = type_array; id->bim_type; id++) {
1805 if (strcmp(fstype, id->bim_type) == 0)
1806 return 1;
1807 }
1808 return 0;
1809 }
1810
1811 #ifdef TEST_PROGRAM
main(int argc,char ** argv)1812 int main(int argc, char **argv)
1813 {
1814 blkid_dev dev;
1815 blkid_cache cache;
1816 int ret;
1817
1818 if (argc != 2) {
1819 fprintf(stderr, "Usage: %s device\n"
1820 "Probe a single device to determine type\n", argv[0]);
1821 exit(1);
1822 }
1823 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
1824 fprintf(stderr, "%s: error creating cache (%d)\n",
1825 argv[0], ret);
1826 exit(1);
1827 }
1828 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
1829 if (!dev) {
1830 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
1831 return (1);
1832 }
1833 printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
1834 if (dev->bid_label)
1835 printf("LABEL='%s'\n", dev->bid_label);
1836 if (dev->bid_uuid)
1837 printf("UUID='%s'\n", dev->bid_uuid);
1838
1839 blkid_free_dev(dev);
1840 return (0);
1841 }
1842 #endif
1843