]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
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 | ||
316670eb | 31 | #include <sys/kernel_types.h> |
39037602 A |
32 | #include <stdbool.h> |
33 | #include <sys/vnode.h> | |
316670eb | 34 | |
b0d623f7 A |
35 | #define MAX_DECMPFS_XATTR_SIZE 3802 |
36 | ||
37 | /* | |
38 | NOTE: decmpfs can only be used by thread-safe filesystems | |
39 | */ | |
40 | ||
41 | #define DECMPFS_MAGIC 0x636d7066 /* cmpf */ | |
42 | ||
43 | #define DECMPFS_XATTR_NAME "com.apple.decmpfs" /* extended attribute to use for decmpfs */ | |
44 | ||
45 | typedef struct __attribute__((packed)) { | |
46 | /* this structure represents the xattr on disk; the fields below are little-endian */ | |
47 | uint32_t compression_magic; | |
48 | uint32_t compression_type; /* see the enum below */ | |
49 | uint64_t uncompressed_size; | |
50 | unsigned char attr_bytes[0]; /* the bytes of the attribute after the header */ | |
51 | } decmpfs_disk_header; | |
52 | ||
53 | typedef struct __attribute__((packed)) { | |
54 | /* this structure represents the xattr in memory; the fields below are host-endian */ | |
55 | uint32_t attr_size; | |
56 | uint32_t compression_magic; | |
57 | uint32_t compression_type; | |
58 | uint64_t uncompressed_size; | |
59 | unsigned char attr_bytes[0]; /* the bytes of the attribute after the header */ | |
60 | } decmpfs_header; | |
61 | ||
62 | /* compression_type values */ | |
63 | enum { | |
64 | CMP_Type1 = 1, /* uncompressed data in xattr */ | |
65 | ||
66 | /* additional types defined in AppleFSCompression project */ | |
67 | ||
316670eb | 68 | CMP_MAX = 255 /* Highest compression_type supported */ |
b0d623f7 A |
69 | }; |
70 | ||
71 | typedef struct { | |
72 | void *buf; | |
73 | user_ssize_t size; | |
74 | } decmpfs_vector; | |
75 | ||
76 | #if KERNEL | |
77 | ||
316670eb | 78 | #if XNU_KERNEL_PRIVATE |
b0d623f7 | 79 | |
316670eb | 80 | #include <kern/locks.h> |
b0d623f7 | 81 | |
39037602 | 82 | struct decmpfs_cnode { |
316670eb A |
83 | uint8_t cmp_state; |
84 | uint8_t cmp_minimal_xattr; /* if non-zero, this file's com.apple.decmpfs xattr contained only the minimal decmpfs_disk_header */ | |
85 | uint32_t cmp_type; | |
86 | uint32_t lockcount; | |
87 | void *lockowner; /* cnode's lock owner (if a thread is currently holding an exclusive lock) */ | |
6d2010ae | 88 | uint64_t uncompressed_size __attribute__((aligned(8))); |
316670eb | 89 | uint64_t decompression_flags; |
b0d623f7 | 90 | lck_rw_t compressed_data_lock; |
39037602 A |
91 | }; |
92 | ||
93 | #endif // XNU_KERNEL_PRIVATE | |
94 | ||
95 | typedef struct decmpfs_cnode decmpfs_cnode; | |
b0d623f7 A |
96 | |
97 | /* return values from decmpfs_file_is_compressed */ | |
98 | enum { | |
99 | FILE_TYPE_UNKNOWN = 0, | |
100 | FILE_IS_NOT_COMPRESSED = 1, | |
101 | FILE_IS_COMPRESSED = 2, | |
102 | FILE_IS_CONVERTING = 3 /* file is converting from compressed to decompressed */ | |
103 | }; | |
104 | ||
105 | /* vfs entrypoints */ | |
106 | extern vfs_context_t decmpfs_ctx; | |
107 | ||
108 | /* client filesystem entrypoints */ | |
109 | void decmpfs_init(void); | |
39037602 A |
110 | decmpfs_cnode *decmpfs_cnode_alloc(void); |
111 | void decmpfs_cnode_free(decmpfs_cnode *dp); | |
b0d623f7 A |
112 | void decmpfs_cnode_init(decmpfs_cnode *cp); |
113 | void decmpfs_cnode_destroy(decmpfs_cnode *cp); | |
114 | ||
115 | int decmpfs_hides_rsrc(vfs_context_t ctx, decmpfs_cnode *cp); | |
116 | int decmpfs_hides_xattr(vfs_context_t ctx, decmpfs_cnode *cp, const char *xattr); | |
117 | ||
39037602 | 118 | bool decmpfs_trylock_compressed_data(decmpfs_cnode *cp, int exclusive); |
b0d623f7 A |
119 | void decmpfs_lock_compressed_data(decmpfs_cnode *cp, int exclusive); |
120 | void decmpfs_unlock_compressed_data(decmpfs_cnode *cp, int exclusive); | |
121 | ||
122 | uint32_t decmpfs_cnode_get_vnode_state(decmpfs_cnode *cp); | |
123 | void decmpfs_cnode_set_vnode_state(decmpfs_cnode *cp, uint32_t state, int skiplock); | |
124 | uint64_t decmpfs_cnode_get_vnode_cached_size(decmpfs_cnode *cp); | |
39037602 | 125 | uint32_t decmpfs_cnode_cmp_type(decmpfs_cnode *cp); |
b0d623f7 A |
126 | |
127 | int decmpfs_file_is_compressed(vnode_t vp, decmpfs_cnode *cp); | |
128 | errno_t decmpfs_validate_compressed_file(vnode_t vp, decmpfs_cnode *cp); | |
129 | 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 */ | |
130 | int decmpfs_free_compressed_data(vnode_t vp, decmpfs_cnode *cp); | |
131 | int decmpfs_update_attributes(vnode_t vp, struct vnode_attr *vap); | |
132 | /* 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 */ | |
133 | errno_t decmpfs_pagein_compressed(struct vnop_pagein_args *ap, int *is_compressed, decmpfs_cnode *cp); | |
134 | errno_t decmpfs_read_compressed(struct vnop_read_args *ap, int *is_compressed, decmpfs_cnode *cp); | |
135 | ||
136 | /* types shared between the kernel and kexts */ | |
137 | typedef int (*decmpfs_validate_compressed_file_func)(vnode_t vp, vfs_context_t ctx, decmpfs_header *hdr); | |
138 | typedef void (*decmpfs_adjust_fetch_region_func)(vnode_t vp, vfs_context_t ctx, decmpfs_header *hdr, off_t *offset, user_ssize_t *size); | |
139 | 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); | |
140 | typedef int (*decmpfs_free_compressed_data_func)(vnode_t vp, vfs_context_t ctx, decmpfs_header *hdr); | |
316670eb A |
141 | 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 |
142 | ||
143 | enum { | |
144 | DECMPFS_FLAGS_FORCE_FLUSH_ON_DECOMPRESS = 1 << 0, | |
145 | }; | |
146 | ||
147 | /* Versions that are supported for binary compatibility */ | |
148 | #define DECMPFS_REGISTRATION_VERSION_V1 1 | |
149 | #define DECMPFS_REGISTRATION_VERSION_V3 3 | |
150 | ||
151 | #define DECMPFS_REGISTRATION_VERSION (DECMPFS_REGISTRATION_VERSION_V3) | |
b0d623f7 | 152 | |
b0d623f7 A |
153 | typedef struct { |
154 | int decmpfs_registration; | |
155 | decmpfs_validate_compressed_file_func validate; | |
156 | decmpfs_adjust_fetch_region_func adjust_fetch; | |
157 | decmpfs_fetch_uncompressed_data_func fetch; | |
158 | decmpfs_free_compressed_data_func free_data; | |
316670eb | 159 | decmpfs_get_decompression_flags_func get_flags; |
b0d623f7 A |
160 | } decmpfs_registration; |
161 | ||
162 | /* hooks for kexts to call */ | |
163 | errno_t register_decmpfs_decompressor(uint32_t compression_type, decmpfs_registration *registration); | |
164 | errno_t unregister_decmpfs_decompressor(uint32_t compression_type, decmpfs_registration *registration); | |
165 | ||
166 | #endif /* KERNEL */ | |
167 | ||
168 | #endif /* _SYS_DECMPFS_H_ */ |