]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/decmpfs.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / decmpfs.c
index 4040d9b6a59a4004cc7d23328ce281b70dabbf8b..1f5bc434f6ef96208a8ab97d8e10922db6253907 100644 (file)
@@ -80,22 +80,6 @@ UNUSED_SYMBOL(decmpfs_validate_compressed_file)
 #define COMPRESSION_DEBUG_VERBOSE 0
 #define MALLOC_DEBUG 0
 
-static const char *
-baseName(const char *path)
-{
-       if (!path) {
-               return NULL;
-       }
-       const char *ret = path;
-       int i;
-       for (i = 0; path[i] != 0; i++) {
-               if (path[i] == '/') {
-                       ret = &path[i + 1];
-               }
-       }
-       return ret;
-}
-
 #if COMPRESSION_DEBUG
 static char*
 vnpath(vnode_t vp, char *path, int len)
@@ -108,11 +92,21 @@ vnpath(vnode_t vp, char *path, int len)
 }
 #endif
 
-#define ErrorLog(x, args...) printf("%s:%d:%s: " x, baseName(__FILE__), __LINE__, __FUNCTION__, ## args)
+#define ErrorLog(x, args...) \
+       printf("%s:%d:%s: " x, __FILE_NAME__, __LINE__, __FUNCTION__, ## args)
 #if COMPRESSION_DEBUG
-#define ErrorLogWithPath(x, args...) do { char *path; MALLOC(path, char *, PATH_MAX, M_TEMP, M_WAITOK); printf("%s:%d:%s: %s: " x, baseName(__FILE__), __LINE__, __FUNCTION__, vnpath(vp, path, PATH_MAX), ## args); FREE(path, M_TEMP); } while(0)
+#define ErrorLogWithPath(x, args...) do { \
+       char *path = zalloc(ZV_NAMEI); \
+       printf("%s:%d:%s: %s: " x, __FILE_NAME__, __LINE__, __FUNCTION__, \
+           vnpath(vp, path, PATH_MAX), ## args); \
+       zfree(ZV_NAMEI, path); \
+} while(0)
 #else
-#define ErrorLogWithPath(x, args...) do { (void*)vp; printf("%s:%d:%s: %s: " x, baseName(__FILE__), __LINE__, __FUNCTION__, "<private>", ## args); } while(0)
+#define ErrorLogWithPath(x, args...) do { \
+       (void*)vp; \
+       printf("%s:%d:%s: %s: " x, __FILE_NAME__, __LINE__, __FUNCTION__, \
+           "<private>", ## args); \
+} while(0)
 #endif
 
 #if COMPRESSION_DEBUG
@@ -131,88 +125,14 @@ vnpath(vnode_t vp, char *path, int len)
 #define VerboseLogWithPath(x...) do { } while(0)
 #endif
 
-#if MALLOC_DEBUG
-
-static SInt32 totalAlloc;
-
-typedef struct {
-       uint32_t allocSz;
-       uint32_t magic;
-       const char *file;
-       int line;
-} allocated;
-
-static void *
-_malloc(uint32_t sz, __unused int type, __unused int flags, const char *file, int line)
-{
-       uint32_t allocSz = sz + 2 * sizeof(allocated);
-
-       allocated *alloc = NULL;
-       MALLOC(alloc, allocated *, allocSz, type, flags);
-       if (!alloc) {
-               ErrorLog("malloc failed\n");
-               return NULL;
-       }
-
-       char *ret = (char*)&alloc[1];
-       allocated *alloc2 = (allocated*)(ret + sz);
-
-       alloc->allocSz = allocSz;
-       alloc->magic = 0xdadadada;
-       alloc->file = file;
-       alloc->line = line;
-
-       *alloc2 = *alloc;
-
-       int s = OSAddAtomic(sz, &totalAlloc);
-       ErrorLog("malloc(%d) -> %p, total allocations %d\n", sz, ret, s + sz);
-
-       return ret;
-}
-
-static void
-_free(char *ret, __unused int type, const char *file, int line)
-{
-       if (!ret) {
-               ErrorLog("freeing null\n");
-               return;
-       }
-       allocated *alloc = (allocated*)ret;
-       alloc--;
-       uint32_t sz = alloc->allocSz - 2 * sizeof(allocated);
-       allocated *alloc2 = (allocated*)(ret + sz);
-
-       if (alloc->magic != 0xdadadada) {
-               panic("freeing bad pointer");
-       }
-
-       if (memcmp(alloc, alloc2, sizeof(*alloc)) != 0) {
-               panic("clobbered data");
-       }
-
-       memset(ret, 0xce, sz);
-       alloc2->file = file;
-       alloc2->line = line;
-       FREE(alloc, type);
-       int s = OSAddAtomic(-sz, &totalAlloc);
-       ErrorLog("free(%p,%d) -> total allocations %d\n", ret, sz, s - sz);
-}
-
-#undef MALLOC
-#undef FREE
-#define MALLOC(space, cast, size, type, flags) (space) = (cast)_malloc(size, type, flags, __FILE__, __LINE__)
-#define FREE(addr, type) _free((void *)addr, type, __FILE__, __LINE__)
-
-#endif /* MALLOC_DEBUG */
-
 #pragma mark --- globals ---
 
-static lck_grp_t *decmpfs_lockgrp;
+static LCK_GRP_DECLARE(decmpfs_lockgrp, "VFSCOMP");
+static LCK_RW_DECLARE(decompressorsLock, &decmpfs_lockgrp);
+static LCK_MTX_DECLARE(decompress_channel_mtx, &decmpfs_lockgrp);
 
 static const decmpfs_registration *decompressors[CMP_MAX]; /* the registered compressors */
-static lck_rw_t * decompressorsLock;
 static int decompress_channel; /* channel used by decompress_file to wake up waiters */
-static lck_mtx_t *decompress_channel_mtx;
 
 vfs_context_t decmpfs_ctx;
 
@@ -280,20 +200,20 @@ _decmp_get_func(vnode_t vp, uint32_t type, uintptr_t offset, uint32_t discrimina
                snprintf(resourceName, sizeof(resourceName), "com.apple.AppleFSCompression.Type%u", type);
                ErrorLogWithPath("waiting for %s\n", resourceName);
                while (decompressors[type] == NULL) {
-                       lck_rw_unlock_shared(decompressorsLock); // we have to unlock to allow the kext to register
+                       lck_rw_unlock_shared(&decompressorsLock); // we have to unlock to allow the kext to register
                        if (IOServiceWaitForMatchingResource(resourceName, delay)) {
-                               lck_rw_lock_shared(decompressorsLock);
+                               lck_rw_lock_shared(&decompressorsLock);
                                break;
                        }
                        if (!IOCatalogueMatchingDriversPresent(providesName)) {
                                //
                                ErrorLogWithPath("the kext with %s is no longer present\n", providesName);
-                               lck_rw_lock_shared(decompressorsLock);
+                               lck_rw_lock_shared(&decompressorsLock);
                                break;
                        }
                        ErrorLogWithPath("still waiting for %s\n", resourceName);
                        delay *= 2;
-                       lck_rw_lock_shared(decompressorsLock);
+                       lck_rw_lock_shared(&decompressorsLock);
                }
                // IOKit says the kext is loaded, so it should be registered too!
                if (decompressors[type] == NULL) {
@@ -351,13 +271,13 @@ void
 decmpfs_cnode_init(decmpfs_cnode *cp)
 {
        memset(cp, 0, sizeof(*cp));
-       lck_rw_init(&cp->compressed_data_lock, decmpfs_lockgrp, NULL);
+       lck_rw_init(&cp->compressed_data_lock, &decmpfs_lockgrp, NULL);
 }
 
 void
 decmpfs_cnode_destroy(decmpfs_cnode *cp)
 {
-       lck_rw_destroy(&cp->compressed_data_lock, decmpfs_lockgrp);
+       lck_rw_destroy(&cp->compressed_data_lock, &decmpfs_lockgrp);
 }
 
 bool
@@ -549,7 +469,7 @@ decmpfs_cnode_cmp_type(decmpfs_cnode *cp)
 #pragma mark --- decmpfs state routines ---
 
 static int
-decmpfs_fetch_compressed_header(vnode_t vp, decmpfs_cnode *cp, decmpfs_header **hdrOut, int returnInvalid)
+decmpfs_fetch_compressed_header(vnode_t vp, decmpfs_cnode *cp, decmpfs_header **hdrOut, int returnInvalid, size_t *hdr_size)
 {
        /*
         *  fetches vp's compression xattr, converting it into a decmpfs_header; returns 0 or errno
@@ -559,6 +479,7 @@ decmpfs_fetch_compressed_header(vnode_t vp, decmpfs_cnode *cp, decmpfs_header **
 
        size_t read_size             = 0;
        size_t attr_size             = 0;
+       size_t alloc_size            = 0;
        uio_t attr_uio               = NULL;
        int err                      = 0;
        char *data                   = NULL;
@@ -581,7 +502,8 @@ decmpfs_fetch_compressed_header(vnode_t vp, decmpfs_cnode *cp, decmpfs_header **
        if (no_additional_data) {
                /* this file's xattr didn't have any extra data when we fetched it, so we can synthesize a header from the data in the cnode */
 
-               MALLOC(data, char *, sizeof(decmpfs_header), M_TEMP, M_WAITOK);
+               alloc_size = sizeof(decmpfs_header);
+               data = kheap_alloc(KHEAP_TEMP, alloc_size, Z_WAITOK);
                if (!data) {
                        err = ENOMEM;
                        goto out;
@@ -609,6 +531,7 @@ decmpfs_fetch_compressed_header(vnode_t vp, decmpfs_cnode *cp, decmpfs_header **
                if (err != 0) {
                        goto out;
                }
+               alloc_size = attr_size + sizeof(hdr->attr_size);
 
                if (attr_size < sizeof(decmpfs_disk_header) || attr_size > MAX_DECMPFS_XATTR_SIZE) {
                        err = EINVAL;
@@ -616,7 +539,7 @@ decmpfs_fetch_compressed_header(vnode_t vp, decmpfs_cnode *cp, decmpfs_header **
                }
 
                /* allocation includes space for the extra attr_size field of a compressed_header */
-               MALLOC(data, char *, attr_size + sizeof(hdr->attr_size), M_TEMP, M_WAITOK);
+               data = kheap_alloc(KHEAP_TEMP, alloc_size, Z_WAITOK);
                if (!data) {
                        err = ENOMEM;
                        goto out;
@@ -669,12 +592,11 @@ decmpfs_fetch_compressed_header(vnode_t vp, decmpfs_cnode *cp, decmpfs_header **
 out:
        if (err && (err != ERANGE)) {
                DebugLogWithPath("err %d\n", err);
-               if (data) {
-                       FREE(data, M_TEMP);
-               }
+               kheap_free(KHEAP_TEMP, data, alloc_size);
                *hdrOut = NULL;
        } else {
                *hdrOut = hdr;
+               *hdr_size = alloc_size;
        }
        /*
         * Trace the following parameters on return with event-id 0x03120004.
@@ -744,9 +666,10 @@ errno_t
 decmpfs_validate_compressed_file(vnode_t vp, decmpfs_cnode *cp)
 {
        /* give a compressor a chance to indicate that a compressed file is invalid */
-
        decmpfs_header *hdr = NULL;
-       errno_t err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0);
+       size_t alloc_size = 0;
+       errno_t err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0, &alloc_size);
+
        if (err) {
                /* we couldn't get the header */
                if (decmpfs_fast_get_state(cp) == FILE_IS_NOT_COMPRESSED) {
@@ -757,7 +680,7 @@ decmpfs_validate_compressed_file(vnode_t vp, decmpfs_cnode *cp)
        }
 
        if (!decmpfs_type_is_dataless(hdr->compression_type)) {
-               lck_rw_lock_shared(decompressorsLock);
+               lck_rw_lock_shared(&decompressorsLock);
                decmpfs_validate_compressed_file_func validate = decmp_get_func(vp, hdr->compression_type, validate);
                if (validate) { /* make sure this validation function is valid */
                        /* is the data okay? */
@@ -769,11 +692,11 @@ decmpfs_validate_compressed_file(vnode_t vp, decmpfs_cnode *cp)
                        /* no validate registered, so nothing to do */
                        err = 0;
                }
-               lck_rw_unlock_shared(decompressorsLock);
+               lck_rw_unlock_shared(&decompressorsLock);
        }
 out:
-       if (hdr) {
-               FREE(hdr, M_TEMP);
+       if (hdr != NULL) {
+               kheap_free(KHEAP_TEMP, hdr, alloc_size);
        }
 #if COMPRESSION_DEBUG
        if (err) {
@@ -799,6 +722,7 @@ decmpfs_file_is_compressed(vnode_t vp, decmpfs_cnode *cp)
        uint32_t cmp_state;
        struct vnode_attr va_fetch;
        decmpfs_header *hdr = NULL;
+       size_t alloc_size = 0;
        mount_t mp = NULL;
        int cnode_locked = 0;
        int saveInvalid = 0; // save the header data even though the type was out of range
@@ -882,7 +806,7 @@ decmpfs_file_is_compressed(vnode_t vp, decmpfs_cnode *cp)
        }
        if (va_fetch.va_flags & UF_COMPRESSED) {
                /* UF_COMPRESSED is on, make sure the file has the DECMPFS_XATTR_NAME xattr */
-               error = decmpfs_fetch_compressed_header(vp, cp, &hdr, 1);
+               error = decmpfs_fetch_compressed_header(vp, cp, &hdr, 1, &alloc_size);
                if ((hdr != NULL) && (error == ERANGE)) {
                        saveInvalid = 1;
                }
@@ -942,12 +866,12 @@ done:
                        ubc_setsize(vp, hdr->uncompressed_size);
 
                        /* update the decompression flags in the decmpfs cnode */
-                       lck_rw_lock_shared(decompressorsLock);
+                       lck_rw_lock_shared(&decompressorsLock);
                        decmpfs_get_decompression_flags_func get_flags = decmp_get_func(vp, hdr->compression_type, get_flags);
                        if (get_flags) {
                                decompression_flags = get_flags(vp, decmpfs_ctx, hdr);
                        }
-                       lck_rw_unlock_shared(decompressorsLock);
+                       lck_rw_unlock_shared(&decompressorsLock);
                        decmpfs_cnode_set_decompression_flags(cp, decompression_flags);
                }
        } else {
@@ -959,9 +883,10 @@ done:
                decmpfs_unlock_compressed_data(cp, 1);
        }
 
-       if (hdr) {
-               FREE(hdr, M_TEMP);
+       if (hdr != NULL) {
+               kheap_free(KHEAP_TEMP, hdr, alloc_size);
        }
+
        /*
         * Trace the following parameters on return with event-id 0x03120014.
         *
@@ -1021,7 +946,8 @@ decmpfs_update_attributes(vnode_t vp, struct vnode_attr *vap)
                                }
 
                                decmpfs_header *hdr = NULL;
-                               error = decmpfs_fetch_compressed_header(vp, NULL, &hdr, 1);
+                               size_t alloc_size = 0;
+                               error = decmpfs_fetch_compressed_header(vp, NULL, &hdr, 1, &alloc_size);
                                if (error == 0) {
                                        /*
                                         * Allow the flag to be set since the decmpfs attribute
@@ -1043,8 +969,8 @@ decmpfs_update_attributes(vnode_t vp, struct vnode_attr *vap)
                                        /* no DECMPFS_XATTR_NAME attribute, so deny the update */
                                        vap->va_flags &= ~UF_COMPRESSED;
                                }
-                               if (hdr) {
-                                       FREE(hdr, M_TEMP);
+                               if (hdr != NULL) {
+                                       kheap_free(KHEAP_TEMP, hdr, alloc_size);
                                }
                        }
                }
@@ -1057,15 +983,15 @@ static int
 wait_for_decompress(decmpfs_cnode *cp)
 {
        int state;
-       lck_mtx_lock(decompress_channel_mtx);
+       lck_mtx_lock(&decompress_channel_mtx);
        do {
                state = decmpfs_fast_get_state(cp);
                if (state != FILE_IS_CONVERTING) {
                        /* file is not decompressing */
-                       lck_mtx_unlock(decompress_channel_mtx);
+                       lck_mtx_unlock(&decompress_channel_mtx);
                        return state;
                }
-               msleep((caddr_t)&decompress_channel, decompress_channel_mtx, PINOD, "wait_for_decompress", NULL);
+               msleep((caddr_t)&decompress_channel, &decompress_channel_mtx, PINOD, "wait_for_decompress", NULL);
        } while (1);
 }
 
@@ -1145,7 +1071,7 @@ register_decmpfs_decompressor(uint32_t compression_type, const decmpfs_registrat
                goto out;
        }
 
-       lck_rw_lock_exclusive(decompressorsLock); locked = 1;
+       lck_rw_lock_exclusive(&decompressorsLock); locked = 1;
 
        /* make sure the registration for this type is zero */
        if (decompressors[compression_type] != NULL) {
@@ -1158,7 +1084,7 @@ register_decmpfs_decompressor(uint32_t compression_type, const decmpfs_registrat
 
 out:
        if (locked) {
-               lck_rw_unlock_exclusive(decompressorsLock);
+               lck_rw_unlock_exclusive(&decompressorsLock);
        }
        return ret;
 }
@@ -1177,7 +1103,7 @@ unregister_decmpfs_decompressor(uint32_t compression_type, decmpfs_registration
                goto out;
        }
 
-       lck_rw_lock_exclusive(decompressorsLock); locked = 1;
+       lck_rw_lock_exclusive(&decompressorsLock); locked = 1;
        if (decompressors[compression_type] != registration) {
                ret = EEXIST;
                goto out;
@@ -1188,7 +1114,7 @@ unregister_decmpfs_decompressor(uint32_t compression_type, decmpfs_registration
 
 out:
        if (locked) {
-               lck_rw_unlock_exclusive(decompressorsLock);
+               lck_rw_unlock_exclusive(&decompressorsLock);
        }
        return ret;
 }
@@ -1200,11 +1126,11 @@ compression_type_valid(vnode_t vp, decmpfs_header *hdr)
        int ret = 0;
 
        /* every compressor must have at least a fetch function */
-       lck_rw_lock_shared(decompressorsLock);
+       lck_rw_lock_shared(&decompressorsLock);
        if (decmp_get_func(vp, hdr->compression_type, fetch) != NULL) {
                ret = 1;
        }
-       lck_rw_unlock_shared(decompressorsLock);
+       lck_rw_unlock_shared(&decompressorsLock);
 
        return ret;
 }
@@ -1253,11 +1179,11 @@ decmpfs_fetch_uncompressed_data(vnode_t vp, decmpfs_cnode *cp, decmpfs_header *h
         */
        DECMPFS_EMIT_TRACE_ENTRY(DECMPDBG_FETCH_UNCOMPRESSED_DATA, vp->v_id,
            hdr->compression_type, (int)offset, (int)size);
-       lck_rw_lock_shared(decompressorsLock);
+       lck_rw_lock_shared(&decompressorsLock);
        decmpfs_fetch_uncompressed_data_func fetch = decmp_get_func(vp, hdr->compression_type, fetch);
        if (fetch) {
                err = fetch(vp, decmpfs_ctx, hdr, offset, size, nvec, vec, bytes_read);
-               lck_rw_unlock_shared(decompressorsLock);
+               lck_rw_unlock_shared(&decompressorsLock);
                if (err == 0) {
                        uint64_t decompression_flags = decmpfs_cnode_get_decompression_flags(cp);
                        if (decompression_flags & DECMPFS_FLAGS_FORCE_FLUSH_ON_DECOMPRESS) {
@@ -1272,7 +1198,7 @@ decmpfs_fetch_uncompressed_data(vnode_t vp, decmpfs_cnode *cp, decmpfs_header *h
                }
        } else {
                err = ENOTSUP;
-               lck_rw_unlock_shared(decompressorsLock);
+               lck_rw_unlock_shared(&decompressorsLock);
        }
        /*
         * Trace the following parameters on return with event-id 0x03120008.
@@ -1333,6 +1259,7 @@ decmpfs_pagein_compressed(struct vnop_pagein_args *ap, int *is_compressed, decmp
        size_t verify_block_size     = 0;
        void *data                   = NULL;
        decmpfs_header *hdr = NULL;
+       size_t alloc_size            = 0;
        uint64_t cachedSize          = 0;
        int cmpdata_locked           = 0;
        bool file_tail_page_valid    = false;
@@ -1349,7 +1276,7 @@ decmpfs_pagein_compressed(struct vnop_pagein_args *ap, int *is_compressed, decmp
                DebugLogWithPath("pagein: unknown flags 0x%08x\n", (flags & ~(UPL_IOSYNC | UPL_NOCOMMIT | UPL_NORDAHEAD)));
        }
 
-       err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0);
+       err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0, &alloc_size);
        if (err != 0) {
                goto out;
        }
@@ -1613,8 +1540,8 @@ out:
        if (data) {
                ubc_upl_unmap(pl);
        }
-       if (hdr) {
-               FREE(hdr, M_TEMP);
+       if (hdr != NULL) {
+               kheap_free(KHEAP_TEMP, hdr, alloc_size);
        }
        if (cmpdata_locked) {
                decmpfs_unlock_compressed_data(cp, 0);
@@ -1622,10 +1549,9 @@ out:
        if (err) {
 #if 0
                if (err != ENXIO && err != ENOSPC) {
-                       char *path;
-                       MALLOC(path, char *, PATH_MAX, M_TEMP, M_WAITOK);
+                       char *path = zalloc(ZV_NAMEI);
                        panic("%s: decmpfs_pagein_compressed: err %d", vnpath(vp, path, PATH_MAX), err);
-                       FREE(path, M_TEMP);
+                       zfree(ZV_NAMEI, path);
                }
 #endif /* 0 */
                ErrorLogWithPath("err %d\n", err);
@@ -1654,6 +1580,7 @@ decmpfs_read_compressed(struct vnop_read_args *ap, int *is_compressed, decmpfs_c
        upl_t upl                    = NULL;
        upl_page_info_t *pli         = NULL;
        decmpfs_header *hdr          = NULL;
+       size_t alloc_size            = 0;
        uint64_t cachedSize          = 0;
        off_t uioPos                 = 0;
        user_ssize_t uioRemaining    = 0;
@@ -1694,7 +1621,7 @@ decmpfs_read_compressed(struct vnop_read_args *ap, int *is_compressed, decmpfs_c
                goto out;
        }
 
-       err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0);
+       err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0, &alloc_size);
        if (err != 0) {
                goto out;
        }
@@ -1709,14 +1636,14 @@ decmpfs_read_compressed(struct vnop_read_args *ap, int *is_compressed, decmpfs_c
        DebugLogWithPath("uplPos %lld uplSize %lld\n", (uint64_t)uplPos, (uint64_t)uplSize);
 #endif
 
-       lck_rw_lock_shared(decompressorsLock);
+       lck_rw_lock_shared(&decompressorsLock);
        decmpfs_adjust_fetch_region_func adjust_fetch = decmp_get_func(vp, hdr->compression_type, adjust_fetch);
        if (adjust_fetch) {
                /* give the compressor a chance to adjust the portion of the file that we read */
                adjust_fetch(vp, decmpfs_ctx, hdr, &uplPos, &uplSize);
                VerboseLogWithPath("adjusted uplPos %lld uplSize %lld\n", (uint64_t)uplPos, (uint64_t)uplSize);
        }
-       lck_rw_unlock_shared(decompressorsLock);
+       lck_rw_unlock_shared(&decompressorsLock);
 
        /* clip the adjusted size to the size of the file */
        if ((uint64_t)uplPos + uplSize > cachedSize) {
@@ -1791,10 +1718,9 @@ decmpfs_read_compressed(struct vnop_read_args *ap, int *is_compressed, decmpfs_c
                if (kr != KERN_SUCCESS) {
                        commit_upl(upl, 0, curUplSize, UPL_ABORT_FREE_ON_EMPTY, 1);
 #if 0
-                       char *path;
-                       MALLOC(path, char *, PATH_MAX, M_TEMP, M_WAITOK);
+                       char *path = zalloc(ZV_NAMEI);
                        panic("%s: decmpfs_read_compressed: ubc_upl_map error %d", vnpath(vp, path, PATH_MAX), (int)kr);
-                       FREE(path, M_TEMP);
+                       zfree(ZV_NAMEI, path);
 #else /* 0 */
                        ErrorLogWithPath("ubc_upl_map kr=0x%x\n", (int)kr);
 #endif /* 0 */
@@ -1901,8 +1827,8 @@ decompress:
 
 out:
 
-       if (hdr) {
-               FREE(hdr, M_TEMP);
+       if (hdr != NULL) {
+               kheap_free(KHEAP_TEMP, hdr, alloc_size);
        }
        if (cmpdata_locked) {
                decmpfs_unlock_compressed_data(cp, 0);
@@ -1929,6 +1855,7 @@ decmpfs_free_compressed_data(vnode_t vp, decmpfs_cnode *cp)
         *  then delete the file's compression xattr
         */
        decmpfs_header *hdr = NULL;
+       size_t alloc_size = 0;
 
        /*
         * Trace the following parameters on entry with event-id 0x03120010.
@@ -1937,11 +1864,11 @@ decmpfs_free_compressed_data(vnode_t vp, decmpfs_cnode *cp)
         */
        DECMPFS_EMIT_TRACE_ENTRY(DECMPDBG_FREE_COMPRESSED_DATA, vp->v_id);
 
-       int err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0);
+       int err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0, &alloc_size);
        if (err) {
                ErrorLogWithPath("decmpfs_fetch_compressed_header err %d\n", err);
        } else {
-               lck_rw_lock_shared(decompressorsLock);
+               lck_rw_lock_shared(&decompressorsLock);
                decmpfs_free_compressed_data_func free_data = decmp_get_func(vp, hdr->compression_type, free_data);
                if (free_data) {
                        err = free_data(vp, decmpfs_ctx, hdr);
@@ -1949,7 +1876,7 @@ decmpfs_free_compressed_data(vnode_t vp, decmpfs_cnode *cp)
                        /* nothing to do, so no error */
                        err = 0;
                }
-               lck_rw_unlock_shared(decompressorsLock);
+               lck_rw_unlock_shared(&decompressorsLock);
 
                if (err != 0) {
                        ErrorLogWithPath("decompressor err %d\n", err);
@@ -1965,13 +1892,9 @@ decmpfs_free_compressed_data(vnode_t vp, decmpfs_cnode *cp)
 
        /* delete the xattr */
        err = vn_removexattr(vp, DECMPFS_XATTR_NAME, 0, decmpfs_ctx);
-       if (err != 0) {
-               goto out;
-       }
 
-out:
-       if (hdr) {
-               FREE(hdr, M_TEMP);
+       if (hdr != NULL) {
+               kheap_free(KHEAP_TEMP, hdr, alloc_size);
        }
        return err;
 }
@@ -2018,6 +1941,7 @@ decmpfs_decompress_file(vnode_t vp, decmpfs_cnode *cp, off_t toSize, int truncat
        int update_file_state        = 0;
        size_t allocSize             = 0;
        decmpfs_header *hdr          = NULL;
+       size_t hdr_size              = 0;
        int cmpdata_locked           = 0;
        off_t remaining              = 0;
        uint64_t uncompressed_size   = 0;
@@ -2077,7 +2001,7 @@ decompress:
        }
        }
 
-       err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0);
+       err = decmpfs_fetch_compressed_header(vp, cp, &hdr, 0, &hdr_size);
        if (err != 0) {
                goto out;
        }
@@ -2096,7 +2020,7 @@ decompress:
        }
 
        allocSize = MIN(64 * 1024, (size_t)toSize);
-       MALLOC(data, char *, allocSize, M_TEMP, M_WAITOK);
+       data = kheap_alloc(KHEAP_TEMP, allocSize, Z_WAITOK);
        if (!data) {
                err = ENOMEM;
                goto out;
@@ -2210,12 +2134,10 @@ nodecmp:
 #endif
 
 out:
-       if (hdr) {
-               FREE(hdr, M_TEMP);
-       }
-       if (data) {
-               FREE(data, M_TEMP);
+       if (hdr != NULL) {
+               kheap_free(KHEAP_TEMP, hdr, hdr_size);
        }
+       kheap_free(KHEAP_TEMP, data, allocSize);
        if (uio_w) {
                uio_free(uio_w);
        }
@@ -2231,10 +2153,10 @@ out:
        }
 
        if (update_file_state) {
-               lck_mtx_lock(decompress_channel_mtx);
+               lck_mtx_lock(&decompress_channel_mtx);
                decmpfs_cnode_set_vnode_state(cp, new_state, 1);
                wakeup((caddr_t)&decompress_channel); /* wake up anyone who might have been waiting for decompression */
-               lck_mtx_unlock(decompress_channel_mtx);
+               lck_mtx_unlock(&decompress_channel_mtx);
        }
 
        if (cmpdata_locked) {
@@ -2318,7 +2240,7 @@ SECURITY_READ_ONLY_EARLY(static decmpfs_registration) Type1Reg =
 #pragma mark --- decmpfs initialization ---
 
 void
-decmpfs_init()
+decmpfs_init(void)
 {
        static int done = 0;
        if (done) {
@@ -2327,12 +2249,6 @@ decmpfs_init()
 
        decmpfs_ctx = vfs_context_create(vfs_context_kernel());
 
-       lck_grp_attr_t *attr = lck_grp_attr_alloc_init();
-       decmpfs_lockgrp = lck_grp_alloc_init("VFSCOMP", attr);
-       lck_grp_attr_free(attr);
-       decompressorsLock = lck_rw_alloc_init(decmpfs_lockgrp, NULL);
-       decompress_channel_mtx = lck_mtx_alloc_init(decmpfs_lockgrp, NULL);
-
        register_decmpfs_decompressor(CMP_Type1, &Type1Reg);
 
        done = 1;