Whamcloud - gitweb
blkid: Flush cached filesystem information on any error other than EPERM
[tools/e2fsprogs.git] / lib / blkid / probe.c
index b8a75de..917447b 100644 (file)
@@ -25,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
@@ -46,7 +47,7 @@ static int figure_label_len(const unsigned char *label, int len)
 }
 
 static unsigned char *get_buffer(struct blkid_probe *pr, 
-                         unsigned off, size_t len)
+                         blkid_loff_t off, size_t len)
 {
        ssize_t         ret_read;
        unsigned char   *newbuf;
@@ -74,7 +75,7 @@ static unsigned char *get_buffer(struct blkid_probe *pr,
                        pr->buf = newbuf;
                        pr->buf_max = len;
                }
-               if (lseek(pr->fd, off, SEEK_SET) < 0)
+               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)
@@ -116,7 +117,7 @@ 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;
 }
@@ -131,7 +132,8 @@ static void set_uuid(blkid_dev dev, uuid_t uuid, char *tag)
        }
 }
 
-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;
@@ -146,61 +148,248 @@ static void get_ext2_info(blkid_dev dev, unsigned char *buf)
        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"));
 }
 
-static int probe_ext3(struct blkid_probe *probe, 
-                     struct blkid_magic *id __BLKID_ATTR((unused)), 
+/*
+ * 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(probe->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 ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
-           !uuid_is_null(es->s_journal_uuid))
-               set_uuid(probe->dev, es->s_journal_uuid, "EXT_JOURNAL");
+       /*
+        * 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;
+
+       /* 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;
 
-       blkid_set_tag(probe->dev, "SEC_TYPE", "ext2", sizeof("ext2"));
+       /* 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(struct blkid_probe *probe,
-                     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(probe->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(struct blkid_probe *probe,
-                    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;
@@ -209,7 +398,7 @@ static int probe_jbd(struct blkid_probe *probe,
              EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
                return -BLKID_ERR_PARAM;
 
-       get_ext2_info(probe->dev, buf);
+       get_ext2_info(probe->dev, id, buf);
 
        return 0;
 }
@@ -284,6 +473,8 @@ static int probe_fat(struct blkid_probe *probe,
                        (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)
@@ -398,6 +589,114 @@ static int probe_fat_nomagic(struct blkid_probe *probe,
        return probe_fat(probe, id, buf);
 }
 
+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)
@@ -423,6 +722,10 @@ static int probe_reiserfs(struct blkid_probe *probe,
 
        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;
@@ -470,6 +773,18 @@ static int probe_jfs(struct blkid_probe *probe,
        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;
+}
+
 static int probe_romfs(struct blkid_probe *probe,
                       struct blkid_magic *id __BLKID_ATTR((unused)), 
                       unsigned char *buf)
@@ -692,6 +1007,19 @@ static int probe_gfs2(struct blkid_probe *probe,
        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.
@@ -709,8 +1037,10 @@ static int probe_gfs2(struct blkid_probe *probe,
 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 },
@@ -726,8 +1056,9 @@ static struct blkid_magic type_array[] = {
   { "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 },
@@ -748,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 },
@@ -777,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 }
 };
 
@@ -819,7 +1155,7 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
        if (((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) ||
            (fstat(probe.fd, &st) < 0)) {
                if (probe.fd >= 0) close(probe.fd);
-               if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
+               if (errno != EPERM) {
                        blkid_free_dev(dev);
                        return NULL;
                }
@@ -878,6 +1214,9 @@ try_again:
                /*
                 * 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);