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