]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: use separate function to verify checksums
authorKarel Zak <kzak@redhat.com>
Wed, 11 Sep 2013 11:19:44 +0000 (13:19 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 11 Sep 2013 14:24:56 +0000 (16:24 +0200)
 * consolidate "incorrect checksum" debug messages
 * verify all on one place

Based on patch from Gabriel de Perthuis <g2p.code@gmail.com>

Signed-off-by: Karel Zak <kzak@redhat.com>
libblkid/src/blkidP.h
libblkid/src/probe.c
libblkid/src/superblocks/lvm.c
libblkid/src/superblocks/nilfs.c
libblkid/src/superblocks/silicon_raid.c
libblkid/src/superblocks/via_raid.c

index 1c0596832d4184aff94235dc71d9f55803945372..0bbf310a05c5558c54a125c1be407ef53cf2e8b0 100644 (file)
@@ -516,6 +516,9 @@ extern int blkid_probe_set_magic(blkid_probe pr, blkid_loff_t offset,
                                size_t len, unsigned char *magic)
                        __attribute__((nonnull));
 
+extern int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected)
+                       __attribute__((nonnull));
+
 extern void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len)
                        __attribute__((nonnull));
 extern int blkid_uuid_is_empty(const unsigned char *buf, size_t len);
index d2b301d7f0242f955423a3c103e2ad29ff756150..6e3ae42dc75c9951289e3a6075e23f1f6675349c 100644 (file)
@@ -336,6 +336,16 @@ struct blkid_chain *blkid_probe_get_chain(blkid_probe pr)
        return pr->cur_chain;
 }
 
+static const char *blkid_probe_get_probername(blkid_probe pr)
+{
+       struct blkid_chain *chn = blkid_probe_get_chain(pr);
+
+       if (chn && chn->idx >= 0 && chn->idx < chn->driver->nidinfos)
+               return chn->driver->idinfos[chn->idx]->name;
+
+       return NULL;
+}
+
 void *blkid_probe_get_binary_data(blkid_probe pr, struct blkid_chain *chn)
 {
        int rc, org_prob_flags;
@@ -1341,6 +1351,20 @@ int blkid_probe_set_magic(blkid_probe pr, blkid_loff_t offset,
        return rc;
 }
 
+int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected)
+{
+       if (csum != expected) {
+               DBG(LOWPROBE, blkid_debug(
+                               "incorrect checksum for type %s,"
+                               " got %jX, expected %jX",
+                               blkid_probe_get_probername(pr),
+                               csum, expected));
+               return 0;
+       }
+
+       return 1;
+}
+
 /**
  * blkid_probe_get_devno:
  * @pr: probe
index dc38f2e2c531ad3c0a169d0606d255b2a673bbd4..65c7c35a4f7cb8fe69daa0c3f0b0c07f0f7543dd 100644 (file)
@@ -97,13 +97,12 @@ static int probe_lvm2(blkid_probe pr, const struct blkid_idmag *mag)
        if (le64_to_cpu(label->sector_xl) != (unsigned) sector)
                return 1;
 
-       if (lvm2_calc_crc(&label->offset_xl, LVM2_LABEL_SIZE -
-                       ((char *) &label->offset_xl - (char *) label)) !=
-                       le32_to_cpu(label->crc_xl)) {
-               DBG(PROBE, blkid_debug("LVM2: label checksum incorrect at sector %d",
-                          sector));
+       if (!blkid_probe_verify_csum(
+               pr, lvm2_calc_crc(
+                       &label->offset_xl, LVM2_LABEL_SIZE -
+                       ((char *) &label->offset_xl - (char *) label)),
+                       le32_to_cpu(label->crc_xl)))
                return 1;
-       }
 
        format_lvm_uuid(uuid, (char *) label->pv_uuid);
        blkid_probe_sprintf_uuid(pr, label->pv_uuid, sizeof(label->pv_uuid),
index 1f8f3a69fc83cdc38e0824a2ef489e74adb7bbe0..9207677a9976de5e13362885f61b47366bc23c95 100644 (file)
@@ -89,8 +89,8 @@ static int probe_nilfs2(blkid_probe pr, const struct blkid_idmag *mag)
        crc = crc32(crc, sum, 4);
        crc = crc32(crc, (unsigned char *)sb + sumoff + 4, bytes - sumoff - 4);
 
-       if (crc != le32_to_cpu(sb->s_sum))
-               return -1;
+       if (!blkid_probe_verify_csum(pr, crc, le32_to_cpu(sb->s_sum)))
+               return 1;
 
        if (strlen(sb->s_volume_name))
                blkid_probe_set_label(pr, (unsigned char *) sb->s_volume_name,
index 7abc46afd03eea237ae0453ee98c1a26fb3d57ec..10a302313945c4243b9d1cbcb6de5372f13c2727 100644 (file)
@@ -67,7 +67,7 @@ struct silicon_metadata {
 
 #define SILICON_MAGIC          0x2F000000
 
-static int checksum(struct silicon_metadata *sil)
+static uint16_t silraid_checksum(struct silicon_metadata *sil)
 {
        int sum = 0;
        unsigned short count = offsetof(struct silicon_metadata, checksum1) / 2;
@@ -78,7 +78,7 @@ static int checksum(struct silicon_metadata *sil)
                sum += le16_to_cpu(x);
        }
 
-       return (-sum & 0xFFFF) == le16_to_cpu(sil->checksum1);
+       return (-sum & 0xFFFF);
 }
 
 static int probe_silraid(blkid_probe pr,
@@ -104,10 +104,8 @@ static int probe_silraid(blkid_probe pr,
                return 1;
        if (sil->disk_number >= 8)
                return 1;
-       if (!checksum(sil)) {
-               DBG(LOWPROBE, blkid_debug("silicon raid: incorrect checksum"));
+       if (!blkid_probe_verify_csum(pr, silraid_checksum(sil), le16_to_cpu(sil->checksum1)))
                return 1;
-       }
 
        if (blkid_probe_sprintf_version(pr, "%u.%u",
                                le16_to_cpu(sil->major_ver),
index 5f91cc40539e21f015c21da6fcdd25ce705a2481..5c15167aa823a48b671ba746a7ab0feb5e5cc47e 100644 (file)
@@ -42,7 +42,7 @@ static uint8_t via_checksum(struct via_metadata *v)
        while (i--)
                cs += ((uint8_t*) v)[i];
 
-       return cs == v->checksum;
+       return cs;
 }
 
 static int probe_viaraid(blkid_probe pr,
@@ -69,7 +69,7 @@ static int probe_viaraid(blkid_probe pr,
                return 1;
        if (v->version_number > 2)
                return 1;
-       if (!via_checksum(v))
+       if (!blkid_probe_verify_csum(pr, via_checksum(v), v->checksum))
                return 1;
 
        if (blkid_probe_sprintf_version(pr, "%u", v->version_number) != 0)