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