2 * Copyright (c) 2000-2002 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;
147 OSStatus
ReleaseBTreeBlock(FileReference vp
, BlockDescPtr blockPtr
, ReleaseBlockOptions options
)
149 struct hfsmount
*hfsmp
= VTOHFS(vp
);
150 extern int bdwrite_internal(struct buf
*, int);
151 OSStatus retval
= E_NONE
;
152 struct buf
*bp
= NULL
;
154 bp
= (struct buf
*) blockPtr
->blockHeader
;
161 if (options
& kTrashBlock
) {
162 bp
->b_flags
|= B_INVAL
;
163 if (hfsmp
->jnl
&& (bp
->b_flags
& B_LOCKED
)) {
164 journal_kill_block(hfsmp
->jnl
, bp
);
166 brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
169 if (options
& kForceWriteBlock
) {
171 if (blockPtr
->isModified
== 0) {
172 panic("hfs: releaseblock: modified is 0 but forcewrite set! bp 0x%x\n", bp
);
174 retval
= journal_modify_block_end(hfsmp
->jnl
, bp
);
175 blockPtr
->isModified
= 0;
177 retval
= VOP_BWRITE(bp
);
179 } else if (options
& kMarkBlockDirty
) {
180 if ((options
& kLockTransaction
) && hfsmp
->jnl
== NULL
) {
183 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
184 * the buffer onto the LOCKED free list. This is necessary, otherwise
185 * getnewbuf() would try to reclaim the buffers using bawrite, which
186 * isn't going to work.
189 extern int count_lock_queue
__P((void));
190 /* Don't hog all the buffers... */
191 if (count_lock_queue() > kMaxLockedMetaBuffers
) {
192 hfs_btsync(vp
, HFS_SYNCTRANS
);
193 /* Rollback sync time to cause a sync on lock release... */
194 (void) BTSetLastSync(VTOF(vp
), time
.tv_sec
- (kMaxSecsForFsync
+ 1));
197 bp
->b_flags
|= B_LOCKED
;
201 * Delay-write this block.
202 * If the maximum delayed buffers has been exceeded then
203 * free up some buffers and fall back to an asynchronous write.
206 if (blockPtr
->isModified
== 0) {
207 panic("hfs: releaseblock: modified is 0 but markdirty set! bp 0x%x\n", bp
);
209 retval
= journal_modify_block_end(hfsmp
->jnl
, bp
);
210 blockPtr
->isModified
= 0;
211 } else if (bdwrite_internal(bp
, 1) != 0) {
213 /* Rollback sync time to cause a sync on lock release... */
214 (void) BTSetLastSync(VTOF(vp
), time
.tv_sec
- (kMaxSecsForFsync
+ 1));
215 bp
->b_flags
&= ~B_LOCKED
;
219 // check if we had previously called journal_modify_block_start()
220 // on this block and if so, abort it (which will call brelse()).
221 if (hfsmp
->jnl
&& blockPtr
->isModified
) {
222 // XXXdbg - I don't want to call modify_block_abort()
223 // because I think it may be screwing up the
224 // journal and blowing away a block that has
227 // journal_modify_block_abort(hfsmp->jnl, bp);
228 //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp);
229 journal_modify_block_end(hfsmp
->jnl
, bp
);
230 blockPtr
->isModified
= 0;
232 brelse(bp
); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
243 OSStatus
ExtendBTreeFile(FileReference vp
, FSSize minEOF
, FSSize maxEOF
)
245 #pragma unused (maxEOF)
247 OSStatus retval
, ret
;
248 UInt64 actualBytesAdded
, origSize
;
250 u_int32_t startAllocation
;
251 u_int32_t fileblocks
;
255 struct proc
*p
= NULL
;
258 filePtr
= GetFileControlBlock(vp
);
260 if ( minEOF
> filePtr
->fcbEOF
)
262 bytesToAdd
= minEOF
- filePtr
->fcbEOF
;
264 if (bytesToAdd
< filePtr
->ff_clumpsize
)
265 bytesToAdd
= filePtr
->ff_clumpsize
; //XXX why not always be a mutiple of clump size?
275 * The Extents B-tree can't have overflow extents. ExtendFileC will
276 * return an error if an attempt is made to extend the Extents B-tree
277 * when the resident extents are exhausted.
279 /* XXX warning - this can leave the volume bitmap unprotected during ExtendFileC call */
280 if(VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
)
283 /* lock extents b-tree (also protects volume bitmap) */
284 retval
= hfs_metafilelocking(VTOHFS(vp
), kHFSExtentsFileID
, LK_EXCLUSIVE
, p
);
289 (void) BTGetInformation(filePtr
, 0, &btInfo
);
293 * The b-tree code expects nodes to be contiguous. So when
294 * the allocation block size is less than the b-tree node
295 * size, we need to force disk allocations to be contiguous.
297 if (vcb
->blockSize
>= btInfo
.nodeSize
) {
300 /* Ensure that all b-tree nodes are contiguous on disk */
301 extendFlags
= kEFContigMask
;
305 origSize
= filePtr
->fcbEOF
;
306 fileblocks
= filePtr
->ff_blocks
;
307 startAllocation
= vcb
->nextAllocation
;
309 // loop trying to get a contiguous chunk that's an integer multiple
310 // of the btree node size. if we can't get a contiguous chunk that
311 // is at least the node size then we break out of the loop and let
312 // the error propagate back up.
314 retval
= ExtendFileC(vcb
, filePtr
, bytesToAdd
, 0, kEFContigMask
, &actualBytesAdded
);
315 if (retval
== dskFulErr
&& actualBytesAdded
== 0) {
317 if (bytesToAdd
== btInfo
.nodeSize
|| bytesToAdd
< (minEOF
- origSize
)) {
318 // if we're here there's nothing else to try, we're out
319 // of space so we break and bail out.
323 if (bytesToAdd
< btInfo
.nodeSize
) {
324 bytesToAdd
= btInfo
.nodeSize
;
325 } else if ((bytesToAdd
% btInfo
.nodeSize
) != 0) {
326 // make sure it's an integer multiple of the nodeSize
327 bytesToAdd
-= (bytesToAdd
% btInfo
.nodeSize
);
331 } while (retval
== dskFulErr
&& actualBytesAdded
== 0);
334 * If a new extent was added then move the roving allocator
335 * reference forward by the current b-tree file size so
336 * there's plenty of room to grow.
339 (vcb
->nextAllocation
> startAllocation
) &&
340 ((vcb
->nextAllocation
+ fileblocks
) < vcb
->totalBlocks
)) {
341 vcb
->nextAllocation
+= fileblocks
;
344 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
346 // XXXdbg ExtendFileC() could have returned an error even though
347 // it grew the file to be big enough for our needs. If this is
348 // the case, we don't care about retval so we blow it away.
350 if (filePtr
->fcbEOF
>= minEOF
&& retval
!= 0) {
354 // XXXdbg if the file grew but isn't large enough or isn't an
355 // even multiple of the nodeSize then trim things back. if
356 // the file isn't large enough we trim back to the original
357 // size. otherwise we trim back to be an even multiple of the
360 if ((filePtr
->fcbEOF
< minEOF
) || (actualBytesAdded
% btInfo
.nodeSize
) != 0) {
362 if (filePtr
->fcbEOF
< minEOF
) {
365 if (filePtr
->fcbEOF
< origSize
) {
366 panic("hfs: btree file eof %lld less than orig size %lld!\n",
367 filePtr
->fcbEOF
, origSize
);
370 trim
= filePtr
->fcbEOF
- origSize
;
371 if (trim
!= actualBytesAdded
) {
372 panic("hfs: trim == %lld but actualBytesAdded == %lld\n",
373 trim
, actualBytesAdded
);
376 trim
= (actualBytesAdded
% btInfo
.nodeSize
);
379 ret
= TruncateFileC(vcb
, filePtr
, filePtr
->fcbEOF
- trim
, 0);
380 filePtr
->fcbEOF
= (u_int64_t
)filePtr
->ff_blocks
* (u_int64_t
)vcb
->blockSize
;
382 // XXXdbg - panic if the file didn't get trimmed back properly
383 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
384 panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb 0x%x\n",
385 filePtr
->fcbEOF
, btInfo
.nodeSize
, filePtr
);
389 // XXXdbg - this probably doesn't need to be a panic()
390 panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %d)\n",
391 filePtr
->fcbEOF
, trim
, ret
);
394 actualBytesAdded
-= trim
;
397 if(VTOC(vp
)->c_fileid
!= kHFSExtentsFileID
) {
399 * Get any extents overflow b-tree changes to disk ASAP!
401 (void) BTFlushPath(VTOF(vcb
->extentsRefNum
));
402 (void) VOP_FSYNC(vcb
->extentsRefNum
, NOCRED
, MNT_WAIT
, p
);
404 (void) hfs_metafilelocking(VTOHFS(vp
), kHFSExtentsFileID
, LK_RELEASE
, p
);
407 if ((filePtr
->fcbEOF
% btInfo
.nodeSize
) != 0) {
408 panic("hfs: extendbtree: fcb 0x%x has eof 0x%llx not a multiple of 0x%x (trim %llx)\n",
409 filePtr
, filePtr
->fcbEOF
, btInfo
.nodeSize
, trim
);
413 * Update the Alternate MDB or Alternate VolumeHeader
415 if ((VTOC(vp
)->c_fileid
== kHFSExtentsFileID
) ||
416 (VTOC(vp
)->c_fileid
== kHFSCatalogFileID
) ||
417 (VTOC(vp
)->c_fileid
== kHFSAttributesFileID
)
420 ret
= hfs_flushvolumeheader(VCBTOHFS(vcb
), MNT_WAIT
, HFS_ALTFLUSH
);
423 ret
= ClearBTNodes(vp
, btInfo
.nodeSize
, filePtr
->fcbEOF
- actualBytesAdded
, actualBytesAdded
);
432 * Clear out (zero) new b-tree nodes on disk.
435 ClearBTNodes(struct vnode
*vp
, long blksize
, off_t offset
, off_t amount
)
437 struct hfsmount
*hfsmp
= VTOHFS(vp
);
438 struct buf
*bp
= NULL
;
442 blk
= offset
/ blksize
;
443 blkcnt
= amount
/ blksize
;
446 bp
= getblk(vp
, blk
, blksize
, 0, 0, BLK_META
);
452 // XXXdbg -- skipping this for now since it makes a transaction
453 // become *way* too large
454 //journal_modify_block_start(hfsmp->jnl, bp);
457 bzero((char *)bp
->b_data
, blksize
);
458 bp
->b_flags
|= B_AGE
;
462 // XXXdbg -- skipping this for now since it makes a transaction
463 // become *way* too large
464 //journal_modify_block_end(hfsmp->jnl, bp);
466 // XXXdbg - remove this once we decide what to do with the
467 // writes to the journal
473 /* wait/yield every 32 blocks so we don't hog all the buffers */