2 * Copyright (c) 2008-2018 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@
30 /* We need these symbols even though compression is turned off */
32 #define UNUSED_SYMBOL(x) asm(".global _" #x "\n.set _" #x ", 0\n");
34 UNUSED_SYMBOL(register_decmpfs_decompressor
)
35 UNUSED_SYMBOL(unregister_decmpfs_decompressor
)
36 UNUSED_SYMBOL(decmpfs_init
)
37 UNUSED_SYMBOL(decmpfs_read_compressed
)
38 UNUSED_SYMBOL(decmpfs_cnode_cmp_type
)
39 UNUSED_SYMBOL(decmpfs_cnode_get_vnode_state
)
40 UNUSED_SYMBOL(decmpfs_cnode_get_vnode_cached_size
)
41 UNUSED_SYMBOL(decmpfs_lock_compressed_data
)
42 UNUSED_SYMBOL(decmpfs_cnode_free
)
43 UNUSED_SYMBOL(decmpfs_cnode_alloc
)
44 UNUSED_SYMBOL(decmpfs_cnode_destroy
)
45 UNUSED_SYMBOL(decmpfs_decompress_file
)
46 UNUSED_SYMBOL(decmpfs_unlock_compressed_data
)
47 UNUSED_SYMBOL(decmpfs_cnode_init
)
48 UNUSED_SYMBOL(decmpfs_cnode_set_vnode_state
)
49 UNUSED_SYMBOL(decmpfs_hides_xattr
)
50 UNUSED_SYMBOL(decmpfs_ctx
)
51 UNUSED_SYMBOL(decmpfs_file_is_compressed
)
52 UNUSED_SYMBOL(decmpfs_update_attributes
)
53 UNUSED_SYMBOL(decmpfs_hides_rsrc
)
54 UNUSED_SYMBOL(decmpfs_pagein_compressed
)
55 UNUSED_SYMBOL(decmpfs_validate_compressed_file
)
57 #else /* FS_COMPRESSION */
58 #include <sys/kernel.h>
59 #include <sys/vnode_internal.h>
60 #include <sys/file_internal.h>
62 #include <sys/fcntl.h>
63 #include <sys/xattr.h>
64 #include <sys/namei.h>
66 #include <sys/mount_internal.h>
68 #include <sys/decmpfs.h>
69 #include <sys/uio_internal.h>
70 #include <libkern/OSByteOrder.h>
71 #include <libkern/section_keywords.h>
73 #pragma mark --- debugging ---
75 #define COMPRESSION_DEBUG 0
76 #define COMPRESSION_DEBUG_VERBOSE 0
77 #define MALLOC_DEBUG 0
80 baseName(const char *path
)
84 const char *ret
= path
;
86 for (i
= 0; path
[i
] != 0; i
++) {
94 vnpath(vnode_t vp
, char *path
, int len
)
98 vn_getpath(vp
, path
, &len
);
99 path
[origlen
- 1] = 0;
103 #define ErrorLog(x, args...) printf("%s:%d:%s: " x, baseName(__FILE__), __LINE__, __FUNCTION__, ## args)
104 #define ErrorLogWithPath(x, args...) do { char *path; MALLOC(path, char *, PATH_MAX, M_TEMP, M_WAITOK); printf("%s:%d:%s: %s: " x, baseName(__FILE__), __LINE__, __FUNCTION__, vnpath(vp, path, PATH_MAX), ## args); FREE(path, M_TEMP); } while(0)
106 #if COMPRESSION_DEBUG
107 #define DebugLog ErrorLog
108 #define DebugLogWithPath ErrorLogWithPath
110 #define DebugLog(x...) do { } while(0)
111 #define DebugLogWithPath(x...) do { } while(0)
114 #if COMPRESSION_DEBUG_VERBOSE
115 #define VerboseLog ErrorLog
116 #define VerboseLogWithPath ErrorLogWithPath
118 #define VerboseLog(x...) do { } while(0)
119 #define VerboseLogWithPath(x...) do { } while(0)
124 static SInt32 totalAlloc
;
134 _malloc(uint32_t sz
, __unused
int type
, __unused
int flags
, const char *file
, int line
)
136 uint32_t allocSz
= sz
+ 2 * sizeof(allocated
);
138 allocated
*alloc
= NULL
;
139 MALLOC(alloc
, allocated
*, allocSz
, type
, flags
);
141 ErrorLog("malloc failed\n");
145 char *ret
= (char*)&alloc
[1];
146 allocated
*alloc2
= (allocated
*)(ret
+ sz
);
148 alloc
->allocSz
= allocSz
;
149 alloc
->magic
= 0xdadadada;
155 int s
= OSAddAtomic(sz
, &totalAlloc
);
156 ErrorLog("malloc(%d) -> %p, total allocations %d\n", sz
, ret
, s
+ sz
);
162 _free(char *ret
, __unused
int type
, const char *file
, int line
)
165 ErrorLog("freeing null\n");
168 allocated
*alloc
= (allocated
*)ret
;
170 uint32_t sz
= alloc
->allocSz
- 2 * sizeof(allocated
);
171 allocated
*alloc2
= (allocated
*)(ret
+ sz
);
173 if (alloc
->magic
!= 0xdadadada) {
174 panic("freeing bad pointer");
177 if (memcmp(alloc
, alloc2
, sizeof(*alloc
)) != 0) {
178 panic("clobbered data");
181 memset(ret
, 0xce, sz
);
185 int s
= OSAddAtomic(-sz
, &totalAlloc
);
186 ErrorLog("free(%p,%d) -> total allocations %d\n", ret
, sz
, s
- sz
);
191 #define MALLOC(space, cast, size, type, flags) (space) = (cast)_malloc(size, type, flags, __FILE__, __LINE__)
192 #define FREE(addr, type) _free((void *)addr, type, __FILE__, __LINE__)
194 #endif /* MALLOC_DEBUG */
196 #pragma mark --- globals ---
198 static lck_grp_t
*decmpfs_lockgrp
;
200 SECURITY_READ_ONLY_EARLY(static decmpfs_registration
*) decompressors
[CMP_MAX
]; /* the registered compressors */
201 static lck_rw_t
* decompressorsLock
;
202 static int decompress_channel
; /* channel used by decompress_file to wake up waiters */
203 static lck_mtx_t
*decompress_channel_mtx
;
205 vfs_context_t decmpfs_ctx
;
207 #pragma mark --- decmp_get_func ---
209 #define offsetof_func(func) ((uintptr_t)(&(((decmpfs_registration*)NULL)->func)))
212 _func_from_offset(uint32_t type
, uintptr_t offset
)
214 /* get the function at the given offset in the registration for the given type */
215 const decmpfs_registration
*reg
= decompressors
[type
];
216 const char *regChar
= (const char*)reg
;
217 const char *func
= ®Char
[offset
];
218 void * const * funcPtr
= (void * const *) func
;
220 switch (reg
->decmpfs_registration
) {
221 case DECMPFS_REGISTRATION_VERSION_V1
:
222 if (offset
> offsetof_func(free_data
))
225 case DECMPFS_REGISTRATION_VERSION_V3
:
226 if (offset
> offsetof_func(get_flags
))
236 extern void IOServicePublishResource( const char * property
, boolean_t value
);
237 extern boolean_t
IOServiceWaitForMatchingResource( const char * property
, uint64_t timeout
);
238 extern boolean_t
IOCatalogueMatchingDriversPresent( const char * property
);
241 _decmp_get_func(vnode_t vp
, uint32_t type
, uintptr_t offset
)
244 this function should be called while holding a shared lock to decompressorsLock,
245 and will return with the lock held
251 if (decompressors
[type
] != NULL
) {
252 // the compressor has already registered but the function might be null
253 return _func_from_offset(type
, offset
);
256 // does IOKit know about a kext that is supposed to provide this type?
257 char providesName
[80];
258 snprintf(providesName
, sizeof(providesName
), "com.apple.AppleFSCompression.providesType%u", type
);
259 if (IOCatalogueMatchingDriversPresent(providesName
)) {
260 // there is a kext that says it will register for this type, so let's wait for it
261 char resourceName
[80];
262 uint64_t delay
= 10000000ULL; // 10 milliseconds.
263 snprintf(resourceName
, sizeof(resourceName
), "com.apple.AppleFSCompression.Type%u", type
);
264 ErrorLogWithPath("waiting for %s\n", resourceName
);
265 while(decompressors
[type
] == NULL
) {
266 lck_rw_unlock_shared(decompressorsLock
); // we have to unlock to allow the kext to register
267 if (IOServiceWaitForMatchingResource(resourceName
, delay
)) {
268 lck_rw_lock_shared(decompressorsLock
);
271 if (!IOCatalogueMatchingDriversPresent(providesName
)) {
273 ErrorLogWithPath("the kext with %s is no longer present\n", providesName
);
274 lck_rw_lock_shared(decompressorsLock
);
277 ErrorLogWithPath("still waiting for %s\n", resourceName
);
279 lck_rw_lock_shared(decompressorsLock
);
281 // IOKit says the kext is loaded, so it should be registered too!
282 if (decompressors
[type
] == NULL
) {
283 ErrorLogWithPath("we found %s, but the type still isn't registered\n", providesName
);
286 // it's now registered, so let's return the function
287 return _func_from_offset(type
, offset
);
290 // the compressor hasn't registered, so it never will unless someone manually kextloads it
291 ErrorLogWithPath("tried to access a compressed file of unregistered type %d\n", type
);
295 #define decmp_get_func(vp, type, func) ((typeof(((decmpfs_registration*)NULL)->func))_decmp_get_func(vp, type, offsetof_func(func)))
297 #pragma mark --- utilities ---
299 #if COMPRESSION_DEBUG
301 vnsize(vnode_t vp
, uint64_t *size
)
303 struct vnode_attr va
;
305 VATTR_WANTED(&va
, va_data_size
);
306 int error
= vnode_getattr(vp
, &va
, decmpfs_ctx
);
308 ErrorLogWithPath("vnode_getattr err %d\n", error
);
311 *size
= va
.va_data_size
;
314 #endif /* COMPRESSION_DEBUG */
316 #pragma mark --- cnode routines ---
318 decmpfs_cnode
*decmpfs_cnode_alloc(void)
321 MALLOC_ZONE(dp
, decmpfs_cnode
*, sizeof(decmpfs_cnode
), M_DECMPFS_CNODE
, M_WAITOK
);
325 void decmpfs_cnode_free(decmpfs_cnode
*dp
)
327 FREE_ZONE(dp
, sizeof(*dp
), M_DECMPFS_CNODE
);
331 decmpfs_cnode_init(decmpfs_cnode
*cp
)
333 memset(cp
, 0, sizeof(*cp
));
334 lck_rw_init(&cp
->compressed_data_lock
, decmpfs_lockgrp
, NULL
);
338 decmpfs_cnode_destroy(decmpfs_cnode
*cp
)
340 lck_rw_destroy(&cp
->compressed_data_lock
, decmpfs_lockgrp
);
344 decmpfs_trylock_compressed_data(decmpfs_cnode
*cp
, int exclusive
)
346 void *thread
= current_thread();
349 if (cp
->lockowner
== thread
) {
350 /* this thread is already holding an exclusive lock, so bump the count */
353 } else if (exclusive
) {
354 if ((retval
= lck_rw_try_lock_exclusive(&cp
->compressed_data_lock
))) {
355 cp
->lockowner
= thread
;
359 if ((retval
= lck_rw_try_lock_shared(&cp
->compressed_data_lock
))) {
360 cp
->lockowner
= (void *)-1;
367 decmpfs_lock_compressed_data(decmpfs_cnode
*cp
, int exclusive
)
369 void *thread
= current_thread();
371 if (cp
->lockowner
== thread
) {
372 /* this thread is already holding an exclusive lock, so bump the count */
374 } else if (exclusive
) {
375 lck_rw_lock_exclusive(&cp
->compressed_data_lock
);
376 cp
->lockowner
= thread
;
379 lck_rw_lock_shared(&cp
->compressed_data_lock
);
380 cp
->lockowner
= (void *)-1;
385 decmpfs_unlock_compressed_data(decmpfs_cnode
*cp
, __unused
int exclusive
)
387 void *thread
= current_thread();
389 if (cp
->lockowner
== thread
) {
390 /* this thread is holding an exclusive lock, so decrement the count */
391 if ((--cp
->lockcount
) > 0) {
392 /* the caller still has outstanding locks, so we're done */
395 cp
->lockowner
= NULL
;
398 lck_rw_done(&cp
->compressed_data_lock
);
402 decmpfs_cnode_get_vnode_state(decmpfs_cnode
*cp
)
404 return cp
->cmp_state
;
408 decmpfs_cnode_set_vnode_state(decmpfs_cnode
*cp
, uint32_t state
, int skiplock
)
410 if (!skiplock
) decmpfs_lock_compressed_data(cp
, 1);
411 cp
->cmp_state
= state
;
412 if (state
== FILE_TYPE_UNKNOWN
) {
413 /* clear out the compression type too */
416 if (!skiplock
) decmpfs_unlock_compressed_data(cp
, 1);
420 decmpfs_cnode_set_vnode_cmp_type(decmpfs_cnode
*cp
, uint32_t cmp_type
, int skiplock
)
422 if (!skiplock
) decmpfs_lock_compressed_data(cp
, 1);
423 cp
->cmp_type
= cmp_type
;
424 if (!skiplock
) decmpfs_unlock_compressed_data(cp
, 1);
428 decmpfs_cnode_set_vnode_minimal_xattr(decmpfs_cnode
*cp
, int minimal_xattr
, int skiplock
)
430 if (!skiplock
) decmpfs_lock_compressed_data(cp
, 1);
431 cp
->cmp_minimal_xattr
= minimal_xattr
;
432 if (!skiplock
) decmpfs_unlock_compressed_data(cp
, 1);
436 decmpfs_cnode_get_vnode_cached_size(decmpfs_cnode
*cp
)
438 return cp
->uncompressed_size
;
442 decmpfs_cnode_set_vnode_cached_size(decmpfs_cnode
*cp
, uint64_t size
)
445 uint64_t old
= cp
->uncompressed_size
;
446 if (OSCompareAndSwap64(old
, size
, (UInt64
*)&cp
->uncompressed_size
)) {
449 /* failed to write our value, so loop */
455 decmpfs_cnode_get_decompression_flags(decmpfs_cnode
*cp
)
457 return cp
->decompression_flags
;
461 decmpfs_cnode_set_decompression_flags(decmpfs_cnode
*cp
, uint64_t flags
)
464 uint64_t old
= cp
->decompression_flags
;
465 if (OSCompareAndSwap64(old
, flags
, (UInt64
*)&cp
->decompression_flags
)) {
468 /* failed to write our value, so loop */
473 uint32_t decmpfs_cnode_cmp_type(decmpfs_cnode
*cp
)
478 #pragma mark --- decmpfs state routines ---
481 decmpfs_fetch_compressed_header(vnode_t vp
, decmpfs_cnode
*cp
, decmpfs_header
**hdrOut
, int returnInvalid
)
484 fetches vp's compression xattr, converting it into a decmpfs_header; returns 0 or errno
485 if returnInvalid == 1, returns the header even if the type was invalid (out of range),
486 and return ERANGE in that case
489 size_t read_size
= 0;
490 size_t attr_size
= 0;
491 uio_t attr_uio
= NULL
;
494 const bool no_additional_data
= ((cp
!= NULL
)
495 && (cp
->cmp_type
!= 0)
496 && (cp
->cmp_minimal_xattr
!= 0));
497 char uio_buf
[ UIO_SIZEOF(1) ];
498 decmpfs_header
*hdr
= NULL
;
501 * Trace the following parameters on entry with event-id 0x03120004
503 * @vp->v_id: vnode-id for which to fetch compressed header.
504 * @no_additional_data: If set true then xattr didn't have any extra data.
505 * @returnInvalid: return the header even though the type is out of range.
507 DECMPFS_EMIT_TRACE_ENTRY(DECMPDBG_FETCH_COMPRESSED_HEADER
, vp
->v_id
,
508 no_additional_data
, returnInvalid
);
510 if (no_additional_data
) {
511 /* this file's xattr didn't have any extra data when we fetched it, so we can synthesize a header from the data in the cnode */
513 MALLOC(data
, char *, sizeof(decmpfs_header
), M_TEMP
, M_WAITOK
);
518 hdr
= (decmpfs_header
*)data
;
519 hdr
->attr_size
= sizeof(decmpfs_disk_header
);
520 hdr
->compression_magic
= DECMPFS_MAGIC
;
521 hdr
->compression_type
= cp
->cmp_type
;
522 hdr
->uncompressed_size
= decmpfs_cnode_get_vnode_cached_size(cp
);
524 /* figure out how big the xattr is on disk */
525 err
= vn_getxattr(vp
, DECMPFS_XATTR_NAME
, NULL
, &attr_size
, XATTR_NOSECURITY
, decmpfs_ctx
);
529 if (attr_size
< sizeof(decmpfs_disk_header
) || attr_size
> MAX_DECMPFS_XATTR_SIZE
) {
534 /* allocation includes space for the extra attr_size field of a compressed_header */
535 MALLOC(data
, char *, attr_size
+ sizeof(hdr
->attr_size
), M_TEMP
, M_WAITOK
);
541 /* read the xattr into our buffer, skipping over the attr_size field at the beginning */
542 attr_uio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
543 uio_addiov(attr_uio
, CAST_USER_ADDR_T(data
+ sizeof(hdr
->attr_size
)), attr_size
);
545 err
= vn_getxattr(vp
, DECMPFS_XATTR_NAME
, attr_uio
, &read_size
, XATTR_NOSECURITY
, decmpfs_ctx
);
548 if (read_size
!= attr_size
) {
552 hdr
= (decmpfs_header
*)data
;
553 hdr
->attr_size
= attr_size
;
554 /* swap the fields to native endian */
555 hdr
->compression_magic
= OSSwapLittleToHostInt32(hdr
->compression_magic
);
556 hdr
->compression_type
= OSSwapLittleToHostInt32(hdr
->compression_type
);
557 hdr
->uncompressed_size
= OSSwapLittleToHostInt64(hdr
->uncompressed_size
);
560 if (hdr
->compression_magic
!= DECMPFS_MAGIC
) {
561 ErrorLogWithPath("invalid compression_magic 0x%08x, should be 0x%08x\n", hdr
->compression_magic
, DECMPFS_MAGIC
);
566 if (hdr
->compression_type
>= CMP_MAX
) {
568 /* return the header even though the type is out of range */
571 ErrorLogWithPath("compression_type %d out of range\n", hdr
->compression_type
);
578 if (err
&& (err
!= ERANGE
)) {
579 DebugLogWithPath("err %d\n", err
);
580 if (data
) FREE(data
, M_TEMP
);
586 * Trace the following parameters on return with event-id 0x03120004.
588 * @vp->v_id: vnode-id for which to fetch compressed header.
589 * @err: value returned from this function.
591 DECMPFS_EMIT_TRACE_RETURN(DECMPDBG_FETCH_COMPRESSED_HEADER
, vp
->v_id
, err
);
596 decmpfs_fast_get_state(decmpfs_cnode
*cp
)
599 return the cached state
600 this should *only* be called when we know that decmpfs_file_is_compressed has already been called,
601 because this implies that the cached state is valid
603 int cmp_state
= decmpfs_cnode_get_vnode_state(cp
);
606 case FILE_IS_NOT_COMPRESSED
:
607 case FILE_IS_COMPRESSED
:
608 case FILE_IS_CONVERTING
:
610 case FILE_TYPE_UNKNOWN
:
612 we should only get here if decmpfs_file_is_compressed was not called earlier on this vnode,
613 which should not be possible
615 ErrorLog("decmpfs_fast_get_state called on unknown file\n");
616 return FILE_IS_NOT_COMPRESSED
;
619 ErrorLog("unknown cmp_state %d\n", cmp_state
);
620 return FILE_IS_NOT_COMPRESSED
;
625 decmpfs_fast_file_is_compressed(decmpfs_cnode
*cp
)
627 int cmp_state
= decmpfs_cnode_get_vnode_state(cp
);
630 case FILE_IS_NOT_COMPRESSED
:
632 case FILE_IS_COMPRESSED
:
633 case FILE_IS_CONVERTING
:
635 case FILE_TYPE_UNKNOWN
:
637 we should only get here if decmpfs_file_is_compressed was not called earlier on this vnode,
638 which should not be possible
640 ErrorLog("decmpfs_fast_get_state called on unknown file\n");
644 ErrorLog("unknown cmp_state %d\n", cmp_state
);
650 decmpfs_validate_compressed_file(vnode_t vp
, decmpfs_cnode
*cp
)
652 /* give a compressor a chance to indicate that a compressed file is invalid */
654 decmpfs_header
*hdr
= NULL
;
655 errno_t err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
657 /* we couldn't get the header */
658 if (decmpfs_fast_get_state(cp
) == FILE_IS_NOT_COMPRESSED
) {
659 /* the file is no longer compressed, so return success */
665 lck_rw_lock_shared(decompressorsLock
);
666 decmpfs_validate_compressed_file_func validate
= decmp_get_func(vp
, hdr
->compression_type
, validate
);
667 if (validate
) { /* make sure this validation function is valid */
668 /* is the data okay? */
669 err
= validate(vp
, decmpfs_ctx
, hdr
);
670 } else if (decmp_get_func(vp
, hdr
->compression_type
, fetch
) == NULL
) {
671 /* the type isn't registered */
674 /* no validate registered, so nothing to do */
677 lck_rw_unlock_shared(decompressorsLock
);
679 if (hdr
) FREE(hdr
, M_TEMP
);
680 #if COMPRESSION_DEBUG
682 DebugLogWithPath("decmpfs_validate_compressed_file ret %d, vp->v_flag %d\n", err
, vp
->v_flag
);
689 decmpfs_file_is_compressed(vnode_t vp
, decmpfs_cnode
*cp
)
692 determines whether vp points to a compressed file
694 to speed up this operation, we cache the result in the cnode, and do as little as possible
695 in the case where the cnode already has a valid cached state
702 struct vnode_attr va_fetch
;
703 decmpfs_header
*hdr
= NULL
;
705 int cnode_locked
= 0;
706 int saveInvalid
= 0; // save the header data even though the type was out of range
707 uint64_t decompression_flags
= 0;
708 bool is_mounted
, is_local_fs
;
710 if (vnode_isnamedstream(vp
)) {
712 named streams can't be compressed
713 since named streams of the same file share the same cnode,
714 we don't want to get/set the state in the cnode, just return 0
719 /* examine the cached a state in this cnode */
720 cmp_state
= decmpfs_cnode_get_vnode_state(cp
);
722 case FILE_IS_NOT_COMPRESSED
:
724 case FILE_IS_COMPRESSED
:
726 case FILE_IS_CONVERTING
:
727 /* treat the file as compressed, because this gives us a way to block future reads until decompression is done */
729 case FILE_TYPE_UNKNOWN
:
730 /* the first time we encountered this vnode, so we need to check it out */
733 /* unknown state, assume file is not compressed */
734 ErrorLogWithPath("unknown cmp_state %d\n", cmp_state
);
738 if (!vnode_isreg(vp
)) {
739 /* only regular files can be compressed */
740 ret
= FILE_IS_NOT_COMPRESSED
;
746 mp
= vnode_mount(vp
);
750 is_local_fs
= ((mp
->mnt_flag
& MNT_LOCAL
));
752 * Trace the following parameters on entry with event-id 0x03120014.
754 * @vp->v_id: vnode-id of the file being queried.
755 * @is_mounted: set to true if @vp belongs to a mounted fs.
756 * @is_local_fs: set to true if @vp belongs to local fs.
758 DECMPFS_EMIT_TRACE_ENTRY(DECMPDBG_FILE_IS_COMPRESSED
, vp
->v_id
,
759 is_mounted
, is_local_fs
);
763 this should only be true before we mount the root filesystem
764 we short-cut this return to avoid the call to getattr below, which
765 will fail before root is mounted
767 ret
= FILE_IS_NOT_COMPRESSED
;
772 /* compression only supported on local filesystems */
773 ret
= FILE_IS_NOT_COMPRESSED
;
777 /* lock our cnode data so that another caller doesn't change the state under us */
778 decmpfs_lock_compressed_data(cp
, 1);
781 VATTR_INIT(&va_fetch
);
782 VATTR_WANTED(&va_fetch
, va_flags
);
783 error
= vnode_getattr(vp
, &va_fetch
, decmpfs_ctx
);
785 /* failed to get the bsd flags so the file is not compressed */
786 ret
= FILE_IS_NOT_COMPRESSED
;
789 if (va_fetch
.va_flags
& UF_COMPRESSED
) {
790 /* UF_COMPRESSED is on, make sure the file has the DECMPFS_XATTR_NAME xattr */
791 error
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 1);
792 if ((hdr
!= NULL
) && (error
== ERANGE
)) {
796 /* failed to get the xattr so the file is not compressed */
797 ret
= FILE_IS_NOT_COMPRESSED
;
800 /* we got the xattr, so the file is compressed */
801 ret
= FILE_IS_COMPRESSED
;
804 /* UF_COMPRESSED isn't on, so the file isn't compressed */
805 ret
= FILE_IS_NOT_COMPRESSED
;
808 if (((ret
== FILE_IS_COMPRESSED
) || saveInvalid
) && hdr
) {
810 cache the uncompressed size away in the cnode
815 we should never get here since the only place ret is set to FILE_IS_COMPRESSED
816 is after the call to decmpfs_lock_compressed_data above
818 decmpfs_lock_compressed_data(cp
, 1);
822 decmpfs_cnode_set_vnode_cached_size(cp
, hdr
->uncompressed_size
);
823 decmpfs_cnode_set_vnode_state(cp
, ret
, 1);
824 decmpfs_cnode_set_vnode_cmp_type(cp
, hdr
->compression_type
, 1);
825 /* remember if the xattr's size was equal to the minimal xattr */
826 if (hdr
->attr_size
== sizeof(decmpfs_disk_header
)) {
827 decmpfs_cnode_set_vnode_minimal_xattr(cp
, 1, 1);
829 if (ret
== FILE_IS_COMPRESSED
) {
830 /* update the ubc's size for this file */
831 ubc_setsize(vp
, hdr
->uncompressed_size
);
833 /* update the decompression flags in the decmpfs cnode */
834 lck_rw_lock_shared(decompressorsLock
);
835 decmpfs_get_decompression_flags_func get_flags
= decmp_get_func(vp
, hdr
->compression_type
, get_flags
);
837 decompression_flags
= get_flags(vp
, decmpfs_ctx
, hdr
);
839 lck_rw_unlock_shared(decompressorsLock
);
840 decmpfs_cnode_set_decompression_flags(cp
, decompression_flags
);
843 /* we might have already taken the lock above; if so, skip taking it again by passing cnode_locked as the skiplock parameter */
844 decmpfs_cnode_set_vnode_state(cp
, ret
, cnode_locked
);
847 if (cnode_locked
) decmpfs_unlock_compressed_data(cp
, 1);
849 if (hdr
) FREE(hdr
, M_TEMP
);
851 * Trace the following parameters on return with event-id 0x03120014.
853 * @vp->v_id: vnode-id of the file being queried.
854 * @return: set to 1 is file is compressed.
857 case FILE_IS_NOT_COMPRESSED
:
858 DECMPFS_EMIT_TRACE_RETURN(DECMPDBG_FILE_IS_COMPRESSED
, vp
->v_id
, 0);
860 case FILE_IS_COMPRESSED
:
861 case FILE_IS_CONVERTING
:
862 DECMPFS_EMIT_TRACE_RETURN(DECMPDBG_FILE_IS_COMPRESSED
, vp
->v_id
, 1);
865 /* unknown state, assume file is not compressed */
866 DECMPFS_EMIT_TRACE_RETURN(DECMPDBG_FILE_IS_COMPRESSED
, vp
->v_id
, 0);
867 ErrorLogWithPath("unknown ret %d\n", ret
);
873 decmpfs_update_attributes(vnode_t vp
, struct vnode_attr
*vap
)
877 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
878 /* the BSD flags are being updated */
879 if (vap
->va_flags
& UF_COMPRESSED
) {
880 /* the compressed bit is being set, did it change? */
881 struct vnode_attr va_fetch
;
883 VATTR_INIT(&va_fetch
);
884 VATTR_WANTED(&va_fetch
, va_flags
);
885 error
= vnode_getattr(vp
, &va_fetch
, decmpfs_ctx
);
889 old_flags
= va_fetch
.va_flags
;
891 if (!(old_flags
& UF_COMPRESSED
)) {
893 * Compression bit was turned on, make sure the file has the DECMPFS_XATTR_NAME attribute.
894 * This precludes anyone from using the UF_COMPRESSED bit for anything else, and it enforces
895 * an order of operation -- you must first do the setxattr and then the chflags.
898 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
900 * don't allow the caller to set the BSD flag and the size in the same call
901 * since this doesn't really make sense
903 vap
->va_flags
&= ~UF_COMPRESSED
;
907 decmpfs_header
*hdr
= NULL
;
908 error
= decmpfs_fetch_compressed_header(vp
, NULL
, &hdr
, 1);
911 allow the flag to be set since the decmpfs attribute is present
912 in that case, we also want to truncate the data fork of the file
914 VATTR_SET_ACTIVE(vap
, va_data_size
);
915 vap
->va_data_size
= 0;
916 } else if (error
== ERANGE
) {
917 /* the file had a decmpfs attribute but the type was out of range, so don't muck with the file's data size */
919 /* no DECMPFS_XATTR_NAME attribute, so deny the update */
920 vap
->va_flags
&= ~UF_COMPRESSED
;
922 if (hdr
) FREE(hdr
, M_TEMP
);
931 wait_for_decompress(decmpfs_cnode
*cp
)
934 lck_mtx_lock(decompress_channel_mtx
);
936 state
= decmpfs_fast_get_state(cp
);
937 if (state
!= FILE_IS_CONVERTING
) {
938 /* file is not decompressing */
939 lck_mtx_unlock(decompress_channel_mtx
);
942 msleep((caddr_t
)&decompress_channel
, decompress_channel_mtx
, PINOD
, "wait_for_decompress", NULL
);
946 #pragma mark --- decmpfs hide query routines ---
949 decmpfs_hides_rsrc(vfs_context_t ctx
, decmpfs_cnode
*cp
)
953 callers may (and do) pass NULL for ctx, so we should only use it
954 for this equality comparison
956 This routine should only be called after a file has already been through decmpfs_file_is_compressed
959 if (ctx
== decmpfs_ctx
)
962 if (!decmpfs_fast_file_is_compressed(cp
))
965 /* all compressed files hide their resource fork */
970 decmpfs_hides_xattr(vfs_context_t ctx
, decmpfs_cnode
*cp
, const char *xattr
)
974 callers may (and do) pass NULL for ctx, so we should only use it
975 for this equality comparison
977 This routine should only be called after a file has already been through decmpfs_file_is_compressed
980 if (ctx
== decmpfs_ctx
)
982 if (strncmp(xattr
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
) - 1) == 0)
983 return decmpfs_hides_rsrc(ctx
, cp
);
984 if (!decmpfs_fast_file_is_compressed(cp
))
985 /* file is not compressed, so don't hide this xattr */
987 if (strncmp(xattr
, DECMPFS_XATTR_NAME
, sizeof(DECMPFS_XATTR_NAME
) - 1) == 0)
988 /* it's our xattr, so hide it */
990 /* don't hide this xattr */
994 #pragma mark --- registration/validation routines ---
996 static inline int registration_valid(const decmpfs_registration
*registration
)
998 return registration
&& ((registration
->decmpfs_registration
== DECMPFS_REGISTRATION_VERSION_V1
) || (registration
->decmpfs_registration
== DECMPFS_REGISTRATION_VERSION_V3
));
1002 register_decmpfs_decompressor(uint32_t compression_type
, const decmpfs_registration
*registration
)
1004 /* called by kexts to register decompressors */
1008 char resourceName
[80];
1010 if ((compression_type
>= CMP_MAX
) || !registration_valid(registration
)) {
1015 lck_rw_lock_exclusive(decompressorsLock
); locked
= 1;
1017 /* make sure the registration for this type is zero */
1018 if (decompressors
[compression_type
] != NULL
) {
1022 decompressors
[compression_type
] = registration
;
1023 snprintf(resourceName
, sizeof(resourceName
), "com.apple.AppleFSCompression.Type%u", compression_type
);
1024 IOServicePublishResource(resourceName
, TRUE
);
1027 if (locked
) lck_rw_unlock_exclusive(decompressorsLock
);
1032 unregister_decmpfs_decompressor(uint32_t compression_type
, decmpfs_registration
*registration
)
1034 /* called by kexts to unregister decompressors */
1038 char resourceName
[80];
1040 if ((compression_type
>= CMP_MAX
) || !registration_valid(registration
)) {
1045 lck_rw_lock_exclusive(decompressorsLock
); locked
= 1;
1046 if (decompressors
[compression_type
] != registration
) {
1050 decompressors
[compression_type
] = NULL
;
1051 snprintf(resourceName
, sizeof(resourceName
), "com.apple.AppleFSCompression.Type%u", compression_type
);
1052 IOServicePublishResource(resourceName
, FALSE
);
1055 if (locked
) lck_rw_unlock_exclusive(decompressorsLock
);
1060 compression_type_valid(vnode_t vp
, decmpfs_header
*hdr
)
1062 /* fast pre-check to determine if the given compressor has checked in */
1065 /* every compressor must have at least a fetch function */
1066 lck_rw_lock_shared(decompressorsLock
);
1067 if (decmp_get_func(vp
, hdr
->compression_type
, fetch
) != NULL
) {
1070 lck_rw_unlock_shared(decompressorsLock
);
1075 #pragma mark --- compression/decompression routines ---
1078 decmpfs_fetch_uncompressed_data(vnode_t vp
, decmpfs_cnode
*cp
, decmpfs_header
*hdr
, off_t offset
, user_ssize_t size
, int nvec
, decmpfs_vector
*vec
, uint64_t *bytes_read
)
1080 /* get the uncompressed bytes for the specified region of vp by calling out to the registered compressor */
1086 if ((uint64_t)offset
>= hdr
->uncompressed_size
) {
1087 /* reading past end of file; nothing to do */
1092 /* tried to read from before start of file */
1096 if ((uint64_t)(offset
+ size
) > hdr
->uncompressed_size
) {
1097 /* adjust size so we don't read past the end of the file */
1098 size
= hdr
->uncompressed_size
- offset
;
1101 /* nothing to read */
1107 * Trace the following parameters on entry with event-id 0x03120008.
1109 * @vp->v_id: vnode-id of the file being decompressed.
1110 * @hdr->compression_type: compression type.
1111 * @offset: offset from where to fetch uncompressed data.
1112 * @size: amount of uncompressed data to fetch.
1114 * Please NOTE: @offset and @size can overflow in theory but
1117 DECMPFS_EMIT_TRACE_ENTRY(DECMPDBG_FETCH_UNCOMPRESSED_DATA
, vp
->v_id
,
1118 hdr
->compression_type
, (int)offset
, (int)size
);
1119 lck_rw_lock_shared(decompressorsLock
);
1120 decmpfs_fetch_uncompressed_data_func fetch
= decmp_get_func(vp
, hdr
->compression_type
, fetch
);
1122 err
= fetch(vp
, decmpfs_ctx
, hdr
, offset
, size
, nvec
, vec
, bytes_read
);
1123 lck_rw_unlock_shared(decompressorsLock
);
1125 uint64_t decompression_flags
= decmpfs_cnode_get_decompression_flags(cp
);
1126 if (decompression_flags
& DECMPFS_FLAGS_FORCE_FLUSH_ON_DECOMPRESS
) {
1127 #if !defined(__i386__) && !defined(__x86_64__)
1129 for (i
= 0; i
< nvec
; i
++) {
1130 flush_dcache64((addr64_t
)(uintptr_t)vec
[i
].buf
, vec
[i
].size
, FALSE
);
1137 lck_rw_unlock_shared(decompressorsLock
);
1140 * Trace the following parameters on return with event-id 0x03120008.
1142 * @vp->v_id: vnode-id of the file being decompressed.
1143 * @bytes_read: amount of uncompressed bytes fetched in bytes.
1144 * @err: value returned from this function.
1146 * Please NOTE: @bytes_read can overflow in theory but here it is safe.
1148 DECMPFS_EMIT_TRACE_RETURN(DECMPDBG_FETCH_UNCOMPRESSED_DATA
, vp
->v_id
,
1149 (int)*bytes_read
, err
);
1154 static kern_return_t
1155 commit_upl(upl_t upl
, upl_offset_t pl_offset
, size_t uplSize
, int flags
, int abort
)
1157 kern_return_t kr
= 0;
1160 upl_unmark_decmp(upl
);
1161 #endif /* CONFIG_IOSCHED */
1163 /* commit the upl pages */
1165 VerboseLog("aborting upl, flags 0x%08x\n", flags
);
1166 kr
= ubc_upl_abort_range(upl
, pl_offset
, uplSize
, flags
);
1167 if (kr
!= KERN_SUCCESS
)
1168 ErrorLog("ubc_upl_abort_range error %d\n", (int)kr
);
1170 VerboseLog("committing upl, flags 0x%08x\n", flags
| UPL_COMMIT_CLEAR_DIRTY
);
1171 kr
= ubc_upl_commit_range(upl
, pl_offset
, uplSize
, flags
| UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_WRITTEN_BY_KERNEL
);
1172 if (kr
!= KERN_SUCCESS
)
1173 ErrorLog("ubc_upl_commit_range error %d\n", (int)kr
);
1180 decmpfs_pagein_compressed(struct vnop_pagein_args
*ap
, int *is_compressed
, decmpfs_cnode
*cp
)
1182 /* handles a page-in request from vfs for a compressed file */
1185 vnode_t vp
= ap
->a_vp
;
1186 upl_t pl
= ap
->a_pl
;
1187 upl_offset_t pl_offset
= ap
->a_pl_offset
;
1188 off_t f_offset
= ap
->a_f_offset
;
1189 size_t size
= ap
->a_size
;
1190 int flags
= ap
->a_flags
;
1192 user_ssize_t uplSize
= 0;
1194 decmpfs_header
*hdr
= NULL
;
1195 uint64_t cachedSize
= 0;
1196 int cmpdata_locked
= 0;
1198 if(!decmpfs_trylock_compressed_data(cp
, 0)) {
1204 if (flags
& ~(UPL_IOSYNC
| UPL_NOCOMMIT
| UPL_NORDAHEAD
)) {
1205 DebugLogWithPath("pagein: unknown flags 0x%08x\n", (flags
& ~(UPL_IOSYNC
| UPL_NOCOMMIT
| UPL_NORDAHEAD
)));
1208 err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
1213 cachedSize
= hdr
->uncompressed_size
;
1215 if (!compression_type_valid(vp
, hdr
)) {
1216 /* compressor not registered */
1222 /* Mark the UPL as the requesting UPL for decompression */
1224 #endif /* CONFIG_IOSCHED */
1226 /* map the upl so we can fetch into it */
1227 kern_return_t kr
= ubc_upl_map(pl
, (vm_offset_t
*)&data
);
1228 if ((kr
!= KERN_SUCCESS
) || (data
== NULL
)) {
1232 upl_unmark_decmp(pl
);
1233 #endif /* CONFIG_IOSCHED */
1240 /* clip the size to the size of the file */
1241 if ((uint64_t)uplPos
+ uplSize
> cachedSize
) {
1242 /* truncate the read to the size of the file */
1243 uplSize
= cachedSize
- uplPos
;
1250 /* the mapped data pointer points to the first page of the page list, so we want to start filling in at an offset of pl_offset */
1251 vec
.buf
= (char*)data
+ pl_offset
;
1254 uint64_t did_read
= 0;
1255 if (decmpfs_fast_get_state(cp
) == FILE_IS_CONVERTING
) {
1256 ErrorLogWithPath("unexpected pagein during decompress\n");
1258 if the file is converting, this must be a recursive call to pagein from underneath a call to decmpfs_decompress_file;
1259 pretend that it succeeded but don't do anything since we're just going to write over the pages anyway
1264 err
= decmpfs_fetch_uncompressed_data(vp
, cp
, hdr
, uplPos
, uplSize
, 1, &vec
, &did_read
);
1267 DebugLogWithPath("decmpfs_fetch_uncompressed_data err %d\n", err
);
1268 int cmp_state
= decmpfs_fast_get_state(cp
);
1269 if (cmp_state
== FILE_IS_CONVERTING
) {
1270 DebugLogWithPath("cmp_state == FILE_IS_CONVERTING\n");
1271 cmp_state
= wait_for_decompress(cp
);
1272 if (cmp_state
== FILE_IS_COMPRESSED
) {
1273 DebugLogWithPath("cmp_state == FILE_IS_COMPRESSED\n");
1274 /* a decompress was attempted but it failed, let's try calling fetch again */
1278 if (cmp_state
== FILE_IS_NOT_COMPRESSED
) {
1279 DebugLogWithPath("cmp_state == FILE_IS_NOT_COMPRESSED\n");
1280 /* the file was decompressed after we started reading it */
1281 *is_compressed
= 0; /* instruct caller to fall back to its normal path */
1285 /* zero out whatever we didn't read, and zero out the end of the last page(s) */
1286 uint64_t total_size
= (size
+ (PAGE_SIZE
- 1)) & ~(PAGE_SIZE
- 1);
1287 if (did_read
< total_size
) {
1288 memset((char*)vec
.buf
+ did_read
, 0, total_size
- did_read
);
1292 upl_unmark_decmp(pl
);
1293 #endif /* CONFIG_IOSCHED */
1295 kr
= ubc_upl_unmap(pl
); data
= NULL
; /* make sure to set data to NULL so we don't try to unmap again below */
1296 if (kr
!= KERN_SUCCESS
)
1297 ErrorLogWithPath("ubc_upl_unmap error %d\n", (int)kr
);
1300 /* commit our pages */
1301 kr
= commit_upl(pl
, pl_offset
, total_size
, UPL_COMMIT_FREE_ON_EMPTY
, 0);
1306 if (data
) ubc_upl_unmap(pl
);
1307 if (hdr
) FREE(hdr
, M_TEMP
);
1308 if (cmpdata_locked
) decmpfs_unlock_compressed_data(cp
, 0);
1311 if (err
!= ENXIO
&& err
!= ENOSPC
) {
1313 MALLOC(path
, char *, PATH_MAX
, M_TEMP
, M_WAITOK
);
1314 panic("%s: decmpfs_pagein_compressed: err %d", vnpath(vp
, path
, PATH_MAX
), err
);
1318 ErrorLogWithPath("err %d\n", err
);
1324 decmpfs_read_compressed(struct vnop_read_args
*ap
, int *is_compressed
, decmpfs_cnode
*cp
)
1326 /* handles a read request from vfs for a compressed file */
1328 uio_t uio
= ap
->a_uio
;
1329 vnode_t vp
= ap
->a_vp
;
1333 user_ssize_t uplSize
= 0;
1334 user_ssize_t uplRemaining
= 0;
1335 off_t curUplPos
= 0;
1336 user_ssize_t curUplSize
= 0;
1337 kern_return_t kr
= KERN_SUCCESS
;
1340 uint64_t did_read
= 0;
1342 upl_page_info_t
*pli
= NULL
;
1343 decmpfs_header
*hdr
= NULL
;
1344 uint64_t cachedSize
= 0;
1346 user_ssize_t uioRemaining
= 0;
1347 int cmpdata_locked
= 0;
1349 decmpfs_lock_compressed_data(cp
, 0); cmpdata_locked
= 1;
1351 uplPos
= uio_offset(uio
);
1352 uplSize
= uio_resid(uio
);
1353 VerboseLogWithPath("uplPos %lld uplSize %lld\n", uplPos
, uplSize
);
1355 cachedSize
= decmpfs_cnode_get_vnode_cached_size(cp
);
1357 if ((uint64_t)uplPos
+ uplSize
> cachedSize
) {
1358 /* truncate the read to the size of the file */
1359 uplSize
= cachedSize
- uplPos
;
1362 /* give the cluster layer a chance to fill in whatever it already has */
1363 countInt
= (uplSize
> INT_MAX
) ? INT_MAX
: uplSize
;
1364 err
= cluster_copy_ubc_data(vp
, uio
, &countInt
, 0);
1368 /* figure out what's left */
1369 uioPos
= uio_offset(uio
);
1370 uioRemaining
= uio_resid(uio
);
1371 if ((uint64_t)uioPos
+ uioRemaining
> cachedSize
) {
1372 /* truncate the read to the size of the file */
1373 uioRemaining
= cachedSize
- uioPos
;
1376 if (uioRemaining
<= 0) {
1381 err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
1385 if (!compression_type_valid(vp
, hdr
)) {
1391 uplSize
= uioRemaining
;
1392 #if COMPRESSION_DEBUG
1393 DebugLogWithPath("uplPos %lld uplSize %lld\n", (uint64_t)uplPos
, (uint64_t)uplSize
);
1396 lck_rw_lock_shared(decompressorsLock
);
1397 decmpfs_adjust_fetch_region_func adjust_fetch
= decmp_get_func(vp
, hdr
->compression_type
, adjust_fetch
);
1399 /* give the compressor a chance to adjust the portion of the file that we read */
1400 adjust_fetch(vp
, decmpfs_ctx
, hdr
, &uplPos
, &uplSize
);
1401 VerboseLogWithPath("adjusted uplPos %lld uplSize %lld\n", (uint64_t)uplPos
, (uint64_t)uplSize
);
1403 lck_rw_unlock_shared(decompressorsLock
);
1405 /* clip the adjusted size to the size of the file */
1406 if ((uint64_t)uplPos
+ uplSize
> cachedSize
) {
1407 /* truncate the read to the size of the file */
1408 uplSize
= cachedSize
- uplPos
;
1417 since we're going to create a upl for the given region of the file,
1418 make sure we're on page boundaries
1421 if (uplPos
& (PAGE_SIZE
- 1)) {
1422 /* round position down to page boundary */
1423 uplSize
+= (uplPos
& (PAGE_SIZE
- 1));
1424 uplPos
&= ~(PAGE_SIZE
- 1);
1426 /* round size up to page multiple */
1427 uplSize
= (uplSize
+ (PAGE_SIZE
- 1)) & ~(PAGE_SIZE
- 1);
1429 VerboseLogWithPath("new uplPos %lld uplSize %lld\n", (uint64_t)uplPos
, (uint64_t)uplSize
);
1431 uplRemaining
= uplSize
;
1435 while(uplRemaining
> 0) {
1436 /* start after the last upl */
1437 curUplPos
+= curUplSize
;
1439 /* clip to max upl size */
1440 curUplSize
= uplRemaining
;
1441 if (curUplSize
> MAX_UPL_SIZE_BYTES
) {
1442 curUplSize
= MAX_UPL_SIZE_BYTES
;
1445 /* create the upl */
1446 kr
= ubc_create_upl_kernel(vp
, curUplPos
, curUplSize
, &upl
, &pli
, UPL_SET_LITE
, VM_KERN_MEMORY_FILE
);
1447 if (kr
!= KERN_SUCCESS
) {
1448 ErrorLogWithPath("ubc_create_upl error %d\n", (int)kr
);
1452 VerboseLogWithPath("curUplPos %lld curUplSize %lld\n", (uint64_t)curUplPos
, (uint64_t)curUplSize
);
1455 /* Mark the UPL as the requesting UPL for decompression */
1456 upl_mark_decmp(upl
);
1457 #endif /* CONFIG_IOSCHED */
1460 kr
= ubc_upl_map(upl
, (vm_offset_t
*)&data
);
1461 if (kr
!= KERN_SUCCESS
) {
1463 commit_upl(upl
, 0, curUplSize
, UPL_ABORT_FREE_ON_EMPTY
, 1);
1466 MALLOC(path
, char *, PATH_MAX
, M_TEMP
, M_WAITOK
);
1467 panic("%s: decmpfs_read_compressed: ubc_upl_map error %d", vnpath(vp
, path
, PATH_MAX
), (int)kr
);
1470 ErrorLogWithPath("ubc_upl_map kr=0x%x\n", (int)kr
);
1476 /* make sure the map succeeded */
1479 commit_upl(upl
, 0, curUplSize
, UPL_ABORT_FREE_ON_EMPTY
, 1);
1481 ErrorLogWithPath("ubc_upl_map mapped null\n");
1486 /* fetch uncompressed data into the mapped upl */
1489 vec
= (decmpfs_vector
){ .buf
= data
, .size
= curUplSize
};
1490 err
= decmpfs_fetch_uncompressed_data(vp
, cp
, hdr
, curUplPos
, curUplSize
, 1, &vec
, &did_read
);
1492 ErrorLogWithPath("decmpfs_fetch_uncompressed_data err %d\n", err
);
1494 /* maybe the file is converting to decompressed */
1495 int cmp_state
= decmpfs_fast_get_state(cp
);
1496 if (cmp_state
== FILE_IS_CONVERTING
) {
1497 ErrorLogWithPath("cmp_state == FILE_IS_CONVERTING\n");
1498 cmp_state
= wait_for_decompress(cp
);
1499 if (cmp_state
== FILE_IS_COMPRESSED
) {
1500 ErrorLogWithPath("cmp_state == FILE_IS_COMPRESSED\n");
1501 /* a decompress was attempted but it failed, let's try fetching again */
1505 if (cmp_state
== FILE_IS_NOT_COMPRESSED
) {
1506 ErrorLogWithPath("cmp_state == FILE_IS_NOT_COMPRESSED\n");
1507 /* the file was decompressed after we started reading it */
1508 abort_read
= 1; /* we're not going to commit our data */
1509 *is_compressed
= 0; /* instruct caller to fall back to its normal path */
1514 /* zero out the remainder of the last page */
1515 memset((char*)data
+ did_read
, 0, curUplSize
- did_read
);
1516 kr
= ubc_upl_unmap(upl
);
1517 if (kr
== KERN_SUCCESS
) {
1519 kr
= commit_upl(upl
, 0, curUplSize
, UPL_ABORT_FREE_ON_EMPTY
, 1);
1521 VerboseLogWithPath("uioPos %lld uioRemaining %lld\n", (uint64_t)uioPos
, (uint64_t)uioRemaining
);
1523 off_t uplOff
= uioPos
- curUplPos
;
1525 ErrorLogWithPath("uplOff %lld should never be negative\n", (int64_t)uplOff
);
1528 off_t count
= curUplPos
+ curUplSize
- uioPos
;
1530 /* this upl is entirely before the uio */
1532 if (count
> uioRemaining
)
1533 count
= uioRemaining
;
1534 int io_resid
= count
;
1535 err
= cluster_copy_upl_data(uio
, upl
, uplOff
, &io_resid
);
1536 int copied
= count
- io_resid
;
1537 VerboseLogWithPath("uplOff %lld count %lld copied %lld\n", (uint64_t)uplOff
, (uint64_t)count
, (uint64_t)copied
);
1539 ErrorLogWithPath("cluster_copy_upl_data err %d\n", err
);
1542 uioRemaining
-= copied
;
1546 kr
= commit_upl(upl
, 0, curUplSize
, UPL_COMMIT_FREE_ON_EMPTY
| UPL_COMMIT_INACTIVATE
, 0);
1552 ErrorLogWithPath("ubc_upl_unmap error %d\n", (int)kr
);
1555 uplRemaining
-= curUplSize
;
1560 if (hdr
) FREE(hdr
, M_TEMP
);
1561 if (cmpdata_locked
) decmpfs_unlock_compressed_data(cp
, 0);
1562 if (err
) {/* something went wrong */
1563 ErrorLogWithPath("err %d\n", err
);
1567 #if COMPRESSION_DEBUG
1568 uplSize
= uio_resid(uio
);
1570 VerboseLogWithPath("still %lld bytes to copy\n", uplSize
);
1576 decmpfs_free_compressed_data(vnode_t vp
, decmpfs_cnode
*cp
)
1579 call out to the decompressor to free remove any data associated with this compressed file
1580 then delete the file's compression xattr
1582 decmpfs_header
*hdr
= NULL
;
1585 * Trace the following parameters on entry with event-id 0x03120010.
1587 * @vp->v_id: vnode-id of the file for which to free compressed data.
1589 DECMPFS_EMIT_TRACE_ENTRY(DECMPDBG_FREE_COMPRESSED_DATA
, vp
->v_id
);
1591 int err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
1593 ErrorLogWithPath("decmpfs_fetch_compressed_header err %d\n", err
);
1595 lck_rw_lock_shared(decompressorsLock
);
1596 decmpfs_free_compressed_data_func free_data
= decmp_get_func(vp
, hdr
->compression_type
, free_data
);
1598 err
= free_data(vp
, decmpfs_ctx
, hdr
);
1600 /* nothing to do, so no error */
1603 lck_rw_unlock_shared(decompressorsLock
);
1606 ErrorLogWithPath("decompressor err %d\n", err
);
1610 * Trace the following parameters on return with event-id 0x03120010.
1612 * @vp->v_id: vnode-id of the file for which to free compressed data.
1613 * @err: value returned from this function.
1615 DECMPFS_EMIT_TRACE_RETURN(DECMPDBG_FREE_COMPRESSED_DATA
, vp
->v_id
, err
);
1617 /* delete the xattr */
1618 err
= vn_removexattr(vp
, DECMPFS_XATTR_NAME
, 0, decmpfs_ctx
);
1624 if (hdr
) FREE(hdr
, M_TEMP
);
1628 #pragma mark --- file conversion routines ---
1631 unset_compressed_flag(vnode_t vp
)
1634 struct vnode_attr va
;
1635 int new_bsdflags
= 0;
1638 VATTR_WANTED(&va
, va_flags
);
1639 err
= vnode_getattr(vp
, &va
, decmpfs_ctx
);
1642 ErrorLogWithPath("vnode_getattr err %d\n", err
);
1644 new_bsdflags
= va
.va_flags
& ~UF_COMPRESSED
;
1647 VATTR_SET(&va
, va_flags
, new_bsdflags
);
1648 err
= vnode_setattr(vp
, &va
, decmpfs_ctx
);
1650 ErrorLogWithPath("vnode_setattr err %d\n", err
);
1657 decmpfs_decompress_file(vnode_t vp
, decmpfs_cnode
*cp
, off_t toSize
, int truncate_okay
, int skiplock
)
1659 /* convert a compressed file to an uncompressed file */
1665 uint32_t old_state
= 0;
1666 uint32_t new_state
= 0;
1667 int update_file_state
= 0;
1669 decmpfs_header
*hdr
= NULL
;
1670 int cmpdata_locked
= 0;
1671 off_t remaining
= 0;
1672 uint64_t uncompressed_size
= 0;
1675 * Trace the following parameters on entry with event-id 0x03120000.
1677 * @vp->v_id: vnode-id of the file being decompressed.
1678 * @toSize: uncompress given bytes of the file.
1679 * @truncate_okay: on error it is OK to truncate.
1680 * @skiplock: compressed data is locked, skip locking again.
1682 * Please NOTE: @toSize can overflow in theory but here it is safe.
1684 DECMPFS_EMIT_TRACE_ENTRY(DECMPDBG_DECOMPRESS_FILE
, vp
->v_id
,
1685 (int)toSize
, truncate_okay
, skiplock
);
1688 decmpfs_lock_compressed_data(cp
, 1); cmpdata_locked
= 1;
1692 old_state
= decmpfs_fast_get_state(cp
);
1695 case FILE_IS_NOT_COMPRESSED
:
1697 /* someone else decompressed the file */
1702 case FILE_TYPE_UNKNOWN
:
1704 /* the file is in an unknown state, so update the state and retry */
1705 (void)decmpfs_file_is_compressed(vp
, cp
);
1711 case FILE_IS_COMPRESSED
:
1713 /* the file is compressed, so decompress it */
1720 this shouldn't happen since multiple calls to decmpfs_decompress_file lock each other out,
1721 and when decmpfs_decompress_file returns, the state should be always be set back to
1722 FILE_IS_NOT_COMPRESSED or FILE_IS_UNKNOWN
1729 err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
1734 uncompressed_size
= hdr
->uncompressed_size
;
1736 toSize
= hdr
->uncompressed_size
;
1739 /* special case truncating the file to zero bytes */
1741 } else if ((uint64_t)toSize
> hdr
->uncompressed_size
) {
1742 /* the caller is trying to grow the file, so we should decompress all the data */
1743 toSize
= hdr
->uncompressed_size
;
1746 allocSize
= MIN(64*1024, toSize
);
1747 MALLOC(data
, char *, allocSize
, M_TEMP
, M_WAITOK
);
1753 uio_w
= uio_create(1, 0LL, UIO_SYSSPACE
, UIO_WRITE
);
1758 uio_w
->uio_flags
|= UIO_FLAGS_IS_COMPRESSED_FILE
;
1762 /* tell the buffer cache that this is an empty file */
1765 /* if we got here, we need to decompress the file */
1766 decmpfs_cnode_set_vnode_state(cp
, FILE_IS_CONVERTING
, 1);
1768 while(remaining
> 0) {
1769 /* loop decompressing data from the file and writing it into the data fork */
1771 uint64_t bytes_read
= 0;
1772 decmpfs_vector vec
= { .buf
= data
, .size
= MIN(allocSize
, remaining
) };
1773 err
= decmpfs_fetch_uncompressed_data(vp
, cp
, hdr
, offset
, vec
.size
, 1, &vec
, &bytes_read
);
1775 ErrorLogWithPath("decmpfs_fetch_uncompressed_data err %d\n", err
);
1779 if (bytes_read
== 0) {
1780 /* we're done reading data */
1784 uio_reset(uio_w
, offset
, UIO_SYSSPACE
, UIO_WRITE
);
1785 err
= uio_addiov(uio_w
, CAST_USER_ADDR_T(data
), bytes_read
);
1787 ErrorLogWithPath("uio_addiov err %d\n", err
);
1792 err
= VNOP_WRITE(vp
, uio_w
, 0, decmpfs_ctx
);
1794 /* if the write failed, truncate the file to zero bytes */
1795 ErrorLogWithPath("VNOP_WRITE err %d\n", err
);
1798 offset
+= bytes_read
;
1799 remaining
-= bytes_read
;
1803 if (offset
!= toSize
) {
1804 ErrorLogWithPath("file decompressed to %lld instead of %lld\n", offset
, toSize
);
1811 /* sync the data and metadata */
1812 err
= VNOP_FSYNC(vp
, MNT_WAIT
, decmpfs_ctx
);
1814 ErrorLogWithPath("VNOP_FSYNC err %d\n", err
);
1820 /* write, setattr, or fsync failed */
1821 ErrorLogWithPath("aborting decompress, err %d\n", err
);
1822 if (truncate_okay
) {
1823 /* truncate anything we might have written */
1824 int error
= vnode_setsize(vp
, 0, 0, decmpfs_ctx
);
1825 ErrorLogWithPath("vnode_setsize err %d\n", error
);
1831 /* if we're truncating the file to zero bytes, we'll skip ahead to here */
1833 /* unset the compressed flag */
1834 unset_compressed_flag(vp
);
1836 /* free the compressed data associated with this file */
1837 err
= decmpfs_free_compressed_data(vp
, cp
);
1839 ErrorLogWithPath("decmpfs_free_compressed_data err %d\n", err
);
1843 even if free_compressed_data or vnode_getattr/vnode_setattr failed, return success
1844 since we succeeded in writing all of the file data to the data fork
1848 /* if we got this far, the file was successfully decompressed */
1849 update_file_state
= 1;
1850 new_state
= FILE_IS_NOT_COMPRESSED
;
1852 #if COMPRESSION_DEBUG
1854 uint64_t filesize
= 0;
1855 vnsize(vp
, &filesize
);
1856 DebugLogWithPath("new file size %lld\n", filesize
);
1861 if (hdr
) FREE(hdr
, M_TEMP
);
1862 if (data
) FREE(data
, M_TEMP
);
1863 if (uio_w
) uio_free(uio_w
);
1866 /* if there was a failure, reset compression flags to unknown and clear the buffer cache data */
1867 update_file_state
= 1;
1868 new_state
= FILE_TYPE_UNKNOWN
;
1869 if (uncompressed_size
) {
1871 ubc_setsize(vp
, uncompressed_size
);
1875 if (update_file_state
) {
1876 lck_mtx_lock(decompress_channel_mtx
);
1877 decmpfs_cnode_set_vnode_state(cp
, new_state
, 1);
1878 wakeup((caddr_t
)&decompress_channel
); /* wake up anyone who might have been waiting for decompression */
1879 lck_mtx_unlock(decompress_channel_mtx
);
1882 if (cmpdata_locked
) decmpfs_unlock_compressed_data(cp
, 1);
1884 * Trace the following parameters on return with event-id 0x03120000.
1886 * @vp->v_id: vnode-id of the file being decompressed.
1887 * @err: value returned from this function.
1889 DECMPFS_EMIT_TRACE_RETURN(DECMPDBG_DECOMPRESS_FILE
, vp
->v_id
, err
);
1893 #pragma mark --- Type1 compressor ---
1896 The "Type1" compressor stores the data fork directly in the compression xattr
1900 decmpfs_validate_compressed_file_Type1(__unused vnode_t vp
, __unused vfs_context_t ctx
, decmpfs_header
*hdr
)
1904 if (hdr
->uncompressed_size
+ sizeof(decmpfs_disk_header
) != (uint64_t)hdr
->attr_size
) {
1913 decmpfs_fetch_uncompressed_data_Type1(__unused vnode_t vp
, __unused vfs_context_t ctx
, decmpfs_header
*hdr
, off_t offset
, user_ssize_t size
, int nvec
, decmpfs_vector
*vec
, uint64_t *bytes_read
)
1917 user_ssize_t remaining
;
1919 if (hdr
->uncompressed_size
+ sizeof(decmpfs_disk_header
) != (uint64_t)hdr
->attr_size
) {
1924 #if COMPRESSION_DEBUG
1925 static int dummy
= 0; // prevent syslog from coalescing printfs
1926 DebugLogWithPath("%d memcpy %lld at %lld\n", dummy
++, size
, (uint64_t)offset
);
1930 for (i
= 0; (i
< nvec
) && (remaining
> 0); i
++) {
1931 user_ssize_t curCopy
= vec
[i
].size
;
1932 if (curCopy
> remaining
)
1933 curCopy
= remaining
;
1934 memcpy(vec
[i
].buf
, hdr
->attr_bytes
+ offset
, curCopy
);
1936 remaining
-= curCopy
;
1939 if ((bytes_read
) && (err
== 0))
1940 *bytes_read
= (size
- remaining
);
1946 SECURITY_READ_ONLY_EARLY(static decmpfs_registration
) Type1Reg
=
1948 .decmpfs_registration
= DECMPFS_REGISTRATION_VERSION
,
1949 .validate
= decmpfs_validate_compressed_file_Type1
,
1950 .adjust_fetch
= NULL
, /* no adjust necessary */
1951 .fetch
= decmpfs_fetch_uncompressed_data_Type1
,
1952 .free_data
= NULL
, /* no free necessary */
1953 .get_flags
= NULL
/* no flags */
1956 #pragma mark --- decmpfs initialization ---
1960 static int done
= 0;
1963 decmpfs_ctx
= vfs_context_create(vfs_context_kernel());
1965 lck_grp_attr_t
*attr
= lck_grp_attr_alloc_init();
1966 decmpfs_lockgrp
= lck_grp_alloc_init("VFSCOMP", attr
);
1967 lck_grp_attr_free(attr
);
1968 decompressorsLock
= lck_rw_alloc_init(decmpfs_lockgrp
, NULL
);
1969 decompress_channel_mtx
= lck_mtx_alloc_init(decmpfs_lockgrp
, NULL
);
1971 register_decmpfs_decompressor(CMP_Type1
, &Type1Reg
);
1975 #endif /* FS_COMPRESSION */