2 * Copyright (c) 2000-2010 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
);
56 void btree_swap_node(struct buf
*bp
, __unused
void *arg
);
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
;
74 OSStatus
GetBTreeBlock(FileReference vp
, u_int32_t blockNum
, GetBlockOptions options
, BlockDescriptor
*block
)
76 OSStatus retval
= E_NONE
;
77 struct buf
*bp
= NULL
;
78 u_int8_t allow_empty_node
;
80 /* If the btree block is being read using hint, it is
81 * fine for the swap code to find zeroed out nodes.
83 if (options
& kGetBlockHint
) {
84 allow_empty_node
= true;
86 allow_empty_node
= false;
89 if (options
& kGetEmptyBlock
) {
93 offset
= (daddr64_t
)blockNum
* (daddr64_t
)block
->blockSize
;
94 bp
= buf_getblk(vp
, (daddr64_t
)blockNum
, block
->blockSize
, 0, 0, BLK_META
);
96 VNOP_BLOCKMAP(vp
, offset
, block
->blockSize
, &blkno
, NULL
, NULL
, 0, NULL
) == 0) {
97 buf_setblkno(bp
, blkno
);
100 retval
= buf_meta_bread(vp
, (daddr64_t
)blockNum
, block
->blockSize
, NOCRED
, &bp
);
103 retval
= -1; //XXX need better error
105 if (retval
== E_NONE
) {
106 block
->blockHeader
= bp
;
107 block
->buffer
= (char *)buf_dataptr(bp
);
108 block
->blockNum
= buf_lblkno(bp
);
109 block
->blockReadFromDisk
= (buf_fromcache(bp
) == 0); /* not found in cache ==> came from disk */
112 block
->isModified
= 0;
114 /* Check and endian swap B-Tree node (only if it's a valid block) */
115 if (!(options
& kGetEmptyBlock
)) {
116 /* This happens when we first open the b-tree, we might not have all the node data on hand */
117 if ((((BTNodeDescriptor
*)block
->buffer
)->kind
== kBTHeaderNode
) &&
118 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
!= buf_count(bp
)) &&
119 (SWAP_BE16 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
) != buf_count(bp
))) {
122 * Don't swap the node descriptor, record offsets, or other records.
123 * This record will be invalidated and re-read with the correct node
124 * size once the B-tree control block is set up with the node size
125 * from the header record.
127 retval
= hfs_swap_BTNode (block
, vp
, kSwapBTNodeHeaderRecordOnly
, allow_empty_node
);
129 } else if (block
->blockReadFromDisk
) {
131 * The node was just read from disk, so always swap/check it.
132 * This is necessary on big endian since the test below won't trigger.
134 retval
= hfs_swap_BTNode (block
, vp
, kSwapBTNodeBigToHost
, allow_empty_node
);
135 } else if (*((u_int16_t
*)((char *)block
->buffer
+ (block
->blockSize
- sizeof (u_int16_t
)))) == 0x0e00) {
137 * The node was left in the cache in non-native order, so swap it.
138 * This only happens on little endian, after the node is written
141 retval
= hfs_swap_BTNode (block
, vp
, kSwapBTNodeBigToHost
, allow_empty_node
);
145 * If we got an error, then the node is only partially swapped.
146 * We mark the buffer invalid so that the next attempt to get the
147 * node will read it and attempt to swap again, and will notice
148 * the error again. If we didn't do this, the next attempt to get
149 * the node might use the partially swapped node as-is.
159 block
->blockHeader
= NULL
;
160 block
->buffer
= NULL
;
167 void ModifyBlockStart(FileReference vp
, BlockDescPtr blockPtr
)
169 struct hfsmount
*hfsmp
= VTOHFS(vp
);
170 struct buf
*bp
= NULL
;
172 if (hfsmp
->jnl
== NULL
) {
176 bp
= (struct buf
*) blockPtr
->blockHeader
;
178 panic("hfs: ModifyBlockStart: null bp for blockdescptr %p?!?\n", blockPtr
);
182 journal_modify_block_start(hfsmp
->jnl
, bp
);
183 blockPtr
->isModified
= 1;
187 btree_swap_node(struct buf
*bp
, __unused
void *arg
)
189 // struct hfsmount *hfsmp = (struct hfsmount *)arg;
191 struct vnode
*vp
= buf_vnode(bp
);
192 BlockDescriptor block
;
194 /* Prepare the block pointer */
195 block
.blockHeader
= bp
;
196 block
.buffer
= (char *)buf_dataptr(bp
);
197 block
.blockNum
= buf_lblkno(bp
);
198 /* not found in cache ==> came from disk */
199 block
.blockReadFromDisk
= (buf_fromcache(bp
) == 0);
200 block
.blockSize
= buf_count(bp
);
202 /* Swap the data now that this node is ready to go to disk.
203 * We allow swapping of zeroed out nodes here because we might
204 * be writing node whose last record just got deleted.
206 retval
= hfs_swap_BTNode (&block
, vp
, kSwapBTNodeHostToBig
, true);
208 panic("hfs: btree_swap_node: about to write corrupt node!\n");
213 btree_journal_modify_block_end(struct hfsmount
*hfsmp
, struct buf
*bp
)
215 return journal_modify_block_end(hfsmp
->jnl
, bp
, btree_swap_node
, hfsmp
);
219 OSStatus
ReleaseBTreeBlock(FileReference vp
, BlockDescPtr blockPtr
, ReleaseBlockOptions options
)
221 struct hfsmount
*hfsmp
= VTOHFS(vp
);
222 OSStatus retval
= E_NONE
;
223 struct buf
*bp
= NULL
;
225 bp
= (struct buf
*) blockPtr
->blockHeader
;
232 if (options
& kTrashBlock
) {
235 if (hfsmp
->jnl
&& (buf_flags(bp
) & B_LOCKED
)) {
236 journal_kill_block(hfsmp
->jnl
, bp
);
238 buf_brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
241 /* Don't let anyone else try to use this bp, it's been consumed */
242 blockPtr
->blockHeader
= NULL
;
245 if (options
& kForceWriteBlock
) {
247 if (blockPtr
->isModified
== 0) {
248 panic("hfs: releaseblock: modified is 0 but forcewrite set! bp %p\n", bp
);
251 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
252 blockPtr
->isModified
= 0;
254 retval
= VNOP_BWRITE(bp
);
257 /* Don't let anyone else try to use this bp, it's been consumed */
258 blockPtr
->blockHeader
= NULL
;
260 } else if (options
& kMarkBlockDirty
) {
263 if ((options
& kLockTransaction
) && hfsmp
->jnl
== NULL
) {
266 * Set the B_LOCKED flag and unlock the buffer, causing buf_brelse to move
267 * the buffer onto the LOCKED free list. This is necessary, otherwise
268 * getnewbuf() would try to reclaim the buffers using buf_bawrite, which
269 * isn't going to work.
272 /* Don't hog all the buffers... */
273 if (count_lock_queue() > kMaxLockedMetaBuffers
) {
274 hfs_btsync(vp
, HFS_SYNCTRANS
);
275 /* Rollback sync time to cause a sync on lock release... */
276 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
- (kMaxSecsForFsync
+ 1));
278 buf_setflags(bp
, B_LOCKED
);
282 * Delay-write this block.
283 * If the maximum delayed buffers has been exceeded then
284 * free up some buffers and fall back to an asynchronous write.
287 if (blockPtr
->isModified
== 0) {
288 panic("hfs: releaseblock: modified is 0 but markdirty set! bp %p\n", bp
);
290 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
291 blockPtr
->isModified
= 0;
292 } else if (bdwrite_internal(bp
, 1) != 0) {
294 /* Rollback sync time to cause a sync on lock release... */
295 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
- (kMaxSecsForFsync
+ 1));
297 buf_clearflags(bp
, B_LOCKED
);
301 /* Don't let anyone else try to use this bp, it's been consumed */
302 blockPtr
->blockHeader
= NULL
;
305 // check if we had previously called journal_modify_block_start()
306 // on this block and if so, abort it (which will call buf_brelse()).
307 if (hfsmp
->jnl
&& blockPtr
->isModified
) {
308 // XXXdbg - I don't want to call modify_block_abort()
309 // because I think it may be screwing up the
310 // journal and blowing away a block that has
313 // journal_modify_block_abort(hfsmp->jnl, bp);
314 //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp);
315 btree_journal_modify_block_end(hfsmp
, bp
);
316 blockPtr
->isModified
= 0;
318 buf_brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
321 /* Don't let anyone else try to use this bp, it's been consumed */
322 blockPtr
->blockHeader
= NULL
;
331 OSStatus
ExtendBTreeFile(FileReference vp
, FSSize minEOF
, FSSize maxEOF
)
333 #pragma unused (maxEOF)
335 OSStatus retval
= 0, ret
= 0;
336 int64_t actualBytesAdded
, origSize
;
337 u_int64_t bytesToAdd
;
338 u_int32_t startAllocation
;
339 u_int32_t fileblocks
;
343 struct proc
*p
= NULL
;
347 filePtr
= GetFileControlBlock(vp
);
349 if ( (off_t
)minEOF
> filePtr
->fcbEOF
)
351 bytesToAdd
= minEOF
- filePtr
->fcbEOF
;
353 if (bytesToAdd
< filePtr
->ff_clumpsize
)
354 bytesToAdd
= filePtr
->ff_clumpsize
; //XXX why not always be a mutiple of clump size?
364 * The Extents B-tree can't have overflow extents. ExtendFileC will
365 * return an error if an attempt is made to extend the Extents B-tree
366 * when the resident extents are exhausted.
369 /* Protect allocation bitmap and extents overflow file. */
370 lockflags
= SFL_BITMAP
;
371 if (VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
)
372 lockflags
|= SFL_EXTENTS
;
373 lockflags
= hfs_systemfile_lock(vcb
, lockflags
, HFS_EXCLUSIVE_LOCK
);
375 (void) BTGetInformation(filePtr
, 0, &btInfo
);
379 * The b-tree code expects nodes to be contiguous. So when
380 * the allocation block size is less than the b-tree node
381 * size, we need to force disk allocations to be contiguous.
383 if (vcb
->blockSize
>= btInfo
.nodeSize
) {
386 /* Ensure that all b-tree nodes are contiguous on disk */
387 extendFlags
= kEFContigMask
;
391 origSize
= filePtr
->fcbEOF
;
392 fileblocks
= filePtr
->ff_blocks
;
393 startAllocation
= vcb
->nextAllocation
;
395 // loop trying to get a contiguous chunk that's an integer multiple
396 // of the btree node size. if we can't get a contiguous chunk that
397 // is at least the node size then we break out of the loop and let
398 // the error propagate back up.
399 while((off_t
)bytesToAdd
>= btInfo
.nodeSize
) {
401 retval
= ExtendFileC(vcb
, filePtr
, bytesToAdd
, 0,
402 kEFContigMask
| kEFMetadataMask
| kEFNoClumpMask
,
403 (int64_t *)&actualBytesAdded
);
404 if (retval
== dskFulErr
&& actualBytesAdded
== 0) {
406 if (bytesToAdd
< btInfo
.nodeSize
) {
408 } else if ((bytesToAdd
% btInfo
.nodeSize
) != 0) {
409 // make sure it's an integer multiple of the nodeSize
410 bytesToAdd
-= (bytesToAdd
% btInfo
.nodeSize
);
413 } while (retval
== dskFulErr
&& actualBytesAdded
== 0);
415 if (retval
== dskFulErr
&& actualBytesAdded
== 0 && bytesToAdd
<= btInfo
.nodeSize
) {
419 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
420 bytesToAdd
= minEOF
- filePtr
->fcbEOF
;
424 * If a new extent was added then move the roving allocator
425 * reference forward by the current b-tree file size so
426 * there's plenty of room to grow.
429 ((VCBTOHFS(vcb
)->hfs_flags
& HFS_METADATA_ZONE
) == 0) &&
430 (vcb
->nextAllocation
> startAllocation
) &&
431 ((vcb
->nextAllocation
+ fileblocks
) < vcb
->allocLimit
)) {
432 HFS_UPDATE_NEXT_ALLOCATION(vcb
, vcb
->nextAllocation
+ fileblocks
);
435 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
437 // XXXdbg ExtendFileC() could have returned an error even though
438 // it grew the file to be big enough for our needs. If this is
439 // the case, we don't care about retval so we blow it away.
441 if (filePtr
->fcbEOF
>= (off_t
)minEOF
&& retval
!= 0) {
445 // XXXdbg if the file grew but isn't large enough or isn't an
446 // even multiple of the nodeSize then trim things back. if
447 // the file isn't large enough we trim back to the original
448 // size. otherwise we trim back to be an even multiple of the
451 if ((filePtr
->fcbEOF
< (off_t
)minEOF
) || ((filePtr
->fcbEOF
- origSize
) % btInfo
.nodeSize
) != 0) {
453 if (filePtr
->fcbEOF
< (off_t
)minEOF
) {
456 if (filePtr
->fcbEOF
< origSize
) {
457 panic("hfs: btree file eof %lld less than orig size %lld!\n",
458 filePtr
->fcbEOF
, origSize
);
461 trim
= filePtr
->fcbEOF
- origSize
;
463 trim
= ((filePtr
->fcbEOF
- origSize
) % btInfo
.nodeSize
);
466 ret
= TruncateFileC(vcb
, filePtr
, filePtr
->fcbEOF
- trim
, 0, 0, FTOC(filePtr
)->c_fileid
, 0);
467 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
469 // XXXdbg - panic if the file didn't get trimmed back properly
470 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
471 panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb %p\n",
472 filePtr
->fcbEOF
, btInfo
.nodeSize
, filePtr
);
476 // XXXdbg - this probably doesn't need to be a panic()
477 panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %ld)\n",
478 filePtr
->fcbEOF
, trim
, (long)ret
);
483 if(VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
) {
485 * Get any extents overflow b-tree changes to disk ASAP!
487 (void) BTFlushPath(VTOF(vcb
->extentsRefNum
));
488 (void) hfs_fsync(vcb
->extentsRefNum
, MNT_WAIT
, 0, p
);
490 hfs_systemfile_unlock(vcb
, lockflags
);
493 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
494 panic("hfs: extendbtree: fcb %p has eof 0x%llx not a multiple of 0x%x (trim %llx)\n",
495 filePtr
, filePtr
->fcbEOF
, btInfo
.nodeSize
, trim
);
499 * Update the Alternate MDB or Alternate VolumeHeader
501 if ((VTOC(vp
)->c_fileid
== kHFSExtentsFileID
) ||
502 (VTOC(vp
)->c_fileid
== kHFSCatalogFileID
) ||
503 (VTOC(vp
)->c_fileid
== kHFSAttributesFileID
)
505 VTOC(vp
)->c_flag
|= C_MODIFIED
;
507 ret
= hfs_flushvolumeheader(VCBTOHFS(vcb
), MNT_WAIT
, HFS_ALTFLUSH
);
509 VTOC(vp
)->c_touch_chgtime
= TRUE
;
510 VTOC(vp
)->c_touch_modtime
= TRUE
;
511 (void) hfs_update(vp
, TRUE
);
514 ret
= ClearBTNodes(vp
, btInfo
.nodeSize
, origSize
, (filePtr
->fcbEOF
- origSize
));
520 hfs_systemfile_unlock(vcb
, lockflags
);
527 * Clear out (zero) new b-tree nodes on disk.
530 ClearBTNodes(struct vnode
*vp
, long blksize
, off_t offset
, off_t amount
)
532 struct hfsmount
*hfsmp
= VTOHFS(vp
);
533 struct buf
*bp
= NULL
;
537 blk
= offset
/ blksize
;
538 blkcnt
= amount
/ blksize
;
541 bp
= buf_getblk(vp
, blk
, blksize
, 0, 0, BLK_META
);
547 // XXXdbg -- skipping this for now since it makes a transaction
548 // become *way* too large
549 //journal_modify_block_start(hfsmp->jnl, bp);
551 bzero((char *)buf_dataptr(bp
), blksize
);
557 // XXXdbg -- skipping this for now since it makes a transaction
558 // become *way* too large
559 //journal_modify_block_end(hfsmp->jnl, bp);
561 // XXXdbg - remove this once we decide what to do with the
562 // writes to the journal
568 /* wait/yield every 32 blocks so we don't hog all the buffers */
582 extern char hfs_attrname
[];
585 * Create an HFS+ Attribute B-tree File.
587 * No global resources should be held.
590 hfs_create_attr_btree(struct hfsmount
*hfsmp
, u_int32_t nodesize
, u_int32_t nodecnt
)
592 struct vnode
* vp
= NULLVP
;
593 struct cat_desc cndesc
;
594 struct cat_attr cnattr
;
595 struct cat_fork cfork
;
596 BlockDescriptor blkdesc
;
597 BTNodeDescriptor
*ndp
;
599 BTreeControlBlockPtr btcb
= NULL
;
600 struct buf
*bp
= NULL
;
604 u_int32_t node_num
, num_map_nodes
;
605 u_int32_t bytes_per_map_record
;
610 int newvnode_flags
= 0;
614 * Serialize creation using HFS_CREATING_BTREE flag.
616 lck_mtx_lock(&hfsmp
->hfs_mutex
);
617 if (hfsmp
->hfs_flags
& HFS_CREATING_BTREE
) {
618 /* Someone else beat us, wait for them to finish. */
619 (void) msleep(hfsmp
->hfs_attribute_cp
, &hfsmp
->hfs_mutex
,
620 PDROP
| PINOD
, "hfs_create_attr_btree", 0);
621 if (hfsmp
->hfs_attribute_vp
) {
626 hfsmp
->hfs_flags
|= HFS_CREATING_BTREE
;
627 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
629 /* Check if were out of usable disk space. */
630 if ((hfs_freeblks(hfsmp
, 1) == 0)) {
636 * Set up Attribute B-tree vnode
637 * (this must be done before we start a transaction
638 * or take any system file locks)
640 bzero(&cndesc
, sizeof(cndesc
));
641 cndesc
.cd_parentcnid
= kHFSRootParentID
;
642 cndesc
.cd_flags
|= CD_ISMETA
;
643 cndesc
.cd_nameptr
= (const u_int8_t
*)hfs_attrname
;
644 cndesc
.cd_namelen
= strlen(hfs_attrname
);
645 cndesc
.cd_cnid
= kHFSAttributesFileID
;
647 bzero(&cnattr
, sizeof(cnattr
));
648 cnattr
.ca_linkcount
= 1;
649 cnattr
.ca_mode
= S_IFREG
;
650 cnattr
.ca_fileid
= cndesc
.cd_cnid
;
652 bzero(&cfork
, sizeof(cfork
));
653 cfork
.cf_clump
= nodesize
* nodecnt
;
655 result
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
,
656 &cfork
, &vp
, &newvnode_flags
);
661 * Set up Attribute B-tree control block
663 MALLOC(btcb
, BTreeControlBlock
*, sizeof(BTreeControlBlock
), M_TEMP
, M_WAITOK
);
664 bzero(btcb
, sizeof(BTreeControlBlock
));
666 btcb
->nodeSize
= nodesize
;
667 btcb
->maxKeyLength
= kHFSPlusAttrKeyMaximumLength
;
668 btcb
->btreeType
= 0xFF;
669 btcb
->attributes
= kBTVariableIndexKeysMask
| kBTBigKeysMask
;
670 btcb
->version
= kBTreeVersion
;
671 btcb
->writeCount
= 1;
672 btcb
->flags
= 0; /* kBTHeaderDirty */
673 btcb
->fileRefNum
= vp
;
674 btcb
->getBlockProc
= GetBTreeBlock
;
675 btcb
->releaseBlockProc
= ReleaseBTreeBlock
;
676 btcb
->setEndOfForkProc
= ExtendBTreeFile
;
677 btcb
->keyCompareProc
= (KeyCompareProcPtr
)hfs_attrkeycompare
;
678 VTOF(vp
)->fcbBTCBPtr
= btcb
;
681 * Allocate some space
683 if (hfs_start_transaction(hfsmp
) != 0) {
689 /* Note ExtendBTreeFile will acquire the necessary system file locks. */
690 result
= ExtendBTreeFile(vp
, nodesize
, cfork
.cf_clump
);
694 btcb
->totalNodes
= VTOF(vp
)->ff_size
/ nodesize
;
697 * Figure out how many map nodes we'll need.
699 * bytes_per_map_record = the number of bytes in the map record of a
700 * map node. Since that is the only record in the node, it is the size
701 * of the node minus the node descriptor at the start, and two record
702 * offsets at the end of the node. The "- 2" is to round the size down
703 * to a multiple of 4 bytes (since sizeof(BTNodeDescriptor) is not a
706 * The value "temp" here is the number of *bits* in the map record of
709 bytes_per_map_record
= nodesize
- sizeof(BTNodeDescriptor
) - 2*sizeof(u_int16_t
) - 2;
710 temp
= 8 * (nodesize
- sizeof(BTNodeDescriptor
)
711 - sizeof(BTHeaderRec
)
712 - kBTreeHeaderUserBytes
713 - 4 * sizeof(u_int16_t
));
714 if (btcb
->totalNodes
> temp
) {
715 num_map_nodes
= howmany(btcb
->totalNodes
- temp
, bytes_per_map_record
* 8);
721 btcb
->freeNodes
= btcb
->totalNodes
- 1 - num_map_nodes
;
724 * Initialize the b-tree header on disk
726 bp
= buf_getblk(vp
, 0, nodesize
, 0, 0, BLK_META
);
732 buffer
= (void *)buf_dataptr(bp
);
733 blkdesc
.buffer
= buffer
;
734 blkdesc
.blockHeader
= (void *)bp
;
735 blkdesc
.blockReadFromDisk
= 0;
736 blkdesc
.isModified
= 0;
738 ModifyBlockStart(vp
, &blkdesc
);
740 if (buf_size(bp
) != nodesize
)
741 panic("hfs_create_attr_btree: bad buffer size (%d)\n", buf_size(bp
));
743 bzero(buffer
, nodesize
);
744 index
= (u_int16_t
*)buffer
;
746 /* FILL IN THE NODE DESCRIPTOR: */
747 ndp
= (BTNodeDescriptor
*)buffer
;
748 if (num_map_nodes
!= 0)
750 ndp
->kind
= kBTHeaderNode
;
752 offset
= sizeof(BTNodeDescriptor
);
753 index
[(nodesize
/ 2) - 1] = offset
;
755 /* FILL IN THE HEADER RECORD: */
756 bthp
= (BTHeaderRec
*)((u_int8_t
*)buffer
+ offset
);
757 bthp
->nodeSize
= nodesize
;
758 bthp
->totalNodes
= btcb
->totalNodes
;
759 bthp
->freeNodes
= btcb
->freeNodes
;
760 bthp
->clumpSize
= cfork
.cf_clump
;
761 bthp
->btreeType
= 0xFF;
762 bthp
->attributes
= kBTVariableIndexKeysMask
| kBTBigKeysMask
;
763 bthp
->maxKeyLength
= kHFSPlusAttrKeyMaximumLength
;
764 bthp
->keyCompareType
= kHFSBinaryCompare
;
765 offset
+= sizeof(BTHeaderRec
);
766 index
[(nodesize
/ 2) - 2] = offset
;
768 /* FILL IN THE USER RECORD: */
769 offset
+= kBTreeHeaderUserBytes
;
770 index
[(nodesize
/ 2) - 3] = offset
;
772 /* Mark the header node and map nodes in use in the map record.
774 * NOTE: Assumes that the header node's map record has at least
775 * (num_map_nodes + 1) bits.
777 bitmap
= (u_int8_t
*) buffer
+ offset
;
778 temp
= num_map_nodes
+ 1; /* +1 for the header node */
783 *bitmap
= ~(0xFF >> temp
);
785 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
786 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
787 index
[(nodesize
/ 2) - 4] = offset
;
790 result
= btree_journal_modify_block_end(hfsmp
, bp
);
792 result
= VNOP_BWRITE(bp
);
797 /* Create the map nodes: node numbers 1 .. num_map_nodes */
798 for (node_num
=1; node_num
<= num_map_nodes
; ++node_num
) {
799 bp
= buf_getblk(vp
, node_num
, nodesize
, 0, 0, BLK_META
);
804 buffer
= (void *)buf_dataptr(bp
);
805 blkdesc
.buffer
= buffer
;
806 blkdesc
.blockHeader
= (void *)bp
;
807 blkdesc
.blockReadFromDisk
= 0;
808 blkdesc
.isModified
= 0;
810 ModifyBlockStart(vp
, &blkdesc
);
812 bzero(buffer
, nodesize
);
813 index
= (u_int16_t
*)buffer
;
815 /* Fill in the node descriptor */
816 ndp
= (BTNodeDescriptor
*)buffer
;
817 if (node_num
!= num_map_nodes
)
818 ndp
->fLink
= node_num
+ 1;
819 ndp
->kind
= kBTMapNode
;
821 offset
= sizeof(BTNodeDescriptor
);
822 index
[(nodesize
/ 2) - 1] = offset
;
825 /* Fill in the map record's offset */
826 /* Note: We assume that the map record is all zeroes */
827 offset
= sizeof(BTNodeDescriptor
) + bytes_per_map_record
;
828 index
[(nodesize
/ 2) - 2] = offset
;
831 result
= btree_journal_modify_block_end(hfsmp
, bp
);
833 result
= VNOP_BWRITE(bp
);
839 /* Update vp/cp for attribute btree */
840 lck_mtx_lock(&hfsmp
->hfs_mutex
);
841 hfsmp
->hfs_attribute_cp
= VTOC(vp
);
842 hfsmp
->hfs_attribute_vp
= vp
;
843 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
845 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
848 hfs_unlock(VTOC(vp
));
857 /* XXX need to give back blocks ? */
860 hfs_end_transaction(hfsmp
);
864 * All done, clear HFS_CREATING_BTREE, and wake up any sleepers.
866 lck_mtx_lock(&hfsmp
->hfs_mutex
);
867 hfsmp
->hfs_flags
&= ~HFS_CREATING_BTREE
;
868 wakeup((caddr_t
)hfsmp
->hfs_attribute_cp
);
869 lck_mtx_unlock(&hfsmp
->hfs_mutex
);