| 1 | /* |
| 2 | * Copyright (c) 2008 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 6 | * This file contains Original Code and/or Modifications of Original Code |
| 7 | * as defined in and that are subject to the Apple Public Source License |
| 8 | * Version 2.0 (the 'License'). You may not use this file except in |
| 9 | * compliance with the License. The rights granted to you under the License |
| 10 | * may not be used to create, or enable the creation or redistribution of, |
| 11 | * unlawful or unlicensed copies of an Apple operating system, or to |
| 12 | * circumvent, violate, or enable the circumvention or violation of, any |
| 13 | * terms of an Apple operating system software license agreement. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 18 | * The Original Code and all software distributed under the License are |
| 19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 23 | * Please see the License for the specific language governing rights and |
| 24 | * limitations under the License. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | #ifndef _SYS_DECMPFS_H_ |
| 29 | #define _SYS_DECMPFS_H_ 1 |
| 30 | |
| 31 | #include <stdbool.h> |
| 32 | #include <sys/kdebug.h> |
| 33 | #include <sys/kernel_types.h> |
| 34 | #include <sys/vnode.h> |
| 35 | #include <sys/cdefs.h> |
| 36 | |
| 37 | __BEGIN_DECLS |
| 38 | /* |
| 39 | * Please switch on @DECMPFS_ENABLE_KDEBUG_TRACES to enable tracepoints. |
| 40 | * Tracepoints are compiled out by default to eliminate any overhead due to |
| 41 | * kernel tracing. |
| 42 | * |
| 43 | * #define DECMPFS_ENABLE_KDEBUG_TRACES 1 |
| 44 | */ |
| 45 | #if DECMPFS_ENABLE_KDEBUG_TRACES |
| 46 | #define DECMPFS_EMIT_TRACE_ENTRY(D, ...) \ |
| 47 | KDBG_FILTERED((D) | DBG_FUNC_START, ## __VA_ARGS__) |
| 48 | #define DECMPFS_EMIT_TRACE_RETURN(D, ...) \ |
| 49 | KDBG_FILTERED((D) | DBG_FUNC_END, ##__VA_ARGS__) |
| 50 | #else |
| 51 | #define DECMPFS_EMIT_TRACE_ENTRY(D, ...) do {} while (0) |
| 52 | #define DECMPFS_EMIT_TRACE_RETURN(D, ...) do {} while (0) |
| 53 | #endif /* DECMPFS_ENABLE_KDEBUG_TRACES */ |
| 54 | |
| 55 | /* |
| 56 | * KERNEL_DEBUG related definitions for decmpfs. |
| 57 | * |
| 58 | * Please NOTE: The Class DBG_FSYSTEM = 3, and Subclass DBG_DECMP = 0x12, so |
| 59 | * these debug codes are of the form 0x0312nnnn. |
| 60 | */ |
| 61 | #define DECMPDBG_CODE(code) FSDBG_CODE(DBG_DECMP, code) |
| 62 | |
| 63 | enum { |
| 64 | DECMPDBG_DECOMPRESS_FILE = DECMPDBG_CODE(0),/* 0x03120000 */ |
| 65 | = DECMPDBG_CODE(1),/* 0x03120004 */ |
| 66 | DECMPDBG_FETCH_UNCOMPRESSED_DATA = DECMPDBG_CODE(2),/* 0x03120008 */ |
| 67 | DECMPDBG_FREE_COMPRESSED_DATA = DECMPDBG_CODE(4),/* 0x03120010 */ |
| 68 | DECMPDBG_FILE_IS_COMPRESSED = DECMPDBG_CODE(5),/* 0x03120014 */ |
| 69 | }; |
| 70 | |
| 71 | #define MAX_DECMPFS_XATTR_SIZE 3802 |
| 72 | |
| 73 | /* |
| 74 | * NOTE: decmpfs can only be used by thread-safe filesystems |
| 75 | */ |
| 76 | |
| 77 | #define DECMPFS_MAGIC 0x636d7066 /* cmpf */ |
| 78 | |
| 79 | #define DECMPFS_XATTR_NAME "com.apple.decmpfs" /* extended attribute to use for decmpfs */ |
| 80 | |
| 81 | /* |
| 82 | * This single field is to be interpreted differently depending on the |
| 83 | * corresponding item type. |
| 84 | * For regular files: it is a 64bits-encoded logical size |
| 85 | * For directories: it is a 64bits-encoded number of children (ie st_nlink - 2) |
| 86 | * For packages: it is 40bits encoded size and 24bits number of children at root |
| 87 | */ |
| 88 | typedef struct __attribute__((packed)) { |
| 89 | uint64_t value; |
| 90 | } decmpfs_raw_item_size; |
| 91 | |
| 92 | #define DECMPFS_PKG_SIZE_MASK 0x000000ffffffffffULL |
| 93 | #define DECMPFS_PKG_COUNT_MASK 0xffffff |
| 94 | #define DECMPFS_PKG_CHLD_COUNT_SHIFT 40 |
| 95 | |
| 96 | #define DECMPFS_PKG_SIZE(x) ((x).value & DECMPFS_PKG_SIZE_MASK) |
| 97 | #define DECMPFS_PKG_CHLD_COUNT(x) ((uint32_t)(((x).value >> DECMPFS_PKG_CHLD_COUNT_SHIFT) & DECMPFS_PKG_COUNT_MASK)) |
| 98 | #define DECMPFS_PKG_VALUE_FROM_SIZE_COUNT(size, count) \ |
| 99 | (((size) & DECMPFS_PKG_SIZE_MASK) | ((uint64_t)(count) << DECMPFS_PKG_CHLD_COUNT_SHIFT)) |
| 100 | |
| 101 | /* Dataless file or directory */ |
| 102 | #define DATALESS_CMPFS_TYPE 0x80000001 |
| 103 | |
| 104 | /* Dataless package, with number of root children and total size encoded on disk */ |
| 105 | #define DATALESS_PKG_CMPFS_TYPE 0x80000002 |
| 106 | |
| 107 | |
| 108 | static inline bool |
| 109 | decmpfs_type_is_dataless(uint32_t cmp_type) |
| 110 | { |
| 111 | return cmp_type == DATALESS_CMPFS_TYPE || cmp_type == DATALESS_PKG_CMPFS_TYPE; |
| 112 | } |
| 113 | |
| 114 | typedef struct __attribute__((packed)) { |
| 115 | /* this structure represents the xattr on disk; the fields below are little-endian */ |
| 116 | uint32_t ; |
| 117 | uint32_t ; /* see the enum below */ |
| 118 | union { |
| 119 | uint64_t uncompressed_size; /* compatility accessor */ |
| 120 | decmpfs_raw_item_size _size; |
| 121 | }; |
| 122 | unsigned char [0]; /* the bytes of the attribute after the header */ |
| 123 | } ; |
| 124 | |
| 125 | typedef struct __attribute__((packed)) { |
| 126 | /* this structure represents the xattr in memory; the fields below are host-endian */ |
| 127 | uint32_t ; |
| 128 | uint32_t ; |
| 129 | uint32_t ; |
| 130 | union { |
| 131 | /* |
| 132 | * although uncompressed_size remains available for backward-compatibility reasons |
| 133 | * the uncompressed size and nchildren should be accessed using the inline helpers |
| 134 | * below |
| 135 | */ |
| 136 | uint64_t uncompressed_size; |
| 137 | decmpfs_raw_item_size _size; |
| 138 | }; |
| 139 | unsigned char [0]; /* the bytes of the attribute after the header */ |
| 140 | } ; |
| 141 | |
| 142 | static inline uint64_t |
| 143 | decmpfs_get_uncompressed_size(const decmpfs_header *hdr) |
| 144 | { |
| 145 | if (hdr->compression_magic == DECMPFS_MAGIC && hdr->compression_type == DATALESS_PKG_CMPFS_TYPE) { |
| 146 | return DECMPFS_PKG_SIZE(hdr->_size); |
| 147 | } |
| 148 | |
| 149 | return hdr->uncompressed_size; |
| 150 | } |
| 151 | |
| 152 | static inline uint32_t |
| 153 | decmpfs_get_directory_entries(const decmpfs_header *hdr) |
| 154 | { |
| 155 | if (hdr->compression_magic == DECMPFS_MAGIC && hdr->compression_type == DATALESS_PKG_CMPFS_TYPE) { |
| 156 | return DECMPFS_PKG_CHLD_COUNT(hdr->_size); |
| 157 | } |
| 158 | |
| 159 | return (uint32_t)hdr->uncompressed_size; |
| 160 | } |
| 161 | |
| 162 | /* compression_type values */ |
| 163 | enum { |
| 164 | CMP_Type1 = 1,/* uncompressed data in xattr */ |
| 165 | |
| 166 | /* additional types defined in AppleFSCompression project */ |
| 167 | |
| 168 | CMP_MAX = 255/* Highest compression_type supported */ |
| 169 | }; |
| 170 | |
| 171 | typedef struct { |
| 172 | void *buf; |
| 173 | user_ssize_t size; |
| 174 | } decmpfs_vector; |
| 175 | |
| 176 | __END_DECLS |
| 177 | |
| 178 | #ifdef KERNEL |
| 179 | |
| 180 | #ifdef XNU_KERNEL_PRIVATE |
| 181 | |
| 182 | #include <kern/locks.h> |
| 183 | |
| 184 | __BEGIN_DECLS |
| 185 | |
| 186 | struct decmpfs_cnode { |
| 187 | uint8_t cmp_state; |
| 188 | uint8_t cmp_minimal_xattr; /* if non-zero, this file's com.apple.decmpfs xattr contained only the minimal decmpfs_disk_header */ |
| 189 | uint32_t cmp_type; |
| 190 | uint32_t lockcount; |
| 191 | void *lockowner; /* cnode's lock owner (if a thread is currently holding an exclusive lock) */ |
| 192 | uint64_t uncompressed_size __attribute__((aligned(8))); |
| 193 | uint64_t nchildren __attribute__((aligned(8))); /* for dataless directories (incl. packages) */ |
| 194 | uint64_t total_size __attribute__((aligned(8)));/* for dataless directories (incl. packages) */ |
| 195 | uint64_t decompression_flags; |
| 196 | lck_rw_t compressed_data_lock; |
| 197 | }; |
| 198 | |
| 199 | __END_DECLS |
| 200 | |
| 201 | #endif // XNU_KERNEL_PRIVATE |
| 202 | |
| 203 | __BEGIN_DECLS |
| 204 | |
| 205 | typedef struct decmpfs_cnode decmpfs_cnode; |
| 206 | |
| 207 | /* return values from decmpfs_file_is_compressed */ |
| 208 | enum { |
| 209 | FILE_TYPE_UNKNOWN = 0, |
| 210 | FILE_IS_NOT_COMPRESSED = 1, |
| 211 | FILE_IS_COMPRESSED = 2, |
| 212 | FILE_IS_CONVERTING = 3/* file is converting from compressed to decompressed */ |
| 213 | }; |
| 214 | |
| 215 | /* vfs entrypoints */ |
| 216 | extern vfs_context_t decmpfs_ctx; |
| 217 | |
| 218 | /* client filesystem entrypoints */ |
| 219 | void decmpfs_init(void); |
| 220 | decmpfs_cnode *decmpfs_cnode_alloc(void); |
| 221 | void decmpfs_cnode_free(decmpfs_cnode *dp); |
| 222 | void decmpfs_cnode_init(decmpfs_cnode *cp); |
| 223 | void decmpfs_cnode_destroy(decmpfs_cnode *cp); |
| 224 | |
| 225 | int decmpfs_hides_rsrc(vfs_context_t ctx, decmpfs_cnode *cp); |
| 226 | int decmpfs_hides_xattr(vfs_context_t ctx, decmpfs_cnode *cp, const char *xattr); |
| 227 | |
| 228 | bool decmpfs_trylock_compressed_data(decmpfs_cnode *cp, int exclusive); |
| 229 | void decmpfs_lock_compressed_data(decmpfs_cnode *cp, int exclusive); |
| 230 | void decmpfs_unlock_compressed_data(decmpfs_cnode *cp, int exclusive); |
| 231 | |
| 232 | uint32_t decmpfs_cnode_get_vnode_state(decmpfs_cnode *cp); |
| 233 | void decmpfs_cnode_set_vnode_state(decmpfs_cnode *cp, uint32_t state, int skiplock); |
| 234 | uint64_t decmpfs_cnode_get_vnode_cached_size(decmpfs_cnode *cp); |
| 235 | uint64_t decmpfs_cnode_get_vnode_cached_nchildren(decmpfs_cnode *cp); |
| 236 | uint64_t decmpfs_cnode_get_vnode_cached_total_size(decmpfs_cnode *cp); |
| 237 | void decmpfs_cnode_set_vnode_cached_size(decmpfs_cnode *cp, uint64_t size); |
| 238 | void decmpfs_cnode_set_vnode_cached_nchildren(decmpfs_cnode *cp, uint64_t nchildren); |
| 239 | void decmpfs_cnode_set_vnode_cached_total_size(decmpfs_cnode *cp, uint64_t total_sz); |
| 240 | uint32_t decmpfs_cnode_cmp_type(decmpfs_cnode *cp); |
| 241 | |
| 242 | int decmpfs_file_is_compressed(vnode_t vp, decmpfs_cnode *cp); |
| 243 | errno_t decmpfs_validate_compressed_file(vnode_t vp, decmpfs_cnode *cp); |
| 244 | int decmpfs_decompress_file(vnode_t vp, decmpfs_cnode *cp, off_t toSize, int truncate_okay, int skiplock); /* if toSize == -1, decompress the entire file */ |
| 245 | int decmpfs_free_compressed_data(vnode_t vp, decmpfs_cnode *cp); |
| 246 | int decmpfs_update_attributes(vnode_t vp, struct vnode_attr *vap); |
| 247 | /* the following two routines will set *is_compressed to 0 if the file was converted from compressed to decompressed before data could be fetched from the decompressor */ |
| 248 | errno_t decmpfs_pagein_compressed(struct vnop_pagein_args *ap, int *is_compressed, decmpfs_cnode *cp); |
| 249 | errno_t decmpfs_read_compressed(struct vnop_read_args *ap, int *is_compressed, decmpfs_cnode *cp); |
| 250 | |
| 251 | /* types shared between the kernel and kexts */ |
| 252 | typedef int (*decmpfs_validate_compressed_file_func)(vnode_t vp, vfs_context_t ctx, decmpfs_header *hdr); |
| 253 | typedef void (*decmpfs_adjust_fetch_region_func)(vnode_t vp, vfs_context_t ctx, decmpfs_header *hdr, off_t *offset, user_ssize_t *size); |
| 254 | typedef int (*decmpfs_fetch_uncompressed_data_func)(vnode_t vp, vfs_context_t ctx, decmpfs_header *hdr, off_t offset, user_ssize_t size, int nvec, decmpfs_vector *vec, uint64_t *bytes_read); |
| 255 | typedef int (*decmpfs_free_compressed_data_func)(vnode_t vp, vfs_context_t ctx, decmpfs_header *hdr); |
| 256 | typedef uint64_t (*decmpfs_get_decompression_flags_func)(vnode_t vp, vfs_context_t ctx, decmpfs_header *hdr); // returns flags from the DECMPFS_FLAGS enumeration below |
| 257 | |
| 258 | enum { |
| 259 | DECMPFS_FLAGS_FORCE_FLUSH_ON_DECOMPRESS = 1 << 0, |
| 260 | }; |
| 261 | |
| 262 | /* Versions that are supported for binary compatibility */ |
| 263 | #define DECMPFS_REGISTRATION_VERSION_V1 1 |
| 264 | #define DECMPFS_REGISTRATION_VERSION_V3 3 |
| 265 | |
| 266 | #define DECMPFS_REGISTRATION_VERSION (DECMPFS_REGISTRATION_VERSION_V3) |
| 267 | |
| 268 | typedef struct { |
| 269 | int decmpfs_registration; |
| 270 | decmpfs_validate_compressed_file_func validate; |
| 271 | decmpfs_adjust_fetch_region_func adjust_fetch; |
| 272 | decmpfs_fetch_uncompressed_data_func fetch; |
| 273 | decmpfs_free_compressed_data_func free_data; |
| 274 | decmpfs_get_decompression_flags_func get_flags; |
| 275 | } decmpfs_registration; |
| 276 | |
| 277 | /* hooks for kexts to call */ |
| 278 | errno_t register_decmpfs_decompressor(uint32_t compression_type, const decmpfs_registration *registration); |
| 279 | errno_t unregister_decmpfs_decompressor(uint32_t compression_type, decmpfs_registration *registration); |
| 280 | |
| 281 | __END_DECLS |
| 282 | #endif /* KERNEL */ |
| 283 | |
| 284 | #endif /* _SYS_DECMPFS_H_ */ |
| 285 | |