Whamcloud - gitweb
blkid: Flush cached filesystem information on any error other than EPERM
[tools/e2fsprogs.git] / lib / blkid / probe.c
index d58e050..917447b 100644 (file)
@@ -3,8 +3,9 @@
  *           struct with the details
  *
  * Copyright (C) 1999 by Andries Brouwer
- * Copyright (C) 1999, 2000 by Theodore Ts'o
+ * 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
 #ifdef HAVE_SYS_MKDEV_H
 #include <sys/mkdev.h>
 #endif
+#include <sys/utsname.h>
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
-#include "blkid/blkid.h"
+#include "blkidP.h"
 #include "uuid/uuid.h"
 #include "probe.h"
 
-/* #define DEBUG_PROBE */
-#ifdef DEBUG_PROBE
-#define DEB_PROBE(fmt, arg...) printf("probe: " fmt, ## arg)
-#else
-#define DEB_PROBE(fmt, arg...) do {} while (0)
-#endif
+static int figure_label_len(const unsigned char *label, int len)
+{
+       const unsigned char *end = label + len - 1;
+
+       while ((*end == ' ' || *end == 0) && end >= label)
+               --end;
+       if (end >= label) {
+               label = label;
+               return end - label + 1;
+       }
+       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;
+       }
+}
+
 
 /*
- * Do the required things for instantiating a new device.  This is called if
- * there is nor a probe handler for a filesystem type, and is also called by
- * the filesystem-specific types to do common initialization tasks.
- *
- * The devname, dev_p, and id fields are required.  The buf is
- * a buffer to return superblock data in.
+ * 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
+ * of the device.
  */
-static int probe_default(int fd, blkid_dev **dev_p, const char *devname,
-                        struct blkid_magic *id, unsigned char *buf,
-                        blkid_loff_t size)
+static int check_mdraid(int fd, unsigned char *ret_uuid)
 {
-       blkid_loff_t offset;
-       blkid_dev *dev;
-       struct stat st;
-       int ret;
-
-       if (!devname || !dev_p || !id || !buf || fd < 0)
+       struct mdp_superblock_s *md;
+       blkid_loff_t            offset;
+       char                    buf[4096];
+       
+       if (fd < 0)
                return -BLKID_ERR_PARAM;
 
-       if (fstat(fd, &st) < 0 || !S_ISBLK(st.st_mode))
-               return -BLKID_ERR_DEV;
-
-       offset = (blkid_loff_t)id->bim_kboff << 10;
-       if (id->bim_kboff < 0)
-               offset = (size & ~((blkid_loff_t)id->bim_align - 1)) + offset;
+       offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
 
        if (blkid_llseek(fd, offset, 0) < 0 ||
-           read(fd, buf, id->bim_kbsize << 10) != id->bim_kbsize << 10)
+           read(fd, buf, 4096) != 4096)
                return -BLKID_ERR_IO;
 
-       /* Revalidate magic for blkid_validate_devname */
-       if (memcmp(id->bim_magic, buf + id->bim_sboff, id->bim_len))
+       /* Check for magic number */
+       if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4))
                return -BLKID_ERR_PARAM;
 
-       dev = blkid_new_dev();
-       if (!dev)
-               return -BLKID_ERR_MEM;
+       if (!ret_uuid)
+               return 0;
+       *ret_uuid = 0;
 
-       dev->bid_name = string_copy(devname);
-       if (!dev->bid_name) {
-               ret = -BLKID_ERR_MEM;
-               goto exit_dev;
+       /* The MD UUID is not contiguous in the superblock, make it so */
+       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 + 4, &md->set_uuid1, 12);
        }
+       return 0;
+}
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
-       dev->bid_devno = st.st_rdev;
-       dev->bid_devsize = size;
-       dev->bid_time = time(0);
-       dev->bid_flags |= BLKID_BID_FL_VERIFIED;
+static void set_uuid(blkid_dev dev, uuid_t uuid, char *tag)
+{
+       char    str[37];
 
-       if (id->bim_type)
-               blkid_create_tag(dev, NULL, "TYPE", id->bim_type,
-                                strlen(id->bim_type));
+       if (!uuid_is_null(uuid)) {
+               uuid_unparse(uuid, str);
+               blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
+       }
+}
 
-       DEB_PROBE("%s: devno 0x%04Lx, type %s\n", devname,
-                 st.st_rdev, id->bim_type);
+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;
 
-       return 0;
-exit_dev:
-       blkid_free_dev(dev);
+       DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n", 
+                  blkid_le32(es->s_feature_compat),
+                  blkid_le32(es->s_feature_incompat),
+                  blkid_le32(es->s_feature_ro_compat)));
+
+       if (strlen(es->s_volume_name))
+               label = es->s_volume_name;
+       blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
+
+       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"));
+}
+
+/*
+ * 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 probe_ext2(int fd, blkid_dev **dev_p, const char *devname,
-                     struct blkid_magic *id, unsigned char *buf,
-                     blkid_loff_t size)
+static int system_supports_ext4dev()
 {
-       blkid_dev *dev;
-       struct ext2_super_block *es;
-       int ret;
+       static time_t   last_check = 0;
+       static int      ret = -1;
+       time_t          now = time(0);
 
-       if ((ret = probe_default(fd, &dev, devname, id, buf, size)) < 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;
 
-       DEB_PROBE("size = %Ld, ext2_sb.compat = %08X:%08X:%08X\n", size,
-                 le32_to_cpu(es->s_feature_compat),
-                 le32_to_cpu(es->s_feature_incompat),
-                 le32_to_cpu(es->s_feature_ro_compat));
-
-       /* Make sure we don't keep re-probing as ext2 for a journaled fs */
-       if (!strcmp(id->bim_type, "ext2") &&
-           (le32_to_cpu(es->s_feature_compat) &
-            EXT3_FEATURE_COMPAT_HAS_JOURNAL ||
-            le32_to_cpu(es->s_feature_incompat) &
-            EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
-               blkid_free_dev(dev);
+       /* Distinguish from jbd */
+       if (blkid_le32(es->s_feature_incompat) &
+           EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
                return -BLKID_ERR_PARAM;
-       }
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
+       /* ext4dev requires a journal */
+       if (!(blkid_le32(es->s_feature_compat) &
+             EXT3_FEATURE_COMPAT_HAS_JOURNAL))
+               return -BLKID_ERR_PARAM;
 
-       dev->bid_size = (blkid_loff_t)le32_to_cpu(es->s_blocks_count) <<
-               (le32_to_cpu(es->s_log_block_size) + 10);
+       /*
+        * 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;
 
-       /* This is a safe (minimum) number, as it ignores metadata usage. */
-       dev->bid_free = (blkid_loff_t)le32_to_cpu(es->s_free_blocks_count) <<
-               (le32_to_cpu(es->s_log_block_size) + 10);
+       get_ext2_info(probe->dev, id, buf);
+       return 0;
+}
 
-       if (strlen(es->s_volume_name)) {
-               blkid_create_tag(dev, NULL, "LABEL", es->s_volume_name,
-                                sizeof(es->s_volume_name));
-       }
+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;
 
-       if (!uuid_is_null(es->s_uuid)) {
-               unsigned char uuid[37];
-               uuid_unparse(es->s_uuid, uuid);
-               blkid_create_tag(dev, NULL, "UUID", uuid, sizeof(uuid));
-       }
+       /* Distinguish from jbd */
+       if (blkid_le32(es->s_feature_incompat) & 
+           EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
+               return -BLKID_ERR_PARAM;
 
+       /* ext4 requires journal */
+       if (!(blkid_le32(es->s_feature_compat) &
+             EXT3_FEATURE_COMPAT_HAS_JOURNAL))
+               return -BLKID_ERR_PARAM;
+
+       /* 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_jbd(int fd, blkid_dev **dev_p, const char *devname,
-                    struct blkid_magic *id, unsigned char *buf,
-                    blkid_loff_t size)
+static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id,
+                     unsigned char *buf)
 {
-       blkid_dev *dev;
        struct ext2_super_block *es;
-       int ret;
+       es = (struct ext2_super_block *)buf;
 
-       if ((ret = probe_ext2(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
+       /* Distinguish from ext4dev */
+       if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS)
+               return -BLKID_ERR_PARAM;
 
-       es = (struct ext2_super_block *)buf;
+       /* ext3 requires journal */
+       if (!(blkid_le32(es->s_feature_compat) &
+             EXT3_FEATURE_COMPAT_HAS_JOURNAL))
+               return -BLKID_ERR_PARAM;
 
-       if (!(le32_to_cpu(es->s_feature_incompat) &
-             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
-               blkid_free_dev(dev);
+       /* 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;
-       }
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
+       get_ext2_info(probe->dev, id, buf);
        return 0;
 }
 
-static int probe_ext3(int fd, blkid_dev **dev_p, const char *devname,
-                    struct blkid_magic *id, unsigned char *buf,
-                    blkid_loff_t size)
+static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id,
+                     unsigned char *buf)
 {
-       blkid_dev *dev;
        struct ext2_super_block *es;
-       int ret;
-
-       if ((ret = probe_ext2(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
 
        es = (struct ext2_super_block *)buf;
 
-       if (!(le32_to_cpu(es->s_feature_compat) &
-             EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
-               blkid_free_dev(dev);
-               *dev_p = NULL;
+       /* Distinguish between ext3 and ext2 */
+       if ((blkid_le32(es->s_feature_compat) &
+             EXT3_FEATURE_COMPAT_HAS_JOURNAL))
                return -BLKID_ERR_PARAM;
-       }
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
 
-       if (!(le32_to_cpu(es->s_feature_incompat) &
-             EXT3_FEATURE_INCOMPAT_RECOVER)) {
-               blkid_create_tag(dev, NULL, "TYPE", "ext2", 4);
-               dev->bid_flags |= BLKID_BID_FL_MTYPE;
-       }
+       /* 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_vfat(int fd, blkid_dev **dev_p, const char *devname,
-                     struct blkid_magic *id, unsigned char *buf,
-                     blkid_loff_t size)
+static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id,
+                    unsigned char *buf)
 {
-       blkid_dev *dev;
-       struct vfat_super_block *vs;
-       char serno[10];
-       blkid_loff_t sectors;
-       int cluster_size;
-       int ret;
+       struct ext2_super_block *es = (struct ext2_super_block *) buf;
 
-       if ((ret = probe_default(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
+       if (!(blkid_le32(es->s_feature_incompat) &
+             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
+               return -BLKID_ERR_PARAM;
 
-       vs = (struct vfat_super_block *)buf;
+       get_ext2_info(probe->dev, id, buf);
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
+       return 0;
+}
 
-       sectors = ((vs->vs_sectors[1] << 8) | vs->vs_sectors[0]);
-       if (sectors == 0)
-               sectors = vs->vs_total_sect;
-       cluster_size = ((vs->vs_sector_size[1] << 8) | vs->vs_sector_size[0]);
-       dev->bid_size = sectors * cluster_size;
-       DEB_PROBE("%Ld %d byte sectors\n", sectors, cluster_size);
+#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
 
-       if (strncmp(vs->vs_label, "NO NAME", 7)) {
-               unsigned char *end = vs->vs_label + sizeof(vs->vs_label) - 1;
+static char *no_name = "NO NAME    ";
 
-               while (*end == ' ' && end >= vs->vs_label)
-                       --end;
-               if (end >= vs->vs_label)
-                       blkid_create_tag(dev, NULL, "LABEL", vs->vs_label,
-                                        end - vs->vs_label + 1);
-       }
+static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
+{
+       int i;
 
-       /* We can't just print them as %04X, because they are unaligned */
-       sprintf(serno, "%02X%02X-%02X%02X", vs->vs_serno[3], vs->vs_serno[2],
-               vs->vs_serno[1], vs->vs_serno[0]);
-       blkid_create_tag(dev, NULL, "UUID", serno, sizeof(serno));
+       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;
 }
 
-static int probe_msdos(int fd, blkid_dev **dev_p, const char *devname,
-                      struct blkid_magic *id, unsigned char *buf,
-                      blkid_loff_t size)
+/* 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)
 {
-       blkid_dev *dev;
-       struct msdos_super_block *ms;
+       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];
-       int cluster_size;
-       blkid_loff_t sectors;
-       int ret;
+       const unsigned char *label = 0, *vol_label = 0, *tmp;
+       unsigned char   *vol_serno;
+       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 */
+       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;
+
+       tmp = (unsigned char *)&ms->ms_dir_entries;
+       dir_entries = tmp[0] + (tmp[1] << 8);
+       reserved =  blkid_le16(ms->ms_reserved);
+       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);
+
+       fat_length = blkid_le16(ms->ms_fat_length);
+       if (fat_length == 0)
+               fat_length = blkid_le32(vs->vs_fat32_length);
+
+       fat_size = fat_length * ms->ms_fats;
+       dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
+                       (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) {
+               /* 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(probe->dev, "SEC_TYPE", "msdos", 
+                             sizeof("msdos"));
+       } else {
+               /* 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;
 
-       if ((ret = probe_default(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
+                       count = buf_size / sizeof(struct vfat_dir_entry);
 
-       ms = (struct msdos_super_block *)buf;
+                       vol_label = search_fat_label(dir, count);
+                       if (vol_label)
+                               break;
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
+                       /* 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;
 
-       sectors = ((ms->ms_sectors[1] << 8) | ms->ms_sectors[0]);
-       if (sectors == 0)
-               sectors = ms->ms_total_sect;
-       cluster_size = ((ms->ms_sector_size[1] << 8) | ms->ms_sector_size[0]);
-       dev->bid_size = sectors * cluster_size;
-       DEB_PROBE("%Ld %d byte sectors\n", sectors, cluster_size);
+                       /* set next cluster */
+                       next = blkid_le32(*((__u32 *) buf) & 0x0fffffff);
+               }
 
-       if (strncmp(ms->ms_label, "NO NAME", 7)) {
-               unsigned char *end = ms->ms_label + sizeof(ms->ms_label) - 1;
+               if (!vol_label || !memcmp(vol_label, no_name, 11))
+                       vol_label = vs->vs_label;
+               vol_serno = vs->vs_serno;
+       }
 
-               while (*end == ' ' && end >= ms->ms_label)
-                       --end;
-               if (end >= ms->ms_label)
-                       blkid_create_tag(dev, NULL, "LABEL", ms->ms_label,
-                                        end - ms->ms_label + 1);
+       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", ms->ms_serno[3], ms->ms_serno[2],
-               ms->ms_serno[1], ms->ms_serno[0]);
-       blkid_create_tag(dev, NULL, "UUID", serno, sizeof(serno));
+       sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
+               vol_serno[1], vol_serno[0]);
+
+       blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len);
+       blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1);
 
        return 0;
 }
 
-static int probe_xfs(int fd, blkid_dev **dev_p, const char *devname,
-                    struct blkid_magic *id, unsigned char *buf,
-                    blkid_loff_t size)
+/*
+ * The FAT filesystem could be without a magic string in superblock
+ * (e.g. old floppies).  This heuristic for FAT detection is inspired
+ * by http://vrfy.org/projects/volume_id/ and Linux kernel.
+ * [7-Jul-2005, Karel Zak <kzak@redhat.com>]
+ */
+static int probe_fat_nomagic(struct blkid_probe *probe,
+                            struct blkid_magic *id __BLKID_ATTR((unused)), 
+                            unsigned char *buf)
 {
-       blkid_dev *dev;
-       struct xfs_super_block *xs;
-       int ret;
+       struct vfat_super_block *vs;
 
-       if ((ret = probe_default(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
+       vs = (struct vfat_super_block *)buf;
 
-       xs = (struct xfs_super_block *)buf;
+       /* heads check */
+       if (vs->vs_heads == 0)
+               return 1;
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
-       /* If the filesystem size is larger than the device, this is bad */
-       dev->bid_size = be64_to_cpu(xs->xs_dblocks) *
-               be32_to_cpu(xs->xs_blocksize);
-       dev->bid_free = be64_to_cpu(xs->xs_fdblocks) *
-               be32_to_cpu(xs->xs_blocksize);
+       /* cluster size check*/ 
+       if (vs->vs_cluster_size == 0 ||
+           (vs->vs_cluster_size & (vs->vs_cluster_size-1)))
+               return 1;
 
-       if (strlen(xs->xs_fname))
-               blkid_create_tag(dev, NULL, "LABEL", xs->xs_fname,
-                                sizeof(xs->xs_fname));
+       /* media check */
+       if (vs->vs_media < 0xf8 && vs->vs_media != 0xf0)
+               return 1;
 
-       if (!uuid_is_null(xs->xs_uuid)) {
-               char uuid[37];
-               uuid_unparse(xs->xs_uuid, uuid);
-               blkid_create_tag(dev, NULL, "UUID", uuid, sizeof(uuid));
-       }
-       return 0;
+       /* fat counts(Linux kernel expects at least 1 FAT table) */
+       if (!vs->vs_fats)
+               return 1;
+
+       return probe_fat(probe, id, buf);
 }
 
-static int probe_reiserfs(int fd, blkid_dev **dev_p, const char *devname,
-                         struct blkid_magic *id, unsigned char *buf,
-                         blkid_loff_t size)
+static int probe_ntfs(struct blkid_probe *probe,
+                     struct blkid_magic *id __BLKID_ATTR((unused)), 
+                     unsigned char *buf)
 {
-       blkid_dev *dev;
-       struct reiserfs_super_block *rs;
-       unsigned int blocksize;
-       int ret;
+       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 ((ret = probe_default(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
+       if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) ||
+           (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters))
+               return 1;
 
-       rs = (struct reiserfs_super_block *)buf;
+       off = blkid_le64(ns->mft_mirror_cluster_location) * 
+               bytes_per_sector * sectors_per_cluster;
 
-       blocksize = le16_to_cpu(rs->rs_blocksize);
+       buf_mft = get_buffer(probe, off, mft_record_size);
+       if (!buf_mft)
+               return 1;
 
-       /* If the superblock is inside the journal, we have the wrong one */
-       if (id->bim_kboff/(blocksize>>10) > le32_to_cpu(rs->rs_journal_block)) {
-               blkid_free_dev(dev);
-               return -BLKID_ERR_BIG;
-       }
+       if (memcmp(buf_mft, "FILE", 4))
+               return 1;
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
+       off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector * 
+               sectors_per_cluster;
 
-       /* If the filesystem size is larger than the device, this is bad */
-       dev->bid_size = le32_to_cpu(rs->rs_blocks_count) * blocksize;
-       dev->bid_free = le32_to_cpu(rs->rs_free_blocks) * blocksize;
+       buf_mft = get_buffer(probe, off, mft_record_size);
+       if (!buf_mft)
+               return 1;
 
-       /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
-       if (!strcmp(id->bim_magic, "ReIsEr2Fs") ||
-           !strcmp(id->bim_magic, "ReIsEr3Fs")) {
-               if (strlen(rs->rs_label)) {
-                       blkid_create_tag(dev, NULL, "LABEL", rs->rs_label,
-                                        sizeof(rs->rs_label));
-               }
+       if (memcmp(buf_mft, "FILE", 4))
+               return 1;
 
-               if (!uuid_is_null(rs->rs_uuid)) {
-                       unsigned char uuid[37];
-                       uuid_unparse(rs->rs_uuid, uuid);
-                       blkid_create_tag(dev, NULL, "UUID", uuid, sizeof(uuid));
-               }
-       }
+       off += MFT_RECORD_VOLUME * mft_record_size;
 
-       return 0;
-}
+       buf_mft = get_buffer(probe, off, mft_record_size);
+       if (!buf_mft)
+               return 1;
 
-static int probe_minix(int fd, blkid_dev **dev_p, const char *devname,
-                      struct blkid_magic *id, unsigned char *buf,
-                      blkid_loff_t size)
-{
-       blkid_dev *dev;
-       struct minix_super_block *ms;
-       int ret;
+       if (memcmp(buf_mft, "FILE", 4))
+               return 1;
 
-       if ((ret = probe_default(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
+       mft = (struct master_file_table_record *) buf_mft;
 
-       ms = (struct minix_super_block *)buf;
+       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);
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
-       dev->bid_size = ms->ms_nzones << ms->ms_log_zone_size;
-       return 0;
-}
-
-static int probe_swap(int fd, blkid_dev **dev_p, const char *devname,
-                     struct blkid_magic *id, unsigned char *buf,
-                     blkid_loff_t size)
-{
-       blkid_dev *dev;
-       struct swap_header *sh;
-       int psize;
-       int ret;
+               attr_off += attr_len;
 
-       if ((ret = probe_default(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
-
-       /* PAGE_SIZE can be found by where the magic is located */
-       psize = (id->bim_kboff << 10) + (id->bim_sboff + 10);
+               if ((attr_off > mft_record_size) ||
+                   (attr_len == 0))
+                       break;
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
+               if (attr_type == MFT_RECORD_ATTR_END)
+                       break;
 
-       sh = (struct swap_header *)buf;
-       /* Is swap data in local endian format? */
-       dev->bid_size = (blkid_loff_t)(sh->sh_last_page + 1) * psize;
+               if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
+                       if (val_len > sizeof(label_str))
+                               val_len = sizeof(label_str)-1;
 
-       /* A label can not exist on the old (128MB max) swap format */
-       if (!strcmp(id->bim_magic, "SWAPSPACE2") && sh->sh_label[0]) {
-               blkid_create_tag(dev, NULL, "LABEL", sh->sh_label,
-                                sizeof(sh->sh_label));
+                       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_mdraid(int fd, blkid_dev **dev_p, const char *devname,
-                       struct blkid_magic *id, unsigned char *buf,
-                       blkid_loff_t size)
+
+static int probe_xfs(struct blkid_probe *probe,
+                    struct blkid_magic *id __BLKID_ATTR((unused)), 
+                    unsigned char *buf)
 {
-       blkid_dev *dev;
-       struct mdp_superblock_s *md;
-       int ret;
+       struct xfs_super_block *xs;
+       const char *label = 0;
 
-       if ((ret = probe_default(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
+       xs = (struct xfs_super_block *)buf;
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
+       if (strlen(xs->xs_fname))
+               label = xs->xs_fname;
+       blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname));
+       set_uuid(probe->dev, xs->xs_uuid, 0);
+       return 0;
+}
 
-       md = (struct mdp_superblock_s *)buf;
-       /* What units is md->size in?  Assume 512-byte sectors? */
-       dev->bid_size = md->size * 512;
+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;
+       unsigned int blocksize;
+       const char *label = 0;
 
-       /* The MD UUID is not contiguous in the superblock, make it so */
-       if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
-               unsigned char md_uuid[16];
-               unsigned char uuid[37];
+       blocksize = blkid_le16(rs->rs_blocksize);
 
-               memcpy(md_uuid, &md->set_uuid0, 4);
-               memcpy(md_uuid + 4, &md->set_uuid1, 12);
+       /* The blocksize must be at least 1k */
+       if ((blocksize >> 10) == 0)
+               return -BLKID_ERR_PARAM;
 
-               uuid_unparse(md_uuid, uuid);
-               blkid_create_tag(dev, NULL, "UUID", uuid, sizeof(uuid));
+       /* 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 (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') {
+               if (strlen(rs->rs_label))
+                       label = rs->rs_label;
+               set_uuid(probe->dev, rs->rs_uuid, 0);
        }
+       blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label));
+
        return 0;
 }
 
-static int probe_hfs(int fd, blkid_dev **dev_p, const char *devname,
-                    struct blkid_magic *id, unsigned char *buf,
-                    blkid_loff_t size)
+static int probe_reiserfs4(struct blkid_probe *probe,
+                          struct blkid_magic *id __BLKID_ATTR((unused)), 
+                          unsigned char *buf)
 {
-       blkid_dev *dev;
-       struct hfs_super_block *hfs;
-       int ret;
+       struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf;
+       const unsigned char *label = 0;
 
-       if ((ret = probe_default(fd, &dev, devname, id, buf, size)) < 0)
-               return ret;
+       if (strlen((char *) rs4->rs4_label))
+               label = rs4->rs4_label;
+       set_uuid(probe->dev, rs4->rs4_uuid, 0);
+       blkid_set_tag(probe->dev, "LABEL", (const char *) label, 
+                     sizeof(rs4->rs4_label));
 
-       hfs = (struct hfs_super_block *)buf;
+       return 0;
+}
 
-       if (be32_to_cpu(hfs->h_blksize) != 512)
-               return -BLKID_ERR_PARAM;
+static int probe_jfs(struct blkid_probe *probe,
+                    struct blkid_magic *id __BLKID_ATTR((unused)), 
+                    unsigned char *buf)
+{
+       struct jfs_super_block *js;
+       const char *label = 0;
 
-       /* Don't set this until there is no chance of error */
-       *dev_p = dev;
+       js = (struct jfs_super_block *)buf;
 
+       if (strlen((char *) js->js_label))
+               label = (char *) js->js_label;
+       blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label));
+       set_uuid(probe->dev, js->js_uuid, 0);
        return 0;
 }
 
+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;
+}
 
-/*
- * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
- * in the type_array table below + bim_kbalign.  If we ever start looking for magics
- * relative to the end of a device, we can start using negative offsets
- * in type_array.
- */
-#define BLKID_BLK_BITS (10)
-#define BLKID_BLK_KBITS        (BLKID_BLK_BITS - 10)
-#define BLKID_BLK_SIZE (1024 << BLKID_BLK_KBITS)
-#define BLKID_BLK_MASK  (BLKID_BLK_SIZE - 1)
-#define BLKID_BLK_OFFS 128     /* currently MDRAID kboff + align */
+static int probe_romfs(struct blkid_probe *probe,
+                      struct blkid_magic *id __BLKID_ATTR((unused)), 
+                      unsigned char *buf)
+{
+       struct romfs_super_block *ros;
+       const char *label = 0;
 
-/*
- * Various filesystem magics that we can check for.  Note that kboff and
- * sboff are in kilobytes and bytes respectively.  All magics are in
- * byte strings so we don't worry about endian issues.
- */
-struct blkid_magic type_array[] = {
-/*  type     kboff   sboff len  magic           align kbsize probe */
-  { "MDRAID",  -64,      0,  4, "\251+N\374",   65536,  4, probe_mdraid },
-/*{ "LVM",       0,      0,  4, "HM\001\000",       1,  4, probe_lvm },*/
-  { "jbd",       1,   0x38,  2, "\123\357",         1,  1, probe_jbd },
-  { "ext3",      1,   0x38,  2, "\123\357",         1,  1, probe_ext3 },
-  { "ext2",      1,   0x38,  2, "\123\357",         1,  1, probe_ext2 },
-  { "reiserfs",  8,   0x34,  8, "ReIsErFs",         1,  1, probe_reiserfs },
-  { "reiserfs", 64,   0x34,  9, "ReIsEr2Fs",        1,  1, probe_reiserfs },
-  { "reiserfs", 64,   0x34,  9, "ReIsEr3Fs",        1,  1, probe_reiserfs },
-  { "reiserfs", 64,   0x34,  8, "ReIsErFs",         1,  1, probe_reiserfs },
-  { "reiserfs",  8,     20,  8, "ReIsErFs",         1,  1, probe_reiserfs },
-  { "ntfs",      0,      3,  8, "NTFS    ",         1,  1, probe_default },
-  { "vfat",      0,   0x52,  5, "MSWIN",            1,  1, probe_vfat },
-  { "vfat",      0,   0x52,  8, "FAT32   ",         1,  1, probe_vfat },
-  { "msdos",     0,   0x36,  5, "MSDOS",            1,  1, probe_msdos },
-  { "msdos",     0,   0x36,  8, "FAT16   ",         1,  1, probe_msdos },
-  { "msdos",     0,   0x36,  8, "FAT12   ",         1,  1, probe_msdos },
-  { "minix",     1,   0x10,  2, "\177\023",         1,  1, probe_minix },
-  { "minix",     1,   0x10,  2, "\217\023",         1,  1, probe_minix },
-  { "minix",     1,   0x10,  2, "\150\044",         1,  1, probe_minix },
-  { "minix",     1,   0x10,  2, "\170\044",         1,  1, probe_minix },
-  { "vxfs",      1,      0,  4, "\365\374\001\245", 1,  1, probe_default },
-  { "xfs",       0,      0,  4, "XFSB",             1,  1, probe_xfs },
-  { "romfs",     0,      0,  8, "-rom1fs-",         1,  1, probe_default },
-  { "bfs",       0,      0,  4, "\316\372\173\033", 1,  1, probe_default },
-  { "cramfs",    0,      0,  4, "E=\315\034",       1,  1, probe_default },
-  { "qnx4",      0,      4,  6, "QNX4FS",           1,  1, probe_default },
-  { "iso9660",  32,      1,  5, "CD001",            1,  1, probe_default },
-  { "iso9660",  32,      9,  5, "CDROM",            1,  1, probe_default },
-  { "udf",      32,      1,  5, "BEA01",            1,  1, probe_default },
-  { "udf",      32,      1,  5, "BOOT2",            1,  1, probe_default },
-  { "udf",      32,      1,  5, "CD001",            1,  1, probe_default },
-  { "udf",      32,      1,  5, "CDW02",            1,  1, probe_default },
-  { "udf",      32,      1,  5, "NSR02",            1,  1, probe_default },
-  { "udf",      32,      1,  5, "NSR03",            1,  1, probe_default },
-  { "udf",      32,      1,  5, "TEA01",            1,  1, probe_default },
-  { "jfs",      32,      0,  4, "JFS1",             1,  1, probe_default },
-  { "hfs",       1,      0,  2, "BD",               1,  1, probe_hfs },
-  { "ufs",       8,  0x55c,  4, "T\031\001\000",    1,  1, probe_default },
-  { "hpfs",      8,      0,  4, "I\350\225\371",    1,  1, probe_default },
-  { "sysv",      0,  0x3f8,  4, "\020~\030\375",    1,  1, probe_default },
-  { "swap",      0,  0xff6, 10, "SWAP-SPACE",       1,  4, probe_swap },
-  { "swap",      0,  0xff6, 10, "SWAPSPACE2",       1,  4, probe_swap },
-  { "swap",      0, 0x1ff6, 10, "SWAP-SPACE",       1,  8, probe_swap },
-  { "swap",      0, 0x1ff6, 10, "SWAPSPACE2",       1,  8, probe_swap },
-  { "swap",      0, 0x3ff6, 10, "SWAP-SPACE",       1, 16, probe_swap },
-  { "swap",      0, 0x3ff6, 10, "SWAPSPACE2",       1, 16, probe_swap },
-  {   NULL,      0,      0,  0, NULL,               1,  0, NULL }
-};
+       ros = (struct romfs_super_block *)buf;
 
+       if (strlen((char *) ros->ros_volume))
+               label = (char *) ros->ros_volume;
+       blkid_set_tag(probe->dev, "LABEL", label, 0);
+       return 0;
+}
 
-/*
- * When probing for a lot of magics, we handle everything in 1kB buffers so
- * that we don't have to worry about reading each combination of block sizes.
- */
-static unsigned char *read_one_buf(int fd, blkid_loff_t offset)
+static int probe_cramfs(struct blkid_probe *probe,
+                       struct blkid_magic *id __BLKID_ATTR((unused)), 
+                       unsigned char *buf)
 {
-       char *buf;
-
-       if (lseek(fd, offset, SEEK_SET) < 0)
-               return NULL;
+       struct cramfs_super_block *csb;
+       const char *label = 0;
 
-       if (!(buf = (unsigned char *)malloc(BLKID_BLK_SIZE)))
-               return NULL;
+       csb = (struct cramfs_super_block *)buf;
 
-       if (read(fd, buf, BLKID_BLK_SIZE) != BLKID_BLK_SIZE) {
-               free(buf);
-               return NULL;
-       }
-
-       return buf;
+       if (strlen((char *) csb->name))
+               label = (char *) csb->name;
+       blkid_set_tag(probe->dev, "LABEL", label, 0);
+       return 0;
 }
 
-static unsigned char *read_sb_buf(int fd, unsigned char **bufs, int kboff,
-                                 blkid_loff_t start)
+static int probe_swap0(struct blkid_probe *probe,
+                      struct blkid_magic *id __BLKID_ATTR((unused)),
+                      unsigned char *buf __BLKID_ATTR((unused)))
 {
-       int index = kboff >> BLKID_BLK_KBITS;
-       unsigned char **buf;
+       blkid_set_tag(probe->dev, "UUID", 0, 0);
+       blkid_set_tag(probe->dev, "LABEL", 0, 0);
+       return 0;
+}
 
-       if (index > BLKID_BLK_OFFS || index < -BLKID_BLK_OFFS) {
-               fprintf(stderr, "reading from invalid offset %d (%d)!\n",
-                       kboff, index);
-               return NULL;
+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(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).
+        */
+       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(probe->dev, "LABEL", sws->sws_volume, 
+                                     sizeof(sws->sws_volume));
+               if (sws->sws_uuid[0])
+                       set_uuid(probe->dev, sws->sws_uuid, 0);
        }
-
-       buf = bufs + index;
-       if (!*buf)
-               *buf = read_one_buf(fd, start);
-
-       return *buf;
+       return 0;
 }
 
-static struct blkid_magic *devname_to_magic(const char *devname, int fd,
-                                           unsigned char **bufs,
-                                           struct blkid_magic *id,
-                                           blkid_loff_t size)
+static int probe_iso9660(struct blkid_probe *probe,
+                        struct blkid_magic *id __BLKID_ATTR((unused)), 
+                        unsigned char *buf)
 {
-       struct blkid_magic *ret = NULL;
+       struct iso_volume_descriptor *iso;
+       const unsigned char *label;
 
-       if (!bufs || fd < 0)
-               return NULL;
+       iso = (struct iso_volume_descriptor *) buf;
+       label = iso->volume_id;
 
-       if (id >= type_array + sizeof(type_array) / sizeof(*id))
-               return NULL;
+       blkid_set_tag(probe->dev, "LABEL", (const char *) label, 
+                     figure_label_len(label, 32));
+       return 0;
+}
 
-       for (id = id < type_array ? type_array : id + 1; id->bim_type; ++id) {
-               unsigned char *buf;
-               blkid_loff_t start = 0LL;
-               blkid_loff_t offset = 0LL;
-               int kboff;
 
-               offset = ((blkid_loff_t)id->bim_kboff << 10) +
-                       (id->bim_sboff & ~0x3ffULL);
-               /*
-                * We index negative buffers by their actual offset (including
-                * superblock offsets > 1kB, not the aligned offset, so that
-                * we correctly access negative buffers with different
-                * alignment requirements.
-                */
-               if (id->bim_kboff < 0) {
-                       start = (size & ~((blkid_loff_t)id->bim_align - 1)) +
-                               offset;
-                       if (start < 0) /* Device too small for alignment */
-                               continue;
-                       kboff = (start - size) >> 10;
-               } else {
-                       start = offset;
-                       kboff = offset >> 10;
-               }
+static const char
+*udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
+                "NSR03", "TEA01", 0 };
 
-               if ((buf =
-                    read_sb_buf(fd, bufs, kboff, start)) &&
-                   !memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ffULL),
-                           id->bim_len)) {
-                       ret = id;
+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;
+       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++) {
+               isosb = (struct iso_volume_descriptor *) 
+                       get_buffer(probe, bs*2048+32768, sizeof(isosb));
+               if (!isosb)
+                       return 1;
+               if (isosb->vd_id[0])
                        break;
-               }
        }
 
-       return ret;
+       /* Scan up to another 64 blocks looking for additional VSD's */
+       for (j = 1; j < 64; j++) {
+               if (j > 1) {
+                       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 (!memcmp(isosb->vd_id, "NSR0", 4))
+                       return 0;
+               for (m = udf_magic; *m; m++)
+                       if (!memcmp(*m, isosb->vd_id, 5))
+                               break;
+               if (*m == 0)
+                       return 1;
+       }
+       return 1;
 }
 
-/*
- * Get data from a single block special device.
- *
- * Return a blkid_dev with at least the device type and size set.
- * If the passed-in size is zero, then we get the device size here.
- */
-blkid_dev *blkid_devname_to_dev(const char *devname, blkid_loff_t size)
-{
-       unsigned char *buf_array[BLKID_BLK_OFFS * 2 + 1];
-       unsigned char **bufs = buf_array + BLKID_BLK_OFFS;
-       blkid_dev *dev = NULL, *last = NULL;
-       unsigned char *sb_buf = NULL;
-       int sb_size = 0;
-       struct blkid_magic *id = NULL;
-       blkid_loff_t diff_last = 0xf000000000000000ULL;
-       int fd;
-
-       if (!devname)
-               return NULL;
+static int probe_ocfs(struct blkid_probe *probe,
+                     struct blkid_magic *id __BLKID_ATTR((unused)), 
+                     unsigned char *buf)
+{
+       struct ocfs_volume_header ovh;
+       struct ocfs_volume_label ovl;
+       __u32 major;
+
+       memcpy(&ovh, buf, sizeof(ovh));
+       memcpy(&ovl, buf+512, sizeof(ovl));
+
+       major = ocfsmajor(ovh);
+       if (major == 1)
+               blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
+       else if (major >= 9)
+               blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
+       
+       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;
+}
 
-       fd = open(devname, O_RDONLY);
-       if (fd < 0)
-               return NULL;
+static int probe_ocfs2(struct blkid_probe *probe,
+                      struct blkid_magic *id __BLKID_ATTR((unused)), 
+                      unsigned char *buf)
+{
+       struct ocfs2_super_block *osb;
 
-       if (!size)
-               size = blkid_get_dev_size(fd);
-       if (size < 1024)
-               goto exit_fd;
+       osb = (struct ocfs2_super_block *)buf;
 
-       memset(buf_array, 0, sizeof(buf_array));
+       blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
+       set_uuid(probe->dev, osb->s_uuid, 0);
+       return 0;
+}
 
-       while ((id = devname_to_magic(devname, fd, bufs, id, size)) &&
-              diff_last) {
-               int new_sb;
-               blkid_loff_t diff_dev;
+static int probe_oracleasm(struct blkid_probe *probe,
+                          struct blkid_magic *id __BLKID_ATTR((unused)), 
+                          unsigned char *buf)
+{
+       struct oracle_asm_disk_label *dl;
 
-               DEB_PROBE("found type %s (#%d) on %s, probing\n",
-                         id->bim_type, id - type_array, devname);
+       dl = (struct oracle_asm_disk_label *)buf;
 
-               new_sb = id->bim_kbsize << 10;
-               if (sb_size < new_sb) {
-                       unsigned char *sav = sb_buf;
-                       if (!(sb_buf = realloc(sb_buf, new_sb))) {
-                               sb_buf = sav;
-                               continue;
-                       }
-                       sb_size = new_sb;
-               }
+       blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
+       return 0;
+}
 
-               if (id->bim_probe(fd, &dev, devname, id, sb_buf, size) < 0)
-                       continue;
+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;
+}
 
-               diff_dev = size - dev->bid_size;
-               DEB_PROBE("size = %Lu, fs size = %Lu\n", size, dev->bid_size);
-               DEB_PROBE("checking best match: old %Ld, new %Ld\n",
-                         diff_last, diff_dev);
-               /* See which type is a better match by checking size */
-               if ((diff_last < 0 && diff_dev > diff_last) ||
-                   (diff_last > 0 && diff_dev >= 0 && diff_dev < diff_last)) {
-                       if (last)
-                               blkid_free_dev(last);
-                       last = dev;
-                       diff_last = diff_dev;
-               } else
-                       blkid_free_dev(dev);
+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;
+}
 
-       if (!last)
-               DEB_PROBE("unknown device type on %s\n", devname);
-       else
-               DEB_DUMP_DEV(last);
+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;
 
-       /* Free up any buffers we allocated */
-       for (bufs = buf_array; bufs - buf_array < sizeof(buf_array) /
-                                               sizeof(buf_array[0]); bufs++) {
-               if (*bufs)
-                       free(*bufs);
-       }
+       /* Check for a HFS+ volume embedded in a HFS volume */
+       if (memcmp(sbd->embed_sig, "H+", 2) == 0)
+               return 0;
 
-       if (sb_buf)
-               free(sb_buf);
-exit_fd:
-       close(fd);
-       return last;
+       return 1;
 }
 
 /*
+ * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
+ * in the type_array table below + bim_kbalign.
+ *
+ * When probing for a lot of magics, we handle everything in 1kB buffers so
+ * that we don't have to worry about reading each combination of block sizes.
+ */
+#define BLKID_BLK_OFFS 64      /* currently reiserfs */
+
+/*
+ * Various filesystem magics that we can check for.  Note that kboff and
+ * sboff are in kilobytes and bytes respectively.  All magics are in
+ * byte strings so we don't worry about endian issues.
+ */
+static struct blkid_magic type_array[] = {
+/*  type     kboff   sboff len  magic                  probe */
+  { "oracleasm", 0,    32,  8, "ORCLDISK",             probe_oracleasm },
+  { "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 },
+  { "reiserfs", 64,   0x34,  9, "ReIsEr2Fs",           probe_reiserfs },
+  { "reiserfs", 64,   0x34,  9, "ReIsEr3Fs",           probe_reiserfs },
+  { "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,  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 },
+  { "minix",    1,   0x10,  2, "\170\044",             0 },
+  { "vxfs",     1,      0,  4, "\365\374\001\245",     0 },
+  { "xfs",      0,      0,  4, "XFSB",                 probe_xfs },
+  { "romfs",    0,      0,  8, "-rom1fs-",             probe_romfs },
+  { "bfs",      0,      0,  4, "\316\372\173\033",     0 },
+  { "cramfs",   0,      0,  4, "E=\315\050",           probe_cramfs },
+  { "qnx4",     0,      4,  6, "QNX4FS",               0 },
+  { "udf",     32,      1,  5, "BEA01",                probe_udf },
+  { "udf",     32,      1,  5, "BOOT2",                probe_udf },
+  { "udf",     32,      1,  5, "CD001",                probe_udf },
+  { "udf",     32,      1,  5, "CDW02",                probe_udf },
+  { "udf",     32,      1,  5, "NSR02",                probe_udf },
+  { "udf",     32,      1,  5, "NSR03",                probe_udf },
+  { "udf",     32,      1,  5, "TEA01",                probe_udf },
+  { "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 },
+  { "sysv",     0,  0x3f8,  4, "\020~\030\375",        0 },
+  { "swap",     0,  0xff6, 10, "SWAP-SPACE",           probe_swap0 },
+  { "swap",     0,  0xff6, 10, "SWAPSPACE2",           probe_swap1 },
+  { "swsuspend", 0,  0xff6,  9, "S1SUSPEND",           probe_swap1 },
+  { "swsuspend", 0,  0xff6,  9, "S2SUSPEND",           probe_swap1 },
+  { "swap",     0, 0x1ff6, 10, "SWAP-SPACE",           probe_swap0 },
+  { "swap",     0, 0x1ff6, 10, "SWAPSPACE2",           probe_swap1 },
+  { "swsuspend", 0, 0x1ff6,  9, "S1SUSPEND",           probe_swap1 },
+  { "swsuspend", 0, 0x1ff6,  9, "S2SUSPEND",           probe_swap1 },
+  { "swap",     0, 0x3ff6, 10, "SWAP-SPACE",           probe_swap0 },
+  { "swap",     0, 0x3ff6, 10, "SWAPSPACE2",           probe_swap1 },
+  { "swsuspend", 0, 0x3ff6,  9, "S1SUSPEND",           probe_swap1 },
+  { "swsuspend", 0, 0x3ff6,  9, "S2SUSPEND",           probe_swap1 },
+  { "swap",     0, 0x7ff6, 10, "SWAP-SPACE",           probe_swap0 },
+  { "swap",     0, 0x7ff6, 10, "SWAPSPACE2",           probe_swap1 },
+  { "swsuspend", 0, 0x7ff6,  9, "S1SUSPEND",           probe_swap1 },
+  { "swsuspend", 0, 0x7ff6,  9, "S2SUSPEND",           probe_swap1 },
+  { "swap",     0, 0xfff6, 10, "SWAP-SPACE",           probe_swap0 },
+  { "swap",     0, 0xfff6, 10, "SWAPSPACE2",           probe_swap1 },
+  { "swsuspend", 0, 0xfff6,  9, "S1SUSPEND",           probe_swap1 },
+  { "swsuspend", 0, 0xfff6,  9, "S2SUSPEND",           probe_swap1 },
+  { "ocfs",     0,      8,  9, "OracleCFS",            probe_ocfs },
+  { "ocfs2",    1,      0,  6, "OCFSV2",               probe_ocfs2 },
+  { "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 }
+};
+
+/*
  * Verify that the data in dev is consistent with what is on the actual
  * block device (using the devname field only).  Normally this will be
  * called when finding items in the cache, but for long running processes
@@ -738,101 +1125,174 @@ exit_fd:
  * If we are unable to revalidate the data, we return the old data and
  * do not set the BLKID_BID_FL_VERIFIED flag on it.
  */
-blkid_dev *blkid_verify_devname(blkid_cache *cache, blkid_dev *dev)
+blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
 {
-       blkid_loff_t size;
        struct blkid_magic *id;
-       blkid_dev *new = NULL;
-       char *sb_buf = NULL;
-       int sb_size = 0;
-       time_t diff;
-       int fd;
+       struct blkid_probe probe;
+       blkid_tag_iterate iter;
+       unsigned char *buf;
+       const char *type, *value;
+       struct stat st;
+       time_t diff, now;
+       int idx;
 
        if (!dev)
                return NULL;
 
-       diff = time(0) - dev->bid_time;
+       now = time(0);
+       diff = now - dev->bid_time;
 
-       if (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;
 
-       DEB_PROBE("need to revalidate %s\n", dev->bid_name);
+       DBG(DEBUG_PROBE,
+           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) {
-               if (errno == ENXIO || errno == ENODEV) {
-                       fprintf(stderr, "unable to open %s for revalidation\n",
-                               dev->bid_name);
+       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;
                }
                /* We don't have read permission, just return cache data. */
-               DEB_PROBE("returning unverified data for %s\n", dev->bid_name);
+               DBG(DEBUG_PROBE,
+                   printf("returning unverified data for %s\n",
+                          dev->bid_name));
                return dev;
        }
 
-       size = blkid_get_dev_size(fd);
-
-       /* See if we can probe this device by its existing type directly */
+       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,
+        * then try that first.  If it doesn't work, then blow away
+        * the type information, and try again.
+        * 
+        */
+try_again:
+       type = 0;
+       if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
+               uuid_t  uuid;
+
+               if (check_mdraid(probe.fd, uuid) == 0) {
+                       set_uuid(dev, uuid, 0);
+                       type = "mdraid";
+                       goto found_type;
+               }
+       }
        for (id = type_array; id->bim_type; id++) {
-               if (!strcmp(id->bim_type, dev->bid_type)) {
-                       int new_sb = id->bim_kbsize << 10;
-                       /* See if we need to allocate a larger sb buffer */
-                       if (sb_size < new_sb) {
-                               char *sav = sb_buf;
-
-                               /* We can't revalidate, return old dev */
-                               if (!(sb_buf = realloc(sb_buf, new_sb))) {
-                                       fprintf(stderr, "not enough memory for "
-                                               "%s revalidation\n",
-                                               dev->bid_name);
-                                       free(sav);
-                                       goto exit_fd;
-                               }
-                               sb_size = new_sb;
-                       }
+               if (dev->bid_type &&
+                   strcmp(id->bim_type, dev->bid_type))
+                       continue;
 
-                       if (id->bim_probe(fd, &new, dev->bid_name, id, sb_buf,
-                                         size) == 0)
-                               break;
+               idx = id->bim_kboff + (id->bim_sboff >> 10);
+               buf = get_buffer(&probe, idx << 10, 1024);
+               if (!buf)
+                       continue;
+
+               if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
+                          id->bim_len))
+                       continue;
+
+               if ((id->bim_probe == NULL) ||
+                   (id->bim_probe(&probe, id, buf) == 0)) {
+                       type = id->bim_type;
+                       goto found_type;
                }
        }
 
-       if (sb_buf)
-               free(sb_buf);
+       if (!id->bim_type && dev->bid_type) {
+               /*
+                * Zap the device filesystem information and try again
+                */
+               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;
+       }
 
-       /* Otherwise we need to determine the device type first */
-       if (new || (new = blkid_devname_to_dev(dev->bid_name, size))) {
-               new->bid_id = dev->bid_id; /* save old id for cache */
+       if (!dev->bid_type) {
                blkid_free_dev(dev);
-               dev = blkid_add_dev_to_cache(cache, new);
+               dev = 0;
+               goto found_type;
+       }
+               
+found_type:
+       if (dev && type) {
+               dev->bid_devno = st.st_rdev;
+               dev->bid_time = time(0);
+               dev->bid_flags |= BLKID_BID_FL_VERIFIED;
+               cache->bic_flags |= BLKID_BIC_FL_CHANGED;
+
+               blkid_set_tag(dev, "TYPE", type, 0);
+                               
+               DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
+                          dev->bid_name, (long long)st.st_rdev, type));
        }
 
-exit_fd:
-       close(fd);
+       if (probe.sbbuf)
+               free(probe.sbbuf);
+       if (probe.buf)
+               free(probe.buf);
+       if (probe.fd >= 0) 
+               close(probe.fd);
 
-       /* In case the cache is missing the device size */
-       if (dev->bid_devsize == 0)
-               dev->bid_devsize = size;
        return dev;
+}
 
+int blkid_known_fstype(const char *fstype)
+{
+       struct blkid_magic *id;
+
+       for (id = type_array; id->bim_type; id++) {
+               if (strcmp(fstype, id->bim_type) == 0)
+                       return 1;
+       }
+       return 0;
 }
 
 #ifdef TEST_PROGRAM
 int main(int argc, char **argv)
 {
-       blkid_dev *dev;
+       blkid_dev dev;
+       blkid_cache cache;
+       int ret;
 
        if (argc != 2) {
                fprintf(stderr, "Usage: %s device\n"
                        "Probe a single device to determine type\n", argv[0]);
                exit(1);
        }
-       dev = blkid_devname_to_dev(argv[1], 0);
-       if (dev)
-               blkid_free_dev(dev);
-       else
+       if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
+               fprintf(stderr, "%s: error creating cache (%d)\n",
+                       argv[0], ret);
+               exit(1);
+       }
+       dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
+       if (!dev) {
                printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
+               return (1);
+       }
+       printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
+       if (dev->bid_label)
+               printf("LABEL='%s'\n", dev->bid_label);
+       if (dev->bid_uuid)
+               printf("UUID='%s'\n", dev->bid_uuid);
+       
+       blkid_free_dev(dev);
        return (0);
 }
 #endif