2 * Copyright (c) 2000-2017 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/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/mount.h>
35 #include <sys/vnode.h>
39 #include "hfs_cnode.h"
41 #include "hfs_endian.h"
42 #include "hfs_btreeio.h"
44 #include "FileMgrInternal.h"
45 #include "BTreesPrivate.h"
47 /* From bsd/vfs/vfs_bio.c */
48 extern int bdwrite_internal(struct buf
*, int);
50 static int ClearBTNodes(struct vnode
*vp
, int blksize
, off_t offset
, off_t amount
);
51 static int btree_journal_modify_block_end(struct hfsmount
*hfsmp
, struct buf
*bp
);
53 void btree_swap_node(struct buf
*bp
, __unused
void *arg
);
56 * Return btree node size for given vnode.
59 * For btree vnode, returns btree node size.
60 * For non-btree vnodes, returns 0.
62 u_int16_t
get_btree_nodesize(struct vnode
*vp
)
64 BTreeControlBlockPtr btree
;
65 u_int16_t node_size
= 0;
67 if (vnode_issystem(vp
)) {
68 btree
= (BTreeControlBlockPtr
) VTOF(vp
)->fcbBTCBPtr
;
70 node_size
= btree
->nodeSize
;
77 OSStatus
SetBTreeBlockSize(FileReference vp
, ByteCount blockSize
, __unused ItemCount minBlockCount
)
79 BTreeControlBlockPtr bTreePtr
;
81 hfs_assert(vp
!= NULL
);
82 hfs_assert(blockSize
>= kMinNodeSize
);
83 if (blockSize
> MAXBSIZE
)
84 return (fsBTBadNodeSize
);
86 bTreePtr
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
87 bTreePtr
->nodeSize
= blockSize
;
93 OSStatus
GetBTreeBlock(FileReference vp
, u_int32_t blockNum
, GetBlockOptions options
, BlockDescriptor
*block
)
95 OSStatus retval
= E_NONE
;
96 struct buf
*bp
= NULL
;
97 u_int8_t allow_empty_node
;
99 /* If the btree block is being read using hint, it is
100 * fine for the swap code to find zeroed out nodes.
102 if (options
& kGetBlockHint
) {
103 allow_empty_node
= true;
105 allow_empty_node
= false;
108 if (options
& kGetEmptyBlock
) {
112 offset
= (daddr64_t
)blockNum
* (daddr64_t
)block
->blockSize
;
113 bp
= buf_getblk(vp
, (daddr64_t
)blockNum
, block
->blockSize
, 0, 0, BLK_META
);
114 if (bp
&& !hfs_vnop_blockmap(&(struct vnop_blockmap_args
){
117 .a_size
= block
->blockSize
,
120 buf_setblkno(bp
, blkno
);
123 retval
= buf_meta_bread(vp
, (daddr64_t
)blockNum
, block
->blockSize
, NOCRED
, &bp
);
126 retval
= -1; //XXX need better error
128 if (retval
== E_NONE
) {
129 block
->blockHeader
= bp
;
130 block
->buffer
= (char *)buf_dataptr(bp
);
131 block
->blockNum
= buf_lblkno(bp
);
132 block
->blockReadFromDisk
= (buf_fromcache(bp
) == 0); /* not found in cache ==> came from disk */
135 block
->isModified
= 0;
137 /* Check and endian swap B-Tree node (only if it's a valid block) */
138 if (!(options
& kGetEmptyBlock
)) {
140 /* This happens when we first open the b-tree, we might not have all the node data on hand */
141 if ((((BTNodeDescriptor
*)block
->buffer
)->kind
== kBTHeaderNode
) &&
142 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
!= buf_count(bp
)) &&
143 (SWAP_BE16 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
) != buf_count(bp
))) {
146 * Don't swap the node descriptor, record offsets, or other records.
147 * This record will be invalidated and re-read with the correct node
148 * size once the B-tree control block is set up with the node size
149 * from the header record.
151 retval
= hfs_swap_BTNode (block
, vp
, kSwapBTNodeHeaderRecordOnly
, allow_empty_node
);
155 * In this case, we have enough data in-hand to do basic validation
156 * on the B-Tree node.
158 if (block
->blockReadFromDisk
) {
160 * The node was just read from disk, so always swap/check it.
161 * This is necessary on big endian since the test below won't trigger.
163 retval
= hfs_swap_BTNode (block
, vp
, kSwapBTNodeBigToHost
, allow_empty_node
);
167 * Block wasn't read from disk; it was found in the cache.
169 if (*((u_int16_t
*)((char *)block
->buffer
+ (block
->blockSize
- sizeof (u_int16_t
)))) == 0x0e00) {
171 * The node was left in the cache in non-native order, so swap it.
172 * This only happens on little endian, after the node is written
175 retval
= hfs_swap_BTNode (block
, vp
, kSwapBTNodeBigToHost
, allow_empty_node
);
177 else if (*((u_int16_t
*)((char *)block
->buffer
+ (block
->blockSize
- sizeof (u_int16_t
)))) == 0x000e) {
179 * The node was in-cache in native-endianness. We don't need to do
180 * anything here, because the node is ready to use. Set retval == 0.
185 * If the node doesn't have hex 14 (0xe) in the last two bytes of the buffer,
186 * it doesn't necessarily mean that this is a bad node. Zeroed nodes that are
187 * marked as unused in the b-tree map node would be OK and not have valid content.
193 * If we got an error, then the node is only partially swapped.
194 * We mark the buffer invalid so that the next attempt to get the
195 * node will read it and attempt to swap again, and will notice
196 * the error again. If we didn't do this, the next attempt to get
197 * the node might use the partially swapped node as-is.
207 block
->blockHeader
= NULL
;
208 block
->buffer
= NULL
;
215 void ModifyBlockStart(FileReference vp
, BlockDescPtr blockPtr
)
217 struct hfsmount
*hfsmp
= VTOHFS(vp
);
218 struct buf
*bp
= NULL
;
220 if (hfsmp
->jnl
== NULL
) {
224 bp
= (struct buf
*) blockPtr
->blockHeader
;
226 panic("hfs: ModifyBlockStart: null bp for blockdescptr %p?!?\n", blockPtr
);
230 journal_modify_block_start(hfsmp
->jnl
, bp
);
231 blockPtr
->isModified
= 1;
235 btree_swap_node(struct buf
*bp
, __unused
void *arg
)
237 // struct hfsmount *hfsmp = (struct hfsmount *)arg;
239 struct vnode
*vp
= buf_vnode(bp
);
240 BlockDescriptor block
;
242 /* Prepare the block pointer */
243 block
.blockHeader
= bp
;
244 block
.buffer
= (char *)buf_dataptr(bp
);
245 block
.blockNum
= buf_lblkno(bp
);
246 /* not found in cache ==> came from disk */
247 block
.blockReadFromDisk
= (buf_fromcache(bp
) == 0);
248 block
.blockSize
= buf_count(bp
);
250 /* Swap the data now that this node is ready to go to disk.
251 * We allow swapping of zeroed out nodes here because we might
252 * be writing node whose last record just got deleted.
254 retval
= hfs_swap_BTNode (&block
, vp
, kSwapBTNodeHostToBig
, true);
256 panic("hfs: btree_swap_node: about to write corrupt node!\n");
261 btree_journal_modify_block_end(struct hfsmount
*hfsmp
, struct buf
*bp
)
263 return journal_modify_block_end(hfsmp
->jnl
, bp
, btree_swap_node
, hfsmp
);
267 OSStatus
ReleaseBTreeBlock(FileReference vp
, BlockDescPtr blockPtr
, ReleaseBlockOptions options
)
269 struct hfsmount
*hfsmp
= VTOHFS(vp
);
270 OSStatus retval
= E_NONE
;
271 struct buf
*bp
= NULL
;
273 bp
= (struct buf
*) blockPtr
->blockHeader
;
280 if (options
& kTrashBlock
) {
283 if (hfsmp
->jnl
&& (buf_flags(bp
) & B_LOCKED
)) {
284 journal_kill_block(hfsmp
->jnl
, bp
);
286 buf_brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
289 /* Don't let anyone else try to use this bp, it's been consumed */
290 blockPtr
->blockHeader
= NULL
;
293 if (options
& kForceWriteBlock
) {
295 if (blockPtr
->isModified
== 0) {
296 panic("hfs: releaseblock: modified is 0 but forcewrite set! bp %p\n", bp
);
299 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
300 blockPtr
->isModified
= 0;
302 retval
= VNOP_BWRITE(bp
);
305 /* Don't let anyone else try to use this bp, it's been consumed */
306 blockPtr
->blockHeader
= NULL
;
308 } else if (options
& kMarkBlockDirty
) {
311 if ((options
& kLockTransaction
) && hfsmp
->jnl
== NULL
) {
314 * Set the B_LOCKED flag and unlock the buffer, causing buf_brelse to move
315 * the buffer onto the LOCKED free list. This is necessary, otherwise
316 * getnewbuf() would try to reclaim the buffers using buf_bawrite, which
317 * isn't going to work.
320 /* Don't hog all the buffers... */
321 if (count_lock_queue() > kMaxLockedMetaBuffers
) {
322 hfs_btsync(vp
, HFS_SYNCTRANS
);
323 /* Rollback sync time to cause a sync on lock release... */
324 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
- (kMaxSecsForFsync
+ 1));
326 buf_setflags(bp
, B_LOCKED
);
330 * Delay-write this block.
331 * If the maximum delayed buffers has been exceeded then
332 * free up some buffers and fall back to an asynchronous write.
335 if (blockPtr
->isModified
== 0) {
336 panic("hfs: releaseblock: modified is 0 but markdirty set! bp %p\n", bp
);
338 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
339 blockPtr
->isModified
= 0;
340 } else if (bdwrite_internal(bp
, 1) != 0) {
342 /* Rollback sync time to cause a sync on lock release... */
343 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
- (kMaxSecsForFsync
+ 1));
345 buf_clearflags(bp
, B_LOCKED
);
349 /* Don't let anyone else try to use this bp, it's been consumed */
350 blockPtr
->blockHeader
= NULL
;
353 // check if we had previously called journal_modify_block_start()
354 // on this block and if so, abort it (which will call buf_brelse()).
355 if (hfsmp
->jnl
&& blockPtr
->isModified
) {
356 // XXXdbg - I don't want to call modify_block_abort()
357 // because I think it may be screwing up the
358 // journal and blowing away a block that has
361 // journal_modify_block_abort(hfsmp->jnl, bp);
362 //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp);
363 btree_journal_modify_block_end(hfsmp
, bp
);
364 blockPtr
->isModified
= 0;
366 buf_brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
369 /* Don't let anyone else try to use this bp, it's been consumed */
370 blockPtr
->blockHeader
= NULL
;
379 OSStatus
ExtendBTreeFile(FileReference vp
, FSSize minEOF
, FSSize maxEOF
)
381 #pragma unused (maxEOF)
383 OSStatus retval
= 0, ret
= 0;
384 int64_t actualBytesAdded
, origSize
;
385 u_int64_t bytesToAdd
;
386 u_int32_t startAllocation
;
387 u_int32_t fileblocks
;
391 struct proc
*p
= NULL
;
395 filePtr
= GetFileControlBlock(vp
);
397 if ( (off_t
)minEOF
> filePtr
->fcbEOF
)
399 bytesToAdd
= minEOF
- filePtr
->fcbEOF
;
401 if (bytesToAdd
< filePtr
->ff_clumpsize
)
402 bytesToAdd
= filePtr
->ff_clumpsize
; //XXX why not always be a mutiple of clump size?
412 * The Extents B-tree can't have overflow extents. ExtendFileC will
413 * return an error if an attempt is made to extend the Extents B-tree
414 * when the resident extents are exhausted.
417 /* Protect allocation bitmap and extents overflow file. */
418 lockflags
= SFL_BITMAP
;
419 if (VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
)
420 lockflags
|= SFL_EXTENTS
;
421 lockflags
= hfs_systemfile_lock(vcb
, lockflags
, HFS_EXCLUSIVE_LOCK
);
423 (void) BTGetInformation(filePtr
, 0, &btInfo
);
427 * The b-tree code expects nodes to be contiguous. So when
428 * the allocation block size is less than the b-tree node
429 * size, we need to force disk allocations to be contiguous.
431 if (vcb
->blockSize
>= btInfo
.nodeSize
) {
434 /* Ensure that all b-tree nodes are contiguous on disk */
435 extendFlags
= kEFContigMask
;
439 origSize
= filePtr
->fcbEOF
;
440 fileblocks
= filePtr
->ff_blocks
;
441 startAllocation
= vcb
->nextAllocation
;
443 // loop trying to get a contiguous chunk that's an integer multiple
444 // of the btree node size. if we can't get a contiguous chunk that
445 // is at least the node size then we break out of the loop and let
446 // the error propagate back up.
447 while((off_t
)bytesToAdd
>= btInfo
.nodeSize
) {
449 retval
= ExtendFileC(vcb
, filePtr
, bytesToAdd
, 0,
450 kEFContigMask
| kEFMetadataMask
| kEFNoClumpMask
,
451 (int64_t *)&actualBytesAdded
);
452 if (retval
== dskFulErr
&& actualBytesAdded
== 0) {
454 if (bytesToAdd
< btInfo
.nodeSize
) {
456 } else if ((bytesToAdd
% btInfo
.nodeSize
) != 0) {
457 // make sure it's an integer multiple of the nodeSize
458 bytesToAdd
-= (bytesToAdd
% btInfo
.nodeSize
);
461 } while (retval
== dskFulErr
&& actualBytesAdded
== 0);
463 if (retval
== dskFulErr
&& actualBytesAdded
== 0 && bytesToAdd
<= btInfo
.nodeSize
) {
467 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
468 bytesToAdd
= minEOF
- filePtr
->fcbEOF
;
472 * If a new extent was added then move the roving allocator
473 * reference forward by the current b-tree file size so
474 * there's plenty of room to grow.
477 ((VCBTOHFS(vcb
)->hfs_flags
& HFS_METADATA_ZONE
) == 0) &&
478 (vcb
->nextAllocation
> startAllocation
) &&
479 ((vcb
->nextAllocation
+ fileblocks
) < vcb
->allocLimit
)) {
480 HFS_UPDATE_NEXT_ALLOCATION(vcb
, vcb
->nextAllocation
+ fileblocks
);
483 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
485 // XXXdbg ExtendFileC() could have returned an error even though
486 // it grew the file to be big enough for our needs. If this is
487 // the case, we don't care about retval so we blow it away.
489 if (filePtr
->fcbEOF
>= (off_t
)minEOF
&& retval
!= 0) {
493 // XXXdbg if the file grew but isn't large enough or isn't an
494 // even multiple of the nodeSize then trim things back. if
495 // the file isn't large enough we trim back to the original
496 // size. otherwise we trim back to be an even multiple of the
499 if ((filePtr
->fcbEOF
< (off_t
)minEOF
) || ((filePtr
->fcbEOF
- origSize
) % btInfo
.nodeSize
) != 0) {
501 if (filePtr
->fcbEOF
< (off_t
)minEOF
) {
504 if (filePtr
->fcbEOF
< origSize
) {
505 panic("hfs: btree file eof %lld less than orig size %lld!\n",
506 filePtr
->fcbEOF
, origSize
);
509 trim
= filePtr
->fcbEOF
- origSize
;
511 trim
= ((filePtr
->fcbEOF
- origSize
) % btInfo
.nodeSize
);
514 ret
= TruncateFileC(vcb
, filePtr
, filePtr
->fcbEOF
- trim
, 0, 0, FTOC(filePtr
)->c_fileid
, 0);
515 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
517 // XXXdbg - panic if the file didn't get trimmed back properly
518 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
519 panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb %p\n",
520 filePtr
->fcbEOF
, btInfo
.nodeSize
, filePtr
);
524 // XXXdbg - this probably doesn't need to be a panic()
525 panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %ld)\n",
526 filePtr
->fcbEOF
, trim
, (long)ret
);
531 if(VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
) {
533 * Get any extents overflow b-tree changes to disk ASAP!
535 (void) BTFlushPath(VTOF(vcb
->extentsRefNum
));
536 (void) hfs_fsync(vcb
->extentsRefNum
, MNT_WAIT
, 0, p
);
538 hfs_systemfile_unlock(vcb
, lockflags
);
541 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
542 panic("hfs: extendbtree: fcb %p has eof 0x%llx not a multiple of 0x%x (trim %llx)\n",
543 filePtr
, filePtr
->fcbEOF
, btInfo
.nodeSize
, trim
);
547 * Update the Alternate MDB or Alternate VolumeHeader
549 VTOC(vp
)->c_flag
|= C_MODIFIED
;
550 if ((VTOC(vp
)->c_fileid
== kHFSExtentsFileID
) ||
551 (VTOC(vp
)->c_fileid
== kHFSCatalogFileID
) ||
552 (VTOC(vp
)->c_fileid
== kHFSAttributesFileID
)
555 ret
= hfs_flushvolumeheader(VCBTOHFS(vcb
), HFS_FVH_WAIT
| HFS_FVH_WRITE_ALT
);
557 VTOC(vp
)->c_touch_chgtime
= TRUE
;
558 VTOC(vp
)->c_touch_modtime
= TRUE
;
559 (void) hfs_update(vp
, 0);
562 ret
= ClearBTNodes(vp
, btInfo
.nodeSize
, origSize
, (filePtr
->fcbEOF
- origSize
));
568 hfs_systemfile_unlock(vcb
, lockflags
);
575 * Clear out (zero) new b-tree nodes on disk.
578 ClearBTNodes(struct vnode
*vp
, int blksize
, off_t offset
, off_t amount
)
580 struct hfsmount
*hfsmp
= VTOHFS(vp
);
581 struct buf
*bp
= NULL
;
585 blk
= offset
/ blksize
;
586 blkcnt
= amount
/ blksize
;
589 bp
= buf_getblk(vp
, blk
, blksize
, 0, 0, BLK_META
);
595 // XXXdbg -- skipping this for now since it makes a transaction
596 // become *way* too large
597 //journal_modify_block_start(hfsmp->jnl, bp);
599 bzero((char *)buf_dataptr(bp
), blksize
);
605 // XXXdbg -- skipping this for now since it makes a transaction
606 // become *way* too large
607 //journal_modify_block_end(hfsmp->jnl, bp);
609 // XXXdbg - remove this once we decide what to do with the
610 // writes to the journal
616 /* wait/yield every 32 blocks so we don't hog all the buffers */
630 extern char hfs_attrname
[];
633 * Create an HFS+ Attribute B-tree File.
635 * No global resources should be held.
638 hfs_create_attr_btree(struct hfsmount
*hfsmp
, u_int32_t nodesize
, u_int32_t nodecnt
)
640 struct vnode
* vp
= NULLVP
;
641 struct cat_desc cndesc
;
642 struct cat_attr cnattr
;
643 struct cat_fork cfork
;
644 BlockDescriptor blkdesc
;
645 BTNodeDescriptor
*ndp
;
647 BTreeControlBlockPtr btcb
= NULL
;
648 struct buf
*bp
= NULL
;
652 u_int32_t node_num
, num_map_nodes
;
653 u_int32_t bytes_per_map_record
;
658 int newvnode_flags
= 0;
662 * Serialize creation using HFS_CREATING_BTREE flag.
664 hfs_lock_mount (hfsmp
);
665 if (hfsmp
->hfs_flags
& HFS_CREATING_BTREE
) {
666 /* Someone else beat us, wait for them to finish. */
667 (void) msleep(&hfsmp
->hfs_attribute_cp
, &hfsmp
->hfs_mutex
,
668 PDROP
| PINOD
, "hfs_create_attr_btree", 0);
669 if (hfsmp
->hfs_attribute_vp
) {
674 hfsmp
->hfs_flags
|= HFS_CREATING_BTREE
;
675 hfs_unlock_mount (hfsmp
);
677 /* Check if were out of usable disk space. */
678 if ((hfs_freeblks(hfsmp
, 1) == 0)) {
684 * Set up Attribute B-tree vnode
685 * (this must be done before we start a transaction
686 * or take any system file locks)
688 bzero(&cndesc
, sizeof(cndesc
));
689 cndesc
.cd_parentcnid
= kHFSRootParentID
;
690 cndesc
.cd_flags
|= CD_ISMETA
;
691 cndesc
.cd_nameptr
= (const u_int8_t
*)hfs_attrname
;
692 cndesc
.cd_namelen
= strlen(hfs_attrname
);
693 cndesc
.cd_cnid
= kHFSAttributesFileID
;
695 bzero(&cnattr
, sizeof(cnattr
));
696 cnattr
.ca_linkcount
= 1;
697 cnattr
.ca_mode
= S_IFREG
;
698 cnattr
.ca_fileid
= cndesc
.cd_cnid
;
700 bzero(&cfork
, sizeof(cfork
));
701 cfork
.cf_clump
= nodesize
* nodecnt
;
703 result
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
,
704 &cfork
, &vp
, &newvnode_flags
);
709 * Set up Attribute B-tree control block
711 btcb
= hfs_mallocz(sizeof(*btcb
));
713 btcb
->nodeSize
= nodesize
;
714 btcb
->maxKeyLength
= kHFSPlusAttrKeyMaximumLength
;
715 btcb
->btreeType
= 0xFF;
716 btcb
->attributes
= kBTVariableIndexKeysMask
| kBTBigKeysMask
;
717 btcb
->version
= kBTreeVersion
;
718 btcb
->writeCount
= 1;
719 btcb
->flags
= 0; /* kBTHeaderDirty */
720 btcb
->fileRefNum
= vp
;
721 btcb
->getBlockProc
= GetBTreeBlock
;
722 btcb
->releaseBlockProc
= ReleaseBTreeBlock
;
723 btcb
->setEndOfForkProc
= ExtendBTreeFile
;
724 btcb
->keyCompareProc
= (KeyCompareProcPtr
)hfs_attrkeycompare
;
727 * NOTE: We must make sure to zero out this pointer if we error out in this function!
728 * If we don't, then unmount will treat it as a valid pointer which can lead to a
731 VTOF(vp
)->fcbBTCBPtr
= btcb
;
734 * Allocate some space
736 if (hfs_start_transaction(hfsmp
) != 0) {
742 /* Note ExtendBTreeFile will acquire the necessary system file locks. */
743 result
= ExtendBTreeFile(vp
, nodesize
, cfork
.cf_clump
);
747 btcb
->totalNodes
= VTOF(vp
)->ff_size
/ nodesize
;
750 * Figure out how many map nodes we'll need.
752 * bytes_per_map_record = the number of bytes in the map record of a
753 * map node. Since that is the only record in the node, it is the size
754 * of the node minus the node descriptor at the start, and two record
755 * offsets at the end of the node. The "- 2" is to round the size down
756 * to a multiple of 4 bytes (since sizeof(BTNodeDescriptor) is not a
759 * The value "temp" here is the number of *bits* in the map record of
762 bytes_per_map_record
= nodesize
- sizeof(BTNodeDescriptor
) - 2*sizeof(u_int16_t
) - 2;
763 temp
= 8 * (nodesize
- sizeof(BTNodeDescriptor
)
764 - sizeof(BTHeaderRec
)
765 - kBTreeHeaderUserBytes
766 - 4 * sizeof(u_int16_t
));
767 if (btcb
->totalNodes
> temp
) {
768 num_map_nodes
= howmany(btcb
->totalNodes
- temp
, bytes_per_map_record
* 8);
774 btcb
->freeNodes
= btcb
->totalNodes
- 1 - num_map_nodes
;
777 * Initialize the b-tree header on disk
779 bp
= buf_getblk(vp
, 0, nodesize
, 0, 0, BLK_META
);
785 buffer
= (void *)buf_dataptr(bp
);
786 blkdesc
.buffer
= buffer
;
787 blkdesc
.blockHeader
= (void *)bp
;
788 blkdesc
.blockReadFromDisk
= 0;
789 blkdesc
.isModified
= 0;
791 ModifyBlockStart(vp
, &blkdesc
);
793 if (buf_size(bp
) != nodesize
)
794 panic("hfs_create_attr_btree: bad buffer size (%d)\n", buf_size(bp
));
796 bzero(buffer
, nodesize
);
797 index
= (u_int16_t
*)buffer
;
799 /* FILL IN THE NODE DESCRIPTOR: */
800 ndp
= (BTNodeDescriptor
*)buffer
;
801 if (num_map_nodes
!= 0)
803 ndp
->kind
= kBTHeaderNode
;
805 offset
= sizeof(BTNodeDescriptor
);
806 index
[(nodesize
/ 2) - 1] = offset
;
808 /* FILL IN THE HEADER RECORD: */
809 bthp
= (BTHeaderRec
*)((u_int8_t
*)buffer
+ offset
);
810 bthp
->nodeSize
= nodesize
;
811 bthp
->totalNodes
= btcb
->totalNodes
;
812 bthp
->freeNodes
= btcb
->freeNodes
;
813 bthp
->clumpSize
= cfork
.cf_clump
;
814 bthp
->btreeType
= 0xFF;
815 bthp
->attributes
= kBTVariableIndexKeysMask
| kBTBigKeysMask
;
816 bthp
->maxKeyLength
= kHFSPlusAttrKeyMaximumLength
;
817 bthp
->keyCompareType
= kHFSBinaryCompare
;
818 offset
+= sizeof(BTHeaderRec
);
819 index
[(nodesize
/ 2) - 2] = offset
;
821 /* FILL IN THE USER RECORD: */
822 offset
+= kBTreeHeaderUserBytes
;
823 index
[(nodesize
/ 2) - 3] = offset
;
825 /* Mark the header node and map nodes in use in the map record.
827 * NOTE: Assumes that the header node's map record has at least
828 * (num_map_nodes + 1) bits.
830 bitmap
= (u_int8_t
*) buffer
+ offset
;
831 temp
= num_map_nodes
+ 1; /* +1 for the header node */
836 *bitmap
= ~(0xFF >> temp
);
838 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
839 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
840 index
[(nodesize
/ 2) - 4] = offset
;
843 result
= btree_journal_modify_block_end(hfsmp
, bp
);
845 result
= VNOP_BWRITE(bp
);
850 /* Create the map nodes: node numbers 1 .. num_map_nodes */
851 for (node_num
=1; node_num
<= num_map_nodes
; ++node_num
) {
852 bp
= buf_getblk(vp
, node_num
, nodesize
, 0, 0, BLK_META
);
857 buffer
= (void *)buf_dataptr(bp
);
858 blkdesc
.buffer
= buffer
;
859 blkdesc
.blockHeader
= (void *)bp
;
860 blkdesc
.blockReadFromDisk
= 0;
861 blkdesc
.isModified
= 0;
863 ModifyBlockStart(vp
, &blkdesc
);
865 bzero(buffer
, nodesize
);
866 index
= (u_int16_t
*)buffer
;
868 /* Fill in the node descriptor */
869 ndp
= (BTNodeDescriptor
*)buffer
;
870 if (node_num
!= num_map_nodes
)
871 ndp
->fLink
= node_num
+ 1;
872 ndp
->kind
= kBTMapNode
;
874 offset
= sizeof(BTNodeDescriptor
);
875 index
[(nodesize
/ 2) - 1] = offset
;
878 /* Fill in the map record's offset */
879 /* Note: We assume that the map record is all zeroes */
880 offset
= sizeof(BTNodeDescriptor
) + bytes_per_map_record
;
881 index
[(nodesize
/ 2) - 2] = offset
;
884 result
= btree_journal_modify_block_end(hfsmp
, bp
);
886 result
= VNOP_BWRITE(bp
);
892 /* Update vp/cp for attribute btree */
893 hfs_lock_mount (hfsmp
);
894 hfsmp
->hfs_attribute_cp
= VTOC(vp
);
895 hfsmp
->hfs_attribute_vp
= vp
;
896 hfs_unlock_mount (hfsmp
);
898 (void) hfs_flushvolumeheader(hfsmp
, HFS_FVH_WAIT
| HFS_FVH_WRITE_ALT
);
901 hfs_end_transaction(hfsmp
);
905 /* Initialize the vnode for virtual attribute data file */
906 result
= init_attrdata_vnode(hfsmp
);
908 printf("hfs_create_attr_btree: vol=%s init_attrdata_vnode() error=%d\n", hfsmp
->vcbVN
, result
);
915 * If we're about to error out, then make sure to zero out the B-Tree control block pointer
916 * from the filefork of the EA B-Tree cnode/vnode. Failing to do this will lead to a use
917 * after free at unmount or BTFlushPath. Since we're about to error out anyway, this memory
920 VTOF(vp
)->fcbBTCBPtr
= NULL
;
925 hfs_unlock(VTOC(vp
));
928 hfs_free(btcb
, sizeof(*btcb
));
932 /* XXX need to give back blocks ? */
935 hfs_end_transaction(hfsmp
);
939 * All done, clear HFS_CREATING_BTREE, and wake up any sleepers.
941 hfs_lock_mount (hfsmp
);
942 hfsmp
->hfs_flags
&= ~HFS_CREATING_BTREE
;
943 wakeup((caddr_t
)&hfsmp
->hfs_attribute_cp
);
944 hfs_unlock_mount (hfsmp
);