2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #ifndef _SYS_DECMPFS_H_
29 #define _SYS_DECMPFS_H_ 1
32 #include <sys/kdebug.h>
33 #include <sys/kernel_types.h>
34 #include <sys/vnode.h>
37 * Please switch on @DECMPFS_ENABLE_KDEBUG_TRACES to enable tracepoints.
38 * Tracepoints are compiled out by default to eliminate any overhead due to
41 * #define DECMPFS_ENABLE_KDEBUG_TRACES 1
43 #if DECMPFS_ENABLE_KDEBUG_TRACES
44 #define DECMPFS_EMIT_TRACE_ENTRY(D, ...)\
45 KDBG_FILTERED((D) | DBG_FUNC_START, ## __VA_ARGS__)
46 #define DECMPFS_EMIT_TRACE_RETURN(D, ...)\
47 KDBG_FILTERED((D) | DBG_FUNC_END, ##__VA_ARGS__)
49 #define DECMPFS_EMIT_TRACE_ENTRY(D, ...) do {} while (0)
50 #define DECMPFS_EMIT_TRACE_RETURN(D, ...) do {} while (0)
51 #endif /* DECMPFS_ENABLE_KDEBUG_TRACES */
54 * KERNEL_DEBUG related definitions for decmpfs.
56 * Please NOTE: The Class DBG_FSYSTEM = 3, and Subclass DBG_DECMP = 0x12, so
57 * these debug codes are of the form 0x0312nnnn.
59 #define DECMPDBG_CODE(code) FSDBG_CODE(DBG_DECMP, code)
62 DECMPDBG_DECOMPRESS_FILE
= DECMPDBG_CODE(0), /* 0x03120000 */
63 DECMPDBG_FETCH_COMPRESSED_HEADER
= DECMPDBG_CODE(1), /* 0x03120004 */
64 DECMPDBG_FETCH_UNCOMPRESSED_DATA
= DECMPDBG_CODE(2), /* 0x03120008 */
65 DECMPDBG_FREE_COMPRESSED_DATA
= DECMPDBG_CODE(4), /* 0x03120010 */
66 DECMPDBG_FILE_IS_COMPRESSED
= DECMPDBG_CODE(5), /* 0x03120014 */
69 #define MAX_DECMPFS_XATTR_SIZE 3802
72 NOTE: decmpfs can only be used by thread-safe filesystems
75 #define DECMPFS_MAGIC 0x636d7066 /* cmpf */
77 #define DECMPFS_XATTR_NAME "com.apple.decmpfs" /* extended attribute to use for decmpfs */
79 typedef struct __attribute__((packed
)) {
80 /* this structure represents the xattr on disk; the fields below are little-endian */
81 uint32_t compression_magic
;
82 uint32_t compression_type
; /* see the enum below */
83 uint64_t uncompressed_size
;
84 unsigned char attr_bytes
[0]; /* the bytes of the attribute after the header */
85 } decmpfs_disk_header
;
87 typedef struct __attribute__((packed
)) {
88 /* this structure represents the xattr in memory; the fields below are host-endian */
90 uint32_t compression_magic
;
91 uint32_t compression_type
;
92 uint64_t uncompressed_size
;
93 unsigned char attr_bytes
[0]; /* the bytes of the attribute after the header */
96 /* compression_type values */
98 CMP_Type1
= 1, /* uncompressed data in xattr */
100 /* additional types defined in AppleFSCompression project */
102 CMP_MAX
= 255 /* Highest compression_type supported */
112 #ifdef XNU_KERNEL_PRIVATE
114 #include <kern/locks.h>
116 struct decmpfs_cnode
{
118 uint8_t cmp_minimal_xattr
; /* if non-zero, this file's com.apple.decmpfs xattr contained only the minimal decmpfs_disk_header */
121 void *lockowner
; /* cnode's lock owner (if a thread is currently holding an exclusive lock) */
122 uint64_t uncompressed_size
__attribute__((aligned(8)));
123 uint64_t decompression_flags
;
124 lck_rw_t compressed_data_lock
;
127 #endif // XNU_KERNEL_PRIVATE
129 typedef struct decmpfs_cnode decmpfs_cnode
;
131 /* return values from decmpfs_file_is_compressed */
133 FILE_TYPE_UNKNOWN
= 0,
134 FILE_IS_NOT_COMPRESSED
= 1,
135 FILE_IS_COMPRESSED
= 2,
136 FILE_IS_CONVERTING
= 3 /* file is converting from compressed to decompressed */
139 /* vfs entrypoints */
140 extern vfs_context_t decmpfs_ctx
;
142 /* client filesystem entrypoints */
143 void decmpfs_init(void);
144 decmpfs_cnode
*decmpfs_cnode_alloc(void);
145 void decmpfs_cnode_free(decmpfs_cnode
*dp
);
146 void decmpfs_cnode_init(decmpfs_cnode
*cp
);
147 void decmpfs_cnode_destroy(decmpfs_cnode
*cp
);
149 int decmpfs_hides_rsrc(vfs_context_t ctx
, decmpfs_cnode
*cp
);
150 int decmpfs_hides_xattr(vfs_context_t ctx
, decmpfs_cnode
*cp
, const char *xattr
);
152 bool decmpfs_trylock_compressed_data(decmpfs_cnode
*cp
, int exclusive
);
153 void decmpfs_lock_compressed_data(decmpfs_cnode
*cp
, int exclusive
);
154 void decmpfs_unlock_compressed_data(decmpfs_cnode
*cp
, int exclusive
);
156 uint32_t decmpfs_cnode_get_vnode_state(decmpfs_cnode
*cp
);
157 void decmpfs_cnode_set_vnode_state(decmpfs_cnode
*cp
, uint32_t state
, int skiplock
);
158 uint64_t decmpfs_cnode_get_vnode_cached_size(decmpfs_cnode
*cp
);
159 uint32_t decmpfs_cnode_cmp_type(decmpfs_cnode
*cp
);
161 int decmpfs_file_is_compressed(vnode_t vp
, decmpfs_cnode
*cp
);
162 errno_t
decmpfs_validate_compressed_file(vnode_t vp
, decmpfs_cnode
*cp
);
163 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 */
164 int decmpfs_free_compressed_data(vnode_t vp
, decmpfs_cnode
*cp
);
165 int decmpfs_update_attributes(vnode_t vp
, struct vnode_attr
*vap
);
166 /* 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 */
167 errno_t
decmpfs_pagein_compressed(struct vnop_pagein_args
*ap
, int *is_compressed
, decmpfs_cnode
*cp
);
168 errno_t
decmpfs_read_compressed(struct vnop_read_args
*ap
, int *is_compressed
, decmpfs_cnode
*cp
);
170 /* types shared between the kernel and kexts */
171 typedef int (*decmpfs_validate_compressed_file_func
)(vnode_t vp
, vfs_context_t ctx
, decmpfs_header
*hdr
);
172 typedef void (*decmpfs_adjust_fetch_region_func
)(vnode_t vp
, vfs_context_t ctx
, decmpfs_header
*hdr
, off_t
*offset
, user_ssize_t
*size
);
173 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
);
174 typedef int (*decmpfs_free_compressed_data_func
)(vnode_t vp
, vfs_context_t ctx
, decmpfs_header
*hdr
);
175 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
178 DECMPFS_FLAGS_FORCE_FLUSH_ON_DECOMPRESS
= 1 << 0,
181 /* Versions that are supported for binary compatibility */
182 #define DECMPFS_REGISTRATION_VERSION_V1 1
183 #define DECMPFS_REGISTRATION_VERSION_V3 3
185 #define DECMPFS_REGISTRATION_VERSION (DECMPFS_REGISTRATION_VERSION_V3)
188 int decmpfs_registration
;
189 decmpfs_validate_compressed_file_func validate
;
190 decmpfs_adjust_fetch_region_func adjust_fetch
;
191 decmpfs_fetch_uncompressed_data_func fetch
;
192 decmpfs_free_compressed_data_func free_data
;
193 decmpfs_get_decompression_flags_func get_flags
;
194 } decmpfs_registration
;
196 /* hooks for kexts to call */
197 errno_t
register_decmpfs_decompressor(uint32_t compression_type
, const decmpfs_registration
*registration
);
198 errno_t
unregister_decmpfs_decompressor(uint32_t compression_type
, decmpfs_registration
*registration
);
202 #endif /* _SYS_DECMPFS_H_ */