2 * Copyright (c) 2004-2008 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 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
34 #include <sys/utfconv.h>
35 #include <sys/vnode.h>
36 #include <sys/xattr.h>
37 #include <sys/fcntl.h>
38 #include <sys/vnode_internal.h>
39 #include <sys/kauth.h>
42 #include "hfs_cnode.h"
43 #include "hfs_mount.h"
44 #include "hfs_format.h"
45 #include "hfs_endian.h"
46 #include "hfs_btreeio.h"
47 #include "hfs_fsctl.h"
49 #include "hfscommon/headers/BTreesInternal.h"
51 #define HFS_XATTR_VERBOSE 0
53 #define ATTRIBUTE_FILE_NODE_SIZE 8192
56 /* State information for the listattr_callback callback function. */
57 struct listattr_callback_state
{
66 #endif /* HFS_COMPRESSION */
69 #define HFS_MAXATTRIBUTESIZE (128 * 1024)
70 #define HFS_MAXATTRBLKS (32 * 1024)
73 /* HFS Internal Names */
74 #define XATTR_EXTENDEDSECURITY_NAME "system.extendedsecurity"
75 #define XATTR_XATTREXTENTS_NAME "system.xattrextents"
77 /* Faster version if we already know this is the data fork. */
78 #define RSRC_FORK_EXISTS(CP) \
79 (((CP)->c_attr.ca_blocks - (CP)->c_datafork->ff_data.cf_blocks) > 0)
81 static u_int32_t emptyfinfo
[8] = {0};
83 const char hfs_attrdatafilename
[] = "Attribute Data";
85 static int listattr_callback(const HFSPlusAttrKey
*key
, const HFSPlusAttrData
*data
,
86 struct listattr_callback_state
*state
);
88 static int remove_attribute_records(struct hfsmount
*hfsmp
, BTreeIterator
* iterator
);
90 static int getnodecount(struct hfsmount
*hfsmp
, size_t nodesize
);
92 static size_t getmaxinlineattrsize(struct vnode
* attrvp
);
94 static int read_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
);
96 static int write_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
);
98 static int alloc_attr_blks(struct hfsmount
*hfsmp
, size_t attrsize
, size_t extentbufsize
, HFSPlusExtentDescriptor
*extents
, int *blocks
);
100 static void free_attr_blks(struct hfsmount
*hfsmp
, int blkcnt
, HFSPlusExtentDescriptor
*extents
);
102 static int has_overflow_extents(HFSPlusForkData
*forkdata
);
104 static int count_extent_blocks(int maxblks
, HFSPlusExtentRecord extents
);
108 * Obtain the vnode for a stream.
111 hfs_vnop_getnamedstream(struct vnop_getnamedstream_args
* ap
)
113 vnode_t vp
= ap
->a_vp
;
114 vnode_t
*svpp
= ap
->a_svpp
;
121 * We only support the "com.apple.ResourceFork" stream.
123 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) != 0) {
127 if ( !S_ISREG(cp
->c_mode
) ) {
131 int hide_rsrc
= hfs_hides_rsrc(ap
->a_context
, VTOC(vp
), 1);
132 #endif /* HFS_COMPRESSION */
133 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
136 if ((!RSRC_FORK_EXISTS(cp
)
139 #endif /* HFS_COMPRESSION */
140 ) && (ap
->a_operation
!= NS_OPEN
)) {
144 error
= hfs_vgetrsrc(VTOHFS(vp
), vp
, svpp
, TRUE
);
154 hfs_vnop_makenamedstream(struct vnop_makenamedstream_args
* ap
)
156 vnode_t vp
= ap
->a_vp
;
157 vnode_t
*svpp
= ap
->a_svpp
;
164 * We only support the "com.apple.ResourceFork" stream.
166 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) != 0) {
170 if ( !S_ISREG(cp
->c_mode
) ) {
174 if (hfs_hides_rsrc(ap
->a_context
, VTOC(vp
), 1)) {
175 if (VNODE_IS_RSRC(vp
)) {
178 error
= decmpfs_decompress_file(vp
, VTOCMP(vp
), -1, 1, 0);
183 #endif /* HFS_COMPRESSION */
184 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
187 error
= hfs_vgetrsrc(VTOHFS(vp
), vp
, svpp
, TRUE
);
197 hfs_vnop_removenamedstream(struct vnop_removenamedstream_args
* ap
)
199 vnode_t svp
= ap
->a_svp
;
204 * We only support the "com.apple.ResourceFork" stream.
206 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) != 0) {
210 if (hfs_hides_rsrc(ap
->a_context
, VTOC(svp
), 1)) {
214 #endif /* HFS_COMPRESSION */
218 /* Take truncate lock before taking cnode lock. */
219 hfs_lock_truncate(scp
, TRUE
);
220 if ((error
= hfs_lock(scp
, HFS_EXCLUSIVE_LOCK
))) {
223 if (VTOF(svp
)->ff_size
!= 0) {
224 error
= hfs_truncate(svp
, 0, IO_NDELAY
, 0, 0, ap
->a_context
);
228 hfs_unlock_truncate(scp
, TRUE
);
235 * Retrieve the data of an extended attribute.
239 hfs_vnop_getxattr(struct vnop_getxattr_args
*ap
)
241 struct vnop_getxattr_args {
242 struct vnodeop_desc *a_desc;
248 vfs_context_t a_context;
252 struct vnode
*vp
= ap
->a_vp
;
254 struct hfsmount
*hfsmp
;
255 uio_t uio
= ap
->a_uio
;
256 struct BTreeIterator
* iterator
= NULL
;
257 struct filefork
*btfile
;
258 FSBufferDescriptor btdata
;
259 HFSPlusAttrRecord
* recp
= NULL
;
266 if (vp
== cp
->c_vp
) {
268 int decmpfs_hide
= hfs_hides_xattr(ap
->a_context
, VTOC(vp
), ap
->a_name
, 1); /* 1 == don't take the cnode lock */
269 if (decmpfs_hide
&& !(ap
->a_options
& XATTR_SHOWCOMPRESSION
))
271 #endif /* HFS_COMPRESSION */
273 /* Get the Finder Info. */
274 if (bcmp(ap
->a_name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0) {
275 u_int8_t finderinfo
[32];
278 if ((result
= hfs_lock(cp
, HFS_SHARED_LOCK
))) {
281 /* Make a copy since we may not export all of it. */
282 bcopy(cp
->c_finderinfo
, finderinfo
, sizeof(finderinfo
));
285 /* Don't expose a symlink's private type/creator. */
286 if (vnode_islnk(vp
)) {
287 struct FndrFileInfo
*fip
;
289 fip
= (struct FndrFileInfo
*)&finderinfo
;
293 /* If Finder Info is empty then it doesn't exist. */
294 if (bcmp(finderinfo
, emptyfinfo
, sizeof(emptyfinfo
)) == 0) {
298 *ap
->a_size
= bufsize
;
301 if ((user_size_t
)uio_resid(uio
) < bufsize
)
304 result
= uiomove((caddr_t
)&finderinfo
, bufsize
, uio
);
308 /* Read the Resource Fork. */
309 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0) {
310 struct vnode
*rvp
= NULL
;
311 int openunlinked
= 0;
314 if ( !S_ISREG(cp
->c_mode
) ) {
317 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
320 namelen
= cp
->c_desc
.cd_namelen
;
322 if ( !RSRC_FORK_EXISTS(cp
)) {
327 if ((cp
->c_flag
& C_DELETED
) && (namelen
== 0)) {
331 result
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
);
337 *ap
->a_size
= (size_t)VTOF(rvp
)->ff_size
;
340 user_ssize_t uio_size
= 0;
342 uio_size
= uio_resid(uio
);
343 #endif /* HFS_COMPRESSION */
344 result
= VNOP_READ(rvp
, uio
, 0, ap
->a_context
);
348 (uio_resid(uio
) == uio_size
)) {
350 we intentionally make the above call to VNOP_READ so that
351 it can return an authorization/permission/etc. error
352 based on ap->a_context and thus deny this operation;
353 in that case, result != 0 and we won't proceed
355 however, if result == 0, it will have returned no data
356 because hfs_vnop_read hid the resource fork
357 (hence uio_resid(uio) == uio_size, i.e. the uio is untouched)
359 in that case, we try again with the decmpfs_ctx context
360 to get the actual data
362 result
= VNOP_READ(rvp
, uio
, 0, decmpfs_ctx
);
364 #endif /* HFS_COMPRESSION */
366 /* force the rsrc fork vnode to recycle right away */
369 vref
= vnode_ref (rvp
);
381 * Standard HFS only supports native FinderInfo and Resource Forks.
383 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
387 if ((result
= hfs_lock(cp
, HFS_SHARED_LOCK
))) {
390 /* Bail if we don't have any extended attributes. */
391 if ((hfsmp
->hfs_attribute_vp
== NULL
) ||
392 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) == 0) {
396 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
398 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
399 if (iterator
== NULL
) {
403 bzero(iterator
, sizeof(*iterator
));
405 bufsize
= sizeof(HFSPlusAttrData
) - 2;
407 bufsize
+= uio_resid(uio
);
408 bufsize
= MAX(bufsize
, sizeof(HFSPlusAttrRecord
));
409 MALLOC(recp
, HFSPlusAttrRecord
*, bufsize
, M_TEMP
, M_WAITOK
);
414 btdata
.bufferAddress
= recp
;
415 btdata
.itemSize
= bufsize
;
416 btdata
.itemCount
= 1;
418 result
= hfs_buildattrkey(VTOC(vp
)->c_fileid
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
422 /* Lookup the attribute. */
423 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
424 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
425 hfs_systemfile_unlock(hfsmp
, lockflags
);
428 if (result
== btNotFound
)
433 switch (recp
->recordType
) {
434 case kHFSPlusAttrInlineData
:
436 * Sanity check record size. It's not required to have any
437 * user data, so the minimum size is 2 bytes less that the
438 * size of HFSPlusAttrData (since HFSPlusAttrData struct
439 * has 2 bytes set aside for attribute data).
441 if (datasize
< (sizeof(HFSPlusAttrData
) - 2)) {
442 printf("hfs_getxattr: %d,%s invalid record size %d (expecting %lu)\n",
443 VTOC(vp
)->c_fileid
, ap
->a_name
, datasize
, sizeof(HFSPlusAttrData
));
447 *ap
->a_size
= recp
->attrData
.attrSize
;
448 if (uio
&& recp
->attrData
.attrSize
!= 0) {
449 if (*ap
->a_size
> (user_size_t
)uio_resid(uio
))
452 result
= uiomove((caddr_t
) &recp
->attrData
.attrData
, recp
->attrData
.attrSize
, uio
);
456 case kHFSPlusAttrForkData
:
457 if (datasize
< sizeof(HFSPlusAttrForkData
)) {
458 printf("hfs_getxattr: %d,%s invalid record size %d (expecting %lu)\n",
459 VTOC(vp
)->c_fileid
, ap
->a_name
, datasize
, sizeof(HFSPlusAttrForkData
));
463 *ap
->a_size
= recp
->forkData
.theFork
.logicalSize
;
467 if (*ap
->a_size
> (user_size_t
)uio_resid(uio
)) {
471 /* Process overflow extents if necessary. */
472 if (has_overflow_extents(&recp
->forkData
.theFork
)) {
473 HFSPlusExtentDescriptor
*extentbuf
;
474 HFSPlusExtentDescriptor
*extentptr
;
475 size_t extentbufsize
;
476 u_int32_t totalblocks
;
480 totalblocks
= recp
->forkData
.theFork
.totalBlocks
;
481 /* Ignore bogus block counts. */
482 if (totalblocks
> HFS_MAXATTRBLKS
) {
486 attrlen
= recp
->forkData
.theFork
.logicalSize
;
488 /* Get a buffer to hold the worst case amount of extents. */
489 extentbufsize
= totalblocks
* sizeof(HFSPlusExtentDescriptor
);
490 extentbufsize
= roundup(extentbufsize
, sizeof(HFSPlusExtentRecord
));
491 MALLOC(extentbuf
, HFSPlusExtentDescriptor
*, extentbufsize
, M_TEMP
, M_WAITOK
);
492 if (extentbuf
== NULL
) {
496 bzero(extentbuf
, extentbufsize
);
497 extentptr
= extentbuf
;
499 /* Grab the first 8 extents. */
500 bcopy(&recp
->forkData
.theFork
.extents
[0], extentptr
, sizeof(HFSPlusExtentRecord
));
501 extentptr
+= kHFSPlusExtentDensity
;
502 blkcnt
= count_extent_blocks(totalblocks
, recp
->forkData
.theFork
.extents
);
504 /* Now lookup the overflow extents. */
505 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
506 while (blkcnt
< totalblocks
) {
507 ((HFSPlusAttrKey
*)&iterator
->key
)->startBlock
= blkcnt
;
508 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
510 (recp
->recordType
!= kHFSPlusAttrExtents
) ||
511 (datasize
< sizeof(HFSPlusAttrExtents
))) {
512 printf("hfs_getxattr: %s missing extents, only %d blks of %d found\n",
513 ap
->a_name
, blkcnt
, totalblocks
);
515 break; /* break from while */
517 /* Grab the next 8 extents. */
518 bcopy(&recp
->overflowExtents
.extents
[0], extentptr
, sizeof(HFSPlusExtentRecord
));
519 extentptr
+= kHFSPlusExtentDensity
;
520 blkcnt
+= count_extent_blocks(totalblocks
, recp
->overflowExtents
.extents
);
522 hfs_systemfile_unlock(hfsmp
, lockflags
);
524 if (blkcnt
< totalblocks
) {
527 result
= read_attr_data(hfsmp
, uio
, attrlen
, extentbuf
);
529 FREE(extentbuf
, M_TEMP
);
531 } else /* No overflow extents. */ {
532 result
= read_attr_data(hfsmp
, uio
, recp
->forkData
.theFork
.logicalSize
, recp
->forkData
.theFork
.extents
);
544 FREE(iterator
, M_TEMP
);
550 return MacToVFSError(result
);
554 * Set the data of an extended attribute.
558 hfs_vnop_setxattr(struct vnop_setxattr_args
*ap
)
560 struct vnop_setxattr_args {
561 struct vnodeop_desc *a_desc;
566 vfs_context_t a_context;
570 struct vnode
*vp
= ap
->a_vp
;
571 struct cnode
*cp
= NULL
;
572 struct hfsmount
*hfsmp
;
573 uio_t uio
= ap
->a_uio
;
574 struct BTreeIterator
* iterator
= NULL
;
575 struct filefork
*btfile
= NULL
;
577 FSBufferDescriptor btdata
;
578 HFSPlusAttrRecord
*recp
= NULL
;
579 HFSPlusExtentDescriptor
*extentptr
= NULL
;
580 HFSPlusAttrRecord attrdata
; /* 90 bytes */
581 void * user_data_ptr
= NULL
;
582 int started_transaction
= 0;
585 int allocatedblks
= 0;
588 if (ap
->a_name
== NULL
|| ap
->a_name
[0] == '\0') {
589 return (EINVAL
); /* invalid name */
592 if (VNODE_IS_RSRC(vp
)) {
597 if (hfs_hides_xattr(ap
->a_context
, VTOC(vp
), ap
->a_name
, 1) ) { /* 1 == don't take the cnode lock */
598 result
= decmpfs_decompress_file(vp
, VTOCMP(vp
), -1, 1, 0);
602 #endif /* HFS_COMPRESSION */
604 /* Set the Finder Info. */
605 if (bcmp(ap
->a_name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0) {
606 u_int8_t finderinfo
[32];
607 struct FndrFileInfo
*fip
;
608 void * finderinfo_start
;
611 attrsize
= sizeof(VTOC(vp
)->c_finderinfo
);
613 if ((user_size_t
)uio_resid(uio
) != attrsize
) {
616 /* Grab the new Finder Info data. */
617 if ((result
= uiomove((caddr_t
)&finderinfo
, attrsize
, uio
))) {
620 fip
= (struct FndrFileInfo
*)&finderinfo
;
622 if ((result
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
627 /* Symlink's don't have an external type/creator. */
628 if (vnode_islnk(vp
)) {
629 /* Skip over type/creator fields. */
630 finderinfo_start
= &cp
->c_finderinfo
[8];
633 finderinfo_start
= &cp
->c_finderinfo
[0];
635 * Don't allow the external setting of
636 * file type to kHardLinkFileType.
638 if (fip
->fdType
== SWAP_BE32(kHardLinkFileType
)) {
644 if (bcmp(finderinfo_start
, emptyfinfo
, attrsize
)) {
645 /* attr exists and "create" was specified. */
646 if (ap
->a_options
& XATTR_CREATE
) {
651 /* attr doesn't exists and "replace" was specified. */
652 if (ap
->a_options
& XATTR_REPLACE
) {
657 /* Set the cnode's Finder Info. */
658 if (attrsize
== sizeof(cp
->c_finderinfo
))
659 bcopy(&finderinfo
[0], finderinfo_start
, attrsize
);
661 bcopy(&finderinfo
[8], finderinfo_start
, attrsize
);
663 /* Updating finderInfo updates change time and modified time */
664 cp
->c_touch_chgtime
= TRUE
;
665 cp
->c_flag
|= C_MODIFIED
;
668 * Mirror the invisible bit to the UF_HIDDEN flag.
670 * The fdFlags for files and frFlags for folders are both 8 bytes
671 * into the userInfo (the first 16 bytes of the Finder Info). They
672 * are both 16-bit fields.
674 fdFlags
= *((u_int16_t
*) &cp
->c_finderinfo
[8]);
675 if (fdFlags
& OSSwapHostToBigConstInt16(kFinderInvisibleMask
))
676 cp
->c_flags
|= UF_HIDDEN
;
678 cp
->c_flags
&= ~UF_HIDDEN
;
680 result
= hfs_update(vp
, FALSE
);
685 /* Write the Resource Fork. */
686 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0) {
687 struct vnode
*rvp
= NULL
;
689 int openunlinked
= 0;
691 if (!vnode_isreg(vp
)) {
694 if ((result
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
698 namelen
= cp
->c_desc
.cd_namelen
;
700 if (RSRC_FORK_EXISTS(cp
)) {
701 /* attr exists and "create" was specified. */
702 if (ap
->a_options
& XATTR_CREATE
) {
707 /* attr doesn't exists and "replace" was specified. */
708 if (ap
->a_options
& XATTR_REPLACE
) {
715 * Note that we could be called on to grab the rsrc fork vnode
716 * for a file that has become open-unlinked.
718 if ((cp
->c_flag
& C_DELETED
) && (namelen
== 0)) {
722 result
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
);
727 /* VNOP_WRITE will update timestamps accordingly */
728 result
= VNOP_WRITE(rvp
, uio
, 0, ap
->a_context
);
730 /* if open unlinked, force it inactive */
733 vref
= vnode_ref (rvp
);
745 * Standard HFS only supports native FinderInfo and Resource Forks.
747 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
750 attrsize
= uio_resid(uio
);
752 /* Enforce an upper limit. */
753 if (attrsize
> HFS_MAXATTRIBUTESIZE
) {
758 * Attempt to copy the users attr data before taking any locks.
761 hfsmp
->hfs_max_inline_attrsize
!= 0 &&
762 attrsize
< hfsmp
->hfs_max_inline_attrsize
) {
763 MALLOC(user_data_ptr
, void *, attrsize
, M_TEMP
, M_WAITOK
);
764 if (user_data_ptr
== NULL
) {
769 result
= uiomove((caddr_t
)user_data_ptr
, attrsize
, uio
);
775 result
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
781 /* Start a transaction for our changes. */
782 if (hfs_start_transaction(hfsmp
) != 0) {
786 started_transaction
= 1;
789 * Once we started the transaction, nobody can compete
790 * with us, so make sure this file is still there.
792 if (cp
->c_flag
& C_NOEXISTS
) {
798 * If there isn't an attributes b-tree then create one.
800 if (hfsmp
->hfs_attribute_vp
== NULL
) {
801 result
= hfs_create_attr_btree(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
,
802 getnodecount(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
));
807 if (hfsmp
->hfs_max_inline_attrsize
== 0) {
808 hfsmp
->hfs_max_inline_attrsize
= getmaxinlineattrsize(hfsmp
->hfs_attribute_vp
);
811 /* Take exclusive access to the attributes b-tree. */
812 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
814 /* Build the b-tree key. */
815 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
816 if (iterator
== NULL
) {
820 bzero(iterator
, sizeof(*iterator
));
821 result
= hfs_buildattrkey(VTOC(vp
)->c_fileid
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
826 /* Preflight for replace/create semantics. */
827 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
828 btdata
.bufferAddress
= &attrdata
;
829 btdata
.itemSize
= sizeof(attrdata
);
830 btdata
.itemCount
= 1;
831 exists
= BTSearchRecord(btfile
, iterator
, &btdata
, NULL
, NULL
) == 0;
833 /* Replace requires that the attribute already exists. */
834 if ((ap
->a_options
& XATTR_REPLACE
) && !exists
) {
838 /* Create requires that the attribute doesn't exist. */
839 if ((ap
->a_options
& XATTR_CREATE
) && exists
) {
843 /* If it won't fit inline then use extent-based attributes. */
844 if (attrsize
> hfsmp
->hfs_max_inline_attrsize
) {
845 size_t extentbufsize
;
848 u_int32_t
*keystartblk
;
851 /* Check if volume supports extent-based attributes */
852 if ((hfsmp
->hfs_flags
& HFS_XATTR_EXTENTS
) == 0) {
857 /* Get some blocks. */
858 blkcnt
= howmany(attrsize
, hfsmp
->blockSize
);
859 extentbufsize
= blkcnt
* sizeof(HFSPlusExtentDescriptor
);
860 extentbufsize
= roundup(extentbufsize
, sizeof(HFSPlusExtentRecord
));
861 MALLOC(extentptr
, HFSPlusExtentDescriptor
*, extentbufsize
, M_TEMP
, M_WAITOK
);
862 if (extentptr
== NULL
) {
866 bzero(extentptr
, extentbufsize
);
867 result
= alloc_attr_blks(hfsmp
, attrsize
, extentbufsize
, extentptr
, &allocatedblks
);
870 goto exit
; /* no more space */
872 /* Copy data into the blocks. */
873 result
= write_attr_data(hfsmp
, uio
, attrsize
, extentptr
);
875 const char *name
= vnode_getname(vp
);
876 printf("hfs_setxattr: write_attr_data err (%d) %s:%s\n",
877 result
, name
? name
: "", ap
->a_name
);
883 /* Now remove any previous attribute. */
885 result
= remove_attribute_records(hfsmp
, iterator
);
887 const char *name
= vnode_getname(vp
);
888 printf("hfs_setxattr: remove_attribute_records err (%d) %s:%s\n",
889 result
, name
? name
: "", ap
->a_name
);
896 /* Create attribute fork data record. */
897 MALLOC(recp
, HFSPlusAttrRecord
*, sizeof(HFSPlusAttrRecord
), M_TEMP
, M_WAITOK
);
902 btdata
.bufferAddress
= recp
;
903 btdata
.itemCount
= 1;
904 btdata
.itemSize
= sizeof(HFSPlusAttrForkData
);
906 recp
->recordType
= kHFSPlusAttrForkData
;
907 recp
->forkData
.reserved
= 0;
908 recp
->forkData
.theFork
.logicalSize
= attrsize
;
909 recp
->forkData
.theFork
.clumpSize
= 0;
910 recp
->forkData
.theFork
.totalBlocks
= blkcnt
;
911 bcopy(extentptr
, recp
->forkData
.theFork
.extents
, sizeof(HFSPlusExtentRecord
));
913 (void) hfs_buildattrkey(VTOC(vp
)->c_fileid
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
915 result
= BTInsertRecord(btfile
, iterator
, &btdata
, btdata
.itemSize
);
917 #if HFS_XATTR_VERBOSE
918 const char *name
= vnode_getname(vp
);
919 printf("hfs_setxattr: BTInsertRecord err (%d) %s:%s\n",
920 MacToVFSError(result
), name
? name
: "", ap
->a_name
);
926 extentblks
= count_extent_blocks(blkcnt
, recp
->forkData
.theFork
.extents
);
927 blkcnt
-= extentblks
;
928 keystartblk
= &((HFSPlusAttrKey
*)&iterator
->key
)->startBlock
;
931 /* Create overflow extents as needed. */
933 /* Initialize the key and record. */
934 *keystartblk
+= (u_int32_t
)extentblks
;
935 btdata
.itemSize
= sizeof(HFSPlusAttrExtents
);
936 recp
->recordType
= kHFSPlusAttrExtents
;
937 recp
->overflowExtents
.reserved
= 0;
939 /* Copy the next set of extents. */
940 i
+= kHFSPlusExtentDensity
;
941 bcopy(&extentptr
[i
], recp
->overflowExtents
.extents
, sizeof(HFSPlusExtentRecord
));
943 result
= BTInsertRecord(btfile
, iterator
, &btdata
, btdata
.itemSize
);
945 const char *name
= vnode_getname(vp
);
946 printf("hfs_setxattr: BTInsertRecord err (%d) %s:%s\n",
947 MacToVFSError(result
), name
? name
: "", ap
->a_name
);
952 extentblks
= count_extent_blocks(blkcnt
, recp
->overflowExtents
.extents
);
953 blkcnt
-= extentblks
;
955 } else /* Inline data */ {
957 result
= remove_attribute_records(hfsmp
, iterator
);
963 /* Calculate size of record rounded up to multiple of 2 bytes. */
964 btdata
.itemSize
= sizeof(HFSPlusAttrData
) - 2 + attrsize
+ ((attrsize
& 1) ? 1 : 0);
965 MALLOC(recp
, HFSPlusAttrRecord
*, btdata
.itemSize
, M_TEMP
, M_WAITOK
);
970 recp
->recordType
= kHFSPlusAttrInlineData
;
971 recp
->attrData
.reserved
[0] = 0;
972 recp
->attrData
.reserved
[1] = 0;
973 recp
->attrData
.attrSize
= attrsize
;
975 /* Copy in the attribute data (if any). */
978 bcopy(user_data_ptr
, &recp
->attrData
.attrData
, attrsize
);
980 result
= uiomove((caddr_t
)&recp
->attrData
.attrData
, attrsize
, uio
);
986 (void) hfs_buildattrkey(VTOC(vp
)->c_fileid
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
988 btdata
.bufferAddress
= recp
;
989 btdata
.itemCount
= 1;
990 result
= BTInsertRecord(btfile
, iterator
, &btdata
, btdata
.itemSize
);
993 if (btfile
&& started_transaction
) {
994 (void) BTFlushPath(btfile
);
997 hfs_systemfile_unlock(hfsmp
, lockflags
);
1001 /* Setting an attribute only updates change time and not
1002 * modified time of the file.
1004 cp
->c_touch_chgtime
= TRUE
;
1005 cp
->c_attr
.ca_recflags
|= kHFSHasAttributesMask
;
1006 if ((bcmp(ap
->a_name
, KAUTH_FILESEC_XATTR
, sizeof(KAUTH_FILESEC_XATTR
)) == 0)) {
1007 cp
->c_attr
.ca_recflags
|= kHFSHasSecurityMask
;
1009 (void) hfs_update(vp
, 0);
1011 if (started_transaction
) {
1012 if (result
&& allocatedblks
) {
1013 free_attr_blks(hfsmp
, allocatedblks
, extentptr
);
1015 hfs_end_transaction(hfsmp
);
1020 if (user_data_ptr
) {
1021 FREE(user_data_ptr
, M_TEMP
);
1027 FREE(extentptr
, M_TEMP
);
1030 FREE(iterator
, M_TEMP
);
1032 return (result
== btNotFound
? ENOATTR
: MacToVFSError(result
));
1036 * Remove an extended attribute.
1040 hfs_vnop_removexattr(struct vnop_removexattr_args
*ap
)
1042 struct vnop_removexattr_args {
1043 struct vnodeop_desc *a_desc;
1047 vfs_context_t a_context;
1051 struct vnode
*vp
= ap
->a_vp
;
1052 struct cnode
*cp
= VTOC(vp
);
1053 struct hfsmount
*hfsmp
;
1054 struct BTreeIterator
* iterator
= NULL
;
1058 if (ap
->a_name
== NULL
|| ap
->a_name
[0] == '\0') {
1059 return (EINVAL
); /* invalid name */
1062 if (VNODE_IS_RSRC(vp
)) {
1067 if (hfs_hides_xattr(ap
->a_context
, VTOC(vp
), ap
->a_name
, 1) && !(ap
->a_options
& XATTR_SHOWCOMPRESSION
))
1069 #endif /* HFS_COMPRESSION */
1071 /* If Resource Fork is non-empty then truncate it. */
1072 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0) {
1073 struct vnode
*rvp
= NULL
;
1075 if ( !vnode_isreg(vp
) ) {
1078 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
1081 if ( !RSRC_FORK_EXISTS(cp
)) {
1085 result
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
);
1091 hfs_lock_truncate(VTOC(rvp
), TRUE
);
1092 if ((result
= hfs_lock(VTOC(rvp
), HFS_EXCLUSIVE_LOCK
))) {
1093 hfs_unlock_truncate(cp
, TRUE
);
1098 /* Start a transaction for encapsulating changes in
1099 * hfs_truncate() and hfs_update()
1101 if ((result
= hfs_start_transaction(hfsmp
))) {
1102 hfs_unlock_truncate(cp
, TRUE
);
1108 result
= hfs_truncate(rvp
, (off_t
)0, IO_NDELAY
, 0, 0, ap
->a_context
);
1110 cp
->c_touch_chgtime
= TRUE
;
1111 cp
->c_flag
|= C_MODIFIED
;
1112 result
= hfs_update(vp
, FALSE
);
1115 hfs_end_transaction(hfsmp
);
1116 hfs_unlock_truncate(VTOC(rvp
), TRUE
);
1117 hfs_unlock(VTOC(rvp
));
1122 /* Clear out the Finder Info. */
1123 if (bcmp(ap
->a_name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0) {
1124 void * finderinfo_start
;
1125 int finderinfo_size
;
1127 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
1131 /* Symlink's don't have an external type/creator. */
1132 if (vnode_islnk(vp
)) {
1133 /* Skip over type/creator fields. */
1134 finderinfo_start
= &cp
->c_finderinfo
[8];
1135 finderinfo_size
= sizeof(cp
->c_finderinfo
) - 8;
1137 finderinfo_start
= &cp
->c_finderinfo
[0];
1138 finderinfo_size
= sizeof(cp
->c_finderinfo
);
1140 if (bcmp(finderinfo_start
, emptyfinfo
, finderinfo_size
) == 0) {
1145 bzero(finderinfo_start
, finderinfo_size
);
1147 /* Updating finderInfo updates change time and modified time */
1148 cp
->c_touch_chgtime
= TRUE
;
1149 cp
->c_flag
|= C_MODIFIED
;
1150 hfs_update(vp
, FALSE
);
1157 * Standard HFS only supports native FinderInfo and Resource Forks.
1159 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1162 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1166 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1167 if (iterator
== NULL
) {
1170 bzero(iterator
, sizeof(*iterator
));
1172 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
1176 result
= hfs_buildattrkey(cp
->c_fileid
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
1181 if (hfs_start_transaction(hfsmp
) != 0) {
1185 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1187 result
= remove_attribute_records(hfsmp
, iterator
);
1189 hfs_systemfile_unlock(hfsmp
, lockflags
);
1192 cp
->c_touch_chgtime
= TRUE
;
1194 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
1196 /* If no more attributes exist, clear attribute bit */
1197 result
= file_attribute_exist(hfsmp
, cp
->c_fileid
);
1199 cp
->c_attr
.ca_recflags
&= ~kHFSHasAttributesMask
;
1201 if (result
== EEXIST
) {
1205 hfs_systemfile_unlock(hfsmp
, lockflags
);
1207 /* If ACL was removed, clear security bit */
1208 if ((bcmp(ap
->a_name
, KAUTH_FILESEC_XATTR
, sizeof(KAUTH_FILESEC_XATTR
)) == 0)) {
1209 cp
->c_attr
.ca_recflags
&= ~kHFSHasSecurityMask
;
1211 (void) hfs_update(vp
, 0);
1214 hfs_end_transaction(hfsmp
);
1217 FREE(iterator
, M_TEMP
);
1218 return MacToVFSError(result
);
1221 /* Check if any attribute record exist for given fileID. This function
1222 * is called by hfs_vnop_removexattr to determine if it should clear the
1223 * attribute bit in the catalog record or not.
1225 * Note - you must acquire a shared lock on the attribute btree before
1226 * calling this function.
1229 * EEXIST - If attribute record was found
1230 * 0 - Attribute was not found
1231 * (other) - Other error (such as EIO)
1234 file_attribute_exist(struct hfsmount
*hfsmp
, uint32_t fileID
)
1236 HFSPlusAttrKey
*key
;
1237 struct BTreeIterator
* iterator
= NULL
;
1238 struct filefork
*btfile
;
1241 // if there's no attribute b-tree we sure as heck
1242 // can't have any attributes!
1243 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1247 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1248 if (iterator
== NULL
) {
1252 bzero(iterator
, sizeof(*iterator
));
1253 key
= (HFSPlusAttrKey
*)&iterator
->key
;
1255 result
= hfs_buildattrkey(fileID
, NULL
, key
);
1260 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1261 result
= BTSearchRecord(btfile
, iterator
, NULL
, NULL
, NULL
);
1262 if (result
&& (result
!= btNotFound
)) {
1266 result
= BTIterateRecord(btfile
, kBTreeNextRecord
, iterator
, NULL
, NULL
);
1267 /* If no next record was found or fileID for next record did not match,
1268 * no more attributes exist for this fileID
1270 if ((result
&& (result
== btNotFound
)) || (key
->fileID
!= fileID
)) {
1278 FREE(iterator
, M_TEMP
);
1285 * Remove all the records for a given attribute.
1287 * - Used by hfs_vnop_removexattr, hfs_vnop_setxattr and hfs_removeallattr.
1288 * - A transaction must have been started.
1289 * - The Attribute b-tree file must be locked exclusive.
1290 * - The Allocation Bitmap file must be locked exclusive.
1291 * - The iterator key must be initialized.
1294 remove_attribute_records(struct hfsmount
*hfsmp
, BTreeIterator
* iterator
)
1296 struct filefork
*btfile
;
1297 FSBufferDescriptor btdata
;
1298 HFSPlusAttrRecord attrdata
; /* 90 bytes */
1302 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1304 btdata
.bufferAddress
= &attrdata
;
1305 btdata
.itemSize
= sizeof(attrdata
);
1306 btdata
.itemCount
= 1;
1307 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
1309 goto exit
; /* no records. */
1312 * Free the blocks from extent based attributes.
1314 * Note that the block references (btree records) are removed
1315 * before releasing the blocks in the allocation bitmap.
1317 if (attrdata
.recordType
== kHFSPlusAttrForkData
) {
1320 u_int32_t
*keystartblk
;
1322 #if HFS_XATTR_VERBOSE
1323 if (datasize
< sizeof(HFSPlusAttrForkData
)) {
1324 printf("hfs: remove_attribute_records: bad record size %d (expecting %d)\n", datasize
, sizeof(HFSPlusAttrForkData
));
1327 totalblks
= attrdata
.forkData
.theFork
.totalBlocks
;
1329 /* Process the first 8 extents. */
1330 extentblks
= count_extent_blocks(totalblks
, attrdata
.forkData
.theFork
.extents
);
1331 if (extentblks
> totalblks
)
1332 panic("hfs: remove_attribute_records: corruption...");
1333 if (BTDeleteRecord(btfile
, iterator
) == 0) {
1334 free_attr_blks(hfsmp
, extentblks
, attrdata
.forkData
.theFork
.extents
);
1336 totalblks
-= extentblks
;
1337 keystartblk
= &((HFSPlusAttrKey
*)&iterator
->key
)->startBlock
;
1339 /* Process any overflow extents. */
1341 *keystartblk
+= (u_int32_t
)extentblks
;
1343 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
1345 (attrdata
.recordType
!= kHFSPlusAttrExtents
) ||
1346 (datasize
< sizeof(HFSPlusAttrExtents
))) {
1347 printf("hfs: remove_attribute_records: BTSearchRecord %d (%d), totalblks %d\n",
1348 MacToVFSError(result
), attrdata
.recordType
!= kHFSPlusAttrExtents
, totalblks
);
1350 break; /* break from while */
1352 /* Process the next 8 extents. */
1353 extentblks
= count_extent_blocks(totalblks
, attrdata
.overflowExtents
.extents
);
1354 if (extentblks
> totalblks
)
1355 panic("hfs: remove_attribute_records: corruption...");
1356 if (BTDeleteRecord(btfile
, iterator
) == 0) {
1357 free_attr_blks(hfsmp
, extentblks
, attrdata
.overflowExtents
.extents
);
1359 totalblks
-= extentblks
;
1362 result
= BTDeleteRecord(btfile
, iterator
);
1364 (void) BTFlushPath(btfile
);
1366 return (result
== btNotFound
? ENOATTR
: MacToVFSError(result
));
1371 * Retrieve the list of extended attribute names.
1375 hfs_vnop_listxattr(struct vnop_listxattr_args
*ap
)
1377 struct vnop_listxattr_args {
1378 struct vnodeop_desc *a_desc;
1383 vfs_context_t a_context;
1386 struct vnode
*vp
= ap
->a_vp
;
1387 struct cnode
*cp
= VTOC(vp
);
1388 struct hfsmount
*hfsmp
;
1389 uio_t uio
= ap
->a_uio
;
1390 struct BTreeIterator
* iterator
= NULL
;
1391 struct filefork
*btfile
;
1392 struct listattr_callback_state state
;
1393 void * finderinfo_start
;
1394 int finderinfo_size
;
1395 user_addr_t user_start
= 0;
1396 user_size_t user_len
= 0;
1400 if (VNODE_IS_RSRC(vp
)) {
1405 int compressed
= hfs_file_is_compressed(cp
, 1); /* 1 == don't take the cnode lock */
1406 #endif /* HFS_COMPRESSION */
1411 if ((result
= hfs_lock(cp
, HFS_SHARED_LOCK
))) {
1415 /* Don't expose a symlink's private type/creator. */
1416 if (vnode_islnk(vp
)) {
1417 /* Skip over type/creator fields. */
1418 finderinfo_start
= &cp
->c_finderinfo
[8];
1419 finderinfo_size
= sizeof(cp
->c_finderinfo
) - 8;
1421 finderinfo_start
= &cp
->c_finderinfo
[0];
1422 finderinfo_size
= sizeof(cp
->c_finderinfo
);
1424 /* If Finder Info is non-empty then export it's name. */
1425 if (bcmp(finderinfo_start
, emptyfinfo
, finderinfo_size
) != 0) {
1427 *ap
->a_size
+= sizeof(XATTR_FINDERINFO_NAME
);
1428 } else if ((user_size_t
)uio_resid(uio
) < sizeof(XATTR_FINDERINFO_NAME
)) {
1432 result
= uiomove(XATTR_FINDERINFO_NAME
,
1433 sizeof(XATTR_FINDERINFO_NAME
), uio
);
1438 /* If Resource Fork is non-empty then export it's name. */
1439 if (S_ISREG(cp
->c_mode
) && RSRC_FORK_EXISTS(cp
)) {
1441 if ((ap
->a_options
& XATTR_SHOWCOMPRESSION
) ||
1443 !hfs_hides_rsrc(ap
->a_context
, VTOC(vp
), 1) /* 1 == don't take the cnode lock */
1445 #endif /* HFS_COMPRESSION */
1448 *ap
->a_size
+= sizeof(XATTR_RESOURCEFORK_NAME
);
1449 } else if ((user_size_t
)uio_resid(uio
) < sizeof(XATTR_RESOURCEFORK_NAME
)) {
1453 result
= uiomove(XATTR_RESOURCEFORK_NAME
,
1454 sizeof(XATTR_RESOURCEFORK_NAME
), uio
);
1461 * Standard HFS only supports native FinderInfo and Resource Forks.
1462 * Return at this point.
1464 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1468 /* Bail if we don't have any extended attributes. */
1469 if ((hfsmp
->hfs_attribute_vp
== NULL
) ||
1470 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) == 0) {
1474 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1476 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1477 if (iterator
== NULL
) {
1481 bzero(iterator
, sizeof(*iterator
));
1482 result
= hfs_buildattrkey(cp
->c_fileid
, NULL
, (HFSPlusAttrKey
*)&iterator
->key
);
1487 * Lock the user's buffer here so that we won't fault on
1488 * it in uiomove while holding the attributes b-tree lock.
1490 if (uio
&& uio_isuserspace(uio
)) {
1491 user_start
= uio_curriovbase(uio
);
1492 user_len
= uio_curriovlen(uio
);
1494 if ((result
= vslock(user_start
, user_len
)) != 0) {
1499 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
1501 result
= BTSearchRecord(btfile
, iterator
, NULL
, NULL
, NULL
);
1502 if (result
&& result
!= btNotFound
) {
1503 hfs_systemfile_unlock(hfsmp
, lockflags
);
1507 state
.fileID
= cp
->c_fileid
;
1512 state
.showcompressed
= !compressed
|| ap
->a_options
& XATTR_SHOWCOMPRESSION
;
1513 state
.ctx
= ap
->a_context
;
1515 #endif /* HFS_COMPRESSION */
1518 * Process entries starting just after iterator->key.
1520 result
= BTIterateRecords(btfile
, kBTreeNextRecord
, iterator
,
1521 (IterateCallBackProcPtr
)listattr_callback
, &state
);
1522 hfs_systemfile_unlock(hfsmp
, lockflags
);
1524 *ap
->a_size
+= state
.size
;
1527 if (state
.result
|| result
== btNotFound
)
1528 result
= state
.result
;
1532 vsunlock(user_start
, user_len
, TRUE
);
1534 FREE(iterator
, M_TEMP
);
1538 return MacToVFSError(result
);
1543 * Callback - called for each attribute
1546 listattr_callback(const HFSPlusAttrKey
*key
, __unused
const HFSPlusAttrData
*data
, struct listattr_callback_state
*state
)
1548 char attrname
[XATTR_MAXNAMELEN
+ 1];
1552 if (state
->fileID
!= key
->fileID
) {
1554 return (0); /* stop */
1557 * Skip over non-primary keys
1559 if (key
->startBlock
!= 0) {
1560 return (1); /* continue */
1563 /* Convert the attribute name into UTF-8. */
1564 result
= utf8_encodestr(key
->attrName
, key
->attrNameLen
* sizeof(UniChar
),
1565 (u_int8_t
*)attrname
, (size_t *)&bytecount
, sizeof(attrname
), '/', 0);
1567 state
->result
= result
;
1568 return (0); /* stop */
1570 bytecount
++; /* account for null termination char */
1572 if (xattr_protected(attrname
))
1573 return (1); /* continue */
1576 if (!state
->showcompressed
&& hfs_hides_xattr(state
->ctx
, VTOC(state
->vp
), attrname
, 1) ) /* 1 == don't take the cnode lock */
1577 return 1; /* continue */
1578 #endif /* HFS_COMPRESSION */
1580 if (state
->uio
== NULL
) {
1581 state
->size
+= bytecount
;
1583 if (bytecount
> uio_resid(state
->uio
)) {
1584 state
->result
= ERANGE
;
1585 return (0); /* stop */
1587 result
= uiomove((caddr_t
) attrname
, bytecount
, state
->uio
);
1589 state
->result
= result
;
1590 return (0); /* stop */
1593 return (1); /* continue */
1597 * Remove all the attributes from a cnode.
1599 * This function creates/ends its own transaction so that each
1600 * attribute is deleted in its own transaction (to avoid having
1601 * a transaction grow too large).
1603 * This function takes the necessary locks on the attribute
1604 * b-tree file and the allocation (bitmap) file.
1608 hfs_removeallattr(struct hfsmount
*hfsmp
, u_int32_t fileid
)
1610 BTreeIterator
*iterator
= NULL
;
1611 HFSPlusAttrKey
*key
;
1612 struct filefork
*btfile
;
1613 int result
, lockflags
;
1615 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1618 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1620 MALLOC(iterator
, BTreeIterator
*, sizeof(BTreeIterator
), M_TEMP
, M_WAITOK
);
1621 if (iterator
== NULL
) {
1624 bzero(iterator
, sizeof(BTreeIterator
));
1625 key
= (HFSPlusAttrKey
*)&iterator
->key
;
1627 /* Loop until there are no more attributes for this file id */
1629 if (hfs_start_transaction(hfsmp
) != 0) {
1634 /* Lock the attribute b-tree and the allocation (bitmap) files */
1635 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1638 * Go to first possible attribute key/record pair
1640 (void) hfs_buildattrkey(fileid
, NULL
, key
);
1641 result
= BTIterateRecord(btfile
, kBTreeNextRecord
, iterator
, NULL
, NULL
);
1642 if (result
|| key
->fileID
!= fileid
) {
1643 hfs_systemfile_unlock(hfsmp
, lockflags
);
1644 hfs_end_transaction(hfsmp
);
1647 result
= remove_attribute_records(hfsmp
, iterator
);
1649 #if HFS_XATTR_VERBOSE
1651 printf("hfs_removeallattr: unexpected err %d\n", result
);
1654 hfs_systemfile_unlock(hfsmp
, lockflags
);
1655 hfs_end_transaction(hfsmp
);
1660 FREE(iterator
, M_TEMP
);
1661 return (result
== btNotFound
? 0: MacToVFSError(result
));
1666 hfs_xattr_init(struct hfsmount
* hfsmp
)
1669 * If there isn't an attributes b-tree then create one.
1671 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
) &&
1672 (hfsmp
->hfs_attribute_vp
== NULL
) &&
1673 !(hfsmp
->hfs_flags
& HFS_READ_ONLY
)) {
1674 (void) hfs_create_attr_btree(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
,
1675 getnodecount(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
));
1677 if (hfsmp
->hfs_attribute_vp
)
1678 hfsmp
->hfs_max_inline_attrsize
= getmaxinlineattrsize(hfsmp
->hfs_attribute_vp
);
1682 * Enable/Disable volume attributes stored as EA for root file system.
1683 * Supported attributes are -
1685 * 2. Extent-based Extended Attributes
1689 hfs_set_volxattr(struct hfsmount
*hfsmp
, unsigned int xattrtype
, int state
)
1691 struct BTreeIterator
* iterator
= NULL
;
1692 struct filefork
*btfile
;
1696 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1701 * If there isn't an attributes b-tree then create one.
1703 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1704 result
= hfs_create_attr_btree(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
,
1705 getnodecount(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
));
1711 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1712 if (iterator
== NULL
) {
1715 bzero(iterator
, sizeof(*iterator
));
1718 * Build a b-tree key.
1719 * We use the root's parent id (1) to hold this volume attribute.
1721 if (xattrtype
== HFS_SETACLSTATE
) {
1723 (void) hfs_buildattrkey(kHFSRootParentID
, XATTR_EXTENDEDSECURITY_NAME
,
1724 (HFSPlusAttrKey
*)&iterator
->key
);
1725 } else if (xattrtype
== HFS_SET_XATTREXTENTS_STATE
) {
1726 /* Extent-based extended attributes */
1727 (void) hfs_buildattrkey(kHFSRootParentID
, XATTR_XATTREXTENTS_NAME
,
1728 (HFSPlusAttrKey
*)&iterator
->key
);
1734 /* Start a transaction for our changes. */
1735 if (hfs_start_transaction(hfsmp
) != 0) {
1739 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1741 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1744 /* Remove the attribute. */
1745 result
= BTDeleteRecord(btfile
, iterator
);
1746 if (result
== btNotFound
)
1749 FSBufferDescriptor btdata
;
1750 HFSPlusAttrData attrdata
;
1753 datasize
= sizeof(attrdata
);
1754 btdata
.bufferAddress
= &attrdata
;
1755 btdata
.itemSize
= datasize
;
1756 btdata
.itemCount
= 1;
1757 attrdata
.recordType
= kHFSPlusAttrInlineData
;
1758 attrdata
.reserved
[0] = 0;
1759 attrdata
.reserved
[1] = 0;
1760 attrdata
.attrSize
= 2;
1761 attrdata
.attrData
[0] = 0;
1762 attrdata
.attrData
[1] = 0;
1764 /* Insert the attribute. */
1765 result
= BTInsertRecord(btfile
, iterator
, &btdata
, datasize
);
1766 if (result
== btExists
)
1769 (void) BTFlushPath(btfile
);
1771 hfs_systemfile_unlock(hfsmp
, lockflags
);
1773 /* Finish the transaction of our changes. */
1774 hfs_end_transaction(hfsmp
);
1777 FREE(iterator
, M_TEMP
);
1780 if (xattrtype
== HFS_SETACLSTATE
) {
1782 vfs_clearextendedsecurity(HFSTOVFS(hfsmp
));
1784 vfs_setextendedsecurity(HFSTOVFS(hfsmp
));
1787 /* HFS_SET_XATTREXTENTS_STATE */
1788 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
1790 hfsmp
->hfs_flags
&= ~HFS_XATTR_EXTENTS
;
1792 hfsmp
->hfs_flags
|= HFS_XATTR_EXTENTS
;
1794 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
1798 return MacToVFSError(result
);
1803 * Check for volume attributes stored as EA for root file system.
1804 * Supported attributes are -
1806 * 2. Extent-based Extended Attributes
1810 hfs_check_volxattr(struct hfsmount
*hfsmp
, unsigned int xattrtype
)
1812 struct BTreeIterator
* iterator
;
1813 struct filefork
*btfile
;
1817 if (hfsmp
->hfs_flags
& HFS_STANDARD
||
1818 hfsmp
->hfs_attribute_vp
== NULL
) {
1822 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1823 if (iterator
== NULL
) {
1826 bzero(iterator
, sizeof(*iterator
));
1829 * Build a b-tree key.
1830 * We use the root's parent id (1) to hold this volume attribute.
1832 if (xattrtype
== HFS_SETACLSTATE
) {
1834 (void) hfs_buildattrkey(kHFSRootParentID
, XATTR_EXTENDEDSECURITY_NAME
,
1835 (HFSPlusAttrKey
*)&iterator
->key
);
1837 /* Extent-based extended attributes */
1838 (void) hfs_buildattrkey(kHFSRootParentID
, XATTR_XATTREXTENTS_NAME
,
1839 (HFSPlusAttrKey
*)&iterator
->key
);
1841 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1843 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1845 /* Check for our attribute. */
1846 result
= BTSearchRecord(btfile
, iterator
, NULL
, NULL
, NULL
);
1848 hfs_systemfile_unlock(hfsmp
, lockflags
);
1849 FREE(iterator
, M_TEMP
);
1852 if (xattrtype
== HFS_SETACLSTATE
) {
1853 vfs_setextendedsecurity(HFSTOVFS(hfsmp
));
1855 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
1856 hfsmp
->hfs_flags
|= HFS_XATTR_EXTENTS
;
1857 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
1864 * hfs_attrkeycompare - compare two attribute b-tree keys.
1866 * The name portion of the key is compared using a 16-bit binary comparison.
1867 * This is called from the b-tree code.
1871 hfs_attrkeycompare(HFSPlusAttrKey
*searchKey
, HFSPlusAttrKey
*trialKey
)
1873 u_int32_t searchFileID
, trialFileID
;
1876 searchFileID
= searchKey
->fileID
;
1877 trialFileID
= trialKey
->fileID
;
1880 if (searchFileID
> trialFileID
) {
1882 } else if (searchFileID
< trialFileID
) {
1885 u_int16_t
* str1
= &searchKey
->attrName
[0];
1886 u_int16_t
* str2
= &trialKey
->attrName
[0];
1887 int length1
= searchKey
->attrNameLen
;
1888 int length2
= trialKey
->attrNameLen
;
1892 if (length1
< length2
) {
1895 } else if (length1
> length2
) {
1918 * Names are equal; compare startBlock
1920 if (searchKey
->startBlock
== trialKey
->startBlock
)
1923 return (searchKey
->startBlock
< trialKey
->startBlock
? -1 : 1);
1931 * hfs_buildattrkey - build an Attribute b-tree key
1935 hfs_buildattrkey(u_int32_t fileID
, const char *attrname
, HFSPlusAttrKey
*key
)
1938 size_t unicodeBytes
= 0;
1940 if (attrname
!= NULL
) {
1942 * Convert filename from UTF-8 into Unicode
1944 result
= utf8_decodestr((const u_int8_t
*)attrname
, strlen(attrname
), key
->attrName
,
1945 &unicodeBytes
, sizeof(key
->attrName
), 0, 0);
1947 if (result
!= ENAMETOOLONG
)
1948 result
= EINVAL
; /* name has invalid characters */
1951 key
->attrNameLen
= unicodeBytes
/ sizeof(UniChar
);
1952 key
->keyLength
= kHFSPlusAttrKeyMinimumLength
+ unicodeBytes
;
1954 key
->attrNameLen
= 0;
1955 key
->keyLength
= kHFSPlusAttrKeyMinimumLength
;
1958 key
->fileID
= fileID
;
1959 key
->startBlock
= 0;
1965 * getnodecount - calculate starting node count for attributes b-tree.
1968 getnodecount(struct hfsmount
*hfsmp
, size_t nodesize
)
1970 u_int64_t freebytes
;
1971 u_int64_t calcbytes
;
1974 * 10.4: Scale base on current catalog file size (20 %) up to 20 MB.
1975 * 10.5: Attempt to be as big as the catalog clump size.
1977 * Use no more than 10 % of the remaining free space.
1979 freebytes
= (u_int64_t
)hfs_freeblks(hfsmp
, 0) * (u_int64_t
)hfsmp
->blockSize
;
1981 calcbytes
= MIN(hfsmp
->hfs_catalog_cp
->c_datafork
->ff_size
/ 5, 20 * 1024 * 1024);
1983 calcbytes
= MAX(calcbytes
, hfsmp
->hfs_catalog_cp
->c_datafork
->ff_clumpsize
);
1985 calcbytes
= MIN(calcbytes
, freebytes
/ 10);
1987 return (MAX(2, (int)(calcbytes
/ nodesize
)));
1992 * getmaxinlineattrsize - calculate maximum inline attribute size.
1994 * This yields 3,802 bytes for an 8K node size.
1997 getmaxinlineattrsize(struct vnode
* attrvp
)
1999 struct BTreeInfoRec btinfo
;
2000 size_t nodesize
= ATTRIBUTE_FILE_NODE_SIZE
;
2003 if (attrvp
!= NULL
) {
2004 (void) hfs_lock(VTOC(attrvp
), HFS_SHARED_LOCK
);
2005 if (BTGetInformation(VTOF(attrvp
), 0, &btinfo
) == 0)
2006 nodesize
= btinfo
.nodeSize
;
2007 hfs_unlock(VTOC(attrvp
));
2010 maxsize
-= sizeof(BTNodeDescriptor
); /* minus node descriptor */
2011 maxsize
-= 3 * sizeof(u_int16_t
); /* minus 3 index slots */
2012 maxsize
/= 2; /* 2 key/rec pairs minumum */
2013 maxsize
-= sizeof(HFSPlusAttrKey
); /* minus maximum key size */
2014 maxsize
-= sizeof(HFSPlusAttrData
) - 2; /* minus data header */
2015 maxsize
&= 0xFFFFFFFE; /* multiple of 2 bytes */
2021 * Get a referenced vnode for attribute data I/O.
2024 get_attr_data_vnode(struct hfsmount
*hfsmp
, vnode_t
*vpp
)
2029 vp
= hfsmp
->hfs_attrdata_vp
;
2031 struct cat_desc cat_desc
;
2032 struct cat_attr cat_attr
;
2033 struct cat_fork cat_fork
;
2035 /* We don't tag it as a system file since we intend to use cluster I/O. */
2036 bzero(&cat_desc
, sizeof(cat_desc
));
2037 cat_desc
.cd_parentcnid
= kHFSRootParentID
;
2038 cat_desc
.cd_nameptr
= (const u_int8_t
*)hfs_attrdatafilename
;
2039 cat_desc
.cd_namelen
= strlen(hfs_attrdatafilename
);
2040 cat_desc
.cd_cnid
= kHFSAttributeDataFileID
;
2042 bzero(&cat_attr
, sizeof(cat_attr
));
2043 cat_attr
.ca_linkcount
= 1;
2044 cat_attr
.ca_mode
= S_IFREG
;
2045 cat_attr
.ca_fileid
= cat_desc
.cd_cnid
;
2046 cat_attr
.ca_blocks
= hfsmp
->totalBlocks
;
2049 * The attribute data file is a virtual file that spans the
2050 * entire file system space.
2052 * Each extent-based attribute occupies a unique portion of
2053 * in this virtual file. The cluster I/O is done using actual
2054 * allocation block offsets so no additional mapping is needed
2055 * for the VNOP_BLOCKMAP call.
2057 * This approach allows the attribute data to be cached without
2058 * incurring the high cost of using a separate vnode per attribute.
2060 * Since we need to acquire the attribute b-tree file lock anyways,
2061 * the virtual file doesn't introduce any additional serialization.
2063 bzero(&cat_fork
, sizeof(cat_fork
));
2064 cat_fork
.cf_size
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
2065 cat_fork
.cf_blocks
= hfsmp
->totalBlocks
;
2066 cat_fork
.cf_extents
[0].startBlock
= 0;
2067 cat_fork
.cf_extents
[0].blockCount
= cat_fork
.cf_blocks
;
2069 result
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cat_desc
, 0, &cat_attr
, &cat_fork
, &vp
);
2071 HFS_MOUNT_LOCK(hfsmp
, 1);
2072 /* Check if someone raced us for creating this vnode. */
2073 if (hfsmp
->hfs_attrdata_vp
!= NULLVP
) {
2074 HFS_MOUNT_UNLOCK(hfsmp
, 1);
2077 vp
= hfsmp
->hfs_attrdata_vp
;
2079 hfsmp
->hfs_attrdata_vp
= vp
;
2080 HFS_MOUNT_UNLOCK(hfsmp
, 1);
2081 /* Keep a reference on this vnode until unmount */
2082 vnode_ref_ext(vp
, O_EVTONLY
);
2083 hfs_unlock(VTOC(vp
));
2087 if ((result
= vnode_get(vp
)))
2095 * Read an extent based attribute.
2098 read_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
)
2100 vnode_t evp
= NULLVP
;
2108 if ((result
= get_attr_data_vnode(hfsmp
, &evp
))) {
2111 hfs_lock_truncate(VTOC(evp
), 0);
2113 bufsize
= (int)uio_resid(uio
);
2114 attrsize
= (int)datasize
;
2115 blksize
= (int)hfsmp
->blockSize
;
2118 * Read the attribute data one extent at a time.
2119 * For the typical case there is only one extent.
2121 for (i
= 0; (attrsize
> 0) && (bufsize
> 0) && (extents
[i
].startBlock
!= 0); ++i
) {
2122 iosize
= (int)extents
[i
].blockCount
* blksize
;
2123 iosize
= MIN(iosize
, attrsize
);
2124 iosize
= MIN(iosize
, bufsize
);
2125 uio_setresid(uio
, iosize
);
2126 uio_setoffset(uio
, (u_int64_t
)extents
[i
].startBlock
* (u_int64_t
)blksize
);
2128 result
= cluster_read(evp
, uio
, VTOF(evp
)->ff_size
, IO_SYNC
| IO_UNIT
);
2130 #if HFS_XATTR_VERBOSE
2131 printf("hfs: read_attr_data: cr iosize %d [%d, %d] (%d)\n",
2132 iosize
, extents
[i
].startBlock
, extents
[i
].blockCount
, result
);
2139 uio_setresid(uio
, bufsize
);
2140 uio_setoffset(uio
, datasize
);
2142 hfs_unlock_truncate(VTOC(evp
), 0);
2148 * Write an extent based attribute.
2151 write_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
)
2153 vnode_t evp
= NULLVP
;
2162 /* Get exclusive use of attribute data vnode. */
2163 if ((result
= get_attr_data_vnode(hfsmp
, &evp
))) {
2166 hfs_lock_truncate(VTOC(evp
), 0);
2168 bufsize
= uio_resid(uio
);
2169 attrsize
= (int) datasize
;
2170 blksize
= (int) hfsmp
->blockSize
;
2171 filesize
= VTOF(evp
)->ff_size
;
2174 * Write the attribute data one extent at a time.
2176 for (i
= 0; (attrsize
> 0) && (bufsize
> 0) && (extents
[i
].startBlock
!= 0); ++i
) {
2177 iosize
= (int)extents
[i
].blockCount
* blksize
;
2178 iosize
= MIN(iosize
, attrsize
);
2179 iosize
= MIN(iosize
, bufsize
);
2180 uio_setresid(uio
, iosize
);
2181 uio_setoffset(uio
, (u_int64_t
)extents
[i
].startBlock
* (u_int64_t
)blksize
);
2183 result
= cluster_write(evp
, uio
, filesize
, filesize
, filesize
,
2184 (off_t
) 0, IO_SYNC
| IO_UNIT
);
2185 #if HFS_XATTR_VERBOSE
2186 printf("hfs: write_attr_data: cw iosize %d [%d, %d] (%d)\n",
2187 iosize
, extents
[i
].startBlock
, extents
[i
].blockCount
, result
);
2194 uio_setresid(uio
, bufsize
);
2195 uio_setoffset(uio
, datasize
);
2197 hfs_unlock_truncate(VTOC(evp
), 0);
2203 * Allocate blocks for an extent based attribute.
2206 alloc_attr_blks(struct hfsmount
*hfsmp
, size_t attrsize
, size_t extentbufsize
, HFSPlusExtentDescriptor
*extents
, int *blocks
)
2215 startblk
= hfsmp
->hfs_metazone_end
;
2216 blkcnt
= howmany(attrsize
, hfsmp
->blockSize
);
2217 if (blkcnt
> (int)hfs_freeblks(hfsmp
, 0)) {
2221 maxextents
= extentbufsize
/ sizeof(HFSPlusExtentDescriptor
);
2223 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2225 for (i
= 0; (blkcnt
> 0) && (i
< maxextents
); i
++) {
2226 result
= BlockAllocate(hfsmp
, startblk
, blkcnt
, blkcnt
, 0, 0,
2227 &extents
[i
].startBlock
, &extents
[i
].blockCount
);
2228 #if HFS_XATTR_VERBOSE
2229 printf("hfs: alloc_attr_blks: BA blkcnt %d [%d, %d] (%d)\n",
2230 blkcnt
, extents
[i
].startBlock
, extents
[i
].blockCount
, result
);
2233 extents
[i
].startBlock
= 0;
2234 extents
[i
].blockCount
= 0;
2237 blkcnt
-= extents
[i
].blockCount
;
2238 startblk
= extents
[i
].startBlock
+ extents
[i
].blockCount
;
2241 * If it didn't fit in the extents buffer then bail.
2246 #if HFS_XATTR_VERBOSE
2247 printf("hfs: alloc_attr_blks: unexpected failure, %d blocks unallocated\n", blkcnt
);
2249 for (; i
<= 0; i
--) {
2250 if ((blkcnt
= extents
[i
].blockCount
) != 0) {
2251 (void) BlockDeallocate(hfsmp
, extents
[i
].startBlock
, blkcnt
);
2252 extents
[i
].startBlock
= 0;
2253 extents
[i
].blockCount
= 0;
2258 hfs_systemfile_unlock(hfsmp
, lockflags
);
2259 return MacToVFSError(result
);
2263 * Release blocks from an extent based attribute.
2266 free_attr_blks(struct hfsmount
*hfsmp
, int blkcnt
, HFSPlusExtentDescriptor
*extents
)
2268 vnode_t evp
= NULLVP
;
2269 int remblks
= blkcnt
;
2273 if (get_attr_data_vnode(hfsmp
, &evp
) != 0) {
2276 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2278 for (i
= 0; (remblks
> 0) && (extents
[i
].blockCount
!= 0); i
++) {
2279 if (extents
[i
].blockCount
> (u_int32_t
)blkcnt
) {
2280 #if HFS_XATTR_VERBOSE
2281 printf("hfs: free_attr_blks: skipping bad extent [%d, %d]\n",
2282 extents
[i
].startBlock
, extents
[i
].blockCount
);
2284 extents
[i
].blockCount
= 0;
2287 if (extents
[i
].startBlock
== 0) {
2290 (void)BlockDeallocate(hfsmp
, extents
[i
].startBlock
, extents
[i
].blockCount
);
2291 extents
[i
].startBlock
= 0;
2292 extents
[i
].blockCount
= 0;
2293 remblks
-= extents
[i
].blockCount
;
2295 #if HFS_XATTR_VERBOSE
2296 printf("hfs: free_attr_blks: BlockDeallocate [%d, %d]\n",
2297 extents
[i
].startBlock
, extents
[i
].blockCount
);
2299 /* Discard any resident pages for this block range. */
2303 start
= (u_int64_t
)extents
[i
].startBlock
* (u_int64_t
)hfsmp
->blockSize
;
2304 end
= start
+ (u_int64_t
)extents
[i
].blockCount
* (u_int64_t
)hfsmp
->blockSize
;
2305 (void) ubc_msync(hfsmp
->hfs_attrdata_vp
, start
, end
, &start
, UBC_INVALIDATE
);
2309 hfs_systemfile_unlock(hfsmp
, lockflags
);
2316 has_overflow_extents(HFSPlusForkData
*forkdata
)
2320 if (forkdata
->extents
[7].blockCount
== 0)
2323 blocks
= forkdata
->extents
[0].blockCount
+
2324 forkdata
->extents
[1].blockCount
+
2325 forkdata
->extents
[2].blockCount
+
2326 forkdata
->extents
[3].blockCount
+
2327 forkdata
->extents
[4].blockCount
+
2328 forkdata
->extents
[5].blockCount
+
2329 forkdata
->extents
[6].blockCount
+
2330 forkdata
->extents
[7].blockCount
;
2332 return (forkdata
->totalBlocks
> blocks
);
2336 count_extent_blocks(int maxblks
, HFSPlusExtentRecord extents
)
2341 for (i
= 0, blocks
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
2342 /* Ignore obvious bogus extents. */
2343 if (extents
[i
].blockCount
> (u_int32_t
)maxblks
)
2345 if (extents
[i
].startBlock
== 0 || extents
[i
].blockCount
== 0)
2347 blocks
+= extents
[i
].blockCount
;