2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/param.h>
24 #include <sys/systm.h>
26 #include <sys/kernel.h>
27 #include <sys/malloc.h>
28 #include <sys/mount.h>
29 #include <sys/vnode.h>
33 #include "hfs_cnode.h"
35 #include "hfs_endian.h"
37 #include "hfscommon/headers/FileMgrInternal.h"
38 #include "hfscommon/headers/BTreesPrivate.h"
40 #define FORCESYNCBTREEWRITES 0
43 static int ClearBTNodes(struct vnode
*vp
, long blksize
, off_t offset
, off_t amount
);
47 OSStatus
SetBTreeBlockSize(FileReference vp
, ByteCount blockSize
, ItemCount minBlockCount
)
49 BTreeControlBlockPtr bTreePtr
;
51 DBG_ASSERT(vp
!= NULL
);
52 DBG_ASSERT(blockSize
>= kMinNodeSize
);
53 if (blockSize
> MAXBSIZE
)
54 return (fsBTBadNodeSize
);
56 bTreePtr
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
57 bTreePtr
->nodeSize
= blockSize
;
64 OSStatus
GetBTreeBlock(FileReference vp
, UInt32 blockNum
, GetBlockOptions options
, BlockDescriptor
*block
)
66 OSStatus retval
= E_NONE
;
67 struct buf
*bp
= NULL
;
69 if (options
& kGetEmptyBlock
) {
73 offset
= (daddr64_t
)blockNum
* (daddr64_t
)block
->blockSize
;
74 bp
= buf_getblk(vp
, (daddr64_t
)blockNum
, block
->blockSize
, 0, 0, BLK_META
);
76 VNOP_BLOCKMAP(vp
, offset
, block
->blockSize
, &blkno
, NULL
, NULL
, 0, NULL
) == 0) {
77 buf_setblkno(bp
, blkno
);
80 retval
= buf_meta_bread(vp
, (daddr64_t
)blockNum
, block
->blockSize
, NOCRED
, &bp
);
83 retval
= -1; //XXX need better error
85 if (retval
== E_NONE
) {
86 block
->blockHeader
= bp
;
87 block
->buffer
= (char *)buf_dataptr(bp
);
88 block
->blockReadFromDisk
= (buf_fromcache(bp
) == 0); /* not found in cache ==> came from disk */
91 block
->isModified
= 0;
93 #if BYTE_ORDER == LITTLE_ENDIAN
94 /* Endian swap B-Tree node (only if it's a valid block) */
95 if (!(options
& kGetEmptyBlock
)) {
96 /* This happens when we first open the b-tree, we might not have all the node data on hand */
97 if ((((BTNodeDescriptor
*)block
->buffer
)->kind
== kBTHeaderNode
) &&
98 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
!= buf_count(bp
)) &&
99 (SWAP_BE16 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
) != buf_count(bp
))) {
101 /* Don't swap the descriptors at all, we don't care (this block will be invalidated) */
102 SWAP_BT_NODE (block
, ISHFSPLUS(VTOVCB(vp
)), VTOC(vp
)->c_fileid
, 3);
104 /* The node needs swapping */
105 } else if (*((UInt16
*)((char *)block
->buffer
+ (block
->blockSize
- sizeof (UInt16
)))) == 0x0e00) {
106 SWAP_BT_NODE (block
, ISHFSPLUS(VTOVCB(vp
)), VTOC(vp
)->c_fileid
, 0);
108 /* The node is not already in native byte order, hence corrupt */
109 } else if (*((UInt16
*)((char *)block
->buffer
+ (block
->blockSize
- sizeof (UInt16
)))) != 0x000e) {
110 panic ("%s Corrupt B-Tree node detected!\n", "GetBTreeBlock:");
118 block
->blockHeader
= NULL
;
119 block
->buffer
= NULL
;
127 void ModifyBlockStart(FileReference vp
, BlockDescPtr blockPtr
)
129 struct hfsmount
*hfsmp
= VTOHFS(vp
);
130 struct buf
*bp
= NULL
;
132 if (hfsmp
->jnl
== NULL
) {
136 bp
= (struct buf
*) blockPtr
->blockHeader
;
138 panic("ModifyBlockStart: null bp for blockdescptr 0x%x?!?\n", blockPtr
);
142 journal_modify_block_start(hfsmp
->jnl
, bp
);
143 blockPtr
->isModified
= 1;
147 btree_journal_modify_block_end(struct hfsmount
*hfsmp
, struct buf
*bp
)
149 #if BYTE_ORDER == LITTLE_ENDIAN
150 struct vnode
*vp
= buf_vnode(bp
);
151 BlockDescriptor block
;
153 /* Prepare the block pointer */
154 block
.blockHeader
= bp
;
155 block
.buffer
= (char *)buf_dataptr(bp
);
156 /* not found in cache ==> came from disk */
157 block
.blockReadFromDisk
= (buf_fromcache(bp
) == 0);
158 block
.blockSize
= buf_count(bp
);
160 // XXXdbg have to swap the data before it goes in the journal
161 SWAP_BT_NODE (&block
, ISHFSPLUS (VTOVCB(vp
)), VTOC(vp
)->c_fileid
, 1);
164 return journal_modify_block_end(hfsmp
->jnl
, bp
);
169 OSStatus
ReleaseBTreeBlock(FileReference vp
, BlockDescPtr blockPtr
, ReleaseBlockOptions options
)
171 struct hfsmount
*hfsmp
= VTOHFS(vp
);
172 extern int bdwrite_internal(struct buf
*, int);
173 OSStatus retval
= E_NONE
;
174 struct buf
*bp
= NULL
;
176 bp
= (struct buf
*) blockPtr
->blockHeader
;
183 if (options
& kTrashBlock
) {
186 if (hfsmp
->jnl
&& (buf_flags(bp
) & B_LOCKED
)) {
187 journal_kill_block(hfsmp
->jnl
, bp
);
189 buf_brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
192 if (options
& kForceWriteBlock
) {
194 if (blockPtr
->isModified
== 0) {
195 panic("hfs: releaseblock: modified is 0 but forcewrite set! bp 0x%x\n", bp
);
198 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
199 blockPtr
->isModified
= 0;
201 retval
= VNOP_BWRITE(bp
);
203 } else if (options
& kMarkBlockDirty
) {
206 if ((options
& kLockTransaction
) && hfsmp
->jnl
== NULL
) {
209 * Set the B_LOCKED flag and unlock the buffer, causing buf_brelse to move
210 * the buffer onto the LOCKED free list. This is necessary, otherwise
211 * getnewbuf() would try to reclaim the buffers using buf_bawrite, which
212 * isn't going to work.
215 extern int count_lock_queue(void);
217 /* Don't hog all the buffers... */
218 if (count_lock_queue() > kMaxLockedMetaBuffers
) {
219 hfs_btsync(vp
, HFS_SYNCTRANS
);
220 /* Rollback sync time to cause a sync on lock release... */
221 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
- (kMaxSecsForFsync
+ 1));
223 buf_setflags(bp
, B_LOCKED
);
227 * Delay-write this block.
228 * If the maximum delayed buffers has been exceeded then
229 * free up some buffers and fall back to an asynchronous write.
232 if (blockPtr
->isModified
== 0) {
233 panic("hfs: releaseblock: modified is 0 but markdirty set! bp 0x%x\n", bp
);
235 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
236 blockPtr
->isModified
= 0;
237 } else if (bdwrite_internal(bp
, 1) != 0) {
239 /* Rollback sync time to cause a sync on lock release... */
240 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
- (kMaxSecsForFsync
+ 1));
242 buf_clearflags(bp
, B_LOCKED
);
246 // check if we had previously called journal_modify_block_start()
247 // on this block and if so, abort it (which will call buf_brelse()).
248 if (hfsmp
->jnl
&& blockPtr
->isModified
) {
249 // XXXdbg - I don't want to call modify_block_abort()
250 // because I think it may be screwing up the
251 // journal and blowing away a block that has
254 // journal_modify_block_abort(hfsmp->jnl, bp);
255 //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp);
256 btree_journal_modify_block_end(hfsmp
, bp
);
257 blockPtr
->isModified
= 0;
259 buf_brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
270 OSStatus
ExtendBTreeFile(FileReference vp
, FSSize minEOF
, FSSize maxEOF
)
272 #pragma unused (maxEOF)
274 OSStatus retval
= 0, ret
= 0;
275 UInt64 actualBytesAdded
, origSize
;
277 u_int32_t startAllocation
;
278 u_int32_t fileblocks
;
282 struct proc
*p
= NULL
;
286 filePtr
= GetFileControlBlock(vp
);
288 if ( minEOF
> filePtr
->fcbEOF
)
290 bytesToAdd
= minEOF
- filePtr
->fcbEOF
;
292 if (bytesToAdd
< filePtr
->ff_clumpsize
)
293 bytesToAdd
= filePtr
->ff_clumpsize
; //XXX why not always be a mutiple of clump size?
303 * The Extents B-tree can't have overflow extents. ExtendFileC will
304 * return an error if an attempt is made to extend the Extents B-tree
305 * when the resident extents are exhausted.
308 /* Protect allocation bitmap and extents overflow file. */
309 lockflags
= SFL_BITMAP
;
310 if (VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
)
311 lockflags
|= SFL_EXTENTS
;
312 lockflags
= hfs_systemfile_lock(vcb
, lockflags
, HFS_EXCLUSIVE_LOCK
);
314 (void) BTGetInformation(filePtr
, 0, &btInfo
);
318 * The b-tree code expects nodes to be contiguous. So when
319 * the allocation block size is less than the b-tree node
320 * size, we need to force disk allocations to be contiguous.
322 if (vcb
->blockSize
>= btInfo
.nodeSize
) {
325 /* Ensure that all b-tree nodes are contiguous on disk */
326 extendFlags
= kEFContigMask
;
330 origSize
= filePtr
->fcbEOF
;
331 fileblocks
= filePtr
->ff_blocks
;
332 startAllocation
= vcb
->nextAllocation
;
334 // loop trying to get a contiguous chunk that's an integer multiple
335 // of the btree node size. if we can't get a contiguous chunk that
336 // is at least the node size then we break out of the loop and let
337 // the error propagate back up.
339 retval
= ExtendFileC(vcb
, filePtr
, bytesToAdd
, 0,
340 kEFContigMask
| kEFMetadataMask
,
342 if (retval
== dskFulErr
&& actualBytesAdded
== 0) {
344 if (bytesToAdd
== btInfo
.nodeSize
|| bytesToAdd
< (minEOF
- origSize
)) {
345 // if we're here there's nothing else to try, we're out
346 // of space so we break and bail out.
350 if (bytesToAdd
< btInfo
.nodeSize
) {
351 bytesToAdd
= btInfo
.nodeSize
;
352 } else if ((bytesToAdd
% btInfo
.nodeSize
) != 0) {
353 // make sure it's an integer multiple of the nodeSize
354 bytesToAdd
-= (bytesToAdd
% btInfo
.nodeSize
);
358 } while (retval
== dskFulErr
&& actualBytesAdded
== 0);
361 * If a new extent was added then move the roving allocator
362 * reference forward by the current b-tree file size so
363 * there's plenty of room to grow.
366 ((VCBTOHFS(vcb
)->hfs_flags
& HFS_METADATA_ZONE
) == 0) &&
367 (vcb
->nextAllocation
> startAllocation
) &&
368 ((vcb
->nextAllocation
+ fileblocks
) < vcb
->totalBlocks
)) {
369 vcb
->nextAllocation
+= fileblocks
;
372 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
374 // XXXdbg ExtendFileC() could have returned an error even though
375 // it grew the file to be big enough for our needs. If this is
376 // the case, we don't care about retval so we blow it away.
378 if (filePtr
->fcbEOF
>= minEOF
&& retval
!= 0) {
382 // XXXdbg if the file grew but isn't large enough or isn't an
383 // even multiple of the nodeSize then trim things back. if
384 // the file isn't large enough we trim back to the original
385 // size. otherwise we trim back to be an even multiple of the
388 if ((filePtr
->fcbEOF
< minEOF
) || (actualBytesAdded
% btInfo
.nodeSize
) != 0) {
390 if (filePtr
->fcbEOF
< minEOF
) {
393 if (filePtr
->fcbEOF
< origSize
) {
394 panic("hfs: btree file eof %lld less than orig size %lld!\n",
395 filePtr
->fcbEOF
, origSize
);
398 trim
= filePtr
->fcbEOF
- origSize
;
399 if (trim
!= actualBytesAdded
) {
400 panic("hfs: trim == %lld but actualBytesAdded == %lld\n",
401 trim
, actualBytesAdded
);
404 trim
= (actualBytesAdded
% btInfo
.nodeSize
);
407 ret
= TruncateFileC(vcb
, filePtr
, filePtr
->fcbEOF
- trim
, 0);
408 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
410 // XXXdbg - panic if the file didn't get trimmed back properly
411 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
412 panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb 0x%x\n",
413 filePtr
->fcbEOF
, btInfo
.nodeSize
, filePtr
);
417 // XXXdbg - this probably doesn't need to be a panic()
418 panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %d)\n",
419 filePtr
->fcbEOF
, trim
, ret
);
422 actualBytesAdded
-= trim
;
425 if(VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
) {
427 * Get any extents overflow b-tree changes to disk ASAP!
429 (void) BTFlushPath(VTOF(vcb
->extentsRefNum
));
430 (void) hfs_fsync(vcb
->extentsRefNum
, MNT_WAIT
, 0, p
);
432 hfs_systemfile_unlock(vcb
, lockflags
);
435 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
436 panic("hfs: extendbtree: fcb 0x%x has eof 0x%llx not a multiple of 0x%x (trim %llx)\n",
437 filePtr
, filePtr
->fcbEOF
, btInfo
.nodeSize
, trim
);
441 * Update the Alternate MDB or Alternate VolumeHeader
443 if ((VTOC(vp
)->c_fileid
== kHFSExtentsFileID
) ||
444 (VTOC(vp
)->c_fileid
== kHFSCatalogFileID
) ||
445 (VTOC(vp
)->c_fileid
== kHFSAttributesFileID
)
447 VTOC(vp
)->c_flag
|= C_MODIFIED
;
449 ret
= hfs_flushvolumeheader(VCBTOHFS(vcb
), MNT_WAIT
, HFS_ALTFLUSH
);
451 VTOC(vp
)->c_touch_chgtime
= TRUE
;
452 VTOC(vp
)->c_touch_modtime
= TRUE
;
453 (void) hfs_update(vp
, TRUE
);
456 ret
= ClearBTNodes(vp
, btInfo
.nodeSize
, filePtr
->fcbEOF
- actualBytesAdded
, actualBytesAdded
);
462 hfs_systemfile_unlock(vcb
, lockflags
);
469 * Clear out (zero) new b-tree nodes on disk.
472 ClearBTNodes(struct vnode
*vp
, long blksize
, off_t offset
, off_t amount
)
474 struct hfsmount
*hfsmp
= VTOHFS(vp
);
475 struct buf
*bp
= NULL
;
479 blk
= offset
/ blksize
;
480 blkcnt
= amount
/ blksize
;
483 bp
= buf_getblk(vp
, blk
, blksize
, 0, 0, BLK_META
);
489 // XXXdbg -- skipping this for now since it makes a transaction
490 // become *way* too large
491 //journal_modify_block_start(hfsmp->jnl, bp);
493 bzero((char *)buf_dataptr(bp
), blksize
);
499 // XXXdbg -- skipping this for now since it makes a transaction
500 // become *way* too large
501 //journal_modify_block_end(hfsmp->jnl, bp);
503 // XXXdbg - remove this once we decide what to do with the
504 // writes to the journal
510 /* wait/yield every 32 blocks so we don't hog all the buffers */
524 extern char hfs_attrname
[];
526 extern int hfs_attrkeycompare(HFSPlusAttrKey
*searchKey
, HFSPlusAttrKey
*trialKey
);
528 int hfs_create_attr_btree(struct hfsmount
*hfsmp
, uint32_t nodesize
, uint32_t nodecnt
);
531 * Create an HFS+ Attribute B-tree File.
533 * A journal transaction must be already started.
536 hfs_create_attr_btree(struct hfsmount
*hfsmp
, uint32_t nodesize
, uint32_t nodecnt
)
538 struct vnode
* vp
= NULL
;
539 struct cat_desc cndesc
;
540 struct cat_attr cnattr
;
541 struct cat_fork cfork
;
542 BlockDescriptor blkdesc
;
543 BTNodeDescriptor
*ndp
;
545 BTreeControlBlockPtr btcb
= NULL
;
546 struct buf
*bp
= NULL
;
552 printf("Creating HFS+ Attribute B-tree File (%d nodes) on %s\n", nodecnt
, hfsmp
->vcbVN
);
555 * Set up Attribute B-tree vnode
557 bzero(&cndesc
, sizeof(cndesc
));
558 cndesc
.cd_parentcnid
= kHFSRootParentID
;
559 cndesc
.cd_flags
|= CD_ISMETA
;
560 cndesc
.cd_nameptr
= hfs_attrname
;
561 cndesc
.cd_namelen
= strlen(hfs_attrname
);
562 cndesc
.cd_cnid
= kHFSAttributesFileID
;
564 bzero(&cnattr
, sizeof(cnattr
));
566 cnattr
.ca_mode
= S_IFREG
;
567 cnattr
.ca_fileid
= cndesc
.cd_cnid
;
569 bzero(&cfork
, sizeof(cfork
));
570 cfork
.cf_clump
= nodesize
* nodecnt
;
572 result
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
, &vp
);
577 * Set up Attribute B-tree control block
579 MALLOC(btcb
, BTreeControlBlock
*, sizeof(BTreeControlBlock
), M_TEMP
, M_WAITOK
);
580 bzero(btcb
, sizeof(BTreeControlBlock
));
582 btcb
->nodeSize
= nodesize
;
583 btcb
->maxKeyLength
= kHFSPlusAttrKeyMaximumLength
;
584 btcb
->btreeType
= 0xFF;
585 btcb
->attributes
= kBTVariableIndexKeysMask
| kBTBigKeysMask
;
586 btcb
->version
= kBTreeVersion
;
587 btcb
->writeCount
= 1;
588 btcb
->flags
= 0; /* kBTHeaderDirty */
589 btcb
->fileRefNum
= vp
;
590 btcb
->getBlockProc
= GetBTreeBlock
;
591 btcb
->releaseBlockProc
= ReleaseBTreeBlock
;
592 btcb
->setEndOfForkProc
= ExtendBTreeFile
;
593 btcb
->keyCompareProc
= (KeyCompareProcPtr
)hfs_attrkeycompare
;
594 VTOF(vp
)->fcbBTCBPtr
= btcb
;
597 * Allocate some space
599 result
= ExtendBTreeFile(vp
, nodesize
, cfork
.cf_clump
);
603 btcb
->totalNodes
= VTOF(vp
)->ff_size
/ nodesize
;
604 btcb
->freeNodes
= btcb
->totalNodes
- 1;
607 * Initialize the b-tree header on disk
609 bp
= buf_getblk(vp
, 0, nodesize
, 0, 0, BLK_META
);
615 buffer
= (void *)buf_dataptr(bp
);
616 blkdesc
.buffer
= buffer
;
617 blkdesc
.blockHeader
= (void *)bp
;
618 blkdesc
.blockReadFromDisk
= 0;
619 blkdesc
.isModified
= 0;
621 ModifyBlockStart(vp
, &blkdesc
);
623 if (buf_size(bp
) != nodesize
)
624 panic("hfs_create_attr_btree: bad buffer size (%d)\n", buf_size(bp
));
626 bzero(buffer
, nodesize
);
627 index
= (int16_t *)buffer
;
629 /* FILL IN THE NODE DESCRIPTOR: */
630 ndp
= (BTNodeDescriptor
*)buffer
;
631 ndp
->kind
= kBTHeaderNode
;
633 offset
= sizeof(BTNodeDescriptor
);
634 index
[(nodesize
/ 2) - 1] = offset
;
636 /* FILL IN THE HEADER RECORD: */
637 bthp
= (BTHeaderRec
*)((UInt8
*)buffer
+ offset
);
638 bthp
->nodeSize
= nodesize
;
639 bthp
->totalNodes
= btcb
->totalNodes
;
640 bthp
->freeNodes
= btcb
->freeNodes
;
641 bthp
->clumpSize
= cfork
.cf_clump
;
642 bthp
->btreeType
= 0xFF;
643 bthp
->attributes
= kBTVariableIndexKeysMask
| kBTBigKeysMask
;
644 bthp
->maxKeyLength
= kHFSPlusAttrKeyMaximumLength
;
645 bthp
->keyCompareType
= kHFSBinaryCompare
;
646 offset
+= sizeof(BTHeaderRec
);
647 index
[(nodesize
/ 2) - 2] = offset
;
649 /* FILL IN THE USER RECORD: */
650 offset
+= kBTreeHeaderUserBytes
;
651 index
[(nodesize
/ 2) - 3] = offset
;
653 /* FILL IN THE MAP RECORD (only one node in use). */
654 *((u_int8_t
*)buffer
+ offset
) = 0x80;
655 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
656 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
657 index
[(nodesize
/ 2) - 4] = offset
;
660 result
= btree_journal_modify_block_end(hfsmp
, bp
);
662 result
= VNOP_BWRITE(bp
);
667 /* Publish new btree file */
668 hfsmp
->hfs_attribute_vp
= vp
;
669 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
672 hfs_unlock(VTOC(vp
));
678 // hfs_truncate(); /* XXX need to give back blocks */