Add -p switch to "zpool get"
[zfs.git] / module / zfs / lzjb.c
index 10952f4..43d0df0 100644 (file)
  */
 
 /*
- * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
- * Use is subject to license terms.
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  */
 
 /*
- * We keep our own copy of this algorithm for 2 main reasons:
+ * We keep our own copy of this algorithm for 3 main reasons:
  *     1. If we didn't, anyone modifying common/os/compress.c would
  *         directly break our on disk format
  *     2. Our version of lzjb does not have a number of checks that the
  *     3. We initialize the lempel to ensure deterministic results,
  *        so that identical blocks can always be deduplicated.
  * In particular, we are adding the "feature" that compress() can
- * take a destination buffer size and return -1 if the data will not
- * compress to d_len or less.
+ * take a destination buffer size and returns the compressed length, or the
+ * source length if compression would overflow the destination buffer.
  */
 
-#include <sys/types.h>
+#include <sys/zfs_context.h>
 
 #define        MATCH_BITS      6
 #define        MATCH_MIN       3
@@ -51,16 +50,19 @@ lzjb_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
 {
        uchar_t *src = s_start;
        uchar_t *dst = d_start;
-       uchar_t *cpy, *copymap;
+       uchar_t *cpy, *copymap = NULL;
        int copymask = 1 << (NBBY - 1);
        int mlen, offset, hash;
        uint16_t *hp;
-       uint16_t lempel[LEMPEL_SIZE] = { 0 };
+       uint16_t *lempel;
 
+       lempel = kmem_zalloc(LEMPEL_SIZE * sizeof (uint16_t), KM_PUSHPAGE);
        while (src < (uchar_t *)s_start + s_len) {
                if ((copymask <<= 1) == (1 << NBBY)) {
-                       if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY)
+                       if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY) {
+                               kmem_free(lempel, LEMPEL_SIZE*sizeof(uint16_t));
                                return (s_len);
+                       }
                        copymask = 1;
                        copymap = dst;
                        *dst++ = 0;
@@ -90,6 +92,8 @@ lzjb_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
                        *dst++ = *src++;
                }
        }
+
+       kmem_free(lempel, LEMPEL_SIZE * sizeof (uint16_t));
        return (dst - (uchar_t *)d_start);
 }
 
@@ -100,7 +104,7 @@ lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
        uchar_t *src = s_start;
        uchar_t *dst = d_start;
        uchar_t *d_end = (uchar_t *)d_start + d_len;
-       uchar_t *cpy, copymap;
+       uchar_t *cpy, copymap = 0;
        int copymask = 1 << (NBBY - 1);
 
        while (dst < d_end) {