2 * Copyright (c) 2004-2013 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 */
71 /* HFS Internal Names */
72 #define XATTR_EXTENDEDSECURITY_NAME "system.extendedsecurity"
73 #define XATTR_XATTREXTENTS_NAME "system.xattrextents"
75 /* Faster version if we already know this is the data fork. */
76 #define RSRC_FORK_EXISTS(CP) \
77 (((CP)->c_attr.ca_blocks - (CP)->c_datafork->ff_data.cf_blocks) > 0)
79 static u_int32_t emptyfinfo
[8] = {0};
81 static int hfs_zero_hidden_fields (struct cnode
*cp
, u_int8_t
*finderinfo
);
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
, HFS_LOCK_DEFAULT
))) {
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
, FALSE
);
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
, HFS_LOCK_DEFAULT
))) {
187 error
= hfs_vgetrsrc(VTOHFS(vp
), vp
, svpp
, TRUE
, FALSE
);
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
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
220 if ((error
= hfs_lock(scp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
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
, HFS_LOCK_DEFAULT
);
234 /* Zero out the date added field for the specified cnode */
235 static int hfs_zero_hidden_fields (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
) || S_ISLNK(cp
->c_attr
.ca_mode
)) {
243 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
244 extinfo
->date_added
= 0;
245 extinfo
->write_gen_counter
= 0;
246 } else if (S_ISDIR(cp
->c_attr
.ca_mode
)) {
247 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
248 extinfo
->date_added
= 0;
250 /* Return an error */
258 * Retrieve the data of an extended attribute.
261 hfs_vnop_getxattr(struct vnop_getxattr_args
*ap
)
263 struct vnop_getxattr_args {
264 struct vnodeop_desc *a_desc;
270 vfs_context_t a_context;
274 struct vnode
*vp
= ap
->a_vp
;
276 struct hfsmount
*hfsmp
;
277 uio_t uio
= ap
->a_uio
;
282 if (vp
== cp
->c_vp
) {
284 int decmpfs_hide
= hfs_hides_xattr(ap
->a_context
, VTOC(vp
), ap
->a_name
, 1); /* 1 == don't take the cnode lock */
285 if (decmpfs_hide
&& !(ap
->a_options
& XATTR_SHOWCOMPRESSION
))
287 #endif /* HFS_COMPRESSION */
289 /* Get the Finder Info. */
290 if (bcmp(ap
->a_name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0) {
291 u_int8_t finderinfo
[32];
294 if ((result
= hfs_lock(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
))) {
297 /* Make a copy since we may not export all of it. */
298 bcopy(cp
->c_finderinfo
, finderinfo
, sizeof(finderinfo
));
301 /* Zero out the date added field in the local copy */
302 hfs_zero_hidden_fields (cp
, finderinfo
);
304 /* Don't expose a symlink's private type/creator. */
305 if (vnode_islnk(vp
)) {
306 struct FndrFileInfo
*fip
;
308 fip
= (struct FndrFileInfo
*)&finderinfo
;
312 /* If Finder Info is empty then it doesn't exist. */
313 if (bcmp(finderinfo
, emptyfinfo
, sizeof(emptyfinfo
)) == 0) {
317 *ap
->a_size
= bufsize
;
320 if ((user_size_t
)uio_resid(uio
) < bufsize
)
323 result
= uiomove((caddr_t
)&finderinfo
, bufsize
, uio
);
327 /* Read the Resource Fork. */
328 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0) {
329 struct vnode
*rvp
= NULL
;
330 int openunlinked
= 0;
333 if ( !S_ISREG(cp
->c_mode
) ) {
336 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
339 namelen
= cp
->c_desc
.cd_namelen
;
341 if ( !RSRC_FORK_EXISTS(cp
)) {
346 if ((cp
->c_flag
& C_DELETED
) && (namelen
== 0)) {
350 result
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, FALSE
);
356 *ap
->a_size
= (size_t)VTOF(rvp
)->ff_size
;
359 user_ssize_t uio_size
= 0;
361 uio_size
= uio_resid(uio
);
362 #endif /* HFS_COMPRESSION */
363 result
= VNOP_READ(rvp
, uio
, 0, ap
->a_context
);
367 (uio_resid(uio
) == uio_size
)) {
369 * We intentionally make the above call to VNOP_READ so that
370 * it can return an authorization/permission/etc. Error
371 * based on ap->a_context and thus deny this operation;
372 * in that case, result != 0 and we won't proceed.
374 * However, if result == 0, it will have returned no data
375 * because hfs_vnop_read hid the resource fork
376 * (hence uio_resid(uio) == uio_size, i.e. the uio is untouched)
378 * In that case, we try again with the decmpfs_ctx context
379 * to get the actual data
381 result
= VNOP_READ(rvp
, uio
, 0, decmpfs_ctx
);
383 #endif /* HFS_COMPRESSION */
385 /* force the rsrc fork vnode to recycle right away */
388 vref
= vnode_ref (rvp
);
400 * Standard HFS only supports native FinderInfo and Resource Forks.
402 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
406 if ((result
= hfs_lock(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
))) {
410 /* Check for non-rsrc, non-finderinfo EAs */
411 result
= hfs_getxattr_internal (cp
, ap
, VTOHFS(cp
->c_vp
), 0);
415 return MacToVFSError(result
);
423 * We break out this internal function which searches the attributes B-Tree and the
424 * overflow extents file to find non-resource, non-finderinfo EAs. There may be cases
425 * where we need to get EAs in contexts where we are already holding the cnode lock,
426 * and to re-enter hfs_vnop_getxattr would cause us to double-lock the cnode. Instead,
427 * we can just directly call this function.
429 * We pass the hfsmp argument directly here because we may not necessarily have a cnode to
430 * operate on. Under normal conditions, we have a file or directory to query, but if we
431 * are operating on the root directory (id 1), then we may not have a cnode. In this case, if hte
432 * 'cp' argument is NULL, then we need to use the 'fileid' argument as the entry to manipulate
434 * NOTE: This function assumes the cnode lock for 'cp' is held exclusive or shared.
436 int hfs_getxattr_internal (struct cnode
*cp
, struct vnop_getxattr_args
*ap
,
437 struct hfsmount
*hfsmp
, u_int32_t fileid
)
440 struct filefork
*btfile
;
441 struct BTreeIterator
* iterator
= NULL
;
443 HFSPlusAttrRecord
*recp
= NULL
;
444 FSBufferDescriptor btdata
;
447 u_int16_t datasize
= 0;
448 uio_t uio
= ap
->a_uio
;
449 u_int32_t target_id
= 0;
452 target_id
= cp
->c_fileid
;
458 /* Bail if we don't have an EA B-Tree. */
459 if ((hfsmp
->hfs_attribute_vp
== NULL
) ||
460 ((cp
) && (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) == 0)) {
465 /* Initialize the B-Tree iterator for searching for the proper EA */
466 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
468 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
469 if (iterator
== NULL
) {
473 bzero(iterator
, sizeof(*iterator
));
475 /* Allocate memory for reading in the attribute record. This buffer is
476 * big enough to read in all types of attribute records. It is not big
477 * enough to read inline attribute data which is read in later.
479 MALLOC(recp
, HFSPlusAttrRecord
*, sizeof(HFSPlusAttrRecord
), M_TEMP
, M_WAITOK
);
484 btdata
.bufferAddress
= recp
;
485 btdata
.itemSize
= sizeof(HFSPlusAttrRecord
);
486 btdata
.itemCount
= 1;
488 result
= hfs_buildattrkey(target_id
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
493 /* Lookup the attribute in the Attribute B-Tree */
494 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
495 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
496 hfs_systemfile_unlock(hfsmp
, lockflags
);
499 if (result
== btNotFound
) {
506 * Operate differently if we have inline EAs that can fit in the attribute B-Tree or if
507 * we have extent based EAs.
509 switch (recp
->recordType
) {
511 /* Attribute fits in the Attribute B-Tree */
512 case kHFSPlusAttrInlineData
: {
514 * Sanity check record size. It's not required to have any
515 * user data, so the minimum size is 2 bytes less that the
516 * size of HFSPlusAttrData (since HFSPlusAttrData struct
517 * has 2 bytes set aside for attribute data).
519 if (datasize
< (sizeof(HFSPlusAttrData
) - 2)) {
520 printf("hfs_getxattr: vol=%s %d,%s invalid record size %d (expecting %lu)\n",
521 hfsmp
->vcbVN
, target_id
, ap
->a_name
, datasize
, sizeof(HFSPlusAttrData
));
525 *ap
->a_size
= recp
->attrData
.attrSize
;
526 if (uio
&& recp
->attrData
.attrSize
!= 0) {
527 if (*ap
->a_size
> (user_size_t
)uio_resid(uio
)) {
528 /* User provided buffer is not large enough for the xattr data */
531 /* Previous BTreeSearchRecord() read in only the attribute record,
532 * and not the attribute data. Now allocate enough memory for
533 * both attribute record and data, and read the attribute record again.
535 bufsize
= sizeof(HFSPlusAttrData
) - 2 + recp
->attrData
.attrSize
;
537 MALLOC(recp
, HFSPlusAttrRecord
*, bufsize
, M_TEMP
, M_WAITOK
);
543 btdata
.bufferAddress
= recp
;
544 btdata
.itemSize
= bufsize
;
545 btdata
.itemCount
= 1;
547 bzero(iterator
, sizeof(*iterator
));
548 result
= hfs_buildattrkey(target_id
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
553 /* Lookup the attribute record and inline data */
554 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
555 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
556 hfs_systemfile_unlock(hfsmp
, lockflags
);
558 if (result
== btNotFound
) {
564 /* Copy-out the attribute data to the user buffer */
565 *ap
->a_size
= recp
->attrData
.attrSize
;
566 result
= uiomove((caddr_t
) &recp
->attrData
.attrData
, recp
->attrData
.attrSize
, uio
);
572 /* Extent-Based EAs */
573 case kHFSPlusAttrForkData
: {
574 if (datasize
< sizeof(HFSPlusAttrForkData
)) {
575 printf("hfs_getxattr: vol=%s %d,%s invalid record size %d (expecting %lu)\n",
576 hfsmp
->vcbVN
, target_id
, ap
->a_name
, datasize
, sizeof(HFSPlusAttrForkData
));
580 *ap
->a_size
= recp
->forkData
.theFork
.logicalSize
;
584 if (*ap
->a_size
> (user_size_t
)uio_resid(uio
)) {
588 /* Process overflow extents if necessary. */
589 if (has_overflow_extents(&recp
->forkData
.theFork
)) {
590 HFSPlusExtentDescriptor
*extentbuf
;
591 HFSPlusExtentDescriptor
*extentptr
;
592 size_t extentbufsize
;
593 u_int32_t totalblocks
;
597 totalblocks
= recp
->forkData
.theFork
.totalBlocks
;
598 /* Ignore bogus block counts. */
599 if (totalblocks
> howmany(HFS_XATTR_MAXSIZE
, hfsmp
->blockSize
)) {
603 attrlen
= recp
->forkData
.theFork
.logicalSize
;
605 /* Get a buffer to hold the worst case amount of extents. */
606 extentbufsize
= totalblocks
* sizeof(HFSPlusExtentDescriptor
);
607 extentbufsize
= roundup(extentbufsize
, sizeof(HFSPlusExtentRecord
));
608 MALLOC(extentbuf
, HFSPlusExtentDescriptor
*, extentbufsize
, M_TEMP
, M_WAITOK
);
609 if (extentbuf
== NULL
) {
613 bzero(extentbuf
, extentbufsize
);
614 extentptr
= extentbuf
;
616 /* Grab the first 8 extents. */
617 bcopy(&recp
->forkData
.theFork
.extents
[0], extentptr
, sizeof(HFSPlusExtentRecord
));
618 extentptr
+= kHFSPlusExtentDensity
;
619 blkcnt
= count_extent_blocks(totalblocks
, recp
->forkData
.theFork
.extents
);
621 /* Now lookup the overflow extents. */
622 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
623 while (blkcnt
< totalblocks
) {
624 ((HFSPlusAttrKey
*)&iterator
->key
)->startBlock
= blkcnt
;
625 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
627 (recp
->recordType
!= kHFSPlusAttrExtents
) ||
628 (datasize
< sizeof(HFSPlusAttrExtents
))) {
629 printf("hfs_getxattr: %s missing extents, only %d blks of %d found\n",
630 ap
->a_name
, blkcnt
, totalblocks
);
632 break; /* break from while */
634 /* Grab the next 8 extents. */
635 bcopy(&recp
->overflowExtents
.extents
[0], extentptr
, sizeof(HFSPlusExtentRecord
));
636 extentptr
+= kHFSPlusExtentDensity
;
637 blkcnt
+= count_extent_blocks(totalblocks
, recp
->overflowExtents
.extents
);
640 /* Release Attr B-Tree lock */
641 hfs_systemfile_unlock(hfsmp
, lockflags
);
643 if (blkcnt
< totalblocks
) {
646 result
= read_attr_data(hfsmp
, uio
, attrlen
, extentbuf
);
648 FREE(extentbuf
, M_TEMP
);
650 } else { /* No overflow extents. */
651 result
= read_attr_data(hfsmp
, uio
, recp
->forkData
.theFork
.logicalSize
, recp
->forkData
.theFork
.extents
);
657 /* We only support Extent or inline EAs. Default to ENOATTR for anything else */
664 FREE(iterator
, M_TEMP
);
676 * Set the data of an extended attribute.
679 hfs_vnop_setxattr(struct vnop_setxattr_args
*ap
)
681 struct vnop_setxattr_args {
682 struct vnodeop_desc *a_desc;
687 vfs_context_t a_context;
691 struct vnode
*vp
= ap
->a_vp
;
692 struct cnode
*cp
= NULL
;
693 struct hfsmount
*hfsmp
;
694 uio_t uio
= ap
->a_uio
;
696 void * user_data_ptr
= NULL
;
698 time_t orig_ctime
=VTOC(vp
)->c_ctime
;
700 if (ap
->a_name
== NULL
|| ap
->a_name
[0] == '\0') {
701 return (EINVAL
); /* invalid name */
704 if (VNODE_IS_RSRC(vp
)) {
709 if (hfs_hides_xattr(ap
->a_context
, VTOC(vp
), ap
->a_name
, 1) ) { /* 1 == don't take the cnode lock */
710 result
= decmpfs_decompress_file(vp
, VTOCMP(vp
), -1, 1, 0);
714 #endif /* HFS_COMPRESSION */
716 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_METADATA_WRITE_OP
, NSPACE_REARM_NO_ARG
);
718 /* Set the Finder Info. */
719 if (bcmp(ap
->a_name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0) {
720 u_int8_t finderinfo
[32];
721 struct FndrFileInfo
*fip
;
722 void * finderinfo_start
;
723 u_int8_t
*finfo
= NULL
;
725 u_int32_t dateadded
= 0;
726 u_int32_t write_gen_counter
= 0;
728 attrsize
= sizeof(VTOC(vp
)->c_finderinfo
);
730 if ((user_size_t
)uio_resid(uio
) != attrsize
) {
733 /* Grab the new Finder Info data. */
734 if ((result
= uiomove((caddr_t
)&finderinfo
, attrsize
, uio
))) {
737 fip
= (struct FndrFileInfo
*)&finderinfo
;
739 if ((result
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
744 /* Symlink's don't have an external type/creator. */
745 if (vnode_islnk(vp
)) {
746 /* Skip over type/creator fields. */
747 finderinfo_start
= &cp
->c_finderinfo
[8];
750 finderinfo_start
= &cp
->c_finderinfo
[0];
752 * Don't allow the external setting of
753 * file type to kHardLinkFileType.
755 if (fip
->fdType
== SWAP_BE32(kHardLinkFileType
)) {
761 /* Grab the current date added from the cnode */
762 dateadded
= hfs_get_dateadded (cp
);
763 if (S_ISREG(cp
->c_attr
.ca_mode
) || S_ISLNK(cp
->c_attr
.ca_mode
)) {
764 write_gen_counter
= hfs_get_gencount(cp
);
767 /* Zero out the date added field to ignore user's attempts to set it */
768 hfs_zero_hidden_fields(cp
, finderinfo
);
770 if (bcmp(finderinfo_start
, emptyfinfo
, attrsize
)) {
771 /* attr exists and "create" was specified. */
772 if (ap
->a_options
& XATTR_CREATE
) {
777 /* attr doesn't exists and "replace" was specified. */
778 if (ap
->a_options
& XATTR_REPLACE
) {
785 * Now restore the date added to the finderinfo to be written out.
786 * Advance to the 2nd half of the finderinfo to write out the date added
789 * Make sure to endian swap the date added back into big endian. When we used
790 * hfs_get_dateadded above to retrieve it, it swapped into local endianness
791 * for us. But now that we're writing it out, put it back into big endian.
793 finfo
= &finderinfo
[16];
795 if (S_ISREG(cp
->c_attr
.ca_mode
) || S_ISLNK(cp
->c_attr
.ca_mode
)) {
796 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
797 extinfo
->date_added
= OSSwapHostToBigInt32(dateadded
);
798 extinfo
->write_gen_counter
= write_gen_counter
;
799 } else if (S_ISDIR(cp
->c_attr
.ca_mode
)) {
800 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
801 extinfo
->date_added
= OSSwapHostToBigInt32(dateadded
);
804 /* Set the cnode's Finder Info. */
805 if (attrsize
== sizeof(cp
->c_finderinfo
)) {
806 bcopy(&finderinfo
[0], finderinfo_start
, attrsize
);
808 bcopy(&finderinfo
[8], finderinfo_start
, attrsize
);
811 /* Updating finderInfo updates change time and modified time */
812 cp
->c_touch_chgtime
= TRUE
;
813 cp
->c_flag
|= C_MODIFIED
;
816 * Mirror the invisible bit to the UF_HIDDEN flag.
818 * The fdFlags for files and frFlags for folders are both 8 bytes
819 * into the userInfo (the first 16 bytes of the Finder Info). They
820 * are both 16-bit fields.
822 fdFlags
= *((u_int16_t
*) &cp
->c_finderinfo
[8]);
823 if (fdFlags
& OSSwapHostToBigConstInt16(kFinderInvisibleMask
)) {
824 cp
->c_bsdflags
|= UF_HIDDEN
;
826 cp
->c_bsdflags
&= ~UF_HIDDEN
;
829 result
= hfs_update(vp
, FALSE
);
834 /* Write the Resource Fork. */
835 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0) {
836 struct vnode
*rvp
= NULL
;
838 int openunlinked
= 0;
840 if (!vnode_isreg(vp
)) {
843 if ((result
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
847 namelen
= cp
->c_desc
.cd_namelen
;
849 if (RSRC_FORK_EXISTS(cp
)) {
850 /* attr exists and "create" was specified. */
851 if (ap
->a_options
& XATTR_CREATE
) {
856 /* attr doesn't exists and "replace" was specified. */
857 if (ap
->a_options
& XATTR_REPLACE
) {
864 * Note that we could be called on to grab the rsrc fork vnode
865 * for a file that has become open-unlinked.
867 if ((cp
->c_flag
& C_DELETED
) && (namelen
== 0)) {
871 result
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, FALSE
);
876 /* VNOP_WRITE marks cnode as needing a modtime update */
877 result
= VNOP_WRITE(rvp
, uio
, 0, ap
->a_context
);
879 /* if open unlinked, force it inactive */
882 vref
= vnode_ref (rvp
);
888 /* cnode is not open-unlinked, so re-lock cnode to sync */
889 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
895 /* hfs fsync rsrc fork to force to disk and update modtime */
896 result
= hfs_fsync (rvp
, MNT_NOWAIT
, 0, vfs_context_proc (ap
->a_context
));
904 * Standard HFS only supports native FinderInfo and Resource Forks.
906 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
909 attrsize
= uio_resid(uio
);
911 /* Enforce an upper limit. */
912 if (attrsize
> HFS_XATTR_MAXSIZE
) {
918 * Attempt to copy the users attr data before taking any locks,
919 * only if it will be an inline attribute. For larger attributes,
920 * the data will be directly read from the uio.
923 hfsmp
->hfs_max_inline_attrsize
!= 0 &&
924 attrsize
< hfsmp
->hfs_max_inline_attrsize
) {
925 MALLOC(user_data_ptr
, void *, attrsize
, M_TEMP
, M_WAITOK
);
926 if (user_data_ptr
== NULL
) {
931 result
= uiomove((caddr_t
)user_data_ptr
, attrsize
, uio
);
937 result
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
944 * If we're trying to set a non-finderinfo, non-resourcefork EA, then
945 * call the breakout function.
947 result
= hfs_setxattr_internal (cp
, user_data_ptr
, attrsize
, ap
, VTOHFS(vp
), 0);
954 FREE(user_data_ptr
, M_TEMP
);
957 return (result
== btNotFound
? ENOATTR
: MacToVFSError(result
));
962 * hfs_setxattr_internal
964 * Internal function to set non-rsrc, non-finderinfo EAs to either the attribute B-Tree or
967 * See comments from hfs_getxattr_internal on why we need to pass 'hfsmp' and fileid here.
968 * The gist is that we could end up writing to the root folder which may not have a cnode.
971 * 1. cnode 'cp' is locked EXCLUSIVE before calling this function.
972 * 2. data_ptr contains data to be written. If gathering data from userland, this must be
973 * done before calling this function.
974 * 3. If data originates entirely in-kernel, use a null UIO, and ensure the size is less than
975 * hfsmp->hfs_max_inline_attrsize bytes long.
977 int hfs_setxattr_internal (struct cnode
*cp
, caddr_t data_ptr
, size_t attrsize
,
978 struct vnop_setxattr_args
*ap
, struct hfsmount
*hfsmp
,
981 uio_t uio
= ap
->a_uio
;
982 struct vnode
*vp
= ap
->a_vp
;
983 int started_transaction
= 0;
984 struct BTreeIterator
* iterator
= NULL
;
985 struct filefork
*btfile
= NULL
;
986 FSBufferDescriptor btdata
;
987 HFSPlusAttrRecord attrdata
; /* 90 bytes */
988 HFSPlusAttrRecord
*recp
= NULL
;
989 HFSPlusExtentDescriptor
*extentptr
= NULL
;
993 int allocatedblks
= 0;
998 target_id
= cp
->c_fileid
;
1001 if (target_id
!= 1) {
1003 * If we are manipulating something other than
1004 * the root folder (id 1), and do not have a cnode-in-hand,
1005 * then we must already hold the requisite b-tree locks from
1006 * earlier up the call stack. (See hfs_makenode)
1012 /* Start a transaction for our changes. */
1013 if (hfs_start_transaction(hfsmp
) != 0) {
1017 started_transaction
= 1;
1020 * Once we started the transaction, nobody can compete
1021 * with us, so make sure this file is still there.
1023 if ((cp
) && (cp
->c_flag
& C_NOEXISTS
)) {
1029 * If there isn't an attributes b-tree then create one.
1031 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1032 result
= hfs_create_attr_btree(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
,
1033 getnodecount(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
));
1038 if (hfsmp
->hfs_max_inline_attrsize
== 0) {
1039 hfsmp
->hfs_max_inline_attrsize
= getmaxinlineattrsize(hfsmp
->hfs_attribute_vp
);
1043 /* Take exclusive access to the attributes b-tree. */
1044 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1047 /* Build the b-tree key. */
1048 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1049 if (iterator
== NULL
) {
1053 bzero(iterator
, sizeof(*iterator
));
1054 result
= hfs_buildattrkey(target_id
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
1059 /* Preflight for replace/create semantics. */
1060 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1061 btdata
.bufferAddress
= &attrdata
;
1062 btdata
.itemSize
= sizeof(attrdata
);
1063 btdata
.itemCount
= 1;
1064 exists
= BTSearchRecord(btfile
, iterator
, &btdata
, NULL
, NULL
) == 0;
1066 /* Replace requires that the attribute already exists. */
1067 if ((ap
->a_options
& XATTR_REPLACE
) && !exists
) {
1071 /* Create requires that the attribute doesn't exist. */
1072 if ((ap
->a_options
& XATTR_CREATE
) && exists
) {
1077 /* If it won't fit inline then use extent-based attributes. */
1078 if (attrsize
> hfsmp
->hfs_max_inline_attrsize
) {
1079 size_t extentbufsize
;
1082 u_int32_t
*keystartblk
;
1087 * setxattrs originating from in-kernel are not supported if they are bigger
1088 * than the inline max size. Just return ENOATTR and force them to do it with a
1095 /* Get some blocks. */
1096 blkcnt
= howmany(attrsize
, hfsmp
->blockSize
);
1097 extentbufsize
= blkcnt
* sizeof(HFSPlusExtentDescriptor
);
1098 extentbufsize
= roundup(extentbufsize
, sizeof(HFSPlusExtentRecord
));
1099 MALLOC(extentptr
, HFSPlusExtentDescriptor
*, extentbufsize
, M_TEMP
, M_WAITOK
);
1100 if (extentptr
== NULL
) {
1104 bzero(extentptr
, extentbufsize
);
1105 result
= alloc_attr_blks(hfsmp
, attrsize
, extentbufsize
, extentptr
, &allocatedblks
);
1108 goto exit
; /* no more space */
1110 /* Copy data into the blocks. */
1111 result
= write_attr_data(hfsmp
, uio
, attrsize
, extentptr
);
1114 const char *name
= vnode_getname(vp
);
1115 printf("hfs_setxattr: write_attr_data vol=%s err (%d) %s:%s\n",
1116 hfsmp
->vcbVN
, result
, name
? name
: "", ap
->a_name
);
1118 vnode_putname(name
);
1123 /* Now remove any previous attribute. */
1125 result
= remove_attribute_records(hfsmp
, iterator
);
1128 const char *name
= vnode_getname(vp
);
1129 printf("hfs_setxattr: remove_attribute_records vol=%s err (%d) %s:%s\n",
1130 hfsmp
->vcbVN
, result
, name
? name
: "", ap
->a_name
);
1132 vnode_putname(name
);
1137 /* Create attribute fork data record. */
1138 MALLOC(recp
, HFSPlusAttrRecord
*, sizeof(HFSPlusAttrRecord
), M_TEMP
, M_WAITOK
);
1143 btdata
.bufferAddress
= recp
;
1144 btdata
.itemCount
= 1;
1145 btdata
.itemSize
= sizeof(HFSPlusAttrForkData
);
1147 recp
->recordType
= kHFSPlusAttrForkData
;
1148 recp
->forkData
.reserved
= 0;
1149 recp
->forkData
.theFork
.logicalSize
= attrsize
;
1150 recp
->forkData
.theFork
.clumpSize
= 0;
1151 recp
->forkData
.theFork
.totalBlocks
= blkcnt
;
1152 bcopy(extentptr
, recp
->forkData
.theFork
.extents
, sizeof(HFSPlusExtentRecord
));
1154 (void) hfs_buildattrkey(target_id
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
1156 result
= BTInsertRecord(btfile
, iterator
, &btdata
, btdata
.itemSize
);
1158 printf ("hfs_setxattr: BTInsertRecord(): vol=%s %d,%s err=%d\n",
1159 hfsmp
->vcbVN
, target_id
, ap
->a_name
, result
);
1162 extentblks
= count_extent_blocks(blkcnt
, recp
->forkData
.theFork
.extents
);
1163 blkcnt
-= extentblks
;
1164 keystartblk
= &((HFSPlusAttrKey
*)&iterator
->key
)->startBlock
;
1167 /* Create overflow extents as needed. */
1168 while (blkcnt
> 0) {
1169 /* Initialize the key and record. */
1170 *keystartblk
+= (u_int32_t
)extentblks
;
1171 btdata
.itemSize
= sizeof(HFSPlusAttrExtents
);
1172 recp
->recordType
= kHFSPlusAttrExtents
;
1173 recp
->overflowExtents
.reserved
= 0;
1175 /* Copy the next set of extents. */
1176 i
+= kHFSPlusExtentDensity
;
1177 bcopy(&extentptr
[i
], recp
->overflowExtents
.extents
, sizeof(HFSPlusExtentRecord
));
1179 result
= BTInsertRecord(btfile
, iterator
, &btdata
, btdata
.itemSize
);
1181 printf ("hfs_setxattr: BTInsertRecord() overflow: vol=%s %d,%s err=%d\n",
1182 hfsmp
->vcbVN
, target_id
, ap
->a_name
, result
);
1185 extentblks
= count_extent_blocks(blkcnt
, recp
->overflowExtents
.extents
);
1186 blkcnt
-= extentblks
;
1188 } else { /* Inline data */
1190 result
= remove_attribute_records(hfsmp
, iterator
);
1196 /* Calculate size of record rounded up to multiple of 2 bytes. */
1197 btdata
.itemSize
= sizeof(HFSPlusAttrData
) - 2 + attrsize
+ ((attrsize
& 1) ? 1 : 0);
1198 MALLOC(recp
, HFSPlusAttrRecord
*, btdata
.itemSize
, M_TEMP
, M_WAITOK
);
1203 recp
->recordType
= kHFSPlusAttrInlineData
;
1204 recp
->attrData
.reserved
[0] = 0;
1205 recp
->attrData
.reserved
[1] = 0;
1206 recp
->attrData
.attrSize
= attrsize
;
1208 /* Copy in the attribute data (if any). */
1211 bcopy(data_ptr
, &recp
->attrData
.attrData
, attrsize
);
1214 * A null UIO meant it originated in-kernel. If they didn't supply data_ptr
1215 * then deny the copy operation.
1221 result
= uiomove((caddr_t
)&recp
->attrData
.attrData
, attrsize
, uio
);
1229 (void) hfs_buildattrkey(target_id
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
1231 btdata
.bufferAddress
= recp
;
1232 btdata
.itemCount
= 1;
1233 result
= BTInsertRecord(btfile
, iterator
, &btdata
, btdata
.itemSize
);
1237 if (btfile
&& started_transaction
) {
1238 (void) BTFlushPath(btfile
);
1241 hfs_systemfile_unlock(hfsmp
, lockflags
);
1246 /* Setting an attribute only updates change time and not
1247 * modified time of the file.
1249 cp
->c_touch_chgtime
= TRUE
;
1250 cp
->c_attr
.ca_recflags
|= kHFSHasAttributesMask
;
1251 if ((bcmp(ap
->a_name
, KAUTH_FILESEC_XATTR
, sizeof(KAUTH_FILESEC_XATTR
)) == 0)) {
1252 cp
->c_attr
.ca_recflags
|= kHFSHasSecurityMask
;
1254 (void) hfs_update(vp
, 0);
1257 if (started_transaction
) {
1258 if (result
&& allocatedblks
) {
1259 free_attr_blks(hfsmp
, allocatedblks
, extentptr
);
1261 hfs_end_transaction(hfsmp
);
1268 FREE(extentptr
, M_TEMP
);
1271 FREE(iterator
, M_TEMP
);
1281 * Remove an extended attribute.
1284 hfs_vnop_removexattr(struct vnop_removexattr_args
*ap
)
1286 struct vnop_removexattr_args {
1287 struct vnodeop_desc *a_desc;
1291 vfs_context_t a_context;
1295 struct vnode
*vp
= ap
->a_vp
;
1296 struct cnode
*cp
= VTOC(vp
);
1297 struct hfsmount
*hfsmp
;
1298 struct BTreeIterator
* iterator
= NULL
;
1301 time_t orig_ctime
=VTOC(vp
)->c_ctime
;
1303 if (ap
->a_name
== NULL
|| ap
->a_name
[0] == '\0') {
1304 return (EINVAL
); /* invalid name */
1307 if (VNODE_IS_RSRC(vp
)) {
1312 if (hfs_hides_xattr(ap
->a_context
, VTOC(vp
), ap
->a_name
, 1) && !(ap
->a_options
& XATTR_SHOWCOMPRESSION
)) {
1315 #endif /* HFS_COMPRESSION */
1317 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_METADATA_DELETE_OP
, NSPACE_REARM_NO_ARG
);
1319 /* If Resource Fork is non-empty then truncate it. */
1320 if (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0) {
1321 struct vnode
*rvp
= NULL
;
1323 if ( !vnode_isreg(vp
) ) {
1326 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
1329 if ( !RSRC_FORK_EXISTS(cp
)) {
1333 result
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, FALSE
);
1339 hfs_lock_truncate(VTOC(rvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
1340 if ((result
= hfs_lock(VTOC(rvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
1341 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
1346 /* Start a transaction for encapsulating changes in
1347 * hfs_truncate() and hfs_update()
1349 if ((result
= hfs_start_transaction(hfsmp
))) {
1350 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
1356 result
= hfs_truncate(rvp
, (off_t
)0, IO_NDELAY
, 0, 0, ap
->a_context
);
1358 cp
->c_touch_chgtime
= TRUE
;
1359 cp
->c_flag
|= C_MODIFIED
;
1360 result
= hfs_update(vp
, FALSE
);
1363 hfs_end_transaction(hfsmp
);
1364 hfs_unlock_truncate(VTOC(rvp
), HFS_LOCK_DEFAULT
);
1365 hfs_unlock(VTOC(rvp
));
1370 /* Clear out the Finder Info. */
1371 if (bcmp(ap
->a_name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0) {
1372 void * finderinfo_start
;
1373 int finderinfo_size
;
1374 u_int8_t finderinfo
[32];
1375 u_int32_t date_added
, write_gen_counter
;
1376 u_int8_t
*finfo
= NULL
;
1378 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
1382 /* Use the local copy to store our temporary changes. */
1383 bcopy(cp
->c_finderinfo
, finderinfo
, sizeof(finderinfo
));
1386 /* Zero out the date added field in the local copy */
1387 hfs_zero_hidden_fields (cp
, finderinfo
);
1389 /* Don't expose a symlink's private type/creator. */
1390 if (vnode_islnk(vp
)) {
1391 struct FndrFileInfo
*fip
;
1393 fip
= (struct FndrFileInfo
*)&finderinfo
;
1398 /* Do the byte compare against the local copy */
1399 if (bcmp(finderinfo
, emptyfinfo
, sizeof(emptyfinfo
)) == 0) {
1405 * If there was other content, zero out everything except
1406 * type/creator and date added. First, save the date added.
1408 finfo
= cp
->c_finderinfo
;
1410 if (S_ISREG(cp
->c_attr
.ca_mode
) || S_ISLNK(cp
->c_attr
.ca_mode
)) {
1411 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
1412 date_added
= extinfo
->date_added
;
1413 write_gen_counter
= extinfo
->write_gen_counter
;
1414 } else if (S_ISDIR(cp
->c_attr
.ca_mode
)) {
1415 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
1416 date_added
= extinfo
->date_added
;
1419 if (vnode_islnk(vp
)) {
1420 /* Ignore type/creator */
1421 finderinfo_start
= &cp
->c_finderinfo
[8];
1422 finderinfo_size
= sizeof(cp
->c_finderinfo
) - 8;
1424 finderinfo_start
= &cp
->c_finderinfo
[0];
1425 finderinfo_size
= sizeof(cp
->c_finderinfo
);
1427 bzero(finderinfo_start
, finderinfo_size
);
1430 /* Now restore the date added */
1431 if (S_ISREG(cp
->c_attr
.ca_mode
) || S_ISLNK(cp
->c_attr
.ca_mode
)) {
1432 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
1433 extinfo
->date_added
= date_added
;
1434 extinfo
->write_gen_counter
= write_gen_counter
;
1435 } else if (S_ISDIR(cp
->c_attr
.ca_mode
)) {
1436 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
1437 extinfo
->date_added
= date_added
;
1440 /* Updating finderInfo updates change time and modified time */
1441 cp
->c_touch_chgtime
= TRUE
;
1442 cp
->c_flag
|= C_MODIFIED
;
1443 hfs_update(vp
, FALSE
);
1450 * Standard HFS only supports native FinderInfo and Resource Forks.
1452 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1455 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1459 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1460 if (iterator
== NULL
) {
1463 bzero(iterator
, sizeof(*iterator
));
1465 if ((result
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
1469 result
= hfs_buildattrkey(cp
->c_fileid
, ap
->a_name
, (HFSPlusAttrKey
*)&iterator
->key
);
1474 if (hfs_start_transaction(hfsmp
) != 0) {
1478 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1480 result
= remove_attribute_records(hfsmp
, iterator
);
1482 hfs_systemfile_unlock(hfsmp
, lockflags
);
1485 cp
->c_touch_chgtime
= TRUE
;
1487 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
1489 /* If no more attributes exist, clear attribute bit */
1490 result
= file_attribute_exist(hfsmp
, cp
->c_fileid
);
1492 cp
->c_attr
.ca_recflags
&= ~kHFSHasAttributesMask
;
1494 if (result
== EEXIST
) {
1498 hfs_systemfile_unlock(hfsmp
, lockflags
);
1500 /* If ACL was removed, clear security bit */
1501 if ((bcmp(ap
->a_name
, KAUTH_FILESEC_XATTR
, sizeof(KAUTH_FILESEC_XATTR
)) == 0)) {
1502 cp
->c_attr
.ca_recflags
&= ~kHFSHasSecurityMask
;
1504 (void) hfs_update(vp
, 0);
1507 hfs_end_transaction(hfsmp
);
1511 FREE(iterator
, M_TEMP
);
1512 return MacToVFSError(result
);
1515 /* Check if any attribute record exist for given fileID. This function
1516 * is called by hfs_vnop_removexattr to determine if it should clear the
1517 * attribute bit in the catalog record or not.
1519 * Note - you must acquire a shared lock on the attribute btree before
1520 * calling this function.
1523 * EEXIST - If attribute record was found
1524 * 0 - Attribute was not found
1525 * (other) - Other error (such as EIO)
1528 file_attribute_exist(struct hfsmount
*hfsmp
, uint32_t fileID
)
1530 HFSPlusAttrKey
*key
;
1531 struct BTreeIterator
* iterator
= NULL
;
1532 struct filefork
*btfile
;
1535 // if there's no attribute b-tree we sure as heck
1536 // can't have any attributes!
1537 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1541 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1542 if (iterator
== NULL
) {
1546 bzero(iterator
, sizeof(*iterator
));
1547 key
= (HFSPlusAttrKey
*)&iterator
->key
;
1549 result
= hfs_buildattrkey(fileID
, NULL
, key
);
1554 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1555 result
= BTSearchRecord(btfile
, iterator
, NULL
, NULL
, NULL
);
1556 if (result
&& (result
!= btNotFound
)) {
1560 result
= BTIterateRecord(btfile
, kBTreeNextRecord
, iterator
, NULL
, NULL
);
1561 /* If no next record was found or fileID for next record did not match,
1562 * no more attributes exist for this fileID
1564 if ((result
&& (result
== btNotFound
)) || (key
->fileID
!= fileID
)) {
1572 FREE(iterator
, M_TEMP
);
1579 * Remove all the records for a given attribute.
1581 * - Used by hfs_vnop_removexattr, hfs_vnop_setxattr and hfs_removeallattr.
1582 * - A transaction must have been started.
1583 * - The Attribute b-tree file must be locked exclusive.
1584 * - The Allocation Bitmap file must be locked exclusive.
1585 * - The iterator key must be initialized.
1588 remove_attribute_records(struct hfsmount
*hfsmp
, BTreeIterator
* iterator
)
1590 struct filefork
*btfile
;
1591 FSBufferDescriptor btdata
;
1592 HFSPlusAttrRecord attrdata
; /* 90 bytes */
1596 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1598 btdata
.bufferAddress
= &attrdata
;
1599 btdata
.itemSize
= sizeof(attrdata
);
1600 btdata
.itemCount
= 1;
1601 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
1603 goto exit
; /* no records. */
1606 * Free the blocks from extent based attributes.
1608 * Note that the block references (btree records) are removed
1609 * before releasing the blocks in the allocation bitmap.
1611 if (attrdata
.recordType
== kHFSPlusAttrForkData
) {
1614 u_int32_t
*keystartblk
;
1616 if (datasize
< sizeof(HFSPlusAttrForkData
)) {
1617 printf("hfs: remove_attribute_records: bad record size %d (expecting %lu)\n", datasize
, sizeof(HFSPlusAttrForkData
));
1619 totalblks
= attrdata
.forkData
.theFork
.totalBlocks
;
1621 /* Process the first 8 extents. */
1622 extentblks
= count_extent_blocks(totalblks
, attrdata
.forkData
.theFork
.extents
);
1623 if (extentblks
> totalblks
)
1624 panic("hfs: remove_attribute_records: corruption...");
1625 if (BTDeleteRecord(btfile
, iterator
) == 0) {
1626 free_attr_blks(hfsmp
, extentblks
, attrdata
.forkData
.theFork
.extents
);
1628 totalblks
-= extentblks
;
1629 keystartblk
= &((HFSPlusAttrKey
*)&iterator
->key
)->startBlock
;
1631 /* Process any overflow extents. */
1633 *keystartblk
+= (u_int32_t
)extentblks
;
1635 result
= BTSearchRecord(btfile
, iterator
, &btdata
, &datasize
, NULL
);
1637 (attrdata
.recordType
!= kHFSPlusAttrExtents
) ||
1638 (datasize
< sizeof(HFSPlusAttrExtents
))) {
1639 printf("hfs: remove_attribute_records: BTSearchRecord: vol=%s, err=%d (%d), totalblks %d\n",
1640 hfsmp
->vcbVN
, MacToVFSError(result
), attrdata
.recordType
!= kHFSPlusAttrExtents
, totalblks
);
1642 break; /* break from while */
1644 /* Process the next 8 extents. */
1645 extentblks
= count_extent_blocks(totalblks
, attrdata
.overflowExtents
.extents
);
1646 if (extentblks
> totalblks
)
1647 panic("hfs: remove_attribute_records: corruption...");
1648 if (BTDeleteRecord(btfile
, iterator
) == 0) {
1649 free_attr_blks(hfsmp
, extentblks
, attrdata
.overflowExtents
.extents
);
1651 totalblks
-= extentblks
;
1654 result
= BTDeleteRecord(btfile
, iterator
);
1656 (void) BTFlushPath(btfile
);
1658 return (result
== btNotFound
? ENOATTR
: MacToVFSError(result
));
1663 * Retrieve the list of extended attribute names.
1666 hfs_vnop_listxattr(struct vnop_listxattr_args
*ap
)
1668 struct vnop_listxattr_args {
1669 struct vnodeop_desc *a_desc;
1674 vfs_context_t a_context;
1677 struct vnode
*vp
= ap
->a_vp
;
1678 struct cnode
*cp
= VTOC(vp
);
1679 struct hfsmount
*hfsmp
;
1680 uio_t uio
= ap
->a_uio
;
1681 struct BTreeIterator
* iterator
= NULL
;
1682 struct filefork
*btfile
;
1683 struct listattr_callback_state state
;
1684 user_addr_t user_start
= 0;
1685 user_size_t user_len
= 0;
1688 u_int8_t finderinfo
[32];
1691 if (VNODE_IS_RSRC(vp
)) {
1696 int compressed
= hfs_file_is_compressed(cp
, 1); /* 1 == don't take the cnode lock */
1697 #endif /* HFS_COMPRESSION */
1703 * Take the truncate lock; this serializes us against the ioctl
1704 * to truncate data & reset the decmpfs state
1705 * in the compressed file handler.
1707 hfs_lock_truncate(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
1709 /* Now the regular cnode lock (shared) */
1710 if ((result
= hfs_lock(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
))) {
1711 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
1716 * Make a copy of the cnode's finderinfo to a local so we can
1717 * zero out the date added field. Also zero out the private type/creator
1720 bcopy(cp
->c_finderinfo
, finderinfo
, sizeof(finderinfo
));
1721 hfs_zero_hidden_fields (cp
, finderinfo
);
1723 /* Don't expose a symlink's private type/creator. */
1724 if (vnode_islnk(vp
)) {
1725 struct FndrFileInfo
*fip
;
1727 fip
= (struct FndrFileInfo
*)&finderinfo
;
1733 /* If Finder Info is non-empty then export it's name. */
1734 if (bcmp(finderinfo
, emptyfinfo
, sizeof(emptyfinfo
)) != 0) {
1736 *ap
->a_size
+= sizeof(XATTR_FINDERINFO_NAME
);
1737 } else if ((user_size_t
)uio_resid(uio
) < sizeof(XATTR_FINDERINFO_NAME
)) {
1741 result
= uiomove(XATTR_FINDERINFO_NAME
,
1742 sizeof(XATTR_FINDERINFO_NAME
), uio
);
1747 /* If Resource Fork is non-empty then export it's name. */
1748 if (S_ISREG(cp
->c_mode
) && RSRC_FORK_EXISTS(cp
)) {
1750 if ((ap
->a_options
& XATTR_SHOWCOMPRESSION
) ||
1752 !hfs_hides_rsrc(ap
->a_context
, VTOC(vp
), 1) /* 1 == don't take the cnode lock */
1754 #endif /* HFS_COMPRESSION */
1757 *ap
->a_size
+= sizeof(XATTR_RESOURCEFORK_NAME
);
1758 } else if ((user_size_t
)uio_resid(uio
) < sizeof(XATTR_RESOURCEFORK_NAME
)) {
1762 result
= uiomove(XATTR_RESOURCEFORK_NAME
,
1763 sizeof(XATTR_RESOURCEFORK_NAME
), uio
);
1770 * Standard HFS only supports native FinderInfo and Resource Forks.
1771 * Return at this point.
1773 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1777 /* Bail if we don't have any extended attributes. */
1778 if ((hfsmp
->hfs_attribute_vp
== NULL
) ||
1779 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) == 0) {
1783 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1785 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1786 if (iterator
== NULL
) {
1790 bzero(iterator
, sizeof(*iterator
));
1791 result
= hfs_buildattrkey(cp
->c_fileid
, NULL
, (HFSPlusAttrKey
*)&iterator
->key
);
1796 * Lock the user's buffer here so that we won't fault on
1797 * it in uiomove while holding the attributes b-tree lock.
1799 if (uio
&& uio_isuserspace(uio
)) {
1800 user_start
= uio_curriovbase(uio
);
1801 user_len
= uio_curriovlen(uio
);
1803 if ((result
= vslock(user_start
, user_len
)) != 0) {
1808 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
1810 result
= BTSearchRecord(btfile
, iterator
, NULL
, NULL
, NULL
);
1811 if (result
&& result
!= btNotFound
) {
1812 hfs_systemfile_unlock(hfsmp
, lockflags
);
1816 state
.fileID
= cp
->c_fileid
;
1821 state
.showcompressed
= !compressed
|| ap
->a_options
& XATTR_SHOWCOMPRESSION
;
1822 state
.ctx
= ap
->a_context
;
1824 #endif /* HFS_COMPRESSION */
1827 * Process entries starting just after iterator->key.
1829 result
= BTIterateRecords(btfile
, kBTreeNextRecord
, iterator
,
1830 (IterateCallBackProcPtr
)listattr_callback
, &state
);
1831 hfs_systemfile_unlock(hfsmp
, lockflags
);
1833 *ap
->a_size
+= state
.size
;
1836 if (state
.result
|| result
== btNotFound
)
1837 result
= state
.result
;
1841 vsunlock(user_start
, user_len
, TRUE
);
1844 FREE(iterator
, M_TEMP
);
1847 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
1849 return MacToVFSError(result
);
1854 * Callback - called for each attribute record
1857 listattr_callback(const HFSPlusAttrKey
*key
, __unused
const HFSPlusAttrData
*data
, struct listattr_callback_state
*state
)
1859 char attrname
[XATTR_MAXNAMELEN
+ 1];
1863 if (state
->fileID
!= key
->fileID
) {
1865 return (0); /* stop */
1868 * Skip over non-primary keys
1870 if (key
->startBlock
!= 0) {
1871 return (1); /* continue */
1874 /* Convert the attribute name into UTF-8. */
1875 result
= utf8_encodestr(key
->attrName
, key
->attrNameLen
* sizeof(UniChar
),
1876 (u_int8_t
*)attrname
, (size_t *)&bytecount
, sizeof(attrname
), '/', 0);
1878 state
->result
= result
;
1879 return (0); /* stop */
1881 bytecount
++; /* account for null termination char */
1883 if (xattr_protected(attrname
))
1884 return (1); /* continue */
1887 if (!state
->showcompressed
&& hfs_hides_xattr(state
->ctx
, VTOC(state
->vp
), attrname
, 1) ) /* 1 == don't take the cnode lock */
1888 return 1; /* continue */
1889 #endif /* HFS_COMPRESSION */
1891 if (state
->uio
== NULL
) {
1892 state
->size
+= bytecount
;
1894 if (bytecount
> uio_resid(state
->uio
)) {
1895 state
->result
= ERANGE
;
1896 return (0); /* stop */
1898 result
= uiomove((caddr_t
) attrname
, bytecount
, state
->uio
);
1900 state
->result
= result
;
1901 return (0); /* stop */
1904 return (1); /* continue */
1908 * Remove all the attributes from a cnode.
1910 * This function creates/ends its own transaction so that each
1911 * attribute is deleted in its own transaction (to avoid having
1912 * a transaction grow too large).
1914 * This function takes the necessary locks on the attribute
1915 * b-tree file and the allocation (bitmap) file.
1918 hfs_removeallattr(struct hfsmount
*hfsmp
, u_int32_t fileid
)
1920 BTreeIterator
*iterator
= NULL
;
1921 HFSPlusAttrKey
*key
;
1922 struct filefork
*btfile
;
1923 int result
, lockflags
;
1925 if (hfsmp
->hfs_attribute_vp
== NULL
) {
1928 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
1930 MALLOC(iterator
, BTreeIterator
*, sizeof(BTreeIterator
), M_TEMP
, M_WAITOK
);
1931 if (iterator
== NULL
) {
1934 bzero(iterator
, sizeof(BTreeIterator
));
1935 key
= (HFSPlusAttrKey
*)&iterator
->key
;
1937 /* Loop until there are no more attributes for this file id */
1939 if (hfs_start_transaction(hfsmp
) != 0) {
1944 /* Lock the attribute b-tree and the allocation (bitmap) files */
1945 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1948 * Go to first possible attribute key/record pair
1950 (void) hfs_buildattrkey(fileid
, NULL
, key
);
1951 result
= BTIterateRecord(btfile
, kBTreeNextRecord
, iterator
, NULL
, NULL
);
1952 if (result
|| key
->fileID
!= fileid
) {
1953 hfs_systemfile_unlock(hfsmp
, lockflags
);
1954 hfs_end_transaction(hfsmp
);
1957 result
= remove_attribute_records(hfsmp
, iterator
);
1959 #if HFS_XATTR_VERBOSE
1961 printf("hfs_removeallattr: unexpected err %d\n", result
);
1964 hfs_systemfile_unlock(hfsmp
, lockflags
);
1965 hfs_end_transaction(hfsmp
);
1970 FREE(iterator
, M_TEMP
);
1971 return (result
== btNotFound
? 0: MacToVFSError(result
));
1976 hfs_xattr_init(struct hfsmount
* hfsmp
)
1979 * If there isn't an attributes b-tree then create one.
1981 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
) &&
1982 (hfsmp
->hfs_attribute_vp
== NULL
) &&
1983 !(hfsmp
->hfs_flags
& HFS_READ_ONLY
)) {
1984 (void) hfs_create_attr_btree(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
,
1985 getnodecount(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
));
1987 if (hfsmp
->hfs_attribute_vp
)
1988 hfsmp
->hfs_max_inline_attrsize
= getmaxinlineattrsize(hfsmp
->hfs_attribute_vp
);
1992 * Enable/Disable volume attributes stored as EA for root file system.
1993 * Supported attributes are -
1994 * 1. Extent-based Extended Attributes
1997 hfs_set_volxattr(struct hfsmount
*hfsmp
, unsigned int xattrtype
, int state
)
1999 struct BTreeIterator
* iterator
= NULL
;
2000 struct filefork
*btfile
;
2004 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
2007 if (xattrtype
!= HFS_SET_XATTREXTENTS_STATE
) {
2012 * If there isn't an attributes b-tree then create one.
2014 if (hfsmp
->hfs_attribute_vp
== NULL
) {
2015 result
= hfs_create_attr_btree(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
,
2016 getnodecount(hfsmp
, ATTRIBUTE_FILE_NODE_SIZE
));
2022 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
2023 if (iterator
== NULL
) {
2026 bzero(iterator
, sizeof(*iterator
));
2029 * Build a b-tree key.
2030 * We use the root's parent id (1) to hold this volume attribute.
2032 (void) hfs_buildattrkey(kHFSRootParentID
, XATTR_XATTREXTENTS_NAME
,
2033 (HFSPlusAttrKey
*)&iterator
->key
);
2035 /* Start a transaction for our changes. */
2036 if (hfs_start_transaction(hfsmp
) != 0) {
2040 btfile
= VTOF(hfsmp
->hfs_attribute_vp
);
2042 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
2045 /* Remove the attribute. */
2046 result
= BTDeleteRecord(btfile
, iterator
);
2047 if (result
== btNotFound
)
2050 FSBufferDescriptor btdata
;
2051 HFSPlusAttrData attrdata
;
2054 datasize
= sizeof(attrdata
);
2055 btdata
.bufferAddress
= &attrdata
;
2056 btdata
.itemSize
= datasize
;
2057 btdata
.itemCount
= 1;
2058 attrdata
.recordType
= kHFSPlusAttrInlineData
;
2059 attrdata
.reserved
[0] = 0;
2060 attrdata
.reserved
[1] = 0;
2061 attrdata
.attrSize
= 2;
2062 attrdata
.attrData
[0] = 0;
2063 attrdata
.attrData
[1] = 0;
2065 /* Insert the attribute. */
2066 result
= BTInsertRecord(btfile
, iterator
, &btdata
, datasize
);
2067 if (result
== btExists
)
2070 (void) BTFlushPath(btfile
);
2072 hfs_systemfile_unlock(hfsmp
, lockflags
);
2074 /* Finish the transaction of our changes. */
2075 hfs_end_transaction(hfsmp
);
2077 /* Update the state in the mount point */
2078 hfs_lock_mount (hfsmp
);
2080 hfsmp
->hfs_flags
&= ~HFS_XATTR_EXTENTS
;
2082 hfsmp
->hfs_flags
|= HFS_XATTR_EXTENTS
;
2084 hfs_unlock_mount (hfsmp
);
2088 FREE(iterator
, M_TEMP
);
2090 return MacToVFSError(result
);
2095 * hfs_attrkeycompare - compare two attribute b-tree keys.
2097 * The name portion of the key is compared using a 16-bit binary comparison.
2098 * This is called from the b-tree code.
2102 hfs_attrkeycompare(HFSPlusAttrKey
*searchKey
, HFSPlusAttrKey
*trialKey
)
2104 u_int32_t searchFileID
, trialFileID
;
2107 searchFileID
= searchKey
->fileID
;
2108 trialFileID
= trialKey
->fileID
;
2111 if (searchFileID
> trialFileID
) {
2113 } else if (searchFileID
< trialFileID
) {
2116 u_int16_t
* str1
= &searchKey
->attrName
[0];
2117 u_int16_t
* str2
= &trialKey
->attrName
[0];
2118 int length1
= searchKey
->attrNameLen
;
2119 int length2
= trialKey
->attrNameLen
;
2123 if (length1
< length2
) {
2126 } else if (length1
> length2
) {
2149 * Names are equal; compare startBlock
2151 if (searchKey
->startBlock
== trialKey
->startBlock
) {
2154 return (searchKey
->startBlock
< trialKey
->startBlock
? -1 : 1);
2163 * hfs_buildattrkey - build an Attribute b-tree key
2167 hfs_buildattrkey(u_int32_t fileID
, const char *attrname
, HFSPlusAttrKey
*key
)
2170 size_t unicodeBytes
= 0;
2172 if (attrname
!= NULL
) {
2174 * Convert filename from UTF-8 into Unicode
2176 result
= utf8_decodestr((const u_int8_t
*)attrname
, strlen(attrname
), key
->attrName
,
2177 &unicodeBytes
, sizeof(key
->attrName
), 0, 0);
2179 if (result
!= ENAMETOOLONG
)
2180 result
= EINVAL
; /* name has invalid characters */
2183 key
->attrNameLen
= unicodeBytes
/ sizeof(UniChar
);
2184 key
->keyLength
= kHFSPlusAttrKeyMinimumLength
+ unicodeBytes
;
2186 key
->attrNameLen
= 0;
2187 key
->keyLength
= kHFSPlusAttrKeyMinimumLength
;
2190 key
->fileID
= fileID
;
2191 key
->startBlock
= 0;
2197 * getnodecount - calculate starting node count for attributes b-tree.
2200 getnodecount(struct hfsmount
*hfsmp
, size_t nodesize
)
2202 u_int64_t freebytes
;
2203 u_int64_t calcbytes
;
2206 * 10.4: Scale base on current catalog file size (20 %) up to 20 MB.
2207 * 10.5: Attempt to be as big as the catalog clump size.
2209 * Use no more than 10 % of the remaining free space.
2211 freebytes
= (u_int64_t
)hfs_freeblks(hfsmp
, 0) * (u_int64_t
)hfsmp
->blockSize
;
2213 calcbytes
= MIN(hfsmp
->hfs_catalog_cp
->c_datafork
->ff_size
/ 5, 20 * 1024 * 1024);
2215 calcbytes
= MAX(calcbytes
, hfsmp
->hfs_catalog_cp
->c_datafork
->ff_clumpsize
);
2217 calcbytes
= MIN(calcbytes
, freebytes
/ 10);
2219 return (MAX(2, (int)(calcbytes
/ nodesize
)));
2224 * getmaxinlineattrsize - calculate maximum inline attribute size.
2226 * This yields 3,802 bytes for an 8K node size.
2229 getmaxinlineattrsize(struct vnode
* attrvp
)
2231 struct BTreeInfoRec btinfo
;
2232 size_t nodesize
= ATTRIBUTE_FILE_NODE_SIZE
;
2235 if (attrvp
!= NULL
) {
2236 (void) hfs_lock(VTOC(attrvp
), HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
2237 if (BTGetInformation(VTOF(attrvp
), 0, &btinfo
) == 0)
2238 nodesize
= btinfo
.nodeSize
;
2239 hfs_unlock(VTOC(attrvp
));
2242 maxsize
-= sizeof(BTNodeDescriptor
); /* minus node descriptor */
2243 maxsize
-= 3 * sizeof(u_int16_t
); /* minus 3 index slots */
2244 maxsize
/= 2; /* 2 key/rec pairs minumum */
2245 maxsize
-= sizeof(HFSPlusAttrKey
); /* minus maximum key size */
2246 maxsize
-= sizeof(HFSPlusAttrData
) - 2; /* minus data header */
2247 maxsize
&= 0xFFFFFFFE; /* multiple of 2 bytes */
2253 * Initialize vnode for attribute data I/O.
2257 * - the attrdata vnode is initialized as hfsmp->hfs_attrdata_vp
2258 * - an iocount is taken on the attrdata vnode which exists
2259 * for the entire duration of the mount. It is only dropped
2261 * - the attrdata cnode is not locked
2264 * - returns non-zero value
2265 * - the caller does not have to worry about any locks or references
2267 int init_attrdata_vnode(struct hfsmount
*hfsmp
)
2271 struct cat_desc cat_desc
;
2272 struct cat_attr cat_attr
;
2273 struct cat_fork cat_fork
;
2274 int newvnode_flags
= 0;
2276 bzero(&cat_desc
, sizeof(cat_desc
));
2277 cat_desc
.cd_parentcnid
= kHFSRootParentID
;
2278 cat_desc
.cd_nameptr
= (const u_int8_t
*)hfs_attrdatafilename
;
2279 cat_desc
.cd_namelen
= strlen(hfs_attrdatafilename
);
2280 cat_desc
.cd_cnid
= kHFSAttributeDataFileID
;
2281 /* Tag vnode as system file, note that we can still use cluster I/O */
2282 cat_desc
.cd_flags
|= CD_ISMETA
;
2284 bzero(&cat_attr
, sizeof(cat_attr
));
2285 cat_attr
.ca_linkcount
= 1;
2286 cat_attr
.ca_mode
= S_IFREG
;
2287 cat_attr
.ca_fileid
= cat_desc
.cd_cnid
;
2288 cat_attr
.ca_blocks
= hfsmp
->totalBlocks
;
2291 * The attribute data file is a virtual file that spans the
2292 * entire file system space.
2294 * Each extent-based attribute occupies a unique portion of
2295 * in this virtual file. The cluster I/O is done using actual
2296 * allocation block offsets so no additional mapping is needed
2297 * for the VNOP_BLOCKMAP call.
2299 * This approach allows the attribute data to be cached without
2300 * incurring the high cost of using a separate vnode per attribute.
2302 * Since we need to acquire the attribute b-tree file lock anyways,
2303 * the virtual file doesn't introduce any additional serialization.
2305 bzero(&cat_fork
, sizeof(cat_fork
));
2306 cat_fork
.cf_size
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
2307 cat_fork
.cf_blocks
= hfsmp
->totalBlocks
;
2308 cat_fork
.cf_extents
[0].startBlock
= 0;
2309 cat_fork
.cf_extents
[0].blockCount
= cat_fork
.cf_blocks
;
2311 result
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cat_desc
, 0, &cat_attr
,
2312 &cat_fork
, &vp
, &newvnode_flags
);
2314 hfsmp
->hfs_attrdata_vp
= vp
;
2315 hfs_unlock(VTOC(vp
));
2321 * Read an extent based attribute.
2324 read_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
)
2326 vnode_t evp
= hfsmp
->hfs_attrdata_vp
;
2334 hfs_lock_truncate(VTOC(evp
), HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
2336 bufsize
= (int)uio_resid(uio
);
2337 attrsize
= (int)datasize
;
2338 blksize
= (int)hfsmp
->blockSize
;
2341 * Read the attribute data one extent at a time.
2342 * For the typical case there is only one extent.
2344 for (i
= 0; (attrsize
> 0) && (bufsize
> 0) && (extents
[i
].startBlock
!= 0); ++i
) {
2345 iosize
= extents
[i
].blockCount
* blksize
;
2346 iosize
= MIN(iosize
, attrsize
);
2347 iosize
= MIN(iosize
, bufsize
);
2348 uio_setresid(uio
, iosize
);
2349 uio_setoffset(uio
, (u_int64_t
)extents
[i
].startBlock
* (u_int64_t
)blksize
);
2351 result
= cluster_read(evp
, uio
, VTOF(evp
)->ff_size
, IO_SYNC
| IO_UNIT
);
2353 #if HFS_XATTR_VERBOSE
2354 printf("hfs: read_attr_data: cr iosize %lld [%d, %d] (%d)\n",
2355 iosize
, extents
[i
].startBlock
, extents
[i
].blockCount
, result
);
2362 uio_setresid(uio
, bufsize
);
2363 uio_setoffset(uio
, datasize
);
2365 hfs_unlock_truncate(VTOC(evp
), HFS_LOCK_DEFAULT
);
2370 * Write an extent based attribute.
2373 write_attr_data(struct hfsmount
*hfsmp
, uio_t uio
, size_t datasize
, HFSPlusExtentDescriptor
*extents
)
2375 vnode_t evp
= hfsmp
->hfs_attrdata_vp
;
2384 hfs_lock_truncate(VTOC(evp
), HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
2386 bufsize
= uio_resid(uio
);
2387 attrsize
= (int) datasize
;
2388 blksize
= (int) hfsmp
->blockSize
;
2389 filesize
= VTOF(evp
)->ff_size
;
2392 * Write the attribute data one extent at a time.
2394 for (i
= 0; (attrsize
> 0) && (bufsize
> 0) && (extents
[i
].startBlock
!= 0); ++i
) {
2395 iosize
= extents
[i
].blockCount
* blksize
;
2396 iosize
= MIN(iosize
, attrsize
);
2397 iosize
= MIN(iosize
, bufsize
);
2398 uio_setresid(uio
, iosize
);
2399 uio_setoffset(uio
, (u_int64_t
)extents
[i
].startBlock
* (u_int64_t
)blksize
);
2401 result
= cluster_write(evp
, uio
, filesize
, filesize
, filesize
,
2402 (off_t
) 0, IO_SYNC
| IO_UNIT
);
2403 #if HFS_XATTR_VERBOSE
2404 printf("hfs: write_attr_data: cw iosize %lld [%d, %d] (%d)\n",
2405 iosize
, extents
[i
].startBlock
, extents
[i
].blockCount
, result
);
2412 uio_setresid(uio
, bufsize
);
2413 uio_setoffset(uio
, datasize
);
2415 hfs_unlock_truncate(VTOC(evp
), HFS_LOCK_DEFAULT
);
2420 * Allocate blocks for an extent based attribute.
2423 alloc_attr_blks(struct hfsmount
*hfsmp
, size_t attrsize
, size_t extentbufsize
, HFSPlusExtentDescriptor
*extents
, int *blocks
)
2432 startblk
= hfsmp
->hfs_metazone_end
;
2433 blkcnt
= howmany(attrsize
, hfsmp
->blockSize
);
2434 if (blkcnt
> (int)hfs_freeblks(hfsmp
, 0)) {
2438 maxextents
= extentbufsize
/ sizeof(HFSPlusExtentDescriptor
);
2440 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2442 for (i
= 0; (blkcnt
> 0) && (i
< maxextents
); i
++) {
2443 /* Try allocating and see if we find something decent */
2444 result
= BlockAllocate(hfsmp
, startblk
, blkcnt
, blkcnt
, 0,
2445 &extents
[i
].startBlock
, &extents
[i
].blockCount
);
2447 * If we couldn't find anything, then re-try the allocation but allow
2450 if (result
== dskFulErr
) {
2451 result
= BlockAllocate(hfsmp
, startblk
, blkcnt
, blkcnt
, HFS_ALLOC_FLUSHTXN
,
2452 &extents
[i
].startBlock
, &extents
[i
].blockCount
);
2456 #if HFS_XATTR_VERBOSE
2457 printf("hfs: alloc_attr_blks: BA blkcnt %d [%d, %d] (%d)\n",
2458 blkcnt
, extents
[i
].startBlock
, extents
[i
].blockCount
, result
);
2461 extents
[i
].startBlock
= 0;
2462 extents
[i
].blockCount
= 0;
2465 blkcnt
-= extents
[i
].blockCount
;
2466 startblk
= extents
[i
].startBlock
+ extents
[i
].blockCount
;
2469 * If it didn't fit in the extents buffer then bail.
2474 #if HFS_XATTR_VERBOSE
2475 printf("hfs: alloc_attr_blks: unexpected failure, %d blocks unallocated\n", blkcnt
);
2477 for (; i
>= 0; i
--) {
2478 if ((blkcnt
= extents
[i
].blockCount
) != 0) {
2479 (void) BlockDeallocate(hfsmp
, extents
[i
].startBlock
, blkcnt
, 0);
2480 extents
[i
].startBlock
= 0;
2481 extents
[i
].blockCount
= 0;
2486 hfs_systemfile_unlock(hfsmp
, lockflags
);
2487 return MacToVFSError(result
);
2491 * Release blocks from an extent based attribute.
2494 free_attr_blks(struct hfsmount
*hfsmp
, int blkcnt
, HFSPlusExtentDescriptor
*extents
)
2496 vnode_t evp
= hfsmp
->hfs_attrdata_vp
;
2497 int remblks
= blkcnt
;
2501 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2503 for (i
= 0; (remblks
> 0) && (extents
[i
].blockCount
!= 0); i
++) {
2504 if (extents
[i
].blockCount
> (u_int32_t
)blkcnt
) {
2505 #if HFS_XATTR_VERBOSE
2506 printf("hfs: free_attr_blks: skipping bad extent [%d, %d]\n",
2507 extents
[i
].startBlock
, extents
[i
].blockCount
);
2509 extents
[i
].blockCount
= 0;
2512 if (extents
[i
].startBlock
== 0) {
2515 (void)BlockDeallocate(hfsmp
, extents
[i
].startBlock
, extents
[i
].blockCount
, 0);
2516 remblks
-= extents
[i
].blockCount
;
2517 extents
[i
].startBlock
= 0;
2518 extents
[i
].blockCount
= 0;
2520 #if HFS_XATTR_VERBOSE
2521 printf("hfs: free_attr_blks: BlockDeallocate [%d, %d]\n",
2522 extents
[i
].startBlock
, extents
[i
].blockCount
);
2524 /* Discard any resident pages for this block range. */
2528 start
= (u_int64_t
)extents
[i
].startBlock
* (u_int64_t
)hfsmp
->blockSize
;
2529 end
= start
+ (u_int64_t
)extents
[i
].blockCount
* (u_int64_t
)hfsmp
->blockSize
;
2530 (void) ubc_msync(hfsmp
->hfs_attrdata_vp
, start
, end
, &start
, UBC_INVALIDATE
);
2534 hfs_systemfile_unlock(hfsmp
, lockflags
);
2538 has_overflow_extents(HFSPlusForkData
*forkdata
)
2542 if (forkdata
->extents
[7].blockCount
== 0)
2545 blocks
= forkdata
->extents
[0].blockCount
+
2546 forkdata
->extents
[1].blockCount
+
2547 forkdata
->extents
[2].blockCount
+
2548 forkdata
->extents
[3].blockCount
+
2549 forkdata
->extents
[4].blockCount
+
2550 forkdata
->extents
[5].blockCount
+
2551 forkdata
->extents
[6].blockCount
+
2552 forkdata
->extents
[7].blockCount
;
2554 return (forkdata
->totalBlocks
> blocks
);
2558 count_extent_blocks(int maxblks
, HFSPlusExtentRecord extents
)
2563 for (i
= 0, blocks
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
2564 /* Ignore obvious bogus extents. */
2565 if (extents
[i
].blockCount
> (u_int32_t
)maxblks
)
2567 if (extents
[i
].startBlock
== 0 || extents
[i
].blockCount
== 0)
2569 blocks
+= extents
[i
].blockCount
;