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