1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* 27 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 28 */ 29 30 #include <sys/zfs_context.h> 31 #include <sys/compress.h> 32 #include <sys/spa.h> 33 #include <sys/zio.h> 34 #include <sys/zio_compress.h> 35 36 /* 37 * Compression vectors. 38 */ 39 40 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = { 41 {NULL, NULL, 0, "inherit"}, 42 {NULL, NULL, 0, "on"}, 43 {NULL, NULL, 0, "uncompressed"}, 44 {lzjb_compress, lzjb_decompress, 0, "lzjb"}, 45 {NULL, NULL, 0, "empty"}, 46 {gzip_compress, gzip_decompress, 1, "gzip-1"}, 47 {gzip_compress, gzip_decompress, 2, "gzip-2"}, 48 {gzip_compress, gzip_decompress, 3, "gzip-3"}, 49 {gzip_compress, gzip_decompress, 4, "gzip-4"}, 50 {gzip_compress, gzip_decompress, 5, "gzip-5"}, 51 {gzip_compress, gzip_decompress, 6, "gzip-6"}, 52 {gzip_compress, gzip_decompress, 7, "gzip-7"}, 53 {gzip_compress, gzip_decompress, 8, "gzip-8"}, 54 {gzip_compress, gzip_decompress, 9, "gzip-9"}, 55 {zle_compress, zle_decompress, 64, "zle"}, 56 {lz4_compress, lz4_decompress, 0, "lz4"}, 57 }; 58 59 enum zio_compress 60 zio_compress_select(enum zio_compress child, enum zio_compress parent) 61 { 62 ASSERT(child < ZIO_COMPRESS_FUNCTIONS); 63 ASSERT(parent < ZIO_COMPRESS_FUNCTIONS); 64 ASSERT(parent != ZIO_COMPRESS_INHERIT && parent != ZIO_COMPRESS_ON); 65 66 if (child == ZIO_COMPRESS_INHERIT) 67 return (parent); 68 69 if (child == ZIO_COMPRESS_ON) 70 return (ZIO_COMPRESS_ON_VALUE); 71 72 return (child); 73 } 74 75 size_t 76 zio_compress_data(enum zio_compress c, void *src, void *dst, size_t s_len) 77 { 78 uint64_t *word, *word_end; 79 size_t c_len, d_len, r_len; 80 zio_compress_info_t *ci = &zio_compress_table[c]; 81 82 ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS); 83 ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL); 84 85 /* 86 * If the data is all zeroes, we don't even need to allocate 87 * a block for it. We indicate this by returning zero size. 88 */ 89 word_end = (uint64_t *)((char *)src + s_len); 90 for (word = src; word < word_end; word++) 91 if (*word != 0) 92 break; 93 94 if (word == word_end) 95 return (0); 96 97 if (c == ZIO_COMPRESS_EMPTY) 98 return (s_len); 99 100 /* Compress at least 12.5% */ 101 d_len = P2ALIGN(s_len - (s_len >> 3), (size_t)SPA_MINBLOCKSIZE); 102 if (d_len == 0) 103 return (s_len); 104 105 c_len = ci->ci_compress(src, dst, s_len, d_len, ci->ci_level); 106 107 if (c_len > d_len) 108 return (s_len); 109 110 /* 111 * Cool. We compressed at least as much as we were hoping to. 112 * For both security and repeatability, pad out the last sector. 113 */ 114 r_len = P2ROUNDUP(c_len, (size_t)SPA_MINBLOCKSIZE); 115 if (r_len > c_len) { 116 bzero((char *)dst + c_len, r_len - c_len); 117 c_len = r_len; 118 } 119 120 ASSERT3U(c_len, <=, d_len); 121 ASSERT(P2PHASE(c_len, (size_t)SPA_MINBLOCKSIZE) == 0); 122 123 return (c_len); 124 } 125 126 int 127 zio_decompress_data(enum zio_compress c, void *src, void *dst, 128 size_t s_len, size_t d_len) 129 { 130 zio_compress_info_t *ci = &zio_compress_table[c]; 131 132 if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL) 133 return (EINVAL); 134 135 return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level)); 136 } 137