2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 #include <sys/param.h>
27 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/mount.h>
31 #include <sys/vnode.h>
35 #include "hfs_cnode.h"
37 #include "hfs_endian.h"
39 #include "hfscommon/headers/FileMgrInternal.h"
40 #include "hfscommon/headers/BTreesPrivate.h"
42 #define FORCESYNCBTREEWRITES 0
45 static int ClearBTNodes(struct vnode
*vp
, long blksize
, off_t offset
, off_t amount
);
49 OSStatus
SetBTreeBlockSize(FileReference vp
, ByteCount blockSize
, ItemCount minBlockCount
)
51 BTreeControlBlockPtr bTreePtr
;
53 DBG_ASSERT(vp
!= NULL
);
54 DBG_ASSERT(blockSize
>= kMinNodeSize
);
55 if (blockSize
> MAXBSIZE
)
56 return (fsBTBadNodeSize
);
58 bTreePtr
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
59 bTreePtr
->nodeSize
= blockSize
;
66 OSStatus
GetBTreeBlock(FileReference vp
, UInt32 blockNum
, GetBlockOptions options
, BlockDescriptor
*block
)
68 OSStatus retval
= E_NONE
;
69 struct buf
*bp
= NULL
;
71 if (options
& kGetEmptyBlock
)
72 bp
= getblk(vp
, blockNum
, block
->blockSize
, 0, 0, BLK_META
);
74 retval
= meta_bread(vp
, blockNum
, block
->blockSize
, NOCRED
, &bp
);
76 DBG_ASSERT(bp
!= NULL
);
77 DBG_ASSERT(bp
->b_data
!= NULL
);
78 DBG_ASSERT(bp
->b_bcount
== block
->blockSize
);
79 DBG_ASSERT(bp
->b_lblkno
== blockNum
);
82 retval
= -1; //XXX need better error
84 if (retval
== E_NONE
) {
85 block
->blockHeader
= bp
;
86 block
->buffer
= bp
->b_data
;
87 block
->blockReadFromDisk
= (bp
->b_flags
& B_CACHE
) == 0; /* not found in cache ==> came from disk */
90 block
->isModified
= 0;
92 #if BYTE_ORDER == LITTLE_ENDIAN
93 /* Endian swap B-Tree node (only if it's a valid block) */
94 if (!(options
& kGetEmptyBlock
)) {
95 /* This happens when we first open the b-tree, we might not have all the node data on hand */
96 if ((((BTNodeDescriptor
*)block
->buffer
)->kind
== kBTHeaderNode
) &&
97 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
!= bp
->b_bcount
) &&
98 (SWAP_BE16 (((BTHeaderRec
*)((char *)block
->buffer
+ 14))->nodeSize
) != bp
->b_bcount
)) {
100 /* Don't swap the descriptors at all, we don't care (this block will be invalidated) */
101 SWAP_BT_NODE (block
, ISHFSPLUS(VTOVCB(vp
)), VTOC(vp
)->c_fileid
, 3);
103 /* The node needs swapping */
104 } else if (*((UInt16
*)((char *)block
->buffer
+ (block
->blockSize
- sizeof (UInt16
)))) == 0x0e00) {
105 SWAP_BT_NODE (block
, ISHFSPLUS(VTOVCB(vp
)), VTOC(vp
)->c_fileid
, 0);
107 /* The node is not already in native byte order, hence corrupt */
108 } else if (*((UInt16
*)((char *)block
->buffer
+ (block
->blockSize
- sizeof (UInt16
)))) != 0x000e) {
109 panic ("%s Corrupt B-Tree node detected!\n", "GetBTreeBlock:");
117 block
->blockHeader
= NULL
;
118 block
->buffer
= NULL
;
126 void ModifyBlockStart(FileReference vp
, BlockDescPtr blockPtr
)
128 struct hfsmount
*hfsmp
= VTOHFS(vp
);
129 struct buf
*bp
= NULL
;
131 if (hfsmp
->jnl
== NULL
) {
135 bp
= (struct buf
*) blockPtr
->blockHeader
;
137 panic("ModifyBlockStart: null bp for blockdescptr 0x%x?!?\n", blockPtr
);
141 journal_modify_block_start(hfsmp
->jnl
, bp
);
142 blockPtr
->isModified
= 1;
146 btree_journal_modify_block_end(struct hfsmount
*hfsmp
, struct buf
*bp
)
148 #if BYTE_ORDER == LITTLE_ENDIAN
149 struct vnode
*vp
= bp
->b_vp
;
150 BlockDescriptor block
;
152 /* Prepare the block pointer */
153 block
.blockHeader
= bp
;
154 block
.buffer
= bp
->b_data
;
155 /* not found in cache ==> came from disk */
156 block
.blockReadFromDisk
= (bp
->b_flags
& B_CACHE
) == 0;
157 block
.blockSize
= bp
->b_bcount
;
159 // XXXdbg have to swap the data before it goes in the journal
160 SWAP_BT_NODE (&block
, ISHFSPLUS (VTOVCB(vp
)), VTOC(vp
)->c_fileid
, 1);
163 return journal_modify_block_end(hfsmp
->jnl
, bp
);
168 OSStatus
ReleaseBTreeBlock(FileReference vp
, BlockDescPtr blockPtr
, ReleaseBlockOptions options
)
170 struct hfsmount
*hfsmp
= VTOHFS(vp
);
171 extern int bdwrite_internal(struct buf
*, int);
172 OSStatus retval
= E_NONE
;
173 struct buf
*bp
= NULL
;
175 bp
= (struct buf
*) blockPtr
->blockHeader
;
182 if (options
& kTrashBlock
) {
183 bp
->b_flags
|= B_INVAL
;
184 if (hfsmp
->jnl
&& (bp
->b_flags
& B_LOCKED
)) {
185 journal_kill_block(hfsmp
->jnl
, bp
);
187 brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
190 if (options
& kForceWriteBlock
) {
192 if (blockPtr
->isModified
== 0) {
193 panic("hfs: releaseblock: modified is 0 but forcewrite set! bp 0x%x\n", bp
);
196 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
197 blockPtr
->isModified
= 0;
199 retval
= VOP_BWRITE(bp
);
201 } else if (options
& kMarkBlockDirty
) {
202 if ((options
& kLockTransaction
) && hfsmp
->jnl
== NULL
) {
205 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
206 * the buffer onto the LOCKED free list. This is necessary, otherwise
207 * getnewbuf() would try to reclaim the buffers using bawrite, which
208 * isn't going to work.
211 extern int count_lock_queue
__P((void));
212 /* Don't hog all the buffers... */
213 if (count_lock_queue() > kMaxLockedMetaBuffers
) {
214 hfs_btsync(vp
, HFS_SYNCTRANS
);
215 /* Rollback sync time to cause a sync on lock release... */
216 (void) BTSetLastSync(VTOF(vp
), time
.tv_sec
- (kMaxSecsForFsync
+ 1));
219 bp
->b_flags
|= B_LOCKED
;
223 * Delay-write this block.
224 * If the maximum delayed buffers has been exceeded then
225 * free up some buffers and fall back to an asynchronous write.
228 if (blockPtr
->isModified
== 0) {
229 panic("hfs: releaseblock: modified is 0 but markdirty set! bp 0x%x\n", bp
);
231 retval
= btree_journal_modify_block_end(hfsmp
, bp
);
232 blockPtr
->isModified
= 0;
233 } else if (bdwrite_internal(bp
, 1) != 0) {
235 /* Rollback sync time to cause a sync on lock release... */
236 (void) BTSetLastSync(VTOF(vp
), time
.tv_sec
- (kMaxSecsForFsync
+ 1));
237 bp
->b_flags
&= ~B_LOCKED
;
241 // check if we had previously called journal_modify_block_start()
242 // on this block and if so, abort it (which will call brelse()).
243 if (hfsmp
->jnl
&& blockPtr
->isModified
) {
244 // XXXdbg - I don't want to call modify_block_abort()
245 // because I think it may be screwing up the
246 // journal and blowing away a block that has
249 // journal_modify_block_abort(hfsmp->jnl, bp);
250 //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp);
251 btree_journal_modify_block_end(hfsmp
, bp
);
252 blockPtr
->isModified
= 0;
254 brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
265 OSStatus
ExtendBTreeFile(FileReference vp
, FSSize minEOF
, FSSize maxEOF
)
267 #pragma unused (maxEOF)
269 OSStatus retval
, ret
;
270 UInt64 actualBytesAdded
, origSize
;
272 u_int32_t startAllocation
;
273 u_int32_t fileblocks
;
277 struct proc
*p
= NULL
;
280 filePtr
= GetFileControlBlock(vp
);
282 if ( minEOF
> filePtr
->fcbEOF
)
284 bytesToAdd
= minEOF
- filePtr
->fcbEOF
;
286 if (bytesToAdd
< filePtr
->ff_clumpsize
)
287 bytesToAdd
= filePtr
->ff_clumpsize
; //XXX why not always be a mutiple of clump size?
297 * The Extents B-tree can't have overflow extents. ExtendFileC will
298 * return an error if an attempt is made to extend the Extents B-tree
299 * when the resident extents are exhausted.
301 /* XXX warning - this can leave the volume bitmap unprotected during ExtendFileC call */
302 if(VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
)
305 /* lock extents b-tree (also protects volume bitmap) */
306 retval
= hfs_metafilelocking(VTOHFS(vp
), kHFSExtentsFileID
, LK_EXCLUSIVE
, p
);
311 (void) BTGetInformation(filePtr
, 0, &btInfo
);
315 * The b-tree code expects nodes to be contiguous. So when
316 * the allocation block size is less than the b-tree node
317 * size, we need to force disk allocations to be contiguous.
319 if (vcb
->blockSize
>= btInfo
.nodeSize
) {
322 /* Ensure that all b-tree nodes are contiguous on disk */
323 extendFlags
= kEFContigMask
;
327 origSize
= filePtr
->fcbEOF
;
328 fileblocks
= filePtr
->ff_blocks
;
329 startAllocation
= vcb
->nextAllocation
;
331 // loop trying to get a contiguous chunk that's an integer multiple
332 // of the btree node size. if we can't get a contiguous chunk that
333 // is at least the node size then we break out of the loop and let
334 // the error propagate back up.
336 retval
= ExtendFileC(vcb
, filePtr
, bytesToAdd
, 0,
337 kEFContigMask
| kEFMetadataMask
,
339 if (retval
== dskFulErr
&& actualBytesAdded
== 0) {
341 if (bytesToAdd
== btInfo
.nodeSize
|| bytesToAdd
< (minEOF
- origSize
)) {
342 // if we're here there's nothing else to try, we're out
343 // of space so we break and bail out.
347 if (bytesToAdd
< btInfo
.nodeSize
) {
348 bytesToAdd
= btInfo
.nodeSize
;
349 } else if ((bytesToAdd
% btInfo
.nodeSize
) != 0) {
350 // make sure it's an integer multiple of the nodeSize
351 bytesToAdd
-= (bytesToAdd
% btInfo
.nodeSize
);
355 } while (retval
== dskFulErr
&& actualBytesAdded
== 0);
358 * If a new extent was added then move the roving allocator
359 * reference forward by the current b-tree file size so
360 * there's plenty of room to grow.
363 ((VCBTOHFS(vcb
)->hfs_flags
& HFS_METADATA_ZONE
) == 0) &&
364 (vcb
->nextAllocation
> startAllocation
) &&
365 ((vcb
->nextAllocation
+ fileblocks
) < vcb
->totalBlocks
)) {
366 vcb
->nextAllocation
+= fileblocks
;
369 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
371 // XXXdbg ExtendFileC() could have returned an error even though
372 // it grew the file to be big enough for our needs. If this is
373 // the case, we don't care about retval so we blow it away.
375 if (filePtr
->fcbEOF
>= minEOF
&& retval
!= 0) {
379 // XXXdbg if the file grew but isn't large enough or isn't an
380 // even multiple of the nodeSize then trim things back. if
381 // the file isn't large enough we trim back to the original
382 // size. otherwise we trim back to be an even multiple of the
385 if ((filePtr
->fcbEOF
< minEOF
) || (actualBytesAdded
% btInfo
.nodeSize
) != 0) {
387 if (filePtr
->fcbEOF
< minEOF
) {
390 if (filePtr
->fcbEOF
< origSize
) {
391 panic("hfs: btree file eof %lld less than orig size %lld!\n",
392 filePtr
->fcbEOF
, origSize
);
395 trim
= filePtr
->fcbEOF
- origSize
;
396 if (trim
!= actualBytesAdded
) {
397 panic("hfs: trim == %lld but actualBytesAdded == %lld\n",
398 trim
, actualBytesAdded
);
401 trim
= (actualBytesAdded
% btInfo
.nodeSize
);
404 ret
= TruncateFileC(vcb
, filePtr
, filePtr
->fcbEOF
- trim
, 0);
405 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
407 // XXXdbg - panic if the file didn't get trimmed back properly
408 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
409 panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb 0x%x\n",
410 filePtr
->fcbEOF
, btInfo
.nodeSize
, filePtr
);
414 // XXXdbg - this probably doesn't need to be a panic()
415 panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %d)\n",
416 filePtr
->fcbEOF
, trim
, ret
);
419 actualBytesAdded
-= trim
;
422 if(VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
) {
424 * Get any extents overflow b-tree changes to disk ASAP!
426 (void) BTFlushPath(VTOF(vcb
->extentsRefNum
));
427 (void) VOP_FSYNC(vcb
->extentsRefNum
, NOCRED
, MNT_WAIT
, p
);
429 (void) hfs_metafilelocking(VTOHFS(vp
), kHFSExtentsFileID
, LK_RELEASE
, p
);
432 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
433 panic("hfs: extendbtree: fcb 0x%x has eof 0x%llx not a multiple of 0x%x (trim %llx)\n",
434 filePtr
, filePtr
->fcbEOF
, btInfo
.nodeSize
, trim
);
438 * Update the Alternate MDB or Alternate VolumeHeader
440 if ((VTOC(vp
)->c_fileid
== kHFSExtentsFileID
) ||
441 (VTOC(vp
)->c_fileid
== kHFSCatalogFileID
) ||
442 (VTOC(vp
)->c_fileid
== kHFSAttributesFileID
)
445 ret
= hfs_flushvolumeheader(VCBTOHFS(vcb
), MNT_WAIT
, HFS_ALTFLUSH
);
447 struct timeval tv
= time
;
449 VTOC(vp
)->c_flag
|= C_CHANGE
| C_UPDATE
;
450 (void) VOP_UPDATE(vp
, &tv
, &tv
, MNT_WAIT
);
453 ret
= ClearBTNodes(vp
, btInfo
.nodeSize
, filePtr
->fcbEOF
- actualBytesAdded
, actualBytesAdded
);
462 * Clear out (zero) new b-tree nodes on disk.
465 ClearBTNodes(struct vnode
*vp
, long blksize
, off_t offset
, off_t amount
)
467 struct hfsmount
*hfsmp
= VTOHFS(vp
);
468 struct buf
*bp
= NULL
;
472 blk
= offset
/ blksize
;
473 blkcnt
= amount
/ blksize
;
476 bp
= getblk(vp
, blk
, blksize
, 0, 0, BLK_META
);
482 // XXXdbg -- skipping this for now since it makes a transaction
483 // become *way* too large
484 //journal_modify_block_start(hfsmp->jnl, bp);
487 bzero((char *)bp
->b_data
, blksize
);
488 bp
->b_flags
|= B_AGE
;
492 // XXXdbg -- skipping this for now since it makes a transaction
493 // become *way* too large
494 //journal_modify_block_end(hfsmp->jnl, bp);
496 // XXXdbg - remove this once we decide what to do with the
497 // writes to the journal
503 /* wait/yield every 32 blocks so we don't hog all the buffers */