]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/zio_compress.c
Fix typo/etc in module/zfs/zfs_ctldir.c
[mirror_zfs.git] / module / zfs / zio_compress.c
index 7e44d16e403e6358cf878f1155762a1bc6338a74..f5cbc3e8218a93a5ff77b01c4f962c6c418492c0 100644 (file)
  */
 
 /*
- * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
+ * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
  */
 
 #include <sys/zfs_context.h>
-#include <sys/compress.h>
 #include <sys/spa.h>
 #include <sys/zfeature.h>
 #include <sys/zio.h>
 #include <sys/zio_compress.h>
 
+/*
+ * If nonzero, every 1/X decompression attempts will fail, simulating
+ * an undetected memory error.
+ */
+unsigned long zio_decompress_fail_fraction = 0;
+
 /*
  * Compression vectors.
  */
@@ -89,9 +94,7 @@ static int
 zio_compress_zeroed_cb(void *data, size_t len, void *private)
 {
        uint64_t *end = (uint64_t *)((char *)data + len);
-       uint64_t *word;
-
-       for (word = data; word < end; word++)
+       for (uint64_t *word = (uint64_t *)data; word < end; word++)
                if (*word != 0)
                        return (1);
 
@@ -103,7 +106,6 @@ zio_compress_data(enum zio_compress c, abd_t *src, void *dst, size_t s_len)
 {
        size_t c_len, d_len;
        zio_compress_info_t *ci = &zio_compress_table[c];
-       void *tmp;
 
        ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
        ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
@@ -122,7 +124,7 @@ zio_compress_data(enum zio_compress c, abd_t *src, void *dst, size_t s_len)
        d_len = s_len - (s_len >> 3);
 
        /* No compression algorithms can read from ABDs directly */
-       tmp = abd_borrow_buf_copy(src, s_len);
+       void *tmp = abd_borrow_buf_copy(src, s_len);
        c_len = ci->ci_compress(tmp, dst, s_len, d_len, ci->ci_level);
        abd_return_buf(src, tmp, s_len);
 
@@ -152,5 +154,15 @@ zio_decompress_data(enum zio_compress c, abd_t *src, void *dst,
        int ret = zio_decompress_data_buf(c, tmp, dst, s_len, d_len);
        abd_return_buf(src, tmp, s_len);
 
+       /*
+        * Decompression shouldn't fail, because we've already verifyied
+        * the checksum.  However, for extra protection (e.g. against bitflips
+        * in non-ECC RAM), we handle this error (and test it).
+        */
+       ASSERT0(ret);
+       if (zio_decompress_fail_fraction != 0 &&
+           spa_get_random(zio_decompress_fail_fraction) == 0)
+               ret = SET_ERROR(EINVAL);
+
        return (ret);
 }