2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/buf_internal.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/mount.h>
36 #include <sys/vnode.h>
40 #include "hfs_cnode.h"
42 #include "hfs_endian.h"
43 #include "hfs_btreeio.h"
45 #include "hfscommon/headers/FileMgrInternal.h"
46 #include "hfscommon/headers/BTreesPrivate.h"
48 #define FORCESYNCBTREEWRITES 0
50 /* From bsd/vfs/vfs_bio.c */
51 extern int bdwrite_internal(struct buf
*, int);
53 static int ClearBTNodes(struct vnode
*vp
, long blksize
, off_t offset
, off_t amount
);
54 static int btree_journal_modify_block_end(struct hfsmount
*hfsmp
, struct buf
*bp
);
58 OSStatus
SetBTreeBlockSize(FileReference vp
, ByteCount blockSize
, __unused ItemCount minBlockCount
)
60 BTreeControlBlockPtr bTreePtr
;
62 DBG_ASSERT(vp
!= NULL
);
63 DBG_ASSERT(blockSize
>= kMinNodeSize
);
64 if (blockSize
> MAXBSIZE
)
65 return (fsBTBadNodeSize
);
67 bTreePtr
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
68 bTreePtr
->nodeSize
= blockSize
;
75 OSStatus
GetBTreeBlock(FileReference vp
, u_int32_t blockNum
, GetBlockOptions options
, BlockDescriptor
*block
)
77 OSStatus retval
= E_NONE
;
78 struct buf
*bp
= NULL
;
79 u_int8_t allow_empty_node
;
81 /* If the btree block is being read using hint, it is
82 * fine for the swap code to find zeroed out nodes.
84 if (options
& kGetBlockHint
) {
85 allow_empty_node
= true;
87 allow_empty_node
= false;
90 if (options
& kGetEmptyBlock
) {
94 offset
= (daddr64_t
)blockNum
* (daddr64_t
)block
->blockSize
;
95 bp
= buf_getblk(vp
, (daddr64_t
)blockNum
, block
->blockSize
, 0, 0, BLK_META
);
97 VNOP_BLOCKMAP(vp
, offset
, block
->blockSize
, &blkno
, NULL
, NULL
, 0, NULL
) == 0) {
98 buf_setblkno(bp
, blkno
);
101 retval
= buf_meta_bread(vp
, (daddr64_t
)blockNum
, block
->blockSize
, NOCRED
, &bp
);
104 retval
= -1; //XXX need better error
106 if (retval
== E_NONE
) {
107 block
->blockHeader
= bp
;
108 block
->buffer
= (char *)buf_dataptr(bp
);
109 block
->blockNum
= buf_lblkno(bp
);
110 block
->blockReadFromDisk
= (buf_fromcache(bp
) == 0); /* not found in cache ==> came from disk */
113 block
->isModified
= 0;
115 /* Check and endian swap B-Tree node (only if it's a valid block) */
116 if (!(options
& kGetEmptyBlock
)) {
117 /* This happens when we first open the b-tree, we might not have all the node data on hand */
118 if ((((BTNodeDescriptor
*)block
->buffer
)->kind
== kBTHeaderNode
) &&
119 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
!= buf_count(bp
)) &&
120 (SWAP_BE16 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
) != buf_count(bp
))) {
123 * Don't swap the node descriptor, record offsets, or other records.
124 * This record will be invalidated and re-read with the correct node
125 * size once the B-tree control block is set up with the node size
126 * from the header record.
128 retval
= hfs_swap_BTNode (block
, vp
, kSwapBTNodeHeaderRecordOnly
, allow_empty_node
);
130 } else if (block
->blockReadFromDisk
) {
132 * The node was just read from disk, so always swap/check it.
133 * This is necessary on big endian since the test below won't trigger.
135 retval
= hfs_swap_BTNode (block
, vp
, kSwapBTNodeBigToHost
, allow_empty_node
);
136 } else if (*((u_int16_t
*)((char *)block
->buffer
+ (block
->blockSize
- sizeof (u_int16_t
)))) == 0x0e00) {
138 * The node was left in the cache in non-native order, so swap it.
139 * This only happens on little endian, after the node is written
142 retval
= hfs_swap_BTNode (block
, vp
, kSwapBTNodeBigToHost
, allow_empty_node
);
146 * If we got an error, then the node is only partially swapped.
147 * We mark the buffer invalid so that the next attempt to get the
148 * node will read it and attempt to swap again, and will notice
149 * the error again. If we didn't do this, the next attempt to get
150 * the node might use the partially swapped node as-is.
160 block
->blockHeader
= NULL
;
161 block
->buffer
= NULL
;
169 void ModifyBlockStart(FileReference vp
, BlockDescPtr blockPtr
)
171 struct hfsmount
*hfsmp
= VTOHFS(vp
);
172 struct buf
*bp
= NULL
;
174 if (hfsmp
->jnl
== NULL
) {
178 bp
= (struct buf
*) blockPtr
->blockHeader
;
180 panic("ModifyBlockStart: null bp for blockdescptr %p?!?\n", blockPtr
);
184 journal_modify_block_start(hfsmp
->jnl
, bp
);
185 blockPtr
->isModified
= 1;
189 btree_swap_node(struct buf
*bp
, __unused
void *arg
)
191 // struct hfsmount *hfsmp = (struct hfsmount *)arg;
193 struct vnode
*vp
= buf_vnode(bp
);
194 BlockDescriptor block
;
196 /* Prepare the block pointer */
197 block
.blockHeader
= bp
;
198 block
.buffer
= (char *)buf_dataptr(bp
);
199 block
.blockNum
= buf_lblkno(bp
);
200 /* not found in cache ==> came from disk */
201 block
.blockReadFromDisk
= (buf_fromcache(bp
) == 0);
202 block
.blockSize
= buf_count(bp
);
204 /* Swap the data now that this node is ready to go to disk.
205 * We allow swapping of zeroed out nodes here because we might
206 * be writing node whose last record just got deleted.
208 retval
= hfs_swap_BTNode (&block
, vp
, kSwapBTNodeHostToBig
, true);
210 panic("btree_swap_node: about to write corrupt node!\n");
215 btree_journal_modify_block_end(struct hfsmount
*hfsmp
, struct buf
*bp
)
217 return journal_modify_block_end(hfsmp
->jnl
, bp
, btree_swap_node
, hfsmp
);
222 OSStatus
ReleaseBTreeBlock(FileReference vp
, BlockDescPtr blockPtr
, ReleaseBlockOptions options
)
224 struct hfsmount
*hfsmp
= VTOHFS(vp
);
225 OSStatus retval
= E_NONE
;
226 struct buf
*bp
= NULL
;
228 bp
= (struct buf
*) blockPtr
->blockHeader
;
235 if (options
& kTrashBlock
) {
238 if (hfsmp
->jnl
&& (buf_flags(bp
) & B_LOCKED
)) {
239 journal_kill_block(hfsmp
->jnl
, bp
);
241 buf_brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
244 if (options
& kForceWriteBlock
) {
246 if (blockPtr
->isModified
== 0) {
247 panic("hfs: releaseblock: modified is 0 but forcewrite set! bp %p\n", bp
);
250 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
251 blockPtr
->isModified
= 0;
253 retval
= VNOP_BWRITE(bp
);
255 } else if (options
& kMarkBlockDirty
) {
258 if ((options
& kLockTransaction
) && hfsmp
->jnl
== NULL
) {
261 * Set the B_LOCKED flag and unlock the buffer, causing buf_brelse to move
262 * the buffer onto the LOCKED free list. This is necessary, otherwise
263 * getnewbuf() would try to reclaim the buffers using buf_bawrite, which
264 * isn't going to work.
267 /* Don't hog all the buffers... */
268 if (count_lock_queue() > kMaxLockedMetaBuffers
) {
269 hfs_btsync(vp
, HFS_SYNCTRANS
);
270 /* Rollback sync time to cause a sync on lock release... */
271 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
- (kMaxSecsForFsync
+ 1));
273 buf_setflags(bp
, B_LOCKED
);
277 * Delay-write this block.
278 * If the maximum delayed buffers has been exceeded then
279 * free up some buffers and fall back to an asynchronous write.
282 if (blockPtr
->isModified
== 0) {
283 panic("hfs: releaseblock: modified is 0 but markdirty set! bp %p\n", bp
);
285 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
286 blockPtr
->isModified
= 0;
287 } else if (bdwrite_internal(bp
, 1) != 0) {
289 /* Rollback sync time to cause a sync on lock release... */
290 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
- (kMaxSecsForFsync
+ 1));
292 buf_clearflags(bp
, B_LOCKED
);
296 // check if we had previously called journal_modify_block_start()
297 // on this block and if so, abort it (which will call buf_brelse()).
298 if (hfsmp
->jnl
&& blockPtr
->isModified
) {
299 // XXXdbg - I don't want to call modify_block_abort()
300 // because I think it may be screwing up the
301 // journal and blowing away a block that has
304 // journal_modify_block_abort(hfsmp->jnl, bp);
305 //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp);
306 btree_journal_modify_block_end(hfsmp
, bp
);
307 blockPtr
->isModified
= 0;
309 buf_brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
320 OSStatus
ExtendBTreeFile(FileReference vp
, FSSize minEOF
, FSSize maxEOF
)
322 #pragma unused (maxEOF)
324 OSStatus retval
= 0, ret
= 0;
325 int64_t actualBytesAdded
, origSize
;
326 u_int64_t bytesToAdd
;
327 u_int32_t startAllocation
;
328 u_int32_t fileblocks
;
332 struct proc
*p
= NULL
;
336 filePtr
= GetFileControlBlock(vp
);
338 if ( (off_t
)minEOF
> filePtr
->fcbEOF
)
340 bytesToAdd
= minEOF
- filePtr
->fcbEOF
;
342 if (bytesToAdd
< filePtr
->ff_clumpsize
)
343 bytesToAdd
= filePtr
->ff_clumpsize
; //XXX why not always be a mutiple of clump size?
353 * The Extents B-tree can't have overflow extents. ExtendFileC will
354 * return an error if an attempt is made to extend the Extents B-tree
355 * when the resident extents are exhausted.
358 /* Protect allocation bitmap and extents overflow file. */
359 lockflags
= SFL_BITMAP
;
360 if (VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
)
361 lockflags
|= SFL_EXTENTS
;
362 lockflags
= hfs_systemfile_lock(vcb
, lockflags
, HFS_EXCLUSIVE_LOCK
);
364 (void) BTGetInformation(filePtr
, 0, &btInfo
);
368 * The b-tree code expects nodes to be contiguous. So when
369 * the allocation block size is less than the b-tree node
370 * size, we need to force disk allocations to be contiguous.
372 if (vcb
->blockSize
>= btInfo
.nodeSize
) {
375 /* Ensure that all b-tree nodes are contiguous on disk */
376 extendFlags
= kEFContigMask
;
380 origSize
= filePtr
->fcbEOF
;
381 fileblocks
= filePtr
->ff_blocks
;
382 startAllocation
= vcb
->nextAllocation
;
384 // loop trying to get a contiguous chunk that's an integer multiple
385 // of the btree node size. if we can't get a contiguous chunk that
386 // is at least the node size then we break out of the loop and let
387 // the error propagate back up.
388 while((off_t
)bytesToAdd
>= btInfo
.nodeSize
) {
390 retval
= ExtendFileC(vcb
, filePtr
, bytesToAdd
, 0,
391 kEFContigMask
| kEFMetadataMask
| kEFNoClumpMask
,
392 (int64_t *)&actualBytesAdded
);
393 if (retval
== dskFulErr
&& actualBytesAdded
== 0) {
395 if (bytesToAdd
< btInfo
.nodeSize
) {
397 } else if ((bytesToAdd
% btInfo
.nodeSize
) != 0) {
398 // make sure it's an integer multiple of the nodeSize
399 bytesToAdd
-= (bytesToAdd
% btInfo
.nodeSize
);
402 } while (retval
== dskFulErr
&& actualBytesAdded
== 0);
404 if (retval
== dskFulErr
&& actualBytesAdded
== 0 && bytesToAdd
<= btInfo
.nodeSize
) {
408 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
409 bytesToAdd
= minEOF
- filePtr
->fcbEOF
;
413 * If a new extent was added then move the roving allocator
414 * reference forward by the current b-tree file size so
415 * there's plenty of room to grow.
418 ((VCBTOHFS(vcb
)->hfs_flags
& HFS_METADATA_ZONE
) == 0) &&
419 (vcb
->nextAllocation
> startAllocation
) &&
420 ((vcb
->nextAllocation
+ fileblocks
) < vcb
->allocLimit
)) {
421 HFS_UPDATE_NEXT_ALLOCATION(vcb
, vcb
->nextAllocation
+ fileblocks
);
424 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
426 // XXXdbg ExtendFileC() could have returned an error even though
427 // it grew the file to be big enough for our needs. If this is
428 // the case, we don't care about retval so we blow it away.
430 if (filePtr
->fcbEOF
>= (off_t
)minEOF
&& retval
!= 0) {
434 // XXXdbg if the file grew but isn't large enough or isn't an
435 // even multiple of the nodeSize then trim things back. if
436 // the file isn't large enough we trim back to the original
437 // size. otherwise we trim back to be an even multiple of the
440 if ((filePtr
->fcbEOF
< (off_t
)minEOF
) || ((filePtr
->fcbEOF
- origSize
) % btInfo
.nodeSize
) != 0) {
442 if (filePtr
->fcbEOF
< (off_t
)minEOF
) {
445 if (filePtr
->fcbEOF
< origSize
) {
446 panic("hfs: btree file eof %lld less than orig size %lld!\n",
447 filePtr
->fcbEOF
, origSize
);
450 trim
= filePtr
->fcbEOF
- origSize
;
452 trim
= ((filePtr
->fcbEOF
- origSize
) % btInfo
.nodeSize
);
455 ret
= TruncateFileC(vcb
, filePtr
, filePtr
->fcbEOF
- trim
, 0);
456 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
458 // XXXdbg - panic if the file didn't get trimmed back properly
459 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
460 panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb %p\n",
461 filePtr
->fcbEOF
, btInfo
.nodeSize
, filePtr
);
465 // XXXdbg - this probably doesn't need to be a panic()
466 panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %ld)\n",
467 filePtr
->fcbEOF
, trim
, ret
);
472 if(VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
) {
474 * Get any extents overflow b-tree changes to disk ASAP!
476 (void) BTFlushPath(VTOF(vcb
->extentsRefNum
));
477 (void) hfs_fsync(vcb
->extentsRefNum
, MNT_WAIT
, 0, p
);
479 hfs_systemfile_unlock(vcb
, lockflags
);
482 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
483 panic("hfs: extendbtree: fcb %p has eof 0x%llx not a multiple of 0x%x (trim %llx)\n",
484 filePtr
, filePtr
->fcbEOF
, btInfo
.nodeSize
, trim
);
488 * Update the Alternate MDB or Alternate VolumeHeader
490 if ((VTOC(vp
)->c_fileid
== kHFSExtentsFileID
) ||
491 (VTOC(vp
)->c_fileid
== kHFSCatalogFileID
) ||
492 (VTOC(vp
)->c_fileid
== kHFSAttributesFileID
)
494 VTOC(vp
)->c_flag
|= C_MODIFIED
;
496 ret
= hfs_flushvolumeheader(VCBTOHFS(vcb
), MNT_WAIT
, HFS_ALTFLUSH
);
498 VTOC(vp
)->c_touch_chgtime
= TRUE
;
499 VTOC(vp
)->c_touch_modtime
= TRUE
;
500 (void) hfs_update(vp
, TRUE
);
503 ret
= ClearBTNodes(vp
, btInfo
.nodeSize
, origSize
, (filePtr
->fcbEOF
- origSize
));
509 hfs_systemfile_unlock(vcb
, lockflags
);
516 * Clear out (zero) new b-tree nodes on disk.
519 ClearBTNodes(struct vnode
*vp
, long blksize
, off_t offset
, off_t amount
)
521 struct hfsmount
*hfsmp
= VTOHFS(vp
);
522 struct buf
*bp
= NULL
;
526 blk
= offset
/ blksize
;
527 blkcnt
= amount
/ blksize
;
530 bp
= buf_getblk(vp
, blk
, blksize
, 0, 0, BLK_META
);
536 // XXXdbg -- skipping this for now since it makes a transaction
537 // become *way* too large
538 //journal_modify_block_start(hfsmp->jnl, bp);
540 bzero((char *)buf_dataptr(bp
), blksize
);
546 // XXXdbg -- skipping this for now since it makes a transaction
547 // become *way* too large
548 //journal_modify_block_end(hfsmp->jnl, bp);
550 // XXXdbg - remove this once we decide what to do with the
551 // writes to the journal
557 /* wait/yield every 32 blocks so we don't hog all the buffers */
571 extern char hfs_attrname
[];
574 * Create an HFS+ Attribute B-tree File.
576 * No global resources should be held.
579 hfs_create_attr_btree(struct hfsmount
*hfsmp
, u_int32_t nodesize
, u_int32_t nodecnt
)
581 struct vnode
* vp
= NULLVP
;
582 struct cat_desc cndesc
;
583 struct cat_attr cnattr
;
584 struct cat_fork cfork
;
585 BlockDescriptor blkdesc
;
586 BTNodeDescriptor
*ndp
;
588 BTreeControlBlockPtr btcb
= NULL
;
589 struct buf
*bp
= NULL
;
597 * Serialize creation using HFS_CREATING_BTREE flag.
599 lck_mtx_lock(&hfsmp
->hfs_mutex
);
600 if (hfsmp
->hfs_flags
& HFS_CREATING_BTREE
) {
601 /* Someone else beat us, wait for them to finish. */
602 (void) msleep(hfsmp
->hfs_attribute_cp
, &hfsmp
->hfs_mutex
,
603 PDROP
| PINOD
, "hfs_create_attr_btree", 0);
604 if (hfsmp
->hfs_attribute_vp
) {
609 hfsmp
->hfs_flags
|= HFS_CREATING_BTREE
;
610 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
612 /* Check if were out of usable disk space. */
613 if ((hfs_freeblks(hfsmp
, 1) == 0)) {
619 * Set up Attribute B-tree vnode
620 * (this must be done before we start a transaction
621 * or take any system file locks)
623 bzero(&cndesc
, sizeof(cndesc
));
624 cndesc
.cd_parentcnid
= kHFSRootParentID
;
625 cndesc
.cd_flags
|= CD_ISMETA
;
626 cndesc
.cd_nameptr
= (const u_int8_t
*)hfs_attrname
;
627 cndesc
.cd_namelen
= strlen(hfs_attrname
);
628 cndesc
.cd_cnid
= kHFSAttributesFileID
;
630 bzero(&cnattr
, sizeof(cnattr
));
631 cnattr
.ca_linkcount
= 1;
632 cnattr
.ca_mode
= S_IFREG
;
633 cnattr
.ca_fileid
= cndesc
.cd_cnid
;
635 bzero(&cfork
, sizeof(cfork
));
636 cfork
.cf_clump
= nodesize
* nodecnt
;
638 result
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
, &vp
);
643 * Set up Attribute B-tree control block
645 MALLOC(btcb
, BTreeControlBlock
*, sizeof(BTreeControlBlock
), M_TEMP
, M_WAITOK
);
646 bzero(btcb
, sizeof(BTreeControlBlock
));
648 btcb
->nodeSize
= nodesize
;
649 btcb
->maxKeyLength
= kHFSPlusAttrKeyMaximumLength
;
650 btcb
->btreeType
= 0xFF;
651 btcb
->attributes
= kBTVariableIndexKeysMask
| kBTBigKeysMask
;
652 btcb
->version
= kBTreeVersion
;
653 btcb
->writeCount
= 1;
654 btcb
->flags
= 0; /* kBTHeaderDirty */
655 btcb
->fileRefNum
= vp
;
656 btcb
->getBlockProc
= GetBTreeBlock
;
657 btcb
->releaseBlockProc
= ReleaseBTreeBlock
;
658 btcb
->setEndOfForkProc
= ExtendBTreeFile
;
659 btcb
->keyCompareProc
= (KeyCompareProcPtr
)hfs_attrkeycompare
;
660 VTOF(vp
)->fcbBTCBPtr
= btcb
;
663 * Allocate some space
665 if (hfs_start_transaction(hfsmp
) != 0) {
671 /* Note ExtendBTreeFile will acquire the necessary system file locks. */
672 result
= ExtendBTreeFile(vp
, nodesize
, cfork
.cf_clump
);
676 btcb
->totalNodes
= VTOF(vp
)->ff_size
/ nodesize
;
677 btcb
->freeNodes
= btcb
->totalNodes
- 1;
680 * Initialize the b-tree header on disk
682 bp
= buf_getblk(vp
, 0, nodesize
, 0, 0, BLK_META
);
688 buffer
= (void *)buf_dataptr(bp
);
689 blkdesc
.buffer
= buffer
;
690 blkdesc
.blockHeader
= (void *)bp
;
691 blkdesc
.blockReadFromDisk
= 0;
692 blkdesc
.isModified
= 0;
694 ModifyBlockStart(vp
, &blkdesc
);
696 if (buf_size(bp
) != nodesize
)
697 panic("hfs_create_attr_btree: bad buffer size (%d)\n", buf_size(bp
));
699 bzero(buffer
, nodesize
);
700 index
= (u_int16_t
*)buffer
;
702 /* FILL IN THE NODE DESCRIPTOR: */
703 ndp
= (BTNodeDescriptor
*)buffer
;
704 ndp
->kind
= kBTHeaderNode
;
706 offset
= sizeof(BTNodeDescriptor
);
707 index
[(nodesize
/ 2) - 1] = offset
;
709 /* FILL IN THE HEADER RECORD: */
710 bthp
= (BTHeaderRec
*)((u_int8_t
*)buffer
+ offset
);
711 bthp
->nodeSize
= nodesize
;
712 bthp
->totalNodes
= btcb
->totalNodes
;
713 bthp
->freeNodes
= btcb
->freeNodes
;
714 bthp
->clumpSize
= cfork
.cf_clump
;
715 bthp
->btreeType
= 0xFF;
716 bthp
->attributes
= kBTVariableIndexKeysMask
| kBTBigKeysMask
;
717 bthp
->maxKeyLength
= kHFSPlusAttrKeyMaximumLength
;
718 bthp
->keyCompareType
= kHFSBinaryCompare
;
719 offset
+= sizeof(BTHeaderRec
);
720 index
[(nodesize
/ 2) - 2] = offset
;
722 /* FILL IN THE USER RECORD: */
723 offset
+= kBTreeHeaderUserBytes
;
724 index
[(nodesize
/ 2) - 3] = offset
;
726 /* FILL IN THE MAP RECORD (only one node in use). */
727 *((u_int8_t
*)buffer
+ offset
) = 0x80;
728 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
729 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
730 index
[(nodesize
/ 2) - 4] = offset
;
733 result
= btree_journal_modify_block_end(hfsmp
, bp
);
735 result
= VNOP_BWRITE(bp
);
740 /* Update vp/cp for attribute btree */
741 lck_mtx_lock(&hfsmp
->hfs_mutex
);
742 hfsmp
->hfs_attribute_cp
= VTOC(vp
);
743 hfsmp
->hfs_attribute_vp
= vp
;
744 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
746 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
749 hfs_unlock(VTOC(vp
));
758 /* XXX need to give back blocks ? */
761 hfs_end_transaction(hfsmp
);
765 * All done, clear HFS_CREATING_BTREE, and wake up any sleepers.
767 lck_mtx_lock(&hfsmp
->hfs_mutex
);
768 hfsmp
->hfs_flags
&= ~HFS_CREATING_BTREE
;
769 wakeup((caddr_t
)hfsmp
->hfs_attribute_cp
);
770 lck_mtx_unlock(&hfsmp
->hfs_mutex
);