2 * Copyright (c) 2008-2015 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@
29 /* we need these symbols even though compression is turned off */
30 char register_decmpfs_decompressor
;
31 char unregister_decmpfs_decompressor
;
32 #else /* HFS_COMPRESSION */
33 #include <sys/kernel.h>
34 #include <sys/vnode_internal.h>
35 #include <sys/file_internal.h>
37 #include <sys/fcntl.h>
38 #include <sys/xattr.h>
39 #include <sys/namei.h>
41 #include <sys/mount_internal.h>
43 #include <sys/decmpfs.h>
44 #include <sys/uio_internal.h>
45 #include <libkern/OSByteOrder.h>
47 #pragma mark --- debugging ---
49 #define COMPRESSION_DEBUG 0
50 #define COMPRESSION_DEBUG_VERBOSE 0
51 #define MALLOC_DEBUG 0
54 baseName(const char *path
)
58 const char *ret
= path
;
60 for (i
= 0; path
[i
] != 0; i
++) {
68 vnpath(vnode_t vp
, char *path
, int len
)
72 vn_getpath(vp
, path
, &len
);
73 path
[origlen
- 1] = 0;
77 #define ErrorLog(x, args...) printf("%s:%d:%s: " x, baseName(__FILE__), __LINE__, __FUNCTION__, ## args)
78 #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)
81 #define DebugLog ErrorLog
82 #define DebugLogWithPath ErrorLogWithPath
84 #define DebugLog(x...) do { } while(0)
85 #define DebugLogWithPath(x...) do { } while(0)
88 #if COMPRESSION_DEBUG_VERBOSE
89 #define VerboseLog ErrorLog
90 #define VerboseLogWithPath ErrorLogWithPath
92 #define VerboseLog(x...) do { } while(0)
93 #define VerboseLogWithPath(x...) do { } while(0)
98 static SInt32 totalAlloc
;
108 _malloc(uint32_t sz
, __unused
int type
, __unused
int flags
, const char *file
, int line
)
110 uint32_t allocSz
= sz
+ 2 * sizeof(allocated
);
112 allocated
*alloc
= NULL
;
113 MALLOC(alloc
, allocated
*, allocSz
, type
, flags
);
115 ErrorLog("malloc failed\n");
119 char *ret
= (char*)&alloc
[1];
120 allocated
*alloc2
= (allocated
*)(ret
+ sz
);
122 alloc
->allocSz
= allocSz
;
123 alloc
->magic
= 0xdadadada;
129 int s
= OSAddAtomic(sz
, &totalAlloc
);
130 ErrorLog("malloc(%d) -> %p, total allocations %d\n", sz
, ret
, s
+ sz
);
136 _free(char *ret
, __unused
int type
, const char *file
, int line
)
139 ErrorLog("freeing null\n");
142 allocated
*alloc
= (allocated
*)ret
;
144 uint32_t sz
= alloc
->allocSz
- 2 * sizeof(allocated
);
145 allocated
*alloc2
= (allocated
*)(ret
+ sz
);
147 if (alloc
->magic
!= 0xdadadada) {
148 panic("freeing bad pointer");
151 if (memcmp(alloc
, alloc2
, sizeof(*alloc
)) != 0) {
152 panic("clobbered data");
155 memset(ret
, 0xce, sz
);
159 int s
= OSAddAtomic(-sz
, &totalAlloc
);
160 ErrorLog("free(%p,%d) -> total allocations %d\n", ret
, sz
, s
- sz
);
165 #define MALLOC(space, cast, size, type, flags) (space) = (cast)_malloc(size, type, flags, __FILE__, __LINE__)
166 #define FREE(addr, type) _free((void *)addr, type, __FILE__, __LINE__)
168 #endif /* MALLOC_DEBUG */
170 #pragma mark --- globals ---
172 static lck_grp_t
*decmpfs_lockgrp
;
174 static decmpfs_registration
* decompressors
[CMP_MAX
]; /* the registered compressors */
175 static lck_rw_t
* decompressorsLock
;
176 static int decompress_channel
; /* channel used by decompress_file to wake up waiters */
177 static lck_mtx_t
*decompress_channel_mtx
;
179 vfs_context_t decmpfs_ctx
;
181 #pragma mark --- decmp_get_func ---
183 #define offsetof_func(func) ((uintptr_t)(&(((decmpfs_registration*)NULL)->func)))
186 _func_from_offset(uint32_t type
, uintptr_t offset
)
188 /* get the function at the given offset in the registration for the given type */
189 decmpfs_registration
*reg
= decompressors
[type
];
190 char *regChar
= (char*)reg
;
191 char *func
= ®Char
[offset
];
192 void **funcPtr
= (void**)func
;
194 switch (reg
->decmpfs_registration
) {
195 case DECMPFS_REGISTRATION_VERSION_V1
:
196 if (offset
> offsetof_func(free_data
))
199 case DECMPFS_REGISTRATION_VERSION_V3
:
200 if (offset
> offsetof_func(get_flags
))
210 extern void IOServicePublishResource( const char * property
, boolean_t value
);
211 extern boolean_t
IOServiceWaitForMatchingResource( const char * property
, uint64_t timeout
);
212 extern boolean_t
IOCatalogueMatchingDriversPresent( const char * property
);
215 _decmp_get_func(vnode_t vp
, uint32_t type
, uintptr_t offset
)
218 this function should be called while holding a shared lock to decompressorsLock,
219 and will return with the lock held
225 if (decompressors
[type
] != NULL
) {
226 // the compressor has already registered but the function might be null
227 return _func_from_offset(type
, offset
);
230 // does IOKit know about a kext that is supposed to provide this type?
231 char providesName
[80];
232 snprintf(providesName
, sizeof(providesName
), "com.apple.AppleFSCompression.providesType%u", type
);
233 if (IOCatalogueMatchingDriversPresent(providesName
)) {
234 // there is a kext that says it will register for this type, so let's wait for it
235 char resourceName
[80];
236 uint64_t delay
= 10000000ULL; // 10 milliseconds.
237 snprintf(resourceName
, sizeof(resourceName
), "com.apple.AppleFSCompression.Type%u", type
);
238 ErrorLogWithPath("waiting for %s\n", resourceName
);
239 while(decompressors
[type
] == NULL
) {
240 lck_rw_unlock_shared(decompressorsLock
); // we have to unlock to allow the kext to register
241 if (IOServiceWaitForMatchingResource(resourceName
, delay
)) {
242 lck_rw_lock_shared(decompressorsLock
);
245 if (!IOCatalogueMatchingDriversPresent(providesName
)) {
247 ErrorLogWithPath("the kext with %s is no longer present\n", providesName
);
248 lck_rw_lock_shared(decompressorsLock
);
251 ErrorLogWithPath("still waiting for %s\n", resourceName
);
253 lck_rw_lock_shared(decompressorsLock
);
255 // IOKit says the kext is loaded, so it should be registered too!
256 if (decompressors
[type
] == NULL
) {
257 ErrorLogWithPath("we found %s, but the type still isn't registered\n", providesName
);
260 // it's now registered, so let's return the function
261 return _func_from_offset(type
, offset
);
264 // the compressor hasn't registered, so it never will unless someone manually kextloads it
265 ErrorLogWithPath("tried to access a compressed file of unregistered type %d\n", type
);
269 #define decmp_get_func(vp, type, func) ((typeof(((decmpfs_registration*)NULL)->func))_decmp_get_func(vp, type, offsetof_func(func)))
271 #pragma mark --- utilities ---
273 #if COMPRESSION_DEBUG
275 vnsize(vnode_t vp
, uint64_t *size
)
277 struct vnode_attr va
;
279 VATTR_WANTED(&va
, va_data_size
);
280 int error
= vnode_getattr(vp
, &va
, decmpfs_ctx
);
282 ErrorLogWithPath("vnode_getattr err %d\n", error
);
285 *size
= va
.va_data_size
;
288 #endif /* COMPRESSION_DEBUG */
290 #pragma mark --- cnode routines ---
293 decmpfs_cnode_init(decmpfs_cnode
*cp
)
295 memset(cp
, 0, sizeof(*cp
));
296 lck_rw_init(&cp
->compressed_data_lock
, decmpfs_lockgrp
, NULL
);
300 decmpfs_cnode_destroy(decmpfs_cnode
*cp
)
302 lck_rw_destroy(&cp
->compressed_data_lock
, decmpfs_lockgrp
);
306 decmpfs_trylock_compressed_data(decmpfs_cnode
*cp
, int exclusive
)
308 void *thread
= current_thread();
309 boolean_t retval
= FALSE
;
311 if (cp
->lockowner
== thread
) {
312 /* this thread is already holding an exclusive lock, so bump the count */
315 } else if (exclusive
) {
316 if ((retval
= lck_rw_try_lock_exclusive(&cp
->compressed_data_lock
))) {
317 cp
->lockowner
= thread
;
321 if ((retval
= lck_rw_try_lock_shared(&cp
->compressed_data_lock
))) {
322 cp
->lockowner
= (void *)-1;
329 decmpfs_lock_compressed_data(decmpfs_cnode
*cp
, int exclusive
)
331 void *thread
= current_thread();
333 if (cp
->lockowner
== thread
) {
334 /* this thread is already holding an exclusive lock, so bump the count */
336 } else if (exclusive
) {
337 lck_rw_lock_exclusive(&cp
->compressed_data_lock
);
338 cp
->lockowner
= thread
;
341 lck_rw_lock_shared(&cp
->compressed_data_lock
);
342 cp
->lockowner
= (void *)-1;
347 decmpfs_unlock_compressed_data(decmpfs_cnode
*cp
, __unused
int exclusive
)
349 void *thread
= current_thread();
351 if (cp
->lockowner
== thread
) {
352 /* this thread is holding an exclusive lock, so decrement the count */
353 if ((--cp
->lockcount
) > 0) {
354 /* the caller still has outstanding locks, so we're done */
357 cp
->lockowner
= NULL
;
360 lck_rw_done(&cp
->compressed_data_lock
);
364 decmpfs_cnode_get_vnode_state(decmpfs_cnode
*cp
)
366 return cp
->cmp_state
;
370 decmpfs_cnode_set_vnode_state(decmpfs_cnode
*cp
, uint32_t state
, int skiplock
)
372 if (!skiplock
) decmpfs_lock_compressed_data(cp
, 1);
373 cp
->cmp_state
= state
;
374 if (state
== FILE_TYPE_UNKNOWN
) {
375 /* clear out the compression type too */
378 if (!skiplock
) decmpfs_unlock_compressed_data(cp
, 1);
382 decmpfs_cnode_set_vnode_cmp_type(decmpfs_cnode
*cp
, uint32_t cmp_type
, int skiplock
)
384 if (!skiplock
) decmpfs_lock_compressed_data(cp
, 1);
385 cp
->cmp_type
= cmp_type
;
386 if (!skiplock
) decmpfs_unlock_compressed_data(cp
, 1);
390 decmpfs_cnode_set_vnode_minimal_xattr(decmpfs_cnode
*cp
, int minimal_xattr
, int skiplock
)
392 if (!skiplock
) decmpfs_lock_compressed_data(cp
, 1);
393 cp
->cmp_minimal_xattr
= minimal_xattr
;
394 if (!skiplock
) decmpfs_unlock_compressed_data(cp
, 1);
398 decmpfs_cnode_get_vnode_cached_size(decmpfs_cnode
*cp
)
400 return cp
->uncompressed_size
;
404 decmpfs_cnode_set_vnode_cached_size(decmpfs_cnode
*cp
, uint64_t size
)
407 uint64_t old
= cp
->uncompressed_size
;
408 if (OSCompareAndSwap64(old
, size
, (UInt64
*)&cp
->uncompressed_size
)) {
411 /* failed to write our value, so loop */
417 decmpfs_cnode_get_decompression_flags(decmpfs_cnode
*cp
)
419 return cp
->decompression_flags
;
423 decmpfs_cnode_set_decompression_flags(decmpfs_cnode
*cp
, uint64_t flags
)
426 uint64_t old
= cp
->decompression_flags
;
427 if (OSCompareAndSwap64(old
, flags
, (UInt64
*)&cp
->decompression_flags
)) {
430 /* failed to write our value, so loop */
435 #pragma mark --- decmpfs state routines ---
438 decmpfs_fetch_compressed_header(vnode_t vp
, decmpfs_cnode
*cp
, decmpfs_header
**hdrOut
, int returnInvalid
)
441 fetches vp's compression xattr, converting it into a decmpfs_header; returns 0 or errno
442 if returnInvalid == 1, returns the header even if the type was invalid (out of range),
443 and return ERANGE in that case
446 size_t read_size
= 0;
447 size_t attr_size
= 0;
448 uio_t attr_uio
= NULL
;
451 decmpfs_header
*hdr
= NULL
;
452 char uio_buf
[ UIO_SIZEOF(1) ];
455 (cp
->cmp_type
!= 0) &&
456 (cp
->cmp_minimal_xattr
!= 0)) {
457 /* 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 */
459 MALLOC(data
, char *, sizeof(decmpfs_header
), M_TEMP
, M_WAITOK
);
464 hdr
= (decmpfs_header
*)data
;
465 hdr
->attr_size
= sizeof(decmpfs_disk_header
);
466 hdr
->compression_magic
= DECMPFS_MAGIC
;
467 hdr
->compression_type
= cp
->cmp_type
;
468 hdr
->uncompressed_size
= decmpfs_cnode_get_vnode_cached_size(cp
);
470 /* figure out how big the xattr is on disk */
471 err
= vn_getxattr(vp
, DECMPFS_XATTR_NAME
, NULL
, &attr_size
, XATTR_NOSECURITY
, decmpfs_ctx
);
475 if (attr_size
< sizeof(decmpfs_disk_header
) || attr_size
> MAX_DECMPFS_XATTR_SIZE
) {
480 /* allocation includes space for the extra attr_size field of a compressed_header */
481 MALLOC(data
, char *, attr_size
+ sizeof(hdr
->attr_size
), M_TEMP
, M_WAITOK
);
487 /* read the xattr into our buffer, skipping over the attr_size field at the beginning */
488 attr_uio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
489 uio_addiov(attr_uio
, CAST_USER_ADDR_T(data
+ sizeof(hdr
->attr_size
)), attr_size
);
491 err
= vn_getxattr(vp
, DECMPFS_XATTR_NAME
, attr_uio
, &read_size
, XATTR_NOSECURITY
, decmpfs_ctx
);
494 if (read_size
!= attr_size
) {
498 hdr
= (decmpfs_header
*)data
;
499 hdr
->attr_size
= attr_size
;
500 /* swap the fields to native endian */
501 hdr
->compression_magic
= OSSwapLittleToHostInt32(hdr
->compression_magic
);
502 hdr
->compression_type
= OSSwapLittleToHostInt32(hdr
->compression_type
);
503 hdr
->uncompressed_size
= OSSwapLittleToHostInt64(hdr
->uncompressed_size
);
506 if (hdr
->compression_magic
!= DECMPFS_MAGIC
) {
507 ErrorLogWithPath("invalid compression_magic 0x%08x, should be 0x%08x\n", hdr
->compression_magic
, DECMPFS_MAGIC
);
512 if (hdr
->compression_type
>= CMP_MAX
) {
514 /* return the header even though the type is out of range */
517 ErrorLogWithPath("compression_type %d out of range\n", hdr
->compression_type
);
524 if (err
&& (err
!= ERANGE
)) {
525 DebugLogWithPath("err %d\n", err
);
526 if (data
) FREE(data
, M_TEMP
);
535 decmpfs_fast_get_state(decmpfs_cnode
*cp
)
538 return the cached state
539 this should *only* be called when we know that decmpfs_file_is_compressed has already been called,
540 because this implies that the cached state is valid
542 int cmp_state
= decmpfs_cnode_get_vnode_state(cp
);
545 case FILE_IS_NOT_COMPRESSED
:
546 case FILE_IS_COMPRESSED
:
547 case FILE_IS_CONVERTING
:
549 case FILE_TYPE_UNKNOWN
:
551 we should only get here if decmpfs_file_is_compressed was not called earlier on this vnode,
552 which should not be possible
554 ErrorLog("decmpfs_fast_get_state called on unknown file\n");
555 return FILE_IS_NOT_COMPRESSED
;
558 ErrorLog("unknown cmp_state %d\n", cmp_state
);
559 return FILE_IS_NOT_COMPRESSED
;
564 decmpfs_fast_file_is_compressed(decmpfs_cnode
*cp
)
566 int cmp_state
= decmpfs_cnode_get_vnode_state(cp
);
569 case FILE_IS_NOT_COMPRESSED
:
571 case FILE_IS_COMPRESSED
:
572 case FILE_IS_CONVERTING
:
574 case FILE_TYPE_UNKNOWN
:
576 we should only get here if decmpfs_file_is_compressed was not called earlier on this vnode,
577 which should not be possible
579 ErrorLog("decmpfs_fast_get_state called on unknown file\n");
583 ErrorLog("unknown cmp_state %d\n", cmp_state
);
589 decmpfs_validate_compressed_file(vnode_t vp
, decmpfs_cnode
*cp
)
591 /* give a compressor a chance to indicate that a compressed file is invalid */
593 decmpfs_header
*hdr
= NULL
;
594 errno_t err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
596 /* we couldn't get the header */
597 if (decmpfs_fast_get_state(cp
) == FILE_IS_NOT_COMPRESSED
) {
598 /* the file is no longer compressed, so return success */
604 lck_rw_lock_shared(decompressorsLock
);
605 decmpfs_validate_compressed_file_func validate
= decmp_get_func(vp
, hdr
->compression_type
, validate
);
606 if (validate
) { /* make sure this validation function is valid */
607 /* is the data okay? */
608 err
= validate(vp
, decmpfs_ctx
, hdr
);
609 } else if (decmp_get_func(vp
, hdr
->compression_type
, fetch
) == NULL
) {
610 /* the type isn't registered */
613 /* no validate registered, so nothing to do */
616 lck_rw_unlock_shared(decompressorsLock
);
618 if (hdr
) FREE(hdr
, M_TEMP
);
619 #if COMPRESSION_DEBUG
621 DebugLogWithPath("decmpfs_validate_compressed_file ret %d, vp->v_flag %d\n", err
, vp
->v_flag
);
628 decmpfs_file_is_compressed(vnode_t vp
, decmpfs_cnode
*cp
)
631 determines whether vp points to a compressed file
633 to speed up this operation, we cache the result in the cnode, and do as little as possible
634 in the case where the cnode already has a valid cached state
641 struct vnode_attr va_fetch
;
642 decmpfs_header
*hdr
= NULL
;
644 int cnode_locked
= 0;
645 int saveInvalid
= 0; // save the header data even though the type was out of range
646 uint64_t decompression_flags
= 0;
648 if (vnode_isnamedstream(vp
)) {
650 named streams can't be compressed
651 since named streams of the same file share the same cnode,
652 we don't want to get/set the state in the cnode, just return 0
657 /* examine the cached a state in this cnode */
658 cmp_state
= decmpfs_cnode_get_vnode_state(cp
);
660 case FILE_IS_NOT_COMPRESSED
:
662 case FILE_IS_COMPRESSED
:
664 case FILE_IS_CONVERTING
:
665 /* treat the file as compressed, because this gives us a way to block future reads until decompression is done */
667 case FILE_TYPE_UNKNOWN
:
668 /* the first time we encountered this vnode, so we need to check it out */
671 /* unknown state, assume file is not compressed */
672 ErrorLogWithPath("unknown cmp_state %d\n", cmp_state
);
676 if (!vnode_isreg(vp
)) {
677 /* only regular files can be compressed */
678 ret
= FILE_IS_NOT_COMPRESSED
;
682 mp
= vnode_mount(vp
);
685 this should only be true before we mount the root filesystem
686 we short-cut this return to avoid the call to getattr below, which
687 will fail before root is mounted
689 ret
= FILE_IS_NOT_COMPRESSED
;
692 if ((mp
->mnt_flag
& MNT_LOCAL
) == 0) {
693 /* compression only supported on local filesystems */
694 ret
= FILE_IS_NOT_COMPRESSED
;
698 /* lock our cnode data so that another caller doesn't change the state under us */
699 decmpfs_lock_compressed_data(cp
, 1);
702 VATTR_INIT(&va_fetch
);
703 VATTR_WANTED(&va_fetch
, va_flags
);
704 error
= vnode_getattr(vp
, &va_fetch
, decmpfs_ctx
);
706 /* failed to get the bsd flags so the file is not compressed */
707 ret
= FILE_IS_NOT_COMPRESSED
;
710 if (va_fetch
.va_flags
& UF_COMPRESSED
) {
711 /* UF_COMPRESSED is on, make sure the file has the DECMPFS_XATTR_NAME xattr */
712 error
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 1);
713 if ((hdr
!= NULL
) && (error
== ERANGE
)) {
717 /* failed to get the xattr so the file is not compressed */
718 ret
= FILE_IS_NOT_COMPRESSED
;
721 /* we got the xattr, so the file is compressed */
722 ret
= FILE_IS_COMPRESSED
;
725 /* UF_COMPRESSED isn't on, so the file isn't compressed */
726 ret
= FILE_IS_NOT_COMPRESSED
;
729 if (((ret
== FILE_IS_COMPRESSED
) || saveInvalid
) && hdr
) {
731 cache the uncompressed size away in the cnode
736 we should never get here since the only place ret is set to FILE_IS_COMPRESSED
737 is after the call to decmpfs_lock_compressed_data above
739 decmpfs_lock_compressed_data(cp
, 1);
743 decmpfs_cnode_set_vnode_cached_size(cp
, hdr
->uncompressed_size
);
744 decmpfs_cnode_set_vnode_state(cp
, ret
, 1);
745 decmpfs_cnode_set_vnode_cmp_type(cp
, hdr
->compression_type
, 1);
746 /* remember if the xattr's size was equal to the minimal xattr */
747 if (hdr
->attr_size
== sizeof(decmpfs_disk_header
)) {
748 decmpfs_cnode_set_vnode_minimal_xattr(cp
, 1, 1);
750 if (ret
== FILE_IS_COMPRESSED
) {
751 /* update the ubc's size for this file */
752 ubc_setsize(vp
, hdr
->uncompressed_size
);
754 /* update the decompression flags in the decmpfs cnode */
755 lck_rw_lock_shared(decompressorsLock
);
756 decmpfs_get_decompression_flags_func get_flags
= decmp_get_func(vp
, hdr
->compression_type
, get_flags
);
758 decompression_flags
= get_flags(vp
, decmpfs_ctx
, hdr
);
760 lck_rw_unlock_shared(decompressorsLock
);
761 decmpfs_cnode_set_decompression_flags(cp
, decompression_flags
);
764 /* we might have already taken the lock above; if so, skip taking it again by passing cnode_locked as the skiplock parameter */
765 decmpfs_cnode_set_vnode_state(cp
, ret
, cnode_locked
);
768 if (cnode_locked
) decmpfs_unlock_compressed_data(cp
, 1);
770 if (hdr
) FREE(hdr
, M_TEMP
);
773 case FILE_IS_NOT_COMPRESSED
:
775 case FILE_IS_COMPRESSED
:
776 case FILE_IS_CONVERTING
:
779 /* unknown state, assume file is not compressed */
780 ErrorLogWithPath("unknown ret %d\n", ret
);
786 decmpfs_update_attributes(vnode_t vp
, struct vnode_attr
*vap
)
790 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
791 /* the BSD flags are being updated */
792 if (vap
->va_flags
& UF_COMPRESSED
) {
793 /* the compressed bit is being set, did it change? */
794 struct vnode_attr va_fetch
;
796 VATTR_INIT(&va_fetch
);
797 VATTR_WANTED(&va_fetch
, va_flags
);
798 error
= vnode_getattr(vp
, &va_fetch
, decmpfs_ctx
);
802 old_flags
= va_fetch
.va_flags
;
804 if (!(old_flags
& UF_COMPRESSED
)) {
806 * Compression bit was turned on, make sure the file has the DECMPFS_XATTR_NAME attribute.
807 * This precludes anyone from using the UF_COMPRESSED bit for anything else, and it enforces
808 * an order of operation -- you must first do the setxattr and then the chflags.
811 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
813 * don't allow the caller to set the BSD flag and the size in the same call
814 * since this doesn't really make sense
816 vap
->va_flags
&= ~UF_COMPRESSED
;
820 decmpfs_header
*hdr
= NULL
;
821 error
= decmpfs_fetch_compressed_header(vp
, NULL
, &hdr
, 1);
824 allow the flag to be set since the decmpfs attribute is present
825 in that case, we also want to truncate the data fork of the file
827 VATTR_SET_ACTIVE(vap
, va_data_size
);
828 vap
->va_data_size
= 0;
829 } else if (error
== ERANGE
) {
830 /* the file had a decmpfs attribute but the type was out of range, so don't muck with the file's data size */
832 /* no DECMPFS_XATTR_NAME attribute, so deny the update */
833 vap
->va_flags
&= ~UF_COMPRESSED
;
835 if (hdr
) FREE(hdr
, M_TEMP
);
844 wait_for_decompress(decmpfs_cnode
*cp
)
847 lck_mtx_lock(decompress_channel_mtx
);
849 state
= decmpfs_fast_get_state(cp
);
850 if (state
!= FILE_IS_CONVERTING
) {
851 /* file is not decompressing */
852 lck_mtx_unlock(decompress_channel_mtx
);
855 msleep((caddr_t
)&decompress_channel
, decompress_channel_mtx
, PINOD
, "wait_for_decompress", NULL
);
859 #pragma mark --- decmpfs hide query routines ---
862 decmpfs_hides_rsrc(vfs_context_t ctx
, decmpfs_cnode
*cp
)
866 callers may (and do) pass NULL for ctx, so we should only use it
867 for this equality comparison
869 This routine should only be called after a file has already been through decmpfs_file_is_compressed
872 if (ctx
== decmpfs_ctx
)
875 if (!decmpfs_fast_file_is_compressed(cp
))
878 /* all compressed files hide their resource fork */
883 decmpfs_hides_xattr(vfs_context_t ctx
, decmpfs_cnode
*cp
, const char *xattr
)
887 callers may (and do) pass NULL for ctx, so we should only use it
888 for this equality comparison
890 This routine should only be called after a file has already been through decmpfs_file_is_compressed
893 if (ctx
== decmpfs_ctx
)
895 if (strncmp(xattr
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
) - 1) == 0)
896 return decmpfs_hides_rsrc(ctx
, cp
);
897 if (!decmpfs_fast_file_is_compressed(cp
))
898 /* file is not compressed, so don't hide this xattr */
900 if (strncmp(xattr
, DECMPFS_XATTR_NAME
, sizeof(DECMPFS_XATTR_NAME
) - 1) == 0)
901 /* it's our xattr, so hide it */
903 /* don't hide this xattr */
907 #pragma mark --- registration/validation routines ---
909 static inline int registration_valid(decmpfs_registration
*registration
)
911 return registration
&& ((registration
->decmpfs_registration
== DECMPFS_REGISTRATION_VERSION_V1
) || (registration
->decmpfs_registration
== DECMPFS_REGISTRATION_VERSION_V3
));
915 register_decmpfs_decompressor(uint32_t compression_type
, decmpfs_registration
*registration
)
917 /* called by kexts to register decompressors */
921 char resourceName
[80];
923 if ((compression_type
>= CMP_MAX
) || !registration_valid(registration
)) {
928 lck_rw_lock_exclusive(decompressorsLock
); locked
= 1;
930 /* make sure the registration for this type is zero */
931 if (decompressors
[compression_type
] != NULL
) {
935 decompressors
[compression_type
] = registration
;
936 snprintf(resourceName
, sizeof(resourceName
), "com.apple.AppleFSCompression.Type%u", compression_type
);
937 IOServicePublishResource(resourceName
, TRUE
);
940 if (locked
) lck_rw_unlock_exclusive(decompressorsLock
);
945 unregister_decmpfs_decompressor(uint32_t compression_type
, decmpfs_registration
*registration
)
947 /* called by kexts to unregister decompressors */
951 char resourceName
[80];
953 if ((compression_type
>= CMP_MAX
) || !registration_valid(registration
)) {
958 lck_rw_lock_exclusive(decompressorsLock
); locked
= 1;
959 if (decompressors
[compression_type
] != registration
) {
963 decompressors
[compression_type
] = NULL
;
964 snprintf(resourceName
, sizeof(resourceName
), "com.apple.AppleFSCompression.Type%u", compression_type
);
965 IOServicePublishResource(resourceName
, FALSE
);
968 if (locked
) lck_rw_unlock_exclusive(decompressorsLock
);
973 compression_type_valid(vnode_t vp
, decmpfs_header
*hdr
)
975 /* fast pre-check to determine if the given compressor has checked in */
978 /* every compressor must have at least a fetch function */
979 lck_rw_lock_shared(decompressorsLock
);
980 if (decmp_get_func(vp
, hdr
->compression_type
, fetch
) != NULL
) {
983 lck_rw_unlock_shared(decompressorsLock
);
988 #pragma mark --- compression/decompression routines ---
991 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
)
993 /* get the uncompressed bytes for the specified region of vp by calling out to the registered compressor */
999 if ((uint64_t)offset
>= hdr
->uncompressed_size
) {
1000 /* reading past end of file; nothing to do */
1005 /* tried to read from before start of file */
1009 if ((uint64_t)(offset
+ size
) > hdr
->uncompressed_size
) {
1010 /* adjust size so we don't read past the end of the file */
1011 size
= hdr
->uncompressed_size
- offset
;
1014 /* nothing to read */
1019 lck_rw_lock_shared(decompressorsLock
);
1020 decmpfs_fetch_uncompressed_data_func fetch
= decmp_get_func(vp
, hdr
->compression_type
, fetch
);
1022 err
= fetch(vp
, decmpfs_ctx
, hdr
, offset
, size
, nvec
, vec
, bytes_read
);
1023 lck_rw_unlock_shared(decompressorsLock
);
1025 uint64_t decompression_flags
= decmpfs_cnode_get_decompression_flags(cp
);
1026 if (decompression_flags
& DECMPFS_FLAGS_FORCE_FLUSH_ON_DECOMPRESS
) {
1027 #if !defined(__i386__) && !defined(__x86_64__)
1029 for (i
= 0; i
< nvec
; i
++) {
1030 flush_dcache64((addr64_t
)(uintptr_t)vec
[i
].buf
, vec
[i
].size
, FALSE
);
1037 lck_rw_unlock_shared(decompressorsLock
);
1044 static kern_return_t
1045 commit_upl(upl_t upl
, upl_offset_t pl_offset
, size_t uplSize
, int flags
, int abort
)
1047 kern_return_t kr
= 0;
1050 upl_unmark_decmp(upl
);
1051 #endif /* CONFIG_IOSCHED */
1053 /* commit the upl pages */
1055 VerboseLog("aborting upl, flags 0x%08x\n", flags
);
1056 kr
= ubc_upl_abort_range(upl
, pl_offset
, uplSize
, flags
);
1057 if (kr
!= KERN_SUCCESS
)
1058 ErrorLog("ubc_upl_abort_range error %d\n", (int)kr
);
1060 VerboseLog("committing upl, flags 0x%08x\n", flags
| UPL_COMMIT_CLEAR_DIRTY
);
1061 kr
= ubc_upl_commit_range(upl
, pl_offset
, uplSize
, flags
| UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_WRITTEN_BY_KERNEL
);
1062 if (kr
!= KERN_SUCCESS
)
1063 ErrorLog("ubc_upl_commit_range error %d\n", (int)kr
);
1070 decmpfs_pagein_compressed(struct vnop_pagein_args
*ap
, int *is_compressed
, decmpfs_cnode
*cp
)
1072 /* handles a page-in request from vfs for a compressed file */
1075 vnode_t vp
= ap
->a_vp
;
1076 upl_t pl
= ap
->a_pl
;
1077 upl_offset_t pl_offset
= ap
->a_pl_offset
;
1078 off_t f_offset
= ap
->a_f_offset
;
1079 size_t size
= ap
->a_size
;
1080 int flags
= ap
->a_flags
;
1082 user_ssize_t uplSize
= 0;
1084 decmpfs_header
*hdr
= NULL
;
1085 int abort_pagein
= 0;
1086 uint64_t cachedSize
= 0;
1087 int cmpdata_locked
= 0;
1089 if(!decmpfs_trylock_compressed_data(cp
, 0)) {
1095 if (flags
& ~(UPL_IOSYNC
| UPL_NOCOMMIT
| UPL_NORDAHEAD
)) {
1096 DebugLogWithPath("pagein: unknown flags 0x%08x\n", (flags
& ~(UPL_IOSYNC
| UPL_NOCOMMIT
| UPL_NORDAHEAD
)));
1099 err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
1104 cachedSize
= hdr
->uncompressed_size
;
1106 if (!compression_type_valid(vp
, hdr
)) {
1107 /* compressor not registered */
1113 /* Mark the UPL as the requesting UPL for decompression */
1115 #endif /* CONFIG_IOSCHED */
1117 /* map the upl so we can fetch into it */
1118 kern_return_t kr
= ubc_upl_map(pl
, (vm_offset_t
*)&data
);
1119 if ((kr
!= KERN_SUCCESS
) || (data
== NULL
)) {
1122 upl_unmark_decmp(pl
);
1123 #endif /* CONFIG_IOSCHED */
1130 /* clip the size to the size of the file */
1131 if ((uint64_t)uplPos
+ uplSize
> cachedSize
) {
1132 /* truncate the read to the size of the file */
1133 uplSize
= cachedSize
- uplPos
;
1140 /* 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 */
1141 vec
.buf
= (char*)data
+ pl_offset
;
1144 uint64_t did_read
= 0;
1145 if (decmpfs_fast_get_state(cp
) == FILE_IS_CONVERTING
) {
1146 ErrorLogWithPath("unexpected pagein during decompress\n");
1148 if the file is converting, this must be a recursive call to pagein from underneath a call to decmpfs_decompress_file;
1149 pretend that it succeeded but don't do anything since we're just going to write over the pages anyway
1154 err
= decmpfs_fetch_uncompressed_data(vp
, cp
, hdr
, uplPos
, uplSize
, 1, &vec
, &did_read
);
1157 DebugLogWithPath("decmpfs_fetch_uncompressed_data err %d\n", err
);
1158 int cmp_state
= decmpfs_fast_get_state(cp
);
1159 if (cmp_state
== FILE_IS_CONVERTING
) {
1160 DebugLogWithPath("cmp_state == FILE_IS_CONVERTING\n");
1161 cmp_state
= wait_for_decompress(cp
);
1162 if (cmp_state
== FILE_IS_COMPRESSED
) {
1163 DebugLogWithPath("cmp_state == FILE_IS_COMPRESSED\n");
1164 /* a decompress was attempted but it failed, let's try calling fetch again */
1168 if (cmp_state
== FILE_IS_NOT_COMPRESSED
) {
1169 DebugLogWithPath("cmp_state == FILE_IS_NOT_COMPRESSED\n");
1170 /* the file was decompressed after we started reading it */
1171 abort_pagein
= 1; /* we're not going to commit our data */
1172 *is_compressed
= 0; /* instruct caller to fall back to its normal path */
1176 /* zero out whatever we didn't read, and zero out the end of the last page(s) */
1177 uint64_t total_size
= (size
+ (PAGE_SIZE
- 1)) & ~(PAGE_SIZE
- 1);
1178 if (did_read
< total_size
) {
1179 memset((char*)vec
.buf
+ did_read
, 0, total_size
- did_read
);
1183 upl_unmark_decmp(pl
);
1184 #endif /* CONFIG_IOSCHED */
1186 kr
= ubc_upl_unmap(pl
); data
= NULL
; /* make sure to set data to NULL so we don't try to unmap again below */
1187 if (kr
!= KERN_SUCCESS
)
1188 ErrorLogWithPath("ubc_upl_unmap error %d\n", (int)kr
);
1190 if (!abort_pagein
) {
1191 /* commit our pages */
1192 kr
= commit_upl(pl
, pl_offset
, total_size
, UPL_COMMIT_FREE_ON_EMPTY
, 0);
1197 if (data
) ubc_upl_unmap(pl
);
1198 if (hdr
) FREE(hdr
, M_TEMP
);
1199 if (cmpdata_locked
) decmpfs_unlock_compressed_data(cp
, 0);
1201 #if DEVELOPMENT || DEBUG
1203 MALLOC(path
, char *, PATH_MAX
, M_TEMP
, M_WAITOK
);
1204 panic("%s: decmpfs_pagein_compressed: err %d", vnpath(vp
, path
, PATH_MAX
), err
);
1207 ErrorLogWithPath("err %d\n", err
);
1214 decmpfs_read_compressed(struct vnop_read_args
*ap
, int *is_compressed
, decmpfs_cnode
*cp
)
1216 /* handles a read request from vfs for a compressed file */
1218 uio_t uio
= ap
->a_uio
;
1219 vnode_t vp
= ap
->a_vp
;
1223 user_ssize_t uplSize
= 0;
1224 user_ssize_t uplRemaining
= 0;
1225 off_t curUplPos
= 0;
1226 user_ssize_t curUplSize
= 0;
1227 kern_return_t kr
= KERN_SUCCESS
;
1230 uint64_t did_read
= 0;
1232 upl_page_info_t
*pli
= NULL
;
1233 decmpfs_header
*hdr
= NULL
;
1234 uint64_t cachedSize
= 0;
1236 user_ssize_t uioRemaining
= 0;
1237 int cmpdata_locked
= 0;
1239 decmpfs_lock_compressed_data(cp
, 0); cmpdata_locked
= 1;
1241 uplPos
= uio_offset(uio
);
1242 uplSize
= uio_resid(uio
);
1243 VerboseLogWithPath("uplPos %lld uplSize %lld\n", uplPos
, uplSize
);
1245 cachedSize
= decmpfs_cnode_get_vnode_cached_size(cp
);
1247 if ((uint64_t)uplPos
+ uplSize
> cachedSize
) {
1248 /* truncate the read to the size of the file */
1249 uplSize
= cachedSize
- uplPos
;
1252 /* give the cluster layer a chance to fill in whatever it already has */
1253 countInt
= (uplSize
> INT_MAX
) ? INT_MAX
: uplSize
;
1254 err
= cluster_copy_ubc_data(vp
, uio
, &countInt
, 0);
1258 /* figure out what's left */
1259 uioPos
= uio_offset(uio
);
1260 uioRemaining
= uio_resid(uio
);
1261 if ((uint64_t)uioPos
+ uioRemaining
> cachedSize
) {
1262 /* truncate the read to the size of the file */
1263 uioRemaining
= cachedSize
- uioPos
;
1266 if (uioRemaining
<= 0) {
1271 err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
1275 if (!compression_type_valid(vp
, hdr
)) {
1281 uplSize
= uioRemaining
;
1282 #if COMPRESSION_DEBUG
1283 DebugLogWithPath("uplPos %lld uplSize %lld\n", (uint64_t)uplPos
, (uint64_t)uplSize
);
1286 lck_rw_lock_shared(decompressorsLock
);
1287 decmpfs_adjust_fetch_region_func adjust_fetch
= decmp_get_func(vp
, hdr
->compression_type
, adjust_fetch
);
1289 /* give the compressor a chance to adjust the portion of the file that we read */
1290 adjust_fetch(vp
, decmpfs_ctx
, hdr
, &uplPos
, &uplSize
);
1291 VerboseLogWithPath("adjusted uplPos %lld uplSize %lld\n", (uint64_t)uplPos
, (uint64_t)uplSize
);
1293 lck_rw_unlock_shared(decompressorsLock
);
1295 /* clip the adjusted size to the size of the file */
1296 if ((uint64_t)uplPos
+ uplSize
> cachedSize
) {
1297 /* truncate the read to the size of the file */
1298 uplSize
= cachedSize
- uplPos
;
1307 since we're going to create a upl for the given region of the file,
1308 make sure we're on page boundaries
1311 if (uplPos
& (PAGE_SIZE
- 1)) {
1312 /* round position down to page boundary */
1313 uplSize
+= (uplPos
& (PAGE_SIZE
- 1));
1314 uplPos
&= ~(PAGE_SIZE
- 1);
1316 /* round size up to page multiple */
1317 uplSize
= (uplSize
+ (PAGE_SIZE
- 1)) & ~(PAGE_SIZE
- 1);
1319 VerboseLogWithPath("new uplPos %lld uplSize %lld\n", (uint64_t)uplPos
, (uint64_t)uplSize
);
1321 uplRemaining
= uplSize
;
1325 while(uplRemaining
> 0) {
1326 /* start after the last upl */
1327 curUplPos
+= curUplSize
;
1329 /* clip to max upl size */
1330 curUplSize
= uplRemaining
;
1331 if (curUplSize
> MAX_UPL_SIZE_BYTES
) {
1332 curUplSize
= MAX_UPL_SIZE_BYTES
;
1335 /* create the upl */
1336 kr
= ubc_create_upl(vp
, curUplPos
, curUplSize
, &upl
, &pli
, UPL_SET_LITE
);
1337 if (kr
!= KERN_SUCCESS
) {
1338 ErrorLogWithPath("ubc_create_upl error %d\n", (int)kr
);
1342 VerboseLogWithPath("curUplPos %lld curUplSize %lld\n", (uint64_t)curUplPos
, (uint64_t)curUplSize
);
1345 /* Mark the UPL as the requesting UPL for decompression */
1346 upl_mark_decmp(upl
);
1347 #endif /* CONFIG_IOSCHED */
1350 kr
= ubc_upl_map(upl
, (vm_offset_t
*)&data
);
1351 if (kr
!= KERN_SUCCESS
) {
1353 commit_upl(upl
, 0, curUplSize
, UPL_ABORT_FREE_ON_EMPTY
, 1);
1354 #if DEVELOPMENT || DEBUG
1356 MALLOC(path
, char *, PATH_MAX
, M_TEMP
, M_WAITOK
);
1357 panic("%s: decmpfs_read_compressed: ubc_upl_map error %d", vnpath(vp
, path
, PATH_MAX
), (int)kr
);
1360 ErrorLogWithPath("ubc_upl_map error %d\n", (int)kr
);
1366 /* make sure the map succeeded */
1369 commit_upl(upl
, 0, curUplSize
, UPL_ABORT_FREE_ON_EMPTY
, 1);
1371 ErrorLogWithPath("ubc_upl_map mapped null\n");
1376 /* fetch uncompressed data into the mapped upl */
1379 vec
= (decmpfs_vector
){ .buf
= data
, .size
= curUplSize
};
1380 err
= decmpfs_fetch_uncompressed_data(vp
, cp
, hdr
, curUplPos
, curUplSize
, 1, &vec
, &did_read
);
1382 ErrorLogWithPath("decmpfs_fetch_uncompressed_data err %d\n", err
);
1384 /* maybe the file is converting to decompressed */
1385 int cmp_state
= decmpfs_fast_get_state(cp
);
1386 if (cmp_state
== FILE_IS_CONVERTING
) {
1387 ErrorLogWithPath("cmp_state == FILE_IS_CONVERTING\n");
1388 cmp_state
= wait_for_decompress(cp
);
1389 if (cmp_state
== FILE_IS_COMPRESSED
) {
1390 ErrorLogWithPath("cmp_state == FILE_IS_COMPRESSED\n");
1391 /* a decompress was attempted but it failed, let's try fetching again */
1395 if (cmp_state
== FILE_IS_NOT_COMPRESSED
) {
1396 ErrorLogWithPath("cmp_state == FILE_IS_NOT_COMPRESSED\n");
1397 /* the file was decompressed after we started reading it */
1398 abort_read
= 1; /* we're not going to commit our data */
1399 *is_compressed
= 0; /* instruct caller to fall back to its normal path */
1404 /* zero out the remainder of the last page */
1405 memset((char*)data
+ did_read
, 0, curUplSize
- did_read
);
1406 kr
= ubc_upl_unmap(upl
);
1407 if (kr
== KERN_SUCCESS
) {
1409 kr
= commit_upl(upl
, 0, curUplSize
, UPL_ABORT_FREE_ON_EMPTY
, 1);
1411 VerboseLogWithPath("uioPos %lld uioRemaining %lld\n", (uint64_t)uioPos
, (uint64_t)uioRemaining
);
1413 off_t uplOff
= uioPos
- curUplPos
;
1415 ErrorLogWithPath("uplOff %lld should never be negative\n", (int64_t)uplOff
);
1418 off_t count
= curUplPos
+ curUplSize
- uioPos
;
1420 /* this upl is entirely before the uio */
1422 if (count
> uioRemaining
)
1423 count
= uioRemaining
;
1424 int io_resid
= count
;
1425 err
= cluster_copy_upl_data(uio
, upl
, uplOff
, &io_resid
);
1426 int copied
= count
- io_resid
;
1427 VerboseLogWithPath("uplOff %lld count %lld copied %lld\n", (uint64_t)uplOff
, (uint64_t)count
, (uint64_t)copied
);
1429 ErrorLogWithPath("cluster_copy_upl_data err %d\n", err
);
1432 uioRemaining
-= copied
;
1436 kr
= commit_upl(upl
, 0, curUplSize
, UPL_COMMIT_FREE_ON_EMPTY
| UPL_COMMIT_INACTIVATE
, 0);
1442 ErrorLogWithPath("ubc_upl_unmap error %d\n", (int)kr
);
1445 uplRemaining
-= curUplSize
;
1450 if (hdr
) FREE(hdr
, M_TEMP
);
1451 if (cmpdata_locked
) decmpfs_unlock_compressed_data(cp
, 0);
1452 if (err
) {/* something went wrong */
1453 ErrorLogWithPath("err %d\n", err
);
1457 #if COMPRESSION_DEBUG
1458 uplSize
= uio_resid(uio
);
1460 VerboseLogWithPath("still %lld bytes to copy\n", uplSize
);
1466 decmpfs_free_compressed_data(vnode_t vp
, decmpfs_cnode
*cp
)
1469 call out to the decompressor to free remove any data associated with this compressed file
1470 then delete the file's compression xattr
1473 decmpfs_header
*hdr
= NULL
;
1474 int err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
1476 ErrorLogWithPath("decmpfs_fetch_compressed_header err %d\n", err
);
1478 lck_rw_lock_shared(decompressorsLock
);
1479 decmpfs_free_compressed_data_func free_data
= decmp_get_func(vp
, hdr
->compression_type
, free_data
);
1481 err
= free_data(vp
, decmpfs_ctx
, hdr
);
1483 /* nothing to do, so no error */
1486 lck_rw_unlock_shared(decompressorsLock
);
1489 ErrorLogWithPath("decompressor err %d\n", err
);
1493 /* delete the xattr */
1494 err
= vn_removexattr(vp
, DECMPFS_XATTR_NAME
, 0, decmpfs_ctx
);
1500 if (hdr
) FREE(hdr
, M_TEMP
);
1504 #pragma mark --- file conversion routines ---
1507 unset_compressed_flag(vnode_t vp
)
1510 struct vnode_attr va
;
1511 int new_bsdflags
= 0;
1514 VATTR_WANTED(&va
, va_flags
);
1515 err
= vnode_getattr(vp
, &va
, decmpfs_ctx
);
1518 ErrorLogWithPath("vnode_getattr err %d\n", err
);
1520 new_bsdflags
= va
.va_flags
& ~UF_COMPRESSED
;
1523 VATTR_SET(&va
, va_flags
, new_bsdflags
);
1524 err
= vnode_setattr(vp
, &va
, decmpfs_ctx
);
1526 ErrorLogWithPath("vnode_setattr err %d\n", err
);
1533 decmpfs_decompress_file(vnode_t vp
, decmpfs_cnode
*cp
, off_t toSize
, int truncate_okay
, int skiplock
)
1535 /* convert a compressed file to an uncompressed file */
1541 uint32_t old_state
= 0;
1542 uint32_t new_state
= 0;
1543 int update_file_state
= 0;
1545 decmpfs_header
*hdr
= NULL
;
1546 int cmpdata_locked
= 0;
1547 off_t remaining
= 0;
1548 uint64_t uncompressed_size
= 0;
1551 decmpfs_lock_compressed_data(cp
, 1); cmpdata_locked
= 1;
1555 old_state
= decmpfs_fast_get_state(cp
);
1558 case FILE_IS_NOT_COMPRESSED
:
1560 /* someone else decompressed the file */
1565 case FILE_TYPE_UNKNOWN
:
1567 /* the file is in an unknown state, so update the state and retry */
1568 (void)decmpfs_file_is_compressed(vp
, cp
);
1574 case FILE_IS_COMPRESSED
:
1576 /* the file is compressed, so decompress it */
1583 this shouldn't happen since multiple calls to decmpfs_decompress_file lock each other out,
1584 and when decmpfs_decompress_file returns, the state should be always be set back to
1585 FILE_IS_NOT_COMPRESSED or FILE_IS_UNKNOWN
1592 err
= decmpfs_fetch_compressed_header(vp
, cp
, &hdr
, 0);
1597 uncompressed_size
= hdr
->uncompressed_size
;
1599 toSize
= hdr
->uncompressed_size
;
1602 /* special case truncating the file to zero bytes */
1604 } else if ((uint64_t)toSize
> hdr
->uncompressed_size
) {
1605 /* the caller is trying to grow the file, so we should decompress all the data */
1606 toSize
= hdr
->uncompressed_size
;
1609 allocSize
= MIN(64*1024, toSize
);
1610 MALLOC(data
, char *, allocSize
, M_TEMP
, M_WAITOK
);
1616 uio_w
= uio_create(1, 0LL, UIO_SYSSPACE
, UIO_WRITE
);
1621 uio_w
->uio_flags
|= UIO_FLAGS_IS_COMPRESSED_FILE
;
1625 /* tell the buffer cache that this is an empty file */
1628 /* if we got here, we need to decompress the file */
1629 decmpfs_cnode_set_vnode_state(cp
, FILE_IS_CONVERTING
, 1);
1631 while(remaining
> 0) {
1632 /* loop decompressing data from the file and writing it into the data fork */
1634 uint64_t bytes_read
= 0;
1635 decmpfs_vector vec
= { .buf
= data
, .size
= MIN(allocSize
, remaining
) };
1636 err
= decmpfs_fetch_uncompressed_data(vp
, cp
, hdr
, offset
, vec
.size
, 1, &vec
, &bytes_read
);
1638 ErrorLogWithPath("decmpfs_fetch_uncompressed_data err %d\n", err
);
1642 if (bytes_read
== 0) {
1643 /* we're done reading data */
1647 uio_reset(uio_w
, offset
, UIO_SYSSPACE
, UIO_WRITE
);
1648 err
= uio_addiov(uio_w
, CAST_USER_ADDR_T(data
), bytes_read
);
1650 ErrorLogWithPath("uio_addiov err %d\n", err
);
1655 err
= VNOP_WRITE(vp
, uio_w
, 0, decmpfs_ctx
);
1657 /* if the write failed, truncate the file to zero bytes */
1658 ErrorLogWithPath("VNOP_WRITE err %d\n", err
);
1661 offset
+= bytes_read
;
1662 remaining
-= bytes_read
;
1666 if (offset
!= toSize
) {
1667 ErrorLogWithPath("file decompressed to %lld instead of %lld\n", offset
, toSize
);
1674 /* sync the data and metadata */
1675 err
= VNOP_FSYNC(vp
, MNT_WAIT
, decmpfs_ctx
);
1677 ErrorLogWithPath("VNOP_FSYNC err %d\n", err
);
1683 /* write, setattr, or fsync failed */
1684 ErrorLogWithPath("aborting decompress, err %d\n", err
);
1685 if (truncate_okay
) {
1686 /* truncate anything we might have written */
1687 int error
= vnode_setsize(vp
, 0, 0, decmpfs_ctx
);
1688 ErrorLogWithPath("vnode_setsize err %d\n", error
);
1694 /* if we're truncating the file to zero bytes, we'll skip ahead to here */
1696 /* unset the compressed flag */
1697 unset_compressed_flag(vp
);
1699 /* free the compressed data associated with this file */
1700 err
= decmpfs_free_compressed_data(vp
, cp
);
1702 ErrorLogWithPath("decmpfs_free_compressed_data err %d\n", err
);
1706 even if free_compressed_data or vnode_getattr/vnode_setattr failed, return success
1707 since we succeeded in writing all of the file data to the data fork
1711 /* if we got this far, the file was successfully decompressed */
1712 update_file_state
= 1;
1713 new_state
= FILE_IS_NOT_COMPRESSED
;
1715 #if COMPRESSION_DEBUG
1717 uint64_t filesize
= 0;
1718 vnsize(vp
, &filesize
);
1719 DebugLogWithPath("new file size %lld\n", filesize
);
1724 if (hdr
) FREE(hdr
, M_TEMP
);
1725 if (data
) FREE(data
, M_TEMP
);
1726 if (uio_w
) uio_free(uio_w
);
1729 /* if there was a failure, reset compression flags to unknown and clear the buffer cache data */
1730 update_file_state
= 1;
1731 new_state
= FILE_TYPE_UNKNOWN
;
1732 if (uncompressed_size
) {
1734 ubc_setsize(vp
, uncompressed_size
);
1738 if (update_file_state
) {
1739 lck_mtx_lock(decompress_channel_mtx
);
1740 decmpfs_cnode_set_vnode_state(cp
, new_state
, 1);
1741 wakeup((caddr_t
)&decompress_channel
); /* wake up anyone who might have been waiting for decompression */
1742 lck_mtx_unlock(decompress_channel_mtx
);
1745 if (cmpdata_locked
) decmpfs_unlock_compressed_data(cp
, 1);
1750 #pragma mark --- Type1 compressor ---
1753 The "Type1" compressor stores the data fork directly in the compression xattr
1757 decmpfs_validate_compressed_file_Type1(__unused vnode_t vp
, __unused vfs_context_t ctx
, decmpfs_header
*hdr
)
1761 if (hdr
->uncompressed_size
+ sizeof(decmpfs_disk_header
) != (uint64_t)hdr
->attr_size
) {
1770 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
)
1774 user_ssize_t remaining
;
1776 if (hdr
->uncompressed_size
+ sizeof(decmpfs_disk_header
) != (uint64_t)hdr
->attr_size
) {
1781 #if COMPRESSION_DEBUG
1782 static int dummy
= 0; // prevent syslog from coalescing printfs
1783 DebugLogWithPath("%d memcpy %lld at %lld\n", dummy
++, size
, (uint64_t)offset
);
1787 for (i
= 0; (i
< nvec
) && (remaining
> 0); i
++) {
1788 user_ssize_t curCopy
= vec
[i
].size
;
1789 if (curCopy
> remaining
)
1790 curCopy
= remaining
;
1791 memcpy(vec
[i
].buf
, hdr
->attr_bytes
+ offset
, curCopy
);
1793 remaining
-= curCopy
;
1796 if ((bytes_read
) && (err
== 0))
1797 *bytes_read
= (size
- remaining
);
1803 static decmpfs_registration Type1Reg
=
1805 .decmpfs_registration
= DECMPFS_REGISTRATION_VERSION
,
1806 .validate
= decmpfs_validate_compressed_file_Type1
,
1807 .adjust_fetch
= NULL
, /* no adjust necessary */
1808 .fetch
= decmpfs_fetch_uncompressed_data_Type1
,
1809 .free_data
= NULL
, /* no free necessary */
1810 .get_flags
= NULL
/* no flags */
1813 #pragma mark --- decmpfs initialization ---
1817 static int done
= 0;
1820 decmpfs_ctx
= vfs_context_create(vfs_context_kernel());
1822 lck_grp_attr_t
*attr
= lck_grp_attr_alloc_init();
1823 decmpfs_lockgrp
= lck_grp_alloc_init("VFSCOMP", attr
);
1824 decompressorsLock
= lck_rw_alloc_init(decmpfs_lockgrp
, NULL
);
1825 decompress_channel_mtx
= lck_mtx_alloc_init(decmpfs_lockgrp
, NULL
);
1827 register_decmpfs_decompressor(CMP_Type1
, &Type1Reg
);
1831 #endif /* HFS_COMPRESSION */