Whamcloud - gitweb
blkid: Flush cached filesystem information on any error other than EPERM
[tools/e2fsprogs.git] / lib / blkid / probe.c
index 2e2ad2e..917447b 100644 (file)
@@ -5,6 +5,7 @@
  * Copyright (C) 1999 by Andries Brouwer
  * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
  * Copyright (C) 2001 by Andreas Dilger
+ * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
  *
  * %Begin-Header%
  * This file may be redistributed under the terms of the
@@ -24,6 +25,7 @@
 #ifdef HAVE_SYS_MKDEV_H
 #include <sys/mkdev.h>
 #endif
+#include <sys/utsname.h>
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
@@ -44,6 +46,45 @@ static int figure_label_len(const unsigned char *label, int len)
        return 0;
 }
 
+static unsigned char *get_buffer(struct blkid_probe *pr, 
+                         blkid_loff_t off, size_t len)
+{
+       ssize_t         ret_read;
+       unsigned char   *newbuf;
+
+       if (off + len <= SB_BUFFER_SIZE) {
+               if (!pr->sbbuf) {
+                       pr->sbbuf = malloc(SB_BUFFER_SIZE);
+                       if (!pr->sbbuf)
+                               return NULL;
+                       if (lseek(pr->fd, 0, SEEK_SET) < 0)
+                               return NULL;
+                       ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE);
+                       if (ret_read < 0)
+                               ret_read = 0;
+                       pr->sb_valid = ret_read;
+               }
+               if (off+len > pr->sb_valid)
+                       return NULL;
+               return pr->sbbuf + off;
+       } else {
+               if (len > pr->buf_max) {
+                       newbuf = realloc(pr->buf, len);
+                       if (newbuf == NULL)
+                               return NULL;
+                       pr->buf = newbuf;
+                       pr->buf_max = len;
+               }
+               if (blkid_llseek(pr->fd, off, SEEK_SET) < 0)
+                       return NULL;
+               ret_read = read(pr->fd, pr->buf, len);
+               if (ret_read != (ssize_t) len)
+                       return NULL;
+               return pr->buf;
+       }
+}
+
+
 /*
  * This is a special case code to check for an MDRAID device.  We do
  * this special since it requires checking for a superblock at the end
@@ -65,7 +106,7 @@ static int check_mdraid(int fd, unsigned char *ret_uuid)
                return -BLKID_ERR_IO;
 
        /* Check for magic number */
-       if (memcmp("\251+N\374", buf, 4))
+       if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4))
                return -BLKID_ERR_PARAM;
 
        if (!ret_uuid)
@@ -76,22 +117,23 @@ static int check_mdraid(int fd, unsigned char *ret_uuid)
        md = (struct mdp_superblock_s *)buf;
        if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
                memcpy(ret_uuid, &md->set_uuid0, 4);
-               memcpy(ret_uuid, &md->set_uuid1, 12);
+               memcpy(ret_uuid + 4, &md->set_uuid1, 12);
        }
        return 0;
 }
 
-static void set_uuid(blkid_dev dev, uuid_t uuid)
+static void set_uuid(blkid_dev dev, uuid_t uuid, char *tag)
 {
        char    str[37];
 
        if (!uuid_is_null(uuid)) {
                uuid_unparse(uuid, str);
-               blkid_set_tag(dev, "UUID", str, sizeof(str));
+               blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
        }
 }
 
-static void get_ext2_info(blkid_dev dev, unsigned char *buf)
+static void get_ext2_info(blkid_dev dev, struct blkid_magic *id,
+                         unsigned char *buf)
 {
        struct ext2_super_block *es = (struct ext2_super_block *) buf;
        const char *label = 0;
@@ -105,65 +147,249 @@ static void get_ext2_info(blkid_dev dev, unsigned char *buf)
                label = es->s_volume_name;
        blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
 
-       set_uuid(dev, es->s_uuid);
+       set_uuid(dev, es->s_uuid, 0);
+
+       if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
+           !uuid_is_null(es->s_journal_uuid))
+               set_uuid(dev, es->s_journal_uuid, "EXT_JOURNAL");
+
+       if (strcmp(id->bim_type, "ext2") &&
+           ((blkid_le32(es->s_feature_incompat) &
+             EXT2_FEATURE_INCOMPAT_UNSUPPORTED) == 0))
+               blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2"));
 }
 
-static int probe_ext3(int fd __BLKID_ATTR((unused)), 
-                     blkid_cache cache __BLKID_ATTR((unused)), 
-                     blkid_dev dev,
-                     struct blkid_magic *id __BLKID_ATTR((unused)), 
-                     unsigned char *buf)
+/*
+ * Check to see if a filesystem is in /proc/filesystems.
+ * Returns 1 if found, 0 if not
+ */
+int fs_proc_check(const char *fs_name)
+{
+       FILE    *f;
+       char    buf[80], *cp, *t;
+
+       f = fopen("/proc/filesystems", "r");
+       if (!f)
+               return (0);
+       while (!feof(f)) {
+               if (!fgets(buf, sizeof(buf), f))
+                       break;
+               cp = buf;
+               if (!isspace(*cp)) {
+                       while (*cp && !isspace(*cp))
+                               cp++;
+               }
+               while (*cp && isspace(*cp))
+                       cp++;
+               if ((t = strchr(cp, '\n')) != NULL)
+                       *t = 0;
+               if ((t = strchr(cp, '\t')) != NULL)
+                       *t = 0;
+               if ((t = strchr(cp, ' ')) != NULL)
+                       *t = 0;
+               if (!strcmp(fs_name, cp)) {
+                       fclose(f);
+                       return (1);
+               }
+       }
+       fclose(f);
+       return (0);
+}
+
+/*
+ * Check to see if a filesystem is available as a module
+ * Returns 1 if found, 0 if not
+ */
+int check_for_modules(const char *fs_name)
+{
+       struct utsname  uts;
+       FILE            *f;
+       char            buf[1024], *cp, *t;
+       int             i;
+
+       if (uname(&uts))
+               return (0);
+       snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
+
+       f = fopen(buf, "r");
+       if (!f)
+               return (0);
+       while (!feof(f)) {
+               if (!fgets(buf, sizeof(buf), f))
+                       break;
+               if ((cp = strchr(buf, ':')) != NULL)
+                       *cp = 0;
+               else
+                       continue;
+               if ((cp = strrchr(buf, '/')) != NULL)
+                       cp++;
+               i = strlen(cp);
+               if (i > 3) {
+                       t = cp + i - 3;
+                       if (!strcmp(t, ".ko"))
+                               *t = 0;
+               }
+               if (!strcmp(cp, fs_name))
+                       return (1);
+       }
+       fclose(f);
+       return (0);
+}
+
+static int system_supports_ext4()
+{
+       static time_t   last_check = 0;
+       static int      ret = -1;
+       time_t          now = time(0);
+
+       if (ret != -1 || (last_check - now) < 5)
+               return ret;
+       last_check = now;
+       ret = (fs_proc_check("ext4") || check_for_modules("ext4"));
+       return ret;
+}
+
+static int system_supports_ext4dev()
+{
+       static time_t   last_check = 0;
+       static int      ret = -1;
+       time_t          now = time(0);
+
+       if (ret != -1 || (last_check - now) < 5)
+               return ret;
+       last_check = now;
+       ret = (fs_proc_check("ext4dev") || check_for_modules("ext4dev"));
+       return ret;
+}
+
+static int probe_ext4dev(struct blkid_probe *probe,
+                        struct blkid_magic *id,
+                        unsigned char *buf)
 {
        struct ext2_super_block *es;
+       es = (struct ext2_super_block *)buf;
+
+       /* Distinguish from jbd */
+       if (blkid_le32(es->s_feature_incompat) &
+           EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
+               return -BLKID_ERR_PARAM;
+
+       /* ext4dev requires a journal */
+       if (!(blkid_le32(es->s_feature_compat) &
+             EXT3_FEATURE_COMPAT_HAS_JOURNAL))
+               return -BLKID_ERR_PARAM;
+
+       /*
+        * If the filesystem is marked as OK for use by in-development
+        * filesystem code, but ext4dev is not supported, and ext4 is,
+        * then don't call ourselves ext4dev, since we should be
+        * detected as ext4 in that case.
+        *
+        * If the filesystem is marked as in use by production
+        * filesystem, then it can only be used by ext4 and NOT by
+        * ext4dev, so always disclaim we are ext4dev in that case.
+        */
+       if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
+               if (!system_supports_ext4dev() && system_supports_ext4())
+                       return -BLKID_ERR_PARAM;
+       } else
+               return -BLKID_ERR_PARAM;
 
+       get_ext2_info(probe->dev, id, buf);
+       return 0;
+}
+
+static int probe_ext4(struct blkid_probe *probe, struct blkid_magic *id,
+                     unsigned char *buf)
+{
+       struct ext2_super_block *es;
        es = (struct ext2_super_block *)buf;
 
-       /* Distinguish between jbd and ext2/3 fs */
+       /* Distinguish from jbd */
        if (blkid_le32(es->s_feature_incompat) & 
            EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
                return -BLKID_ERR_PARAM;
 
-       /* Distinguish between ext3 and ext2 */
+       /* ext4 requires journal */
        if (!(blkid_le32(es->s_feature_compat) &
              EXT3_FEATURE_COMPAT_HAS_JOURNAL))
                return -BLKID_ERR_PARAM;
 
-       get_ext2_info(dev, buf);
+       /* Ext4 has at least one feature which ext3 doesn't understand */
+       if (!(blkid_le32(es->s_feature_ro_compat) &
+             EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) &&
+           !(blkid_le32(es->s_feature_incompat) &
+             EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
+               return -BLKID_ERR_PARAM;
+
+       /*
+        * If the filesystem is a OK for use by in-development
+        * filesystem code, and ext4dev is supported or ext4 is not
+        * supported, then don't call ourselves ext4, so we can redo
+        * the detection and mark the filesystem as ext4dev.
+        *
+        * If the filesystem is marked as in use by production
+        * filesystem, then it can only be used by ext4 and NOT by
+        * ext4dev.
+        */
+       if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
+               if (system_supports_ext4dev() || !system_supports_ext4())
+                       return -BLKID_ERR_PARAM;
+       }
+       get_ext2_info(probe->dev, id, buf);
+       return 0;
+}
+
+static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id,
+                     unsigned char *buf)
+{
+       struct ext2_super_block *es;
+       es = (struct ext2_super_block *)buf;
 
-       blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2"));
+       /* Distinguish from ext4dev */
+       if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS)
+               return -BLKID_ERR_PARAM;
 
+       /* ext3 requires journal */
+       if (!(blkid_le32(es->s_feature_compat) &
+             EXT3_FEATURE_COMPAT_HAS_JOURNAL))
+               return -BLKID_ERR_PARAM;
+
+       /* Any features which ext3 doesn't understand */
+       if ((blkid_le32(es->s_feature_ro_compat) &
+            EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) ||
+           (blkid_le32(es->s_feature_incompat) &
+            EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
+               return -BLKID_ERR_PARAM;
+
+       get_ext2_info(probe->dev, id, buf);
        return 0;
 }
 
-static int probe_ext2(int fd __BLKID_ATTR((unused)), 
-                     blkid_cache cache __BLKID_ATTR((unused)), 
-                     blkid_dev dev,
-                     struct blkid_magic *id __BLKID_ATTR((unused)), 
+static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id,
                      unsigned char *buf)
 {
        struct ext2_super_block *es;
 
        es = (struct ext2_super_block *)buf;
 
-       /* Distinguish between jbd and ext2/3 fs */
-       if (blkid_le32(es->s_feature_incompat) & 
-           EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
-               return -BLKID_ERR_PARAM;
-       
        /* Distinguish between ext3 and ext2 */
        if ((blkid_le32(es->s_feature_compat) &
              EXT3_FEATURE_COMPAT_HAS_JOURNAL))
                return -BLKID_ERR_PARAM;
 
-       get_ext2_info(dev, buf);
+       /* Any features which ext2 doesn't understand */
+       if ((blkid_le32(es->s_feature_ro_compat) &
+            EXT2_FEATURE_RO_COMPAT_UNSUPPORTED) ||
+           (blkid_le32(es->s_feature_incompat) &
+            EXT2_FEATURE_INCOMPAT_UNSUPPORTED))
+               return -BLKID_ERR_PARAM;
 
+       get_ext2_info(probe->dev, id, buf);
        return 0;
 }
 
-static int probe_jbd(int fd __BLKID_ATTR((unused)), 
-                    blkid_cache cache __BLKID_ATTR((unused)), 
-                    blkid_dev dev, 
-                    struct blkid_magic *id __BLKID_ATTR((unused)), 
+static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id,
                     unsigned char *buf)
 {
        struct ext2_super_block *es = (struct ext2_super_block *) buf;
@@ -172,41 +398,69 @@ static int probe_jbd(int fd __BLKID_ATTR((unused)),
              EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
                return -BLKID_ERR_PARAM;
 
-       get_ext2_info(dev, buf);
+       get_ext2_info(probe->dev, id, buf);
 
        return 0;
 }
 
-static int probe_fat(int fd __BLKID_ATTR((unused)), 
-                     blkid_cache cache __BLKID_ATTR((unused)), 
-                     blkid_dev dev,
+#define FAT_ATTR_VOLUME_ID             0x08
+#define FAT_ATTR_DIR                   0x10
+#define FAT_ATTR_LONG_NAME             0x0f
+#define FAT_ATTR_MASK                  0x3f
+#define FAT_ENTRY_FREE                 0xe5
+
+static char *no_name = "NO NAME    ";
+
+static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
+{
+       int i;
+
+       for (i = 0; i < count; i++) {
+               if (dir[i].name[0] == 0x00)
+                       break;
+               
+               if ((dir[i].name[0] == FAT_ENTRY_FREE) ||
+                   (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) ||
+                   ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
+                       continue;
+
+               if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == 
+                   FAT_ATTR_VOLUME_ID) {
+                       return dir[i].name;
+               }
+       }
+       return 0;
+}
+
+/* FAT label extraction from the root directory taken from Kay
+ * Sievers's volume_id library */
+static int probe_fat(struct blkid_probe *probe,
                      struct blkid_magic *id __BLKID_ATTR((unused)), 
                      unsigned char *buf)
 {
        struct vfat_super_block *vs = (struct vfat_super_block *) buf;
        struct msdos_super_block *ms = (struct msdos_super_block *) buf;
+       struct vfat_dir_entry *dir;
        char serno[10];
-       const char *label = 0, *vol_label = 0;
+       const unsigned char *label = 0, *vol_label = 0, *tmp;
        unsigned char   *vol_serno;
-       int label_len = 0;
-       __u16 sector_size;
-       __u16 dir_entries;
-       __u32 sect_count;
-       __u16 reserved;
-       __u32 fat_size;
-       __u32 dir_size;
-       __u32 cluster_count;
-       __u32 fat_length;
+       int label_len = 0, maxloop = 100;
+       __u16 sector_size, dir_entries, reserved;
+       __u32 sect_count, fat_size, dir_size, cluster_count, fat_length;
+       __u32 buf_size, start_data_sect, next, root_start, root_dir_entries;
 
        /* sector size check */
-       sector_size = blkid_le16(*((__u16 *) &ms->ms_sector_size));
+       tmp = (unsigned char *)&ms->ms_sector_size;
+       sector_size = tmp[0] + (tmp[1] << 8);
        if (sector_size != 0x200 && sector_size != 0x400 &&
            sector_size != 0x800 && sector_size != 0x1000)
                return 1;
 
-       dir_entries = blkid_le16(*((__u16 *) &ms->ms_dir_entries));
+       tmp = (unsigned char *)&ms->ms_dir_entries;
+       dir_entries = tmp[0] + (tmp[1] << 8);
        reserved =  blkid_le16(ms->ms_reserved);
-       sect_count = blkid_le16(*((__u16 *) &ms->ms_sectors));
+       tmp = (unsigned char *)&ms->ms_sectors;
+       sect_count = tmp[0] + (tmp[1] << 8);
        if (sect_count == 0)
                sect_count = blkid_le32(ms->ms_total_sect);
 
@@ -219,32 +473,84 @@ static int probe_fat(int fd __BLKID_ATTR((unused)),
                        (sector_size-1)) / sector_size;
 
        cluster_count = sect_count - (reserved + fat_size + dir_size);
+       if (ms->ms_cluster_size == 0)
+               return 1;
        cluster_count /= ms->ms_cluster_size;
 
        if (cluster_count > FAT32_MAX)
                return 1;
 
        if (ms->ms_fat_length) {
-               vol_label = ms->ms_label;
+               /* the label may be an attribute in the root directory */
+               root_start = (reserved + fat_size) * sector_size;
+               root_dir_entries = vs->vs_dir_entries[0] + 
+                       (vs->vs_dir_entries[1] << 8);
+
+               buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
+               dir = (struct vfat_dir_entry *) get_buffer(probe, root_start, 
+                                                          buf_size);
+               if (dir)
+                       vol_label = search_fat_label(dir, root_dir_entries);
+
+               if (!vol_label || !memcmp(vol_label, no_name, 11))
+                       vol_label = ms->ms_label;
                vol_serno = ms->ms_serno;
 
-               blkid_set_tag(dev, "SEC_TYPE", "msdos", sizeof("msdos"));
+               blkid_set_tag(probe->dev, "SEC_TYPE", "msdos", 
+                             sizeof("msdos"));
        } else {
-               vol_label = vs->vs_label;
+               /* Search the FAT32 root dir for the label attribute */
+               buf_size = vs->vs_cluster_size * sector_size;
+               start_data_sect = reserved + fat_size;
+
+               next = blkid_le32(vs->vs_root_cluster);
+               while (next && --maxloop) {
+                       __u32 next_sect_off;
+                       __u64 next_off, fat_entry_off;
+                       int count;
+
+                       next_sect_off = (next - 2) * vs->vs_cluster_size;
+                       next_off = (start_data_sect + next_sect_off) * 
+                               sector_size;
+
+                       dir = (struct vfat_dir_entry *) 
+                               get_buffer(probe, next_off, buf_size);
+                       if (dir == NULL)
+                               break;
+
+                       count = buf_size / sizeof(struct vfat_dir_entry);
+
+                       vol_label = search_fat_label(dir, count);
+                       if (vol_label)
+                               break;
+
+                       /* get FAT entry */
+                       fat_entry_off = (reserved * sector_size) + 
+                               (next * sizeof(__u32));
+                       buf = get_buffer(probe, fat_entry_off, buf_size);
+                       if (buf == NULL)
+                               break;
+
+                       /* set next cluster */
+                       next = blkid_le32(*((__u32 *) buf) & 0x0fffffff);
+               }
+
+               if (!vol_label || !memcmp(vol_label, no_name, 11))
+                       vol_label = vs->vs_label;
                vol_serno = vs->vs_serno;
        }
 
-       if (vol_label && memcmp(vol_label, "NO NAME    ", 11)) {
-               label = vol_label;
-               label_len = figure_label_len(vol_label, 11);
+       if (vol_label && memcmp(vol_label, no_name, 11)) {
+               if ((label_len = figure_label_len(vol_label, 11)))
+                       label = vol_label;
        }
 
        /* We can't just print them as %04X, because they are unaligned */
        sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
                vol_serno[1], vol_serno[0]);
 
-       blkid_set_tag(dev, "LABEL", label, label_len);
-       blkid_set_tag(dev, "UUID", serno, sizeof(serno)-1);
+       blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len);
+       blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1);
 
        return 0;
 }
@@ -255,9 +561,7 @@ static int probe_fat(int fd __BLKID_ATTR((unused)),
  * by http://vrfy.org/projects/volume_id/ and Linux kernel.
  * [7-Jul-2005, Karel Zak <kzak@redhat.com>]
  */
-static int probe_fat_nomagic(int fd __BLKID_ATTR((unused)), 
-                            blkid_cache cache __BLKID_ATTR((unused)), 
-                            blkid_dev dev,
+static int probe_fat_nomagic(struct blkid_probe *probe,
                             struct blkid_magic *id __BLKID_ATTR((unused)), 
                             unsigned char *buf)
 {
@@ -282,12 +586,118 @@ static int probe_fat_nomagic(int fd __BLKID_ATTR((unused)),
        if (!vs->vs_fats)
                return 1;
 
-       return probe_fat(fd, cache, dev, id, buf);
+       return probe_fat(probe, id, buf);
 }
 
-static int probe_xfs(int fd __BLKID_ATTR((unused)), 
-                    blkid_cache cache __BLKID_ATTR((unused)), 
-                    blkid_dev dev,
+static int probe_ntfs(struct blkid_probe *probe,
+                     struct blkid_magic *id __BLKID_ATTR((unused)), 
+                     unsigned char *buf)
+{
+       struct ntfs_super_block *ns;
+       struct master_file_table_record *mft;
+       struct file_attribute *attr;
+       char            uuid_str[17], label_str[129], *cp;
+       int             bytes_per_sector, sectors_per_cluster;
+       int             mft_record_size, attr_off, attr_len;
+       unsigned int    i, attr_type, val_len;
+       int             val_off;
+       __u64           nr_clusters;
+       blkid_loff_t off;
+       unsigned char *buf_mft, *val;
+
+       ns = (struct ntfs_super_block *) buf;
+
+       bytes_per_sector = ns->bios_parameter_block[0] +
+               (ns->bios_parameter_block[1]  << 8);
+       sectors_per_cluster = ns->bios_parameter_block[2];
+
+       if ((bytes_per_sector < 512) || (sectors_per_cluster == 0))
+               return 1;
+
+       if (ns->cluster_per_mft_record < 0)
+               mft_record_size = 1 << (0-ns->cluster_per_mft_record);
+       else
+               mft_record_size = ns->cluster_per_mft_record * 
+                       sectors_per_cluster * bytes_per_sector;
+       nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster;
+
+       if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) ||
+           (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters))
+               return 1;
+
+       off = blkid_le64(ns->mft_mirror_cluster_location) * 
+               bytes_per_sector * sectors_per_cluster;
+
+       buf_mft = get_buffer(probe, off, mft_record_size);
+       if (!buf_mft)
+               return 1;
+
+       if (memcmp(buf_mft, "FILE", 4))
+               return 1;
+
+       off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector * 
+               sectors_per_cluster;
+
+       buf_mft = get_buffer(probe, off, mft_record_size);
+       if (!buf_mft)
+               return 1;
+
+       if (memcmp(buf_mft, "FILE", 4))
+               return 1;
+
+       off += MFT_RECORD_VOLUME * mft_record_size;
+
+       buf_mft = get_buffer(probe, off, mft_record_size);
+       if (!buf_mft)
+               return 1;
+
+       if (memcmp(buf_mft, "FILE", 4))
+               return 1;
+
+       mft = (struct master_file_table_record *) buf_mft;
+
+       attr_off = blkid_le16(mft->attrs_offset);
+       label_str[0] = 0;
+       
+       while (1) {
+               attr = (struct file_attribute *) (buf_mft + attr_off);
+               attr_len = blkid_le16(attr->len);
+               attr_type = blkid_le32(attr->type);
+               val_off = blkid_le16(attr->value_offset);
+               val_len = blkid_le32(attr->value_len);
+
+               attr_off += attr_len;
+
+               if ((attr_off > mft_record_size) ||
+                   (attr_len == 0))
+                       break;
+
+               if (attr_type == MFT_RECORD_ATTR_END)
+                       break;
+
+               if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
+                       if (val_len > sizeof(label_str))
+                               val_len = sizeof(label_str)-1;
+
+                       for (i=0, cp=label_str; i < val_len; i+=2,cp++) {
+                               val = ((__u8 *) attr) + val_off + i;
+                               *cp = val[0];
+                               if (val[1])
+                                       *cp = '?';
+                       }
+                       *cp = 0;
+               }
+       }
+
+       sprintf(uuid_str, "%016llX", blkid_le64(ns->volume_serial));
+       blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
+       if (label_str[0])
+               blkid_set_tag(probe->dev, "LABEL", label_str, 0);
+       return 0;
+}
+
+
+static int probe_xfs(struct blkid_probe *probe,
                     struct blkid_magic *id __BLKID_ATTR((unused)), 
                     unsigned char *buf)
 {
@@ -298,14 +708,12 @@ static int probe_xfs(int fd __BLKID_ATTR((unused)),
 
        if (strlen(xs->xs_fname))
                label = xs->xs_fname;
-       blkid_set_tag(dev, "LABEL", label, sizeof(xs->xs_fname));
-       set_uuid(dev, xs->xs_uuid);
+       blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname));
+       set_uuid(probe->dev, xs->xs_uuid, 0);
        return 0;
 }
 
-static int probe_reiserfs(int fd __BLKID_ATTR((unused)), 
-                         blkid_cache cache __BLKID_ATTR((unused)), 
-                         blkid_dev dev,
+static int probe_reiserfs(struct blkid_probe *probe,
                          struct blkid_magic *id, unsigned char *buf)
 {
        struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
@@ -314,42 +722,42 @@ static int probe_reiserfs(int fd __BLKID_ATTR((unused)),
 
        blocksize = blkid_le16(rs->rs_blocksize);
 
+       /* The blocksize must be at least 1k */
+       if ((blocksize >> 10) == 0)
+               return -BLKID_ERR_PARAM;
+
        /* If the superblock is inside the journal, we have the wrong one */
        if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
                return -BLKID_ERR_BIG;
 
        /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
-       if (!strcmp(id->bim_magic, "ReIsEr2Fs") ||
-           !strcmp(id->bim_magic, "ReIsEr3Fs")) {
+       if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') {
                if (strlen(rs->rs_label))
                        label = rs->rs_label;
-               set_uuid(dev, rs->rs_uuid);
+               set_uuid(probe->dev, rs->rs_uuid, 0);
        }
-       blkid_set_tag(dev, "LABEL", label, sizeof(rs->rs_label));
+       blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label));
 
        return 0;
 }
 
-static int probe_reiserfs4(int fd __BLKID_ATTR((unused)), 
-                          blkid_cache cache __BLKID_ATTR((unused)), 
-                          blkid_dev dev,
-                          struct blkid_magic *id, unsigned char *buf)
+static int probe_reiserfs4(struct blkid_probe *probe,
+                          struct blkid_magic *id __BLKID_ATTR((unused)), 
+                          unsigned char *buf)
 {
        struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf;
-       unsigned int blocksize;
-       const char *label = 0;
+       const unsigned char *label = 0;
 
-       if (strlen(rs4->rs4_label))
+       if (strlen((char *) rs4->rs4_label))
                label = rs4->rs4_label;
-       set_uuid(dev, rs4->rs4_uuid);
-       blkid_set_tag(dev, "LABEL", label, sizeof(rs4->rs4_label));
+       set_uuid(probe->dev, rs4->rs4_uuid, 0);
+       blkid_set_tag(probe->dev, "LABEL", (const char *) label, 
+                     sizeof(rs4->rs4_label));
 
        return 0;
 }
 
-static int probe_jfs(int fd __BLKID_ATTR((unused)), 
-                    blkid_cache cache __BLKID_ATTR((unused)), 
-                    blkid_dev dev,
+static int probe_jfs(struct blkid_probe *probe,
                     struct blkid_magic *id __BLKID_ATTR((unused)), 
                     unsigned char *buf)
 {
@@ -360,14 +768,24 @@ static int probe_jfs(int fd __BLKID_ATTR((unused)),
 
        if (strlen((char *) js->js_label))
                label = (char *) js->js_label;
-       blkid_set_tag(dev, "LABEL", label, sizeof(js->js_label));
-       set_uuid(dev, js->js_uuid);
+       blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label));
+       set_uuid(probe->dev, js->js_uuid, 0);
        return 0;
 }
 
-static int probe_romfs(int fd __BLKID_ATTR((unused)), 
-                      blkid_cache cache __BLKID_ATTR((unused)), 
-                      blkid_dev dev,
+static int probe_luks(struct blkid_probe *probe,
+                      struct blkid_magic *id __BLKID_ATTR((unused)),
+                      unsigned char *buf)
+{
+       unsigned char uuid[40];
+       /* 168 is the offset to the 40 character uuid:
+        * http://luks.endorphin.org/LUKS-on-disk-format.pdf */
+       strncpy(uuid, buf+168, 40);
+       blkid_set_tag(probe->dev, "UUID", uuid, sizeof(uuid));
+       return 0;
+}
+
+static int probe_romfs(struct blkid_probe *probe,
                       struct blkid_magic *id __BLKID_ATTR((unused)), 
                       unsigned char *buf)
 {
@@ -378,15 +796,13 @@ static int probe_romfs(int fd __BLKID_ATTR((unused)),
 
        if (strlen((char *) ros->ros_volume))
                label = (char *) ros->ros_volume;
-       blkid_set_tag(dev, "LABEL", label, 0);
+       blkid_set_tag(probe->dev, "LABEL", label, 0);
        return 0;
 }
 
-static int probe_cramfs(int fd __BLKID_ATTR((unused)), 
-                      blkid_cache cache __BLKID_ATTR((unused)), 
-                      blkid_dev dev,
-                      struct blkid_magic *id __BLKID_ATTR((unused)), 
-                      unsigned char *buf)
+static int probe_cramfs(struct blkid_probe *probe,
+                       struct blkid_magic *id __BLKID_ATTR((unused)), 
+                       unsigned char *buf)
 {
        struct cramfs_super_block *csb;
        const char *label = 0;
@@ -395,58 +811,48 @@ static int probe_cramfs(int fd __BLKID_ATTR((unused)),
 
        if (strlen((char *) csb->name))
                label = (char *) csb->name;
-       blkid_set_tag(dev, "LABEL", label, 0);
+       blkid_set_tag(probe->dev, "LABEL", label, 0);
        return 0;
 }
 
-static int probe_swap0(int fd __BLKID_ATTR((unused)),
-                      blkid_cache cache __BLKID_ATTR((unused)),
-                      blkid_dev dev,
+static int probe_swap0(struct blkid_probe *probe,
                       struct blkid_magic *id __BLKID_ATTR((unused)),
                       unsigned char *buf __BLKID_ATTR((unused)))
 {
-       blkid_set_tag(dev, "UUID", 0, 0);
-       blkid_set_tag(dev, "LABEL", 0, 0);
+       blkid_set_tag(probe->dev, "UUID", 0, 0);
+       blkid_set_tag(probe->dev, "LABEL", 0, 0);
        return 0;
 }
 
-static int probe_swap1(int fd,
-                      blkid_cache cache __BLKID_ATTR((unused)),
-                      blkid_dev dev,
+static int probe_swap1(struct blkid_probe *probe,
                       struct blkid_magic *id __BLKID_ATTR((unused)),
                       unsigned char *buf __BLKID_ATTR((unused)))
 {
        struct swap_id_block *sws;
 
-       probe_swap0(fd, cache, dev, id, buf);
+       probe_swap0(probe, id, buf);
        /*
         * Version 1 swap headers are always located at offset of 1024
         * bytes, although the swap signature itself is located at the
         * end of the page (which may vary depending on hardware
         * pagesize).
         */
-       if (lseek(fd, 1024, SEEK_SET) < 0) return 1;
-       if (!(sws = (struct swap_id_block *)malloc(1024))) return 1;
-       if (read(fd, sws, 1024) != 1024) {
-               free(sws);
+       sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024);
+       if (!sws)
                return 1;
-       }
 
        /* arbitrary sanity check.. is there any garbage down there? */
        if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0)  {
                if (sws->sws_volume[0])
-                       blkid_set_tag(dev, "LABEL", sws->sws_volume, 
+                       blkid_set_tag(probe->dev, "LABEL", sws->sws_volume, 
                                      sizeof(sws->sws_volume));
                if (sws->sws_uuid[0])
-                       set_uuid(dev, sws->sws_uuid);
+                       set_uuid(probe->dev, sws->sws_uuid, 0);
        }
-       free(sws);
-
        return 0;
 }
 
-static int probe_iso9660(int fd, blkid_cache cache __BLKID_ATTR((unused)), 
-                        blkid_dev dev __BLKID_ATTR((unused)),
+static int probe_iso9660(struct blkid_probe *probe,
                         struct blkid_magic *id __BLKID_ATTR((unused)), 
                         unsigned char *buf)
 {
@@ -454,10 +860,10 @@ static int probe_iso9660(int fd, blkid_cache cache __BLKID_ATTR((unused)),
        const unsigned char *label;
 
        iso = (struct iso_volume_descriptor *) buf;
-
        label = iso->volume_id;
 
-       blkid_set_tag(dev, "LABEL", label, figure_label_len(label, 32));
+       blkid_set_tag(probe->dev, "LABEL", (const char *) label, 
+                     figure_label_len(label, 32));
        return 0;
 }
 
@@ -466,41 +872,42 @@ static const char
 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
                 "NSR03", "TEA01", 0 };
 
-static int probe_udf(int fd, blkid_cache cache __BLKID_ATTR((unused)), 
-                    blkid_dev dev __BLKID_ATTR((unused)),
-                      struct blkid_magic *id __BLKID_ATTR((unused)), 
+static int probe_udf(struct blkid_probe *probe,
+                    struct blkid_magic *id __BLKID_ATTR((unused)), 
                     unsigned char *buf __BLKID_ATTR((unused)))
 {
        int j, bs;
-       struct iso_volume_descriptor isosb;
+       struct iso_volume_descriptor *isosb;
        const char ** m;
 
        /* determine the block size by scanning in 2K increments
           (block sizes larger than 2K will be null padded) */
        for (bs = 1; bs < 16; bs++) {
-               lseek(fd, bs*2048+32768, SEEK_SET);
-               if (read(fd, (char *)&isosb, sizeof(isosb)) != sizeof(isosb))
+               isosb = (struct iso_volume_descriptor *) 
+                       get_buffer(probe, bs*2048+32768, sizeof(isosb));
+               if (!isosb)
                        return 1;
-               if (isosb.vd_id[0])
+               if (isosb->vd_id[0])
                        break;
        }
 
        /* Scan up to another 64 blocks looking for additional VSD's */
        for (j = 1; j < 64; j++) {
                if (j > 1) {
-                       lseek(fd, j*bs*2048+32768, SEEK_SET);
-                       if (read(fd, (char *)&isosb, sizeof(isosb))
-                           != sizeof(isosb))
+                       isosb = (struct iso_volume_descriptor *) 
+                               get_buffer(probe, j*bs*2048+32768, 
+                                          sizeof(isosb));
+                       if (!isosb)
                                return 1;
                }
                /* If we find NSR0x then call it udf:
                   NSR01 for UDF 1.00
                   NSR02 for UDF 1.50
                   NSR03 for UDF 2.00 */
-               if (!strncmp(isosb.vd_id, "NSR0", 4))
+               if (!memcmp(isosb->vd_id, "NSR0", 4))
                        return 0;
                for (m = udf_magic; *m; m++)
-                       if (!strncmp(*m, isosb.vd_id, 5))
+                       if (!memcmp(*m, isosb->vd_id, 5))
                                break;
                if (*m == 0)
                        return 1;
@@ -508,9 +915,7 @@ static int probe_udf(int fd, blkid_cache cache __BLKID_ATTR((unused)),
        return 1;
 }
 
-static int probe_ocfs(int fd __BLKID_ATTR((unused)), 
-                     blkid_cache cache __BLKID_ATTR((unused)), 
-                     blkid_dev dev,
+static int probe_ocfs(struct blkid_probe *probe,
                      struct blkid_magic *id __BLKID_ATTR((unused)), 
                      unsigned char *buf)
 {
@@ -523,19 +928,17 @@ static int probe_ocfs(int fd __BLKID_ATTR((unused)),
 
        major = ocfsmajor(ovh);
        if (major == 1)
-               blkid_set_tag(dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
+               blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
        else if (major >= 9)
-               blkid_set_tag(dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
+               blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
        
-       blkid_set_tag(dev, "LABEL", ovl.label, ocfslabellen(ovl));
-       blkid_set_tag(dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
-       set_uuid(dev, ovl.vol_id);
+       blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl));
+       blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
+       set_uuid(probe->dev, ovl.vol_id, 0);
        return 0;
 }
 
-static int probe_ocfs2(int fd __BLKID_ATTR((unused)), 
-                      blkid_cache cache __BLKID_ATTR((unused)), 
-                      blkid_dev dev,
+static int probe_ocfs2(struct blkid_probe *probe,
                       struct blkid_magic *id __BLKID_ATTR((unused)), 
                       unsigned char *buf)
 {
@@ -543,14 +946,12 @@ static int probe_ocfs2(int fd __BLKID_ATTR((unused)),
 
        osb = (struct ocfs2_super_block *)buf;
 
-       blkid_set_tag(dev, "LABEL", osb->s_label, sizeof(osb->s_label));
-       set_uuid(dev, osb->s_uuid);
+       blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
+       set_uuid(probe->dev, osb->s_uuid, 0);
        return 0;
 }
 
-static int probe_oracleasm(int fd __BLKID_ATTR((unused)), 
-                          blkid_cache cache __BLKID_ATTR((unused)), 
-                          blkid_dev dev,
+static int probe_oracleasm(struct blkid_probe *probe,
                           struct blkid_magic *id __BLKID_ATTR((unused)), 
                           unsigned char *buf)
 {
@@ -558,10 +959,67 @@ static int probe_oracleasm(int fd __BLKID_ATTR((unused)),
 
        dl = (struct oracle_asm_disk_label *)buf;
 
-       blkid_set_tag(dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
+       blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
        return 0;
 }
 
+static int probe_gfs(struct blkid_probe *probe,
+                    struct blkid_magic *id __BLKID_ATTR((unused)),
+                    unsigned char *buf)
+{
+       struct gfs2_sb *sbd;
+       const char *label = 0;
+
+       sbd = (struct gfs2_sb *)buf;
+
+       if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS &&
+           blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI)
+       {       
+               blkid_set_tag(probe->dev, "UUID", 0, 0);
+       
+               if (strlen(sbd->sb_locktable))
+                       label = sbd->sb_locktable;
+               blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
+               return 0;
+       }
+       return 1;
+}
+
+static int probe_gfs2(struct blkid_probe *probe,
+                    struct blkid_magic *id __BLKID_ATTR((unused)),
+                    unsigned char *buf)
+{
+       struct gfs2_sb *sbd;
+       const char *label = 0;
+
+       sbd = (struct gfs2_sb *)buf;
+
+       if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS &&
+           blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI)
+       {       
+               blkid_set_tag(probe->dev, "UUID", 0, 0);
+       
+               if (strlen(sbd->sb_locktable))
+                       label = sbd->sb_locktable;
+               blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
+               return 0;
+       }
+       return 1;
+}
+
+static int probe_hfsplus(struct blkid_probe *probe,
+                        struct blkid_magic *id __BLKID_ATTR((unused)),
+                        unsigned char *buf)
+{
+       struct hfs_mdb *sbd = (struct hfs_mdb *)buf;
+
+       /* Check for a HFS+ volume embedded in a HFS volume */
+       if (memcmp(sbd->embed_sig, "H+", 2) == 0)
+               return 0;
+
+       return 1;
+}
+
 /*
  * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
  * in the type_array table below + bim_kbalign.
@@ -579,8 +1037,10 @@ static int probe_oracleasm(int fd __BLKID_ATTR((unused)),
 static struct blkid_magic type_array[] = {
 /*  type     kboff   sboff len  magic                  probe */
   { "oracleasm", 0,    32,  8, "ORCLDISK",             probe_oracleasm },
-  { "ntfs",      0,      3,  8, "NTFS    ",             0 },
+  { "ntfs",     0,      3,  8, "NTFS    ",             probe_ntfs },
   { "jbd",      1,   0x38,  2, "\123\357",             probe_jbd },
+  { "ext4dev",  1,   0x38,  2, "\123\357",             probe_ext4dev },
+  { "ext4",     1,   0x38,  2, "\123\357",             probe_ext4 },
   { "ext3",     1,   0x38,  2, "\123\357",             probe_ext3 },
   { "ext2",     1,   0x38,  2, "\123\357",             probe_ext2 },
   { "reiserfs",         8,   0x34,  8, "ReIsErFs",             probe_reiserfs },
@@ -589,13 +1049,16 @@ static struct blkid_magic type_array[] = {
   { "reiserfs", 64,   0x34,  8, "ReIsErFs",            probe_reiserfs },
   { "reiserfs",         8,     20,  8, "ReIsErFs",             probe_reiserfs },
   { "reiser4",  64,     0,  7, "ReIsEr4",              probe_reiserfs4 },
+  { "gfs2",     64,      0,  4, "\x01\x16\x19\x70",     probe_gfs2 },
+  { "gfs",      64,      0,  4, "\x01\x16\x19\x70",     probe_gfs },
   { "vfat",      0,   0x52,  5, "MSWIN",                probe_fat },
   { "vfat",      0,   0x52,  8, "FAT32   ",             probe_fat },
   { "vfat",      0,   0x36,  5, "MSDOS",                probe_fat },
   { "vfat",      0,   0x36,  8, "FAT16   ",             probe_fat },
   { "vfat",      0,   0x36,  8, "FAT12   ",             probe_fat },
-  { "vfat",      0,      0,  2, "\353\220",             probe_fat_nomagic },
+  { "vfat",      0,      0,  1, "\353",                 probe_fat_nomagic },
   { "vfat",      0,      0,  1, "\351",                 probe_fat_nomagic },
+  { "vfat",      0,  0x1fe,  2, "\125\252",             probe_fat_nomagic },
   { "minix",     1,   0x10,  2, "\177\023",             0 },
   { "minix",     1,   0x10,  2, "\217\023",             0 },
   { "minix",    1,   0x10,  2, "\150\044",             0 },
@@ -616,6 +1079,8 @@ static struct blkid_magic type_array[] = {
   { "iso9660", 32,      1,  5, "CD001",                probe_iso9660 },
   { "iso9660", 32,      9,  5, "CDROM",                probe_iso9660 },
   { "jfs",     32,      0,  4, "JFS1",                 probe_jfs },
+  { "hfsplus",  1,      0,  2, "BD",                   probe_hfsplus },
+  { "hfsplus",  1,      0,  2, "H+",                   0 },
   { "hfs",      1,      0,  2, "BD",                   0 },
   { "ufs",      8,  0x55c,  4, "T\031\001\000",        0 },
   { "hpfs",     8,      0,  4, "I\350\225\371",        0 },
@@ -645,6 +1110,9 @@ static struct blkid_magic type_array[] = {
   { "ocfs2",    2,      0,  6, "OCFSV2",               probe_ocfs2 },
   { "ocfs2",    4,      0,  6, "OCFSV2",               probe_ocfs2 },
   { "ocfs2",    8,      0,  6, "OCFSV2",               probe_ocfs2 },
+  { "crypt_LUKS", 0,    0,  6, "LUKS\xba\xbe",         probe_luks },
+  { "squashfs",         0,      0,  4, "sqsh",                 0 },
+  { "squashfs",         0,      0,  4, "hsqs",                 0 },
   {   NULL,     0,      0,  0, NULL,                   NULL }
 };
 
@@ -660,11 +1128,13 @@ static struct blkid_magic type_array[] = {
 blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
 {
        struct blkid_magic *id;
-       unsigned char *bufs[BLKID_BLK_OFFS + 1], *buf;
-       const char *type;
+       struct blkid_probe probe;
+       blkid_tag_iterate iter;
+       unsigned char *buf;
+       const char *type, *value;
        struct stat st;
        time_t diff, now;
-       int fd, idx;
+       int idx;
 
        if (!dev)
                return NULL;
@@ -672,19 +1142,20 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
        now = time(0);
        diff = now - dev->bid_time;
 
-       if ((now < dev->bid_time) ||
-           (diff < BLKID_PROBE_MIN) || 
-           (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
-            diff < BLKID_PROBE_INTERVAL))
+       if ((now > dev->bid_time) && (diff > 0) && 
+           ((diff < BLKID_PROBE_MIN) || 
+            (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
+             diff < BLKID_PROBE_INTERVAL)))
                return dev;
 
        DBG(DEBUG_PROBE,
-           printf("need to revalidate %s (time since last check %lu)\n", 
-                  dev->bid_name, diff));
+           printf("need to revalidate %s (time since last check %llu)\n", 
+                  dev->bid_name, (unsigned long long)diff));
 
-       if (((fd = open(dev->bid_name, O_RDONLY)) < 0) ||
-           (fstat(fd, &st) < 0)) {
-               if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
+       if (((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) ||
+           (fstat(probe.fd, &st) < 0)) {
+               if (probe.fd >= 0) close(probe.fd);
+               if (errno != EPERM) {
                        blkid_free_dev(dev);
                        return NULL;
                }
@@ -695,7 +1166,11 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
                return dev;
        }
 
-       memset(bufs, 0, sizeof(bufs));
+       probe.cache = cache;
+       probe.dev = dev;
+       probe.sbbuf = 0;
+       probe.buf = 0;
+       probe.buf_max = 0;
        
        /*
         * Iterate over the type array.  If we already know the type,
@@ -708,8 +1183,8 @@ try_again:
        if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
                uuid_t  uuid;
 
-               if (check_mdraid(fd, uuid) == 0) {
-                       set_uuid(dev, uuid);
+               if (check_mdraid(probe.fd, uuid) == 0) {
+                       set_uuid(dev, uuid, 0);
                        type = "mdraid";
                        goto found_type;
                }
@@ -720,29 +1195,16 @@ try_again:
                        continue;
 
                idx = id->bim_kboff + (id->bim_sboff >> 10);
-               if (idx > BLKID_BLK_OFFS || idx < 0)
+               buf = get_buffer(&probe, idx << 10, 1024);
+               if (!buf)
                        continue;
-               buf = bufs[idx];
-               if (!buf) {
-                       if (lseek(fd, idx << 10, SEEK_SET) < 0)
-                               continue;
-
-                       if (!(buf = (unsigned char *)malloc(1024)))
-                               continue;
-                       
-                       if (read(fd, buf, 1024) != 1024) {
-                               free(buf);
-                               continue;
-                       }
-                       bufs[idx] = buf;
-               }
 
                if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
                           id->bim_len))
                        continue;
 
                if ((id->bim_probe == NULL) ||
-                   (id->bim_probe(fd, cache, dev, id, buf) == 0)) {
+                   (id->bim_probe(&probe, id, buf) == 0)) {
                        type = id->bim_type;
                        goto found_type;
                }
@@ -750,18 +1212,22 @@ try_again:
 
        if (!id->bim_type && dev->bid_type) {
                /*
-                * Zap the device filesystem type and try again
+                * Zap the device filesystem information and try again
                 */
-               blkid_set_tag(dev, "TYPE", 0, 0);
-               blkid_set_tag(dev, "SEC_TYPE", 0, 0);
-               blkid_set_tag(dev, "LABEL", 0, 0);
-               blkid_set_tag(dev, "UUID", 0, 0);
+               DBG(DEBUG_PROBE,
+                   printf("previous fs type %s not valid, "
+                          "trying full probe\n", dev->bid_type));
+               iter = blkid_tag_iterate_begin(dev);
+               while (blkid_tag_next(iter, &type, &value) == 0)
+                       blkid_set_tag(dev, type, 0, 0);
+               blkid_tag_iterate_end(iter);
                goto try_again;
        }
 
        if (!dev->bid_type) {
                blkid_free_dev(dev);
-               return NULL;
+               dev = 0;
+               goto found_type;
        }
                
 found_type:
@@ -774,10 +1240,15 @@ found_type:
                blkid_set_tag(dev, "TYPE", type, 0);
                                
                DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
-                          dev->bid_name, st.st_rdev, type));
+                          dev->bid_name, (long long)st.st_rdev, type));
        }
 
-       close(fd);
+       if (probe.sbbuf)
+               free(probe.sbbuf);
+       if (probe.buf)
+               free(probe.buf);
+       if (probe.fd >= 0) 
+               close(probe.fd);
 
        return dev;
 }
@@ -800,7 +1271,6 @@ int main(int argc, char **argv)
        blkid_cache cache;
        int ret;
 
-       blkid_debug_mask = DEBUG_ALL;
        if (argc != 2) {
                fprintf(stderr, "Usage: %s device\n"
                        "Probe a single device to determine type\n", argv[0]);
@@ -816,12 +1286,11 @@ int main(int argc, char **argv)
                printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
                return (1);
        }
-       printf("%s is type %s\n", argv[1], dev->bid_type ?
-               dev->bid_type : "(null)");
+       printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
        if (dev->bid_label)
-               printf("\tlabel is '%s'\n", dev->bid_label);
+               printf("LABEL='%s'\n", dev->bid_label);
        if (dev->bid_uuid)
-               printf("\tuuid is %s\n", dev->bid_uuid);
+               printf("UUID='%s'\n", dev->bid_uuid);
        
        blkid_free_dev(dev);
        return (0);