2 * Copyright (c) 2004-2009 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/fsctl.h>
39 #include <sys/vnode_internal.h>
40 #include <sys/kauth.h>
43 #include "hfs_cnode.h"
44 #include "hfs_mount.h"
45 #include "hfs_format.h"
46 #include "hfs_endian.h"
47 #include "hfs_btreeio.h"
48 #include "hfs_fsctl.h"
50 #include "hfscommon/headers/BTreesInternal.h"
52 #define HFS_XATTR_VERBOSE 0
54 #define ATTRIBUTE_FILE_NODE_SIZE 8192
57 /* State information for the listattr_callback callback function. */
58 struct listattr_callback_state
{
67 #endif /* HFS_COMPRESSION */
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 static int hfs_zero_dateadded (struct cnode
*cp
, u_int8_t
*finderinfo
);
85 const char hfs_attrdatafilename
[] = "Attribute Data";
87 static int listattr_callback(const HFSPlusAttrKey
*key
, const HFSPlusAttrData
*data
,
88 struct listattr_callback_state
*state
);
90 static int remove_attribute_records(struct hfsmount
*hfsmp
, BTreeIterator
* iterator
);
92 static int getnodecount(struct hfsmount
*hfsmp
, size_t nodesize
);
94 static size_t getmaxinlineattrsize(struct vnode
* attrvp
);
96 static int read_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
);
98 static int write_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
);
100 static int alloc_attr_blks(struct hfsmount
*hfsmp
, size_t attrsize
, size_t extentbufsize
, HFSPlusExtentDescriptor
*extents
, int *blocks
);
102 static void free_attr_blks(struct hfsmount
*hfsmp
, int blkcnt
, HFSPlusExtentDescriptor
*extents
);
104 static int has_overflow_extents(HFSPlusForkData
*forkdata
);
106 static int count_extent_blocks(int maxblks
, HFSPlusExtentRecord extents
);
110 * Obtain the vnode for a stream.
113 hfs_vnop_getnamedstream(struct vnop_getnamedstream_args
* ap
)
115 vnode_t vp
= ap
->a_vp
;
116 vnode_t
*svpp
= ap
->a_svpp
;
123 * We only support the "com.apple.ResourceFork" stream.
125 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) != 0) {
129 if ( !S_ISREG(cp
->c_mode
) ) {
133 int hide_rsrc
= hfs_hides_rsrc(ap
->a_context
, VTOC(vp
), 1);
134 #endif /* HFS_COMPRESSION */
135 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
138 if ((!RSRC_FORK_EXISTS(cp
)
141 #endif /* HFS_COMPRESSION */
142 ) && (ap
->a_operation
!= NS_OPEN
)) {
146 error
= hfs_vgetrsrc(VTOHFS(vp
), vp
, svpp
, TRUE
, FALSE
);
156 hfs_vnop_makenamedstream(struct vnop_makenamedstream_args
* ap
)
158 vnode_t vp
= ap
->a_vp
;
159 vnode_t
*svpp
= ap
->a_svpp
;
166 * We only support the "com.apple.ResourceFork" stream.
168 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) != 0) {
172 if ( !S_ISREG(cp
->c_mode
) ) {
176 if (hfs_hides_rsrc(ap
->a_context
, VTOC(vp
), 1)) {
177 if (VNODE_IS_RSRC(vp
)) {
180 error
= decmpfs_decompress_file(vp
, VTOCMP(vp
), -1, 1, 0);
185 #endif /* HFS_COMPRESSION */
186 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
189 error
= hfs_vgetrsrc(VTOHFS(vp
), vp
, svpp
, TRUE
, FALSE
);
199 hfs_vnop_removenamedstream(struct vnop_removenamedstream_args
* ap
)
201 vnode_t svp
= ap
->a_svp
;
206 * We only support the "com.apple.ResourceFork" stream.
208 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) != 0) {
212 if (hfs_hides_rsrc(ap
->a_context
, VTOC(svp
), 1)) {
216 #endif /* HFS_COMPRESSION */
220 /* Take truncate lock before taking cnode lock. */
221 hfs_lock_truncate(scp
, HFS_EXCLUSIVE_LOCK
);
222 if ((error
= hfs_lock(scp
, HFS_EXCLUSIVE_LOCK
))) {
225 if (VTOF(svp
)->ff_size
!= 0) {
226 error
= hfs_truncate(svp
, 0, IO_NDELAY
, 0, 0, ap
->a_context
);
230 hfs_unlock_truncate(scp
, 0);
235 /* Zero out the date added field for the specified cnode */
236 static int hfs_zero_dateadded (struct cnode
*cp
, u_int8_t
*finderinfo
) {
237 u_int8_t
*finfo
= finderinfo
;
239 /* Advance finfo by 16 bytes to the 2nd half of the finderinfo */
242 if (S_ISREG(cp
->c_attr
.ca_mode
)) {
243 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
244 extinfo
->date_added
= 0;
246 else if (S_ISDIR(cp
->c_attr
.ca_mode
)) {
247 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
248 extinfo
->date_added
= 0;
251 /* Return an error */
260 * Retrieve the data of an extended attribute.
263 hfs_vnop_getxattr(struct vnop_getxattr_args
*ap
)
265 struct vnop_getxattr_args {
266 struct vnodeop_desc *a_desc;
272 vfs_context_t a_context;
276 struct vnode
*vp
= ap
->a_vp
;
278 struct hfsmount
*hfsmp
;
279 uio_t uio
= ap
->a_uio
;
284 if (vp
== cp
->c_vp
) {
286 int decmpfs_hide
= hfs_hides_xattr(ap
->a_context
, VTOC(vp
), ap
->a_name
, 1); /* 1 == don't take the cnode lock */
287 if (decmpfs_hide
&& !(ap
->a_options
& XATTR_SHOWCOMPRESSION
))
289 #endif /* HFS_COMPRESSION */
291 /* Get the Finder Info. */
292 if (bcmp(ap
->a_name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0) {
293 u_int8_t finderinfo
[32];
296 if ((result
= hfs_lock(cp
, HFS_SHARED_LOCK
))) {
299 /* Make a copy since we may not export all of it. */
300 bcopy(cp
->c_finderinfo
, finderinfo
, sizeof(finderinfo
));
303 /* Zero out the date added field in the local copy */
304 hfs_zero_dateadded (cp
, finderinfo
);
306 /* Don't expose a symlink's private type/creator. */
307 if (vnode_islnk(vp
)) {
308 struct FndrFileInfo
*fip
;
310 fip
= (struct FndrFileInfo
*)&finderinfo
;
314 /* If Finder Info is empty then it doesn't exist. */
315 if (bcmp(finderinfo
, emptyfinfo
, sizeof(emptyfinfo
)) == 0) {
319 *ap
->a_size
= bufsize
;
322 if ((user_size_t
)uio_resid(uio
) < bufsize
)
325 result
= uiomove((caddr_t
)&finderinfo
, bufsize
, uio
);
329 /* Read the Resource Fork. */
330 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0) {
331 struct vnode
*rvp
= NULL
;
332 int openunlinked
= 0;
335 if ( !S_ISREG(cp
->c_mode
) ) {
338 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
341 namelen
= cp
->c_desc
.cd_namelen
;
343 if ( !RSRC_FORK_EXISTS(cp
)) {
348 if ((cp
->c_flag
& C_DELETED
) && (namelen
== 0)) {
352 result
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, FALSE
);
358 *ap
->a_size
= (size_t)VTOF(rvp
)->ff_size
;
361 user_ssize_t uio_size
= 0;
363 uio_size
= uio_resid(uio
);
364 #endif /* HFS_COMPRESSION */
365 result
= VNOP_READ(rvp
, uio
, 0, ap
->a_context
);
369 (uio_resid(uio
) == uio_size
)) {
371 * We intentionally make the above call to VNOP_READ so that
372 * it can return an authorization/permission/etc. Error
373 * based on ap->a_context and thus deny this operation;
374 * in that case, result != 0 and we won't proceed.
376 * However, if result == 0, it will have returned no data
377 * because hfs_vnop_read hid the resource fork
378 * (hence uio_resid(uio) == uio_size, i.e. the uio is untouched)
380 * In that case, we try again with the decmpfs_ctx context
381 * to get the actual data
383 result
= VNOP_READ(rvp
, uio
, 0, decmpfs_ctx
);
385 #endif /* HFS_COMPRESSION */
387 /* force the rsrc fork vnode to recycle right away */
390 vref
= vnode_ref (rvp
);
402 * Standard HFS only supports native FinderInfo and Resource Forks.
404 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
408 if ((result
= hfs_lock(cp
, HFS_SHARED_LOCK
))) {
412 /* Check for non-rsrc, non-finderinfo EAs */
413 result
= hfs_getxattr_internal (cp
, ap
, VTOHFS(cp
->c_vp
), 0);
417 return MacToVFSError(result
);
425 * We break out this internal function which searches the attributes B-Tree and the
426 * overflow extents file to find non-resource, non-finderinfo EAs. There may be cases
427 * where we need to get EAs in contexts where we are already holding the cnode lock,
428 * and to re-enter hfs_vnop_getxattr would cause us to double-lock the cnode. Instead,
429 * we can just directly call this function.
431 * We pass the hfsmp argument directly here because we may not necessarily have a cnode to
432 * operate on. Under normal conditions, we have a file or directory to query, but if we
433 * are operating on the root directory (id 1), then we may not have a cnode. In this case, if hte
434 * 'cp' argument is NULL, then we need to use the 'fileid' argument as the entry to manipulate
436 * NOTE: This function assumes the cnode lock for 'cp' is held exclusive or shared.
440 int hfs_getxattr_internal (struct cnode
*cp
, struct vnop_getxattr_args
*ap
,
441 struct hfsmount
*hfsmp
, u_int32_t fileid
) {
443 struct filefork
*btfile
;
444 struct BTreeIterator
* iterator
= NULL
;
446 HFSPlusAttrRecord
*recp
= NULL
;
447 FSBufferDescriptor btdata
;
450 u_int16_t datasize
= 0;
451 uio_t uio
= ap
->a_uio
;
452 u_int32_t target_id
= 0;
455 target_id
= cp
->c_fileid
;
462 /* Bail if we don't have an EA B-Tree. */
463 if ((hfsmp
->hfs_attribute_vp
== NULL
) ||
464 ((cp
) && (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) == 0)) {
469 /* Initialize the B-Tree iterator for searching for the proper EA */
470 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
472 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
473 if (iterator
== NULL
) {
477 bzero(iterator
, sizeof(*iterator
));
479 bufsize
= sizeof(HFSPlusAttrData
) - 2;
481 bufsize
+= uio_resid(uio
);
483 bufsize
= MAX(bufsize
, sizeof(HFSPlusAttrRecord
));
484 MALLOC(recp
, HFSPlusAttrRecord
*, bufsize
, M_TEMP
, M_WAITOK
);
489 btdata
.bufferAddress
= recp
;
490 btdata
.itemSize
= bufsize
;
491 btdata
.itemCount
= 1;
493 result
= hfs_buildattrkey(target_id
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
498 /* Lookup the attribute in the Attribute B-Tree */
499 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
500 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
501 hfs_systemfile_unlock(hfsmp
, lockflags
);
504 if (result
== btNotFound
) {
511 * Operate differently if we have inline EAs that can fit in the attribute B-Tree or if
512 * we have extent based EAs.
514 switch (recp
->recordType
) {
515 /* Attribute fits in the Attribute B-Tree */
516 case kHFSPlusAttrInlineData
:
518 * Sanity check record size. It's not required to have any
519 * user data, so the minimum size is 2 bytes less that the
520 * size of HFSPlusAttrData (since HFSPlusAttrData struct
521 * has 2 bytes set aside for attribute data).
523 if (datasize
< (sizeof(HFSPlusAttrData
) - 2)) {
524 printf("hfs_getxattr: %d,%s invalid record size %d (expecting %lu)\n",
525 target_id
, ap
->a_name
, datasize
, sizeof(HFSPlusAttrData
));
529 *ap
->a_size
= recp
->attrData
.attrSize
;
530 if (uio
&& recp
->attrData
.attrSize
!= 0) {
531 if (*ap
->a_size
> (user_size_t
)uio_resid(uio
)) {
535 result
= uiomove((caddr_t
) &recp
->attrData
.attrData
, recp
->attrData
.attrSize
, uio
);
539 /* Extent-Based EAs */
540 case kHFSPlusAttrForkData
: {
541 if (datasize
< sizeof(HFSPlusAttrForkData
)) {
542 printf("hfs_getxattr: %d,%s invalid record size %d (expecting %lu)\n",
543 target_id
, ap
->a_name
, datasize
, sizeof(HFSPlusAttrForkData
));
547 *ap
->a_size
= recp
->forkData
.theFork
.logicalSize
;
551 if (*ap
->a_size
> (user_size_t
)uio_resid(uio
)) {
555 /* Process overflow extents if necessary. */
556 if (has_overflow_extents(&recp
->forkData
.theFork
)) {
557 HFSPlusExtentDescriptor
*extentbuf
;
558 HFSPlusExtentDescriptor
*extentptr
;
559 size_t extentbufsize
;
560 u_int32_t totalblocks
;
564 totalblocks
= recp
->forkData
.theFork
.totalBlocks
;
565 /* Ignore bogus block counts. */
566 if (totalblocks
> HFS_MAXATTRBLKS
) {
570 attrlen
= recp
->forkData
.theFork
.logicalSize
;
572 /* Get a buffer to hold the worst case amount of extents. */
573 extentbufsize
= totalblocks
* sizeof(HFSPlusExtentDescriptor
);
574 extentbufsize
= roundup(extentbufsize
, sizeof(HFSPlusExtentRecord
));
575 MALLOC(extentbuf
, HFSPlusExtentDescriptor
*, extentbufsize
, M_TEMP
, M_WAITOK
);
576 if (extentbuf
== NULL
) {
580 bzero(extentbuf
, extentbufsize
);
581 extentptr
= extentbuf
;
583 /* Grab the first 8 extents. */
584 bcopy(&recp
->forkData
.theFork
.extents
[0], extentptr
, sizeof(HFSPlusExtentRecord
));
585 extentptr
+= kHFSPlusExtentDensity
;
586 blkcnt
= count_extent_blocks(totalblocks
, recp
->forkData
.theFork
.extents
);
588 /* Now lookup the overflow extents. */
589 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
590 while (blkcnt
< totalblocks
) {
591 ((HFSPlusAttrKey
*)&iterator
->key
)->startBlock
= blkcnt
;
592 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
594 (recp
->recordType
!= kHFSPlusAttrExtents
) ||
595 (datasize
< sizeof(HFSPlusAttrExtents
))) {
596 printf("hfs_getxattr: %s missing extents, only %d blks of %d found\n",
597 ap
->a_name
, blkcnt
, totalblocks
);
599 break; /* break from while */
601 /* Grab the next 8 extents. */
602 bcopy(&recp
->overflowExtents
.extents
[0], extentptr
, sizeof(HFSPlusExtentRecord
));
603 extentptr
+= kHFSPlusExtentDensity
;
604 blkcnt
+= count_extent_blocks(totalblocks
, recp
->overflowExtents
.extents
);
607 /* Release Attr B-Tree lock */
608 hfs_systemfile_unlock(hfsmp
, lockflags
);
610 if (blkcnt
< totalblocks
) {
614 result
= read_attr_data(hfsmp
, uio
, attrlen
, extentbuf
);
616 FREE(extentbuf
, M_TEMP
);
619 else /* No overflow extents. */ {
620 result
= read_attr_data(hfsmp
, uio
, recp
->forkData
.theFork
.logicalSize
, recp
->forkData
.theFork
.extents
);
626 /* We only support Extent or inline EAs. Default to ENOATTR for anything else */
633 FREE(iterator
, M_TEMP
);
645 * Set the data of an extended attribute.
648 hfs_vnop_setxattr(struct vnop_setxattr_args
*ap
)
650 struct vnop_setxattr_args {
651 struct vnodeop_desc *a_desc;
656 vfs_context_t a_context;
660 struct vnode
*vp
= ap
->a_vp
;
661 struct cnode
*cp
= NULL
;
662 struct hfsmount
*hfsmp
;
663 uio_t uio
= ap
->a_uio
;
665 void * user_data_ptr
= NULL
;
667 time_t orig_ctime
=VTOC(vp
)->c_ctime
;
669 if (ap
->a_name
== NULL
|| ap
->a_name
[0] == '\0') {
670 return (EINVAL
); /* invalid name */
673 if (VNODE_IS_RSRC(vp
)) {
678 if (hfs_hides_xattr(ap
->a_context
, VTOC(vp
), ap
->a_name
, 1) ) { /* 1 == don't take the cnode lock */
679 result
= decmpfs_decompress_file(vp
, VTOCMP(vp
), -1, 1, 0);
684 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_METADATA_WRITE_OP
, NULL
);
685 #endif /* HFS_COMPRESSION */
687 /* Set the Finder Info. */
688 if (bcmp(ap
->a_name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0) {
689 u_int8_t finderinfo
[32];
690 struct FndrFileInfo
*fip
;
691 void * finderinfo_start
;
692 u_int8_t
*finfo
= NULL
;
694 u_int32_t dateadded
= 0;
696 attrsize
= sizeof(VTOC(vp
)->c_finderinfo
);
698 if ((user_size_t
)uio_resid(uio
) != attrsize
) {
701 /* Grab the new Finder Info data. */
702 if ((result
= uiomove((caddr_t
)&finderinfo
, attrsize
, uio
))) {
705 fip
= (struct FndrFileInfo
*)&finderinfo
;
707 if ((result
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
712 /* Symlink's don't have an external type/creator. */
713 if (vnode_islnk(vp
)) {
714 /* Skip over type/creator fields. */
715 finderinfo_start
= &cp
->c_finderinfo
[8];
718 finderinfo_start
= &cp
->c_finderinfo
[0];
720 * Don't allow the external setting of
721 * file type to kHardLinkFileType.
723 if (fip
->fdType
== SWAP_BE32(kHardLinkFileType
)) {
729 /* Grab the current date added from the cnode */
730 dateadded
= hfs_get_dateadded (cp
);
732 /* Zero out the date added field to ignore user's attempts to set it */
733 hfs_zero_dateadded(cp
, finderinfo
);
735 if (bcmp(finderinfo_start
, emptyfinfo
, attrsize
)) {
736 /* attr exists and "create" was specified. */
737 if (ap
->a_options
& XATTR_CREATE
) {
742 /* attr doesn't exists and "replace" was specified. */
743 if (ap
->a_options
& XATTR_REPLACE
) {
750 * Now restore the date added to the finderinfo to be written out.
751 * Advance to the 2nd half of the finderinfo to write out the date added
754 * Make sure to endian swap the date added back into big endian. When we used
755 * hfs_get_dateadded above to retrieve it, it swapped into local endianness
756 * for us. But now that we're writing it out, put it back into big endian.
758 finfo
= &finderinfo
[16];
760 if (S_ISREG(cp
->c_attr
.ca_mode
)) {
761 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
762 extinfo
->date_added
= OSSwapHostToBigInt32(dateadded
);
764 else if (S_ISDIR(cp
->c_attr
.ca_mode
)) {
765 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
766 extinfo
->date_added
= OSSwapHostToBigInt32(dateadded
);
769 /* Set the cnode's Finder Info. */
770 if (attrsize
== sizeof(cp
->c_finderinfo
))
771 bcopy(&finderinfo
[0], finderinfo_start
, attrsize
);
773 bcopy(&finderinfo
[8], finderinfo_start
, attrsize
);
775 /* Updating finderInfo updates change time and modified time */
776 cp
->c_touch_chgtime
= TRUE
;
777 cp
->c_flag
|= C_MODIFIED
;
780 * Mirror the invisible bit to the UF_HIDDEN flag.
782 * The fdFlags for files and frFlags for folders are both 8 bytes
783 * into the userInfo (the first 16 bytes of the Finder Info). They
784 * are both 16-bit fields.
786 fdFlags
= *((u_int16_t
*) &cp
->c_finderinfo
[8]);
787 if (fdFlags
& OSSwapHostToBigConstInt16(kFinderInvisibleMask
))
788 cp
->c_flags
|= UF_HIDDEN
;
790 cp
->c_flags
&= ~UF_HIDDEN
;
792 result
= hfs_update(vp
, FALSE
);
797 /* Write the Resource Fork. */
798 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0) {
799 struct vnode
*rvp
= NULL
;
801 int openunlinked
= 0;
803 if (!vnode_isreg(vp
)) {
806 if ((result
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
810 namelen
= cp
->c_desc
.cd_namelen
;
812 if (RSRC_FORK_EXISTS(cp
)) {
813 /* attr exists and "create" was specified. */
814 if (ap
->a_options
& XATTR_CREATE
) {
819 /* attr doesn't exists and "replace" was specified. */
820 if (ap
->a_options
& XATTR_REPLACE
) {
827 * Note that we could be called on to grab the rsrc fork vnode
828 * for a file that has become open-unlinked.
830 if ((cp
->c_flag
& C_DELETED
) && (namelen
== 0)) {
834 result
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, FALSE
);
839 /* VNOP_WRITE marks cnode as needing a modtime update */
840 result
= VNOP_WRITE(rvp
, uio
, 0, ap
->a_context
);
842 /* if open unlinked, force it inactive */
845 vref
= vnode_ref (rvp
);
852 /* cnode is not open-unlinked, so re-lock cnode to sync */
853 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
859 /* hfs fsync rsrc fork to force to disk and update modtime */
860 result
= hfs_fsync (rvp
, MNT_NOWAIT
, 0, vfs_context_proc (ap
->a_context
));
868 * Standard HFS only supports native FinderInfo and Resource Forks.
870 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
873 attrsize
= uio_resid(uio
);
875 /* Enforce an upper limit. */
876 if (attrsize
> HFS_XATTR_MAXSIZE
) {
882 * Attempt to copy the users attr data before taking any locks.
885 hfsmp
->hfs_max_inline_attrsize
!= 0 &&
886 attrsize
< hfsmp
->hfs_max_inline_attrsize
) {
887 MALLOC(user_data_ptr
, void *, attrsize
, M_TEMP
, M_WAITOK
);
888 if (user_data_ptr
== NULL
) {
893 result
= uiomove((caddr_t
)user_data_ptr
, attrsize
, uio
);
899 result
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
906 * If we're trying to set a non-finderinfo, non-resourcefork EA, then
907 * call the breakout function.
909 result
= hfs_setxattr_internal (cp
, user_data_ptr
, attrsize
, ap
, VTOHFS(vp
), 0);
916 FREE(user_data_ptr
, M_TEMP
);
919 return (result
== btNotFound
? ENOATTR
: MacToVFSError(result
));
924 * hfs_setxattr_internal
926 * Internal function to set non-rsrc, non-finderinfo EAs to either the attribute B-Tree or
929 * See comments from hfs_getxattr_internal on why we need to pass 'hfsmp' and fileid here.
930 * The gist is that we could end up writing to the root folder which may not have a cnode.
933 * 1. cnode 'cp' is locked EXCLUSIVE before calling this function.
934 * 2. data_ptr contains data to be written. If gathering data from userland, this must be
935 * done before calling this function.
936 * 3. If data originates entirely in-kernel, use a null UIO, and ensure the size is less than
937 * hfsmp->hfs_max_inline_attrsize bytes long.
939 int hfs_setxattr_internal (struct cnode
*cp
, caddr_t data_ptr
, size_t attrsize
,
940 struct vnop_setxattr_args
*ap
, struct hfsmount
*hfsmp
,
942 uio_t uio
= ap
->a_uio
;
943 struct vnode
*vp
= ap
->a_vp
;
944 int started_transaction
= 0;
945 struct BTreeIterator
* iterator
= NULL
;
946 struct filefork
*btfile
= NULL
;
947 FSBufferDescriptor btdata
;
948 HFSPlusAttrRecord attrdata
; /* 90 bytes */
949 HFSPlusAttrRecord
*recp
= NULL
;
950 HFSPlusExtentDescriptor
*extentptr
= NULL
;
954 int allocatedblks
= 0;
958 target_id
= cp
->c_fileid
;
964 /* Start a transaction for our changes. */
965 if (hfs_start_transaction(hfsmp
) != 0) {
969 started_transaction
= 1;
972 * Once we started the transaction, nobody can compete
973 * with us, so make sure this file is still there.
975 if ((cp
) && (cp
->c_flag
& C_NOEXISTS
)) {
981 * If there isn't an attributes b-tree then create one.
983 if (hfsmp
->hfs_attribute_vp
== NULL
) {
984 result
= hfs_create_attr_btree(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
,
985 getnodecount(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
));
990 if (hfsmp
->hfs_max_inline_attrsize
== 0) {
991 hfsmp
->hfs_max_inline_attrsize
= getmaxinlineattrsize(hfsmp
->hfs_attribute_vp
);
994 /* Take exclusive access to the attributes b-tree. */
995 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
997 /* Build the b-tree key. */
998 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
999 if (iterator
== NULL
) {
1003 bzero(iterator
, sizeof(*iterator
));
1004 result
= hfs_buildattrkey(target_id
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
1009 /* Preflight for replace/create semantics. */
1010 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1011 btdata
.bufferAddress
= &attrdata
;
1012 btdata
.itemSize
= sizeof(attrdata
);
1013 btdata
.itemCount
= 1;
1014 exists
= BTSearchRecord(btfile
, iterator
, &btdata
, NULL
, NULL
) == 0;
1016 /* Replace requires that the attribute already exists. */
1017 if ((ap
->a_options
& XATTR_REPLACE
) && !exists
) {
1021 /* Create requires that the attribute doesn't exist. */
1022 if ((ap
->a_options
& XATTR_CREATE
) && exists
) {
1027 /* If it won't fit inline then use extent-based attributes. */
1028 if (attrsize
> hfsmp
->hfs_max_inline_attrsize
) {
1029 size_t extentbufsize
;
1032 u_int32_t
*keystartblk
;
1037 * setxattrs originating from in-kernel are not supported if they are bigger
1038 * than the inline max size. Just return ENOATTR and force them to do it with a
1045 /* Get some blocks. */
1046 blkcnt
= howmany(attrsize
, hfsmp
->blockSize
);
1047 extentbufsize
= blkcnt
* sizeof(HFSPlusExtentDescriptor
);
1048 extentbufsize
= roundup(extentbufsize
, sizeof(HFSPlusExtentRecord
));
1049 MALLOC(extentptr
, HFSPlusExtentDescriptor
*, extentbufsize
, M_TEMP
, M_WAITOK
);
1050 if (extentptr
== NULL
) {
1054 bzero(extentptr
, extentbufsize
);
1055 result
= alloc_attr_blks(hfsmp
, attrsize
, extentbufsize
, extentptr
, &allocatedblks
);
1058 goto exit
; /* no more space */
1060 /* Copy data into the blocks. */
1061 result
= write_attr_data(hfsmp
, uio
, attrsize
, extentptr
);
1064 const char *name
= vnode_getname(vp
);
1065 printf("hfs_setxattr: write_attr_data err (%d) %s:%s\n",
1066 result
, name
? name
: "", ap
->a_name
);
1068 vnode_putname(name
);
1073 /* Now remove any previous attribute. */
1075 result
= remove_attribute_records(hfsmp
, iterator
);
1078 const char *name
= vnode_getname(vp
);
1079 printf("hfs_setxattr: remove_attribute_records err (%d) %s:%s\n",
1080 result
, name
? name
: "", ap
->a_name
);
1082 vnode_putname(name
);
1087 /* Create attribute fork data record. */
1088 MALLOC(recp
, HFSPlusAttrRecord
*, sizeof(HFSPlusAttrRecord
), M_TEMP
, M_WAITOK
);
1093 btdata
.bufferAddress
= recp
;
1094 btdata
.itemCount
= 1;
1095 btdata
.itemSize
= sizeof(HFSPlusAttrForkData
);
1097 recp
->recordType
= kHFSPlusAttrForkData
;
1098 recp
->forkData
.reserved
= 0;
1099 recp
->forkData
.theFork
.logicalSize
= attrsize
;
1100 recp
->forkData
.theFork
.clumpSize
= 0;
1101 recp
->forkData
.theFork
.totalBlocks
= blkcnt
;
1102 bcopy(extentptr
, recp
->forkData
.theFork
.extents
, sizeof(HFSPlusExtentRecord
));
1104 (void) hfs_buildattrkey(target_id
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
1106 result
= BTInsertRecord(btfile
, iterator
, &btdata
, btdata
.itemSize
);
1108 printf ("hfs_setxattr: BTInsertRecord() - %d,%s err=%d\n",
1109 target_id
, ap
->a_name
, result
);
1112 extentblks
= count_extent_blocks(blkcnt
, recp
->forkData
.theFork
.extents
);
1113 blkcnt
-= extentblks
;
1114 keystartblk
= &((HFSPlusAttrKey
*)&iterator
->key
)->startBlock
;
1117 /* Create overflow extents as needed. */
1118 while (blkcnt
> 0) {
1119 /* Initialize the key and record. */
1120 *keystartblk
+= (u_int32_t
)extentblks
;
1121 btdata
.itemSize
= sizeof(HFSPlusAttrExtents
);
1122 recp
->recordType
= kHFSPlusAttrExtents
;
1123 recp
->overflowExtents
.reserved
= 0;
1125 /* Copy the next set of extents. */
1126 i
+= kHFSPlusExtentDensity
;
1127 bcopy(&extentptr
[i
], recp
->overflowExtents
.extents
, sizeof(HFSPlusExtentRecord
));
1129 result
= BTInsertRecord(btfile
, iterator
, &btdata
, btdata
.itemSize
);
1131 printf ("hfs_setxattr: BTInsertRecord() overflow - %d,%s err=%d\n",
1132 target_id
, ap
->a_name
, result
);
1135 extentblks
= count_extent_blocks(blkcnt
, recp
->overflowExtents
.extents
);
1136 blkcnt
-= extentblks
;
1139 else { /* Inline data */
1141 result
= remove_attribute_records(hfsmp
, iterator
);
1147 /* Calculate size of record rounded up to multiple of 2 bytes. */
1148 btdata
.itemSize
= sizeof(HFSPlusAttrData
) - 2 + attrsize
+ ((attrsize
& 1) ? 1 : 0);
1149 MALLOC(recp
, HFSPlusAttrRecord
*, btdata
.itemSize
, M_TEMP
, M_WAITOK
);
1154 recp
->recordType
= kHFSPlusAttrInlineData
;
1155 recp
->attrData
.reserved
[0] = 0;
1156 recp
->attrData
.reserved
[1] = 0;
1157 recp
->attrData
.attrSize
= attrsize
;
1159 /* Copy in the attribute data (if any). */
1162 bcopy(data_ptr
, &recp
->attrData
.attrData
, attrsize
);
1166 * A null UIO meant it originated in-kernel. If they didn't supply data_ptr
1167 * then deny the copy operation.
1173 result
= uiomove((caddr_t
)&recp
->attrData
.attrData
, attrsize
, uio
);
1181 (void) hfs_buildattrkey(target_id
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
1183 btdata
.bufferAddress
= recp
;
1184 btdata
.itemCount
= 1;
1185 result
= BTInsertRecord(btfile
, iterator
, &btdata
, btdata
.itemSize
);
1189 if (btfile
&& started_transaction
) {
1190 (void) BTFlushPath(btfile
);
1193 hfs_systemfile_unlock(hfsmp
, lockflags
);
1198 /* Setting an attribute only updates change time and not
1199 * modified time of the file.
1201 cp
->c_touch_chgtime
= TRUE
;
1202 cp
->c_attr
.ca_recflags
|= kHFSHasAttributesMask
;
1203 if ((bcmp(ap
->a_name
, KAUTH_FILESEC_XATTR
, sizeof(KAUTH_FILESEC_XATTR
)) == 0)) {
1204 cp
->c_attr
.ca_recflags
|= kHFSHasSecurityMask
;
1206 (void) hfs_update(vp
, 0);
1209 if (started_transaction
) {
1210 if (result
&& allocatedblks
) {
1211 free_attr_blks(hfsmp
, allocatedblks
, extentptr
);
1213 hfs_end_transaction(hfsmp
);
1220 FREE(extentptr
, M_TEMP
);
1223 FREE(iterator
, M_TEMP
);
1233 * Remove an extended attribute.
1236 hfs_vnop_removexattr(struct vnop_removexattr_args
*ap
)
1238 struct vnop_removexattr_args {
1239 struct vnodeop_desc *a_desc;
1243 vfs_context_t a_context;
1247 struct vnode
*vp
= ap
->a_vp
;
1248 struct cnode
*cp
= VTOC(vp
);
1249 struct hfsmount
*hfsmp
;
1250 struct BTreeIterator
* iterator
= NULL
;
1253 time_t orig_ctime
=VTOC(vp
)->c_ctime
;
1255 if (ap
->a_name
== NULL
|| ap
->a_name
[0] == '\0') {
1256 return (EINVAL
); /* invalid name */
1259 if (VNODE_IS_RSRC(vp
)) {
1264 if (hfs_hides_xattr(ap
->a_context
, VTOC(vp
), ap
->a_name
, 1) && !(ap
->a_options
& XATTR_SHOWCOMPRESSION
)) {
1268 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_METADATA_DELETE_OP
, NULL
);
1269 #endif /* HFS_COMPRESSION */
1271 /* If Resource Fork is non-empty then truncate it. */
1272 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0) {
1273 struct vnode
*rvp
= NULL
;
1275 if ( !vnode_isreg(vp
) ) {
1278 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
1281 if ( !RSRC_FORK_EXISTS(cp
)) {
1285 result
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, FALSE
);
1291 hfs_lock_truncate(VTOC(rvp
), HFS_EXCLUSIVE_LOCK
);
1292 if ((result
= hfs_lock(VTOC(rvp
), HFS_EXCLUSIVE_LOCK
))) {
1293 hfs_unlock_truncate(cp
, 0);
1298 /* Start a transaction for encapsulating changes in
1299 * hfs_truncate() and hfs_update()
1301 if ((result
= hfs_start_transaction(hfsmp
))) {
1302 hfs_unlock_truncate(cp
, 0);
1308 result
= hfs_truncate(rvp
, (off_t
)0, IO_NDELAY
, 0, 0, ap
->a_context
);
1310 cp
->c_touch_chgtime
= TRUE
;
1311 cp
->c_flag
|= C_MODIFIED
;
1312 result
= hfs_update(vp
, FALSE
);
1315 hfs_end_transaction(hfsmp
);
1316 hfs_unlock_truncate(VTOC(rvp
), 0);
1317 hfs_unlock(VTOC(rvp
));
1322 /* Clear out the Finder Info. */
1323 if (bcmp(ap
->a_name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0) {
1324 void * finderinfo_start
;
1325 int finderinfo_size
;
1326 u_int8_t finderinfo
[32];
1327 u_int32_t date_added
;
1328 u_int8_t
*finfo
= NULL
;
1330 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
1334 /* Use the local copy to store our temporary changes. */
1335 bcopy(cp
->c_finderinfo
, finderinfo
, sizeof(finderinfo
));
1338 /* Zero out the date added field in the local copy */
1339 hfs_zero_dateadded (cp
, finderinfo
);
1341 /* Don't expose a symlink's private type/creator. */
1342 if (vnode_islnk(vp
)) {
1343 struct FndrFileInfo
*fip
;
1345 fip
= (struct FndrFileInfo
*)&finderinfo
;
1350 /* Do the byte compare against the local copy */
1351 if (bcmp(finderinfo
, emptyfinfo
, sizeof(emptyfinfo
)) == 0) {
1357 * If there was other content, zero out everything except
1358 * type/creator and date added. First, save the date added.
1360 finfo
= cp
->c_finderinfo
;
1362 if (S_ISREG(cp
->c_attr
.ca_mode
)) {
1363 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
1364 date_added
= extinfo
->date_added
;
1366 else if (S_ISDIR(cp
->c_attr
.ca_mode
)) {
1367 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
1368 date_added
= extinfo
->date_added
;
1371 if (vnode_islnk(vp
)) {
1372 /* Ignore type/creator */
1373 finderinfo_start
= &cp
->c_finderinfo
[8];
1374 finderinfo_size
= sizeof(cp
->c_finderinfo
) - 8;
1377 finderinfo_start
= &cp
->c_finderinfo
[0];
1378 finderinfo_size
= sizeof(cp
->c_finderinfo
);
1380 bzero(finderinfo_start
, finderinfo_size
);
1383 /* Now restore the date added */
1384 if (S_ISREG(cp
->c_attr
.ca_mode
)) {
1385 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
1386 extinfo
->date_added
= date_added
;
1388 else if (S_ISDIR(cp
->c_attr
.ca_mode
)) {
1389 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
1390 extinfo
->date_added
= date_added
;
1393 /* Updating finderInfo updates change time and modified time */
1394 cp
->c_touch_chgtime
= TRUE
;
1395 cp
->c_flag
|= C_MODIFIED
;
1396 hfs_update(vp
, FALSE
);
1403 * Standard HFS only supports native FinderInfo and Resource Forks.
1405 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1408 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1412 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1413 if (iterator
== NULL
) {
1416 bzero(iterator
, sizeof(*iterator
));
1418 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
))) {
1422 result
= hfs_buildattrkey(cp
->c_fileid
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
1427 if (hfs_start_transaction(hfsmp
) != 0) {
1431 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1433 result
= remove_attribute_records(hfsmp
, iterator
);
1435 hfs_systemfile_unlock(hfsmp
, lockflags
);
1438 cp
->c_touch_chgtime
= TRUE
;
1440 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
1442 /* If no more attributes exist, clear attribute bit */
1443 result
= file_attribute_exist(hfsmp
, cp
->c_fileid
);
1445 cp
->c_attr
.ca_recflags
&= ~kHFSHasAttributesMask
;
1447 if (result
== EEXIST
) {
1451 hfs_systemfile_unlock(hfsmp
, lockflags
);
1453 /* If ACL was removed, clear security bit */
1454 if ((bcmp(ap
->a_name
, KAUTH_FILESEC_XATTR
, sizeof(KAUTH_FILESEC_XATTR
)) == 0)) {
1455 cp
->c_attr
.ca_recflags
&= ~kHFSHasSecurityMask
;
1457 (void) hfs_update(vp
, 0);
1460 hfs_end_transaction(hfsmp
);
1464 FREE(iterator
, M_TEMP
);
1465 return MacToVFSError(result
);
1468 /* Check if any attribute record exist for given fileID. This function
1469 * is called by hfs_vnop_removexattr to determine if it should clear the
1470 * attribute bit in the catalog record or not.
1472 * Note - you must acquire a shared lock on the attribute btree before
1473 * calling this function.
1476 * EEXIST - If attribute record was found
1477 * 0 - Attribute was not found
1478 * (other) - Other error (such as EIO)
1481 file_attribute_exist(struct hfsmount
*hfsmp
, uint32_t fileID
)
1483 HFSPlusAttrKey
*key
;
1484 struct BTreeIterator
* iterator
= NULL
;
1485 struct filefork
*btfile
;
1488 // if there's no attribute b-tree we sure as heck
1489 // can't have any attributes!
1490 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1494 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1495 if (iterator
== NULL
) {
1499 bzero(iterator
, sizeof(*iterator
));
1500 key
= (HFSPlusAttrKey
*)&iterator
->key
;
1502 result
= hfs_buildattrkey(fileID
, NULL
, key
);
1507 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1508 result
= BTSearchRecord(btfile
, iterator
, NULL
, NULL
, NULL
);
1509 if (result
&& (result
!= btNotFound
)) {
1513 result
= BTIterateRecord(btfile
, kBTreeNextRecord
, iterator
, NULL
, NULL
);
1514 /* If no next record was found or fileID for next record did not match,
1515 * no more attributes exist for this fileID
1517 if ((result
&& (result
== btNotFound
)) || (key
->fileID
!= fileID
)) {
1525 FREE(iterator
, M_TEMP
);
1532 * Remove all the records for a given attribute.
1534 * - Used by hfs_vnop_removexattr, hfs_vnop_setxattr and hfs_removeallattr.
1535 * - A transaction must have been started.
1536 * - The Attribute b-tree file must be locked exclusive.
1537 * - The Allocation Bitmap file must be locked exclusive.
1538 * - The iterator key must be initialized.
1541 remove_attribute_records(struct hfsmount
*hfsmp
, BTreeIterator
* iterator
)
1543 struct filefork
*btfile
;
1544 FSBufferDescriptor btdata
;
1545 HFSPlusAttrRecord attrdata
; /* 90 bytes */
1549 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1551 btdata
.bufferAddress
= &attrdata
;
1552 btdata
.itemSize
= sizeof(attrdata
);
1553 btdata
.itemCount
= 1;
1554 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
1556 goto exit
; /* no records. */
1559 * Free the blocks from extent based attributes.
1561 * Note that the block references (btree records) are removed
1562 * before releasing the blocks in the allocation bitmap.
1564 if (attrdata
.recordType
== kHFSPlusAttrForkData
) {
1567 u_int32_t
*keystartblk
;
1569 if (datasize
< sizeof(HFSPlusAttrForkData
)) {
1570 printf("hfs: remove_attribute_records: bad record size %d (expecting %lu)\n", datasize
, sizeof(HFSPlusAttrForkData
));
1572 totalblks
= attrdata
.forkData
.theFork
.totalBlocks
;
1574 /* Process the first 8 extents. */
1575 extentblks
= count_extent_blocks(totalblks
, attrdata
.forkData
.theFork
.extents
);
1576 if (extentblks
> totalblks
)
1577 panic("hfs: remove_attribute_records: corruption...");
1578 if (BTDeleteRecord(btfile
, iterator
) == 0) {
1579 free_attr_blks(hfsmp
, extentblks
, attrdata
.forkData
.theFork
.extents
);
1581 totalblks
-= extentblks
;
1582 keystartblk
= &((HFSPlusAttrKey
*)&iterator
->key
)->startBlock
;
1584 /* Process any overflow extents. */
1586 *keystartblk
+= (u_int32_t
)extentblks
;
1588 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
1590 (attrdata
.recordType
!= kHFSPlusAttrExtents
) ||
1591 (datasize
< sizeof(HFSPlusAttrExtents
))) {
1592 printf("hfs: remove_attribute_records: BTSearchRecord %d (%d), totalblks %d\n",
1593 MacToVFSError(result
), attrdata
.recordType
!= kHFSPlusAttrExtents
, totalblks
);
1595 break; /* break from while */
1597 /* Process the next 8 extents. */
1598 extentblks
= count_extent_blocks(totalblks
, attrdata
.overflowExtents
.extents
);
1599 if (extentblks
> totalblks
)
1600 panic("hfs: remove_attribute_records: corruption...");
1601 if (BTDeleteRecord(btfile
, iterator
) == 0) {
1602 free_attr_blks(hfsmp
, extentblks
, attrdata
.overflowExtents
.extents
);
1604 totalblks
-= extentblks
;
1607 result
= BTDeleteRecord(btfile
, iterator
);
1609 (void) BTFlushPath(btfile
);
1611 return (result
== btNotFound
? ENOATTR
: MacToVFSError(result
));
1616 * Retrieve the list of extended attribute names.
1619 hfs_vnop_listxattr(struct vnop_listxattr_args
*ap
)
1621 struct vnop_listxattr_args {
1622 struct vnodeop_desc *a_desc;
1627 vfs_context_t a_context;
1630 struct vnode
*vp
= ap
->a_vp
;
1631 struct cnode
*cp
= VTOC(vp
);
1632 struct hfsmount
*hfsmp
;
1633 uio_t uio
= ap
->a_uio
;
1634 struct BTreeIterator
* iterator
= NULL
;
1635 struct filefork
*btfile
;
1636 struct listattr_callback_state state
;
1637 user_addr_t user_start
= 0;
1638 user_size_t user_len
= 0;
1641 u_int8_t finderinfo
[32];
1643 if (VNODE_IS_RSRC(vp
)) {
1648 int compressed
= hfs_file_is_compressed(cp
, 1); /* 1 == don't take the cnode lock */
1649 #endif /* HFS_COMPRESSION */
1654 if ((result
= hfs_lock(cp
, HFS_SHARED_LOCK
))) {
1659 * Make a copy of the cnode's finderinfo to a local so we can
1660 * zero out the date added field. Also zero out the private type/creator
1663 bcopy(cp
->c_finderinfo
, finderinfo
, sizeof(finderinfo
));
1664 hfs_zero_dateadded (cp
, finderinfo
);
1666 /* Don't expose a symlink's private type/creator. */
1667 if (vnode_islnk(vp
)) {
1668 struct FndrFileInfo
*fip
;
1670 fip
= (struct FndrFileInfo
*)&finderinfo
;
1676 /* If Finder Info is non-empty then export it's name. */
1677 if (bcmp(finderinfo
, emptyfinfo
, sizeof(emptyfinfo
)) != 0) {
1679 *ap
->a_size
+= sizeof(XATTR_FINDERINFO_NAME
);
1680 } else if ((user_size_t
)uio_resid(uio
) < sizeof(XATTR_FINDERINFO_NAME
)) {
1684 result
= uiomove(XATTR_FINDERINFO_NAME
,
1685 sizeof(XATTR_FINDERINFO_NAME
), uio
);
1690 /* If Resource Fork is non-empty then export it's name. */
1691 if (S_ISREG(cp
->c_mode
) && RSRC_FORK_EXISTS(cp
)) {
1693 if ((ap
->a_options
& XATTR_SHOWCOMPRESSION
) ||
1695 !hfs_hides_rsrc(ap
->a_context
, VTOC(vp
), 1) /* 1 == don't take the cnode lock */
1697 #endif /* HFS_COMPRESSION */
1700 *ap
->a_size
+= sizeof(XATTR_RESOURCEFORK_NAME
);
1701 } else if ((user_size_t
)uio_resid(uio
) < sizeof(XATTR_RESOURCEFORK_NAME
)) {
1705 result
= uiomove(XATTR_RESOURCEFORK_NAME
,
1706 sizeof(XATTR_RESOURCEFORK_NAME
), uio
);
1713 * Standard HFS only supports native FinderInfo and Resource Forks.
1714 * Return at this point.
1716 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1720 /* Bail if we don't have any extended attributes. */
1721 if ((hfsmp
->hfs_attribute_vp
== NULL
) ||
1722 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) == 0) {
1726 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1728 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1729 if (iterator
== NULL
) {
1733 bzero(iterator
, sizeof(*iterator
));
1734 result
= hfs_buildattrkey(cp
->c_fileid
, NULL
, (HFSPlusAttrKey
*)&iterator
->key
);
1739 * Lock the user's buffer here so that we won't fault on
1740 * it in uiomove while holding the attributes b-tree lock.
1742 if (uio
&& uio_isuserspace(uio
)) {
1743 user_start
= uio_curriovbase(uio
);
1744 user_len
= uio_curriovlen(uio
);
1746 if ((result
= vslock(user_start
, user_len
)) != 0) {
1751 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
1753 result
= BTSearchRecord(btfile
, iterator
, NULL
, NULL
, NULL
);
1754 if (result
&& result
!= btNotFound
) {
1755 hfs_systemfile_unlock(hfsmp
, lockflags
);
1759 state
.fileID
= cp
->c_fileid
;
1764 state
.showcompressed
= !compressed
|| ap
->a_options
& XATTR_SHOWCOMPRESSION
;
1765 state
.ctx
= ap
->a_context
;
1767 #endif /* HFS_COMPRESSION */
1770 * Process entries starting just after iterator->key.
1772 result
= BTIterateRecords(btfile
, kBTreeNextRecord
, iterator
,
1773 (IterateCallBackProcPtr
)listattr_callback
, &state
);
1774 hfs_systemfile_unlock(hfsmp
, lockflags
);
1776 *ap
->a_size
+= state
.size
;
1779 if (state
.result
|| result
== btNotFound
)
1780 result
= state
.result
;
1784 vsunlock(user_start
, user_len
, TRUE
);
1787 FREE(iterator
, M_TEMP
);
1791 return MacToVFSError(result
);
1796 * Callback - called for each attribute record
1799 listattr_callback(const HFSPlusAttrKey
*key
, __unused
const HFSPlusAttrData
*data
, struct listattr_callback_state
*state
)
1801 char attrname
[XATTR_MAXNAMELEN
+ 1];
1805 if (state
->fileID
!= key
->fileID
) {
1807 return (0); /* stop */
1810 * Skip over non-primary keys
1812 if (key
->startBlock
!= 0) {
1813 return (1); /* continue */
1816 /* Convert the attribute name into UTF-8. */
1817 result
= utf8_encodestr(key
->attrName
, key
->attrNameLen
* sizeof(UniChar
),
1818 (u_int8_t
*)attrname
, (size_t *)&bytecount
, sizeof(attrname
), '/', 0);
1820 state
->result
= result
;
1821 return (0); /* stop */
1823 bytecount
++; /* account for null termination char */
1825 if (xattr_protected(attrname
))
1826 return (1); /* continue */
1829 if (!state
->showcompressed
&& hfs_hides_xattr(state
->ctx
, VTOC(state
->vp
), attrname
, 1) ) /* 1 == don't take the cnode lock */
1830 return 1; /* continue */
1831 #endif /* HFS_COMPRESSION */
1833 if (state
->uio
== NULL
) {
1834 state
->size
+= bytecount
;
1836 if (bytecount
> uio_resid(state
->uio
)) {
1837 state
->result
= ERANGE
;
1838 return (0); /* stop */
1840 result
= uiomove((caddr_t
) attrname
, bytecount
, state
->uio
);
1842 state
->result
= result
;
1843 return (0); /* stop */
1846 return (1); /* continue */
1850 * Remove all the attributes from a cnode.
1852 * This function creates/ends its own transaction so that each
1853 * attribute is deleted in its own transaction (to avoid having
1854 * a transaction grow too large).
1856 * This function takes the necessary locks on the attribute
1857 * b-tree file and the allocation (bitmap) file.
1860 hfs_removeallattr(struct hfsmount
*hfsmp
, u_int32_t fileid
)
1862 BTreeIterator
*iterator
= NULL
;
1863 HFSPlusAttrKey
*key
;
1864 struct filefork
*btfile
;
1865 int result
, lockflags
;
1867 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1870 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1872 MALLOC(iterator
, BTreeIterator
*, sizeof(BTreeIterator
), M_TEMP
, M_WAITOK
);
1873 if (iterator
== NULL
) {
1876 bzero(iterator
, sizeof(BTreeIterator
));
1877 key
= (HFSPlusAttrKey
*)&iterator
->key
;
1879 /* Loop until there are no more attributes for this file id */
1881 if (hfs_start_transaction(hfsmp
) != 0) {
1886 /* Lock the attribute b-tree and the allocation (bitmap) files */
1887 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1890 * Go to first possible attribute key/record pair
1892 (void) hfs_buildattrkey(fileid
, NULL
, key
);
1893 result
= BTIterateRecord(btfile
, kBTreeNextRecord
, iterator
, NULL
, NULL
);
1894 if (result
|| key
->fileID
!= fileid
) {
1895 hfs_systemfile_unlock(hfsmp
, lockflags
);
1896 hfs_end_transaction(hfsmp
);
1899 result
= remove_attribute_records(hfsmp
, iterator
);
1901 #if HFS_XATTR_VERBOSE
1903 printf("hfs_removeallattr: unexpected err %d\n", result
);
1906 hfs_systemfile_unlock(hfsmp
, lockflags
);
1907 hfs_end_transaction(hfsmp
);
1912 FREE(iterator
, M_TEMP
);
1913 return (result
== btNotFound
? 0: MacToVFSError(result
));
1918 hfs_xattr_init(struct hfsmount
* hfsmp
)
1921 * If there isn't an attributes b-tree then create one.
1923 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
) &&
1924 (hfsmp
->hfs_attribute_vp
== NULL
) &&
1925 !(hfsmp
->hfs_flags
& HFS_READ_ONLY
)) {
1926 (void) hfs_create_attr_btree(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
,
1927 getnodecount(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
));
1929 if (hfsmp
->hfs_attribute_vp
)
1930 hfsmp
->hfs_max_inline_attrsize
= getmaxinlineattrsize(hfsmp
->hfs_attribute_vp
);
1934 * Enable/Disable volume attributes stored as EA for root file system.
1935 * Supported attributes are -
1936 * 1. Extent-based Extended Attributes
1939 hfs_set_volxattr(struct hfsmount
*hfsmp
, unsigned int xattrtype
, int state
)
1941 struct BTreeIterator
* iterator
= NULL
;
1942 struct filefork
*btfile
;
1946 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1949 if (xattrtype
!= HFS_SET_XATTREXTENTS_STATE
) {
1954 * If there isn't an attributes b-tree then create one.
1956 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1957 result
= hfs_create_attr_btree(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
,
1958 getnodecount(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
));
1964 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1965 if (iterator
== NULL
) {
1968 bzero(iterator
, sizeof(*iterator
));
1971 * Build a b-tree key.
1972 * We use the root's parent id (1) to hold this volume attribute.
1974 (void) hfs_buildattrkey(kHFSRootParentID
, XATTR_XATTREXTENTS_NAME
,
1975 (HFSPlusAttrKey
*)&iterator
->key
);
1977 /* Start a transaction for our changes. */
1978 if (hfs_start_transaction(hfsmp
) != 0) {
1982 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1984 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1987 /* Remove the attribute. */
1988 result
= BTDeleteRecord(btfile
, iterator
);
1989 if (result
== btNotFound
)
1992 FSBufferDescriptor btdata
;
1993 HFSPlusAttrData attrdata
;
1996 datasize
= sizeof(attrdata
);
1997 btdata
.bufferAddress
= &attrdata
;
1998 btdata
.itemSize
= datasize
;
1999 btdata
.itemCount
= 1;
2000 attrdata
.recordType
= kHFSPlusAttrInlineData
;
2001 attrdata
.reserved
[0] = 0;
2002 attrdata
.reserved
[1] = 0;
2003 attrdata
.attrSize
= 2;
2004 attrdata
.attrData
[0] = 0;
2005 attrdata
.attrData
[1] = 0;
2007 /* Insert the attribute. */
2008 result
= BTInsertRecord(btfile
, iterator
, &btdata
, datasize
);
2009 if (result
== btExists
)
2012 (void) BTFlushPath(btfile
);
2014 hfs_systemfile_unlock(hfsmp
, lockflags
);
2016 /* Finish the transaction of our changes. */
2017 hfs_end_transaction(hfsmp
);
2019 /* Update the state in the mount point */
2020 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
2022 hfsmp
->hfs_flags
&= ~HFS_XATTR_EXTENTS
;
2024 hfsmp
->hfs_flags
|= HFS_XATTR_EXTENTS
;
2026 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
2030 FREE(iterator
, M_TEMP
);
2032 return MacToVFSError(result
);
2037 * hfs_attrkeycompare - compare two attribute b-tree keys.
2039 * The name portion of the key is compared using a 16-bit binary comparison.
2040 * This is called from the b-tree code.
2044 hfs_attrkeycompare(HFSPlusAttrKey
*searchKey
, HFSPlusAttrKey
*trialKey
)
2046 u_int32_t searchFileID
, trialFileID
;
2049 searchFileID
= searchKey
->fileID
;
2050 trialFileID
= trialKey
->fileID
;
2053 if (searchFileID
> trialFileID
) {
2055 } else if (searchFileID
< trialFileID
) {
2058 u_int16_t
* str1
= &searchKey
->attrName
[0];
2059 u_int16_t
* str2
= &trialKey
->attrName
[0];
2060 int length1
= searchKey
->attrNameLen
;
2061 int length2
= trialKey
->attrNameLen
;
2065 if (length1
< length2
) {
2068 } else if (length1
> length2
) {
2091 * Names are equal; compare startBlock
2093 if (searchKey
->startBlock
== trialKey
->startBlock
)
2096 return (searchKey
->startBlock
< trialKey
->startBlock
? -1 : 1);
2104 * hfs_buildattrkey - build an Attribute b-tree key
2108 hfs_buildattrkey(u_int32_t fileID
, const char *attrname
, HFSPlusAttrKey
*key
)
2111 size_t unicodeBytes
= 0;
2113 if (attrname
!= NULL
) {
2115 * Convert filename from UTF-8 into Unicode
2117 result
= utf8_decodestr((const u_int8_t
*)attrname
, strlen(attrname
), key
->attrName
,
2118 &unicodeBytes
, sizeof(key
->attrName
), 0, 0);
2120 if (result
!= ENAMETOOLONG
)
2121 result
= EINVAL
; /* name has invalid characters */
2124 key
->attrNameLen
= unicodeBytes
/ sizeof(UniChar
);
2125 key
->keyLength
= kHFSPlusAttrKeyMinimumLength
+ unicodeBytes
;
2127 key
->attrNameLen
= 0;
2128 key
->keyLength
= kHFSPlusAttrKeyMinimumLength
;
2131 key
->fileID
= fileID
;
2132 key
->startBlock
= 0;
2138 * getnodecount - calculate starting node count for attributes b-tree.
2141 getnodecount(struct hfsmount
*hfsmp
, size_t nodesize
)
2143 u_int64_t freebytes
;
2144 u_int64_t calcbytes
;
2147 * 10.4: Scale base on current catalog file size (20 %) up to 20 MB.
2148 * 10.5: Attempt to be as big as the catalog clump size.
2150 * Use no more than 10 % of the remaining free space.
2152 freebytes
= (u_int64_t
)hfs_freeblks(hfsmp
, 0) * (u_int64_t
)hfsmp
->blockSize
;
2154 calcbytes
= MIN(hfsmp
->hfs_catalog_cp
->c_datafork
->ff_size
/ 5, 20 * 1024 * 1024);
2156 calcbytes
= MAX(calcbytes
, hfsmp
->hfs_catalog_cp
->c_datafork
->ff_clumpsize
);
2158 calcbytes
= MIN(calcbytes
, freebytes
/ 10);
2160 return (MAX(2, (int)(calcbytes
/ nodesize
)));
2165 * getmaxinlineattrsize - calculate maximum inline attribute size.
2167 * This yields 3,802 bytes for an 8K node size.
2170 getmaxinlineattrsize(struct vnode
* attrvp
)
2172 struct BTreeInfoRec btinfo
;
2173 size_t nodesize
= ATTRIBUTE_FILE_NODE_SIZE
;
2176 if (attrvp
!= NULL
) {
2177 (void) hfs_lock(VTOC(attrvp
), HFS_SHARED_LOCK
);
2178 if (BTGetInformation(VTOF(attrvp
), 0, &btinfo
) == 0)
2179 nodesize
= btinfo
.nodeSize
;
2180 hfs_unlock(VTOC(attrvp
));
2183 maxsize
-= sizeof(BTNodeDescriptor
); /* minus node descriptor */
2184 maxsize
-= 3 * sizeof(u_int16_t
); /* minus 3 index slots */
2185 maxsize
/= 2; /* 2 key/rec pairs minumum */
2186 maxsize
-= sizeof(HFSPlusAttrKey
); /* minus maximum key size */
2187 maxsize
-= sizeof(HFSPlusAttrData
) - 2; /* minus data header */
2188 maxsize
&= 0xFFFFFFFE; /* multiple of 2 bytes */
2194 * Initialize vnode for attribute data I/O.
2198 * - the attrdata vnode is initialized as hfsmp->hfs_attrdata_vp
2199 * - an iocount is taken on the attrdata vnode which exists
2200 * for the entire duration of the mount. It is only dropped
2202 * - the attrdata cnode is not locked
2205 * - returns non-zero value
2206 * - the caller does not have to worry about any locks or references
2208 int init_attrdata_vnode(struct hfsmount
*hfsmp
)
2212 struct cat_desc cat_desc
;
2213 struct cat_attr cat_attr
;
2214 struct cat_fork cat_fork
;
2215 int newvnode_flags
= 0;
2217 bzero(&cat_desc
, sizeof(cat_desc
));
2218 cat_desc
.cd_parentcnid
= kHFSRootParentID
;
2219 cat_desc
.cd_nameptr
= (const u_int8_t
*)hfs_attrdatafilename
;
2220 cat_desc
.cd_namelen
= strlen(hfs_attrdatafilename
);
2221 cat_desc
.cd_cnid
= kHFSAttributeDataFileID
;
2222 /* Tag vnode as system file, note that we can still use cluster I/O */
2223 cat_desc
.cd_flags
|= CD_ISMETA
;
2225 bzero(&cat_attr
, sizeof(cat_attr
));
2226 cat_attr
.ca_linkcount
= 1;
2227 cat_attr
.ca_mode
= S_IFREG
;
2228 cat_attr
.ca_fileid
= cat_desc
.cd_cnid
;
2229 cat_attr
.ca_blocks
= hfsmp
->totalBlocks
;
2232 * The attribute data file is a virtual file that spans the
2233 * entire file system space.
2235 * Each extent-based attribute occupies a unique portion of
2236 * in this virtual file. The cluster I/O is done using actual
2237 * allocation block offsets so no additional mapping is needed
2238 * for the VNOP_BLOCKMAP call.
2240 * This approach allows the attribute data to be cached without
2241 * incurring the high cost of using a separate vnode per attribute.
2243 * Since we need to acquire the attribute b-tree file lock anyways,
2244 * the virtual file doesn't introduce any additional serialization.
2246 bzero(&cat_fork
, sizeof(cat_fork
));
2247 cat_fork
.cf_size
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
2248 cat_fork
.cf_blocks
= hfsmp
->totalBlocks
;
2249 cat_fork
.cf_extents
[0].startBlock
= 0;
2250 cat_fork
.cf_extents
[0].blockCount
= cat_fork
.cf_blocks
;
2252 result
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cat_desc
, 0, &cat_attr
,
2253 &cat_fork
, &vp
, &newvnode_flags
);
2255 hfsmp
->hfs_attrdata_vp
= vp
;
2256 hfs_unlock(VTOC(vp
));
2262 * Read an extent based attribute.
2265 read_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
)
2267 vnode_t evp
= hfsmp
->hfs_attrdata_vp
;
2275 hfs_lock_truncate(VTOC(evp
), HFS_SHARED_LOCK
);
2277 bufsize
= (int)uio_resid(uio
);
2278 attrsize
= (int)datasize
;
2279 blksize
= (int)hfsmp
->blockSize
;
2282 * Read the attribute data one extent at a time.
2283 * For the typical case there is only one extent.
2285 for (i
= 0; (attrsize
> 0) && (bufsize
> 0) && (extents
[i
].startBlock
!= 0); ++i
) {
2286 iosize
= (int)extents
[i
].blockCount
* blksize
;
2287 iosize
= MIN(iosize
, attrsize
);
2288 iosize
= MIN(iosize
, bufsize
);
2289 uio_setresid(uio
, iosize
);
2290 uio_setoffset(uio
, (u_int64_t
)extents
[i
].startBlock
* (u_int64_t
)blksize
);
2292 result
= cluster_read(evp
, uio
, VTOF(evp
)->ff_size
, IO_SYNC
| IO_UNIT
);
2294 #if HFS_XATTR_VERBOSE
2295 printf("hfs: read_attr_data: cr iosize %d [%d, %d] (%d)\n",
2296 iosize
, extents
[i
].startBlock
, extents
[i
].blockCount
, result
);
2303 uio_setresid(uio
, bufsize
);
2304 uio_setoffset(uio
, datasize
);
2306 hfs_unlock_truncate(VTOC(evp
), 0);
2311 * Write an extent based attribute.
2314 write_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
)
2316 vnode_t evp
= hfsmp
->hfs_attrdata_vp
;
2325 hfs_lock_truncate(VTOC(evp
), HFS_SHARED_LOCK
);
2327 bufsize
= uio_resid(uio
);
2328 attrsize
= (int) datasize
;
2329 blksize
= (int) hfsmp
->blockSize
;
2330 filesize
= VTOF(evp
)->ff_size
;
2333 * Write the attribute data one extent at a time.
2335 for (i
= 0; (attrsize
> 0) && (bufsize
> 0) && (extents
[i
].startBlock
!= 0); ++i
) {
2336 iosize
= (int)extents
[i
].blockCount
* blksize
;
2337 iosize
= MIN(iosize
, attrsize
);
2338 iosize
= MIN(iosize
, bufsize
);
2339 uio_setresid(uio
, iosize
);
2340 uio_setoffset(uio
, (u_int64_t
)extents
[i
].startBlock
* (u_int64_t
)blksize
);
2342 result
= cluster_write(evp
, uio
, filesize
, filesize
, filesize
,
2343 (off_t
) 0, IO_SYNC
| IO_UNIT
);
2344 #if HFS_XATTR_VERBOSE
2345 printf("hfs: write_attr_data: cw iosize %d [%d, %d] (%d)\n",
2346 iosize
, extents
[i
].startBlock
, extents
[i
].blockCount
, result
);
2353 uio_setresid(uio
, bufsize
);
2354 uio_setoffset(uio
, datasize
);
2356 hfs_unlock_truncate(VTOC(evp
), 0);
2361 * Allocate blocks for an extent based attribute.
2364 alloc_attr_blks(struct hfsmount
*hfsmp
, size_t attrsize
, size_t extentbufsize
, HFSPlusExtentDescriptor
*extents
, int *blocks
)
2373 startblk
= hfsmp
->hfs_metazone_end
;
2374 blkcnt
= howmany(attrsize
, hfsmp
->blockSize
);
2375 if (blkcnt
> (int)hfs_freeblks(hfsmp
, 0)) {
2379 maxextents
= extentbufsize
/ sizeof(HFSPlusExtentDescriptor
);
2381 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2383 for (i
= 0; (blkcnt
> 0) && (i
< maxextents
); i
++) {
2384 result
= BlockAllocate(hfsmp
, startblk
, blkcnt
, blkcnt
, 0,
2385 &extents
[i
].startBlock
, &extents
[i
].blockCount
);
2386 #if HFS_XATTR_VERBOSE
2387 printf("hfs: alloc_attr_blks: BA blkcnt %d [%d, %d] (%d)\n",
2388 blkcnt
, extents
[i
].startBlock
, extents
[i
].blockCount
, result
);
2391 extents
[i
].startBlock
= 0;
2392 extents
[i
].blockCount
= 0;
2395 blkcnt
-= extents
[i
].blockCount
;
2396 startblk
= extents
[i
].startBlock
+ extents
[i
].blockCount
;
2399 * If it didn't fit in the extents buffer then bail.
2404 #if HFS_XATTR_VERBOSE
2405 printf("hfs: alloc_attr_blks: unexpected failure, %d blocks unallocated\n", blkcnt
);
2407 for (; i
>= 0; i
--) {
2408 if ((blkcnt
= extents
[i
].blockCount
) != 0) {
2409 (void) BlockDeallocate(hfsmp
, extents
[i
].startBlock
, blkcnt
, 0);
2410 extents
[i
].startBlock
= 0;
2411 extents
[i
].blockCount
= 0;
2416 hfs_systemfile_unlock(hfsmp
, lockflags
);
2417 return MacToVFSError(result
);
2421 * Release blocks from an extent based attribute.
2424 free_attr_blks(struct hfsmount
*hfsmp
, int blkcnt
, HFSPlusExtentDescriptor
*extents
)
2426 vnode_t evp
= hfsmp
->hfs_attrdata_vp
;
2427 int remblks
= blkcnt
;
2431 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2433 for (i
= 0; (remblks
> 0) && (extents
[i
].blockCount
!= 0); i
++) {
2434 if (extents
[i
].blockCount
> (u_int32_t
)blkcnt
) {
2435 #if HFS_XATTR_VERBOSE
2436 printf("hfs: free_attr_blks: skipping bad extent [%d, %d]\n",
2437 extents
[i
].startBlock
, extents
[i
].blockCount
);
2439 extents
[i
].blockCount
= 0;
2442 if (extents
[i
].startBlock
== 0) {
2445 (void)BlockDeallocate(hfsmp
, extents
[i
].startBlock
, extents
[i
].blockCount
, 0);
2446 remblks
-= extents
[i
].blockCount
;
2447 extents
[i
].startBlock
= 0;
2448 extents
[i
].blockCount
= 0;
2450 #if HFS_XATTR_VERBOSE
2451 printf("hfs: free_attr_blks: BlockDeallocate [%d, %d]\n",
2452 extents
[i
].startBlock
, extents
[i
].blockCount
);
2454 /* Discard any resident pages for this block range. */
2458 start
= (u_int64_t
)extents
[i
].startBlock
* (u_int64_t
)hfsmp
->blockSize
;
2459 end
= start
+ (u_int64_t
)extents
[i
].blockCount
* (u_int64_t
)hfsmp
->blockSize
;
2460 (void) ubc_msync(hfsmp
->hfs_attrdata_vp
, start
, end
, &start
, UBC_INVALIDATE
);
2464 hfs_systemfile_unlock(hfsmp
, lockflags
);
2468 has_overflow_extents(HFSPlusForkData
*forkdata
)
2472 if (forkdata
->extents
[7].blockCount
== 0)
2475 blocks
= forkdata
->extents
[0].blockCount
+
2476 forkdata
->extents
[1].blockCount
+
2477 forkdata
->extents
[2].blockCount
+
2478 forkdata
->extents
[3].blockCount
+
2479 forkdata
->extents
[4].blockCount
+
2480 forkdata
->extents
[5].blockCount
+
2481 forkdata
->extents
[6].blockCount
+
2482 forkdata
->extents
[7].blockCount
;
2484 return (forkdata
->totalBlocks
> blocks
);
2488 count_extent_blocks(int maxblks
, HFSPlusExtentRecord extents
)
2493 for (i
= 0, blocks
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
2494 /* Ignore obvious bogus extents. */
2495 if (extents
[i
].blockCount
> (u_int32_t
)maxblks
)
2497 if (extents
[i
].startBlock
== 0 || extents
[i
].blockCount
== 0)
2499 blocks
+= extents
[i
].blockCount
;