]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/hfs/hfs_btreeio.c
xnu-344.21.73.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_btreeio.c
index 67e52e0efe5d3d000288cbfdd1bf8e1111404155..3f7c3cc0ccb9707815275970f802db62cbe28de7 100644 (file)
@@ -1,50 +1,38 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
-/*     @(#)hfs_btreeio.c
-*
-*      (c) 1998, 2000 Apple Computer, Inc.  All Rights Reserved
-*
-*      hfs_btreeio.c -- I/O Routines for the HFS B-tree files.
-*
-*      HISTORY
-*      15-Feb-2000     Don Brady       Added ClearBTNodes.
-*      16-Jul-1998     Don Brady               In ExtendBtreeFile force all b-tree nodes to be contiguous on disk.
-*       4-Jun-1998     Pat Dirks               Changed to do all B*-Tree writes synchronously (FORCESYNCBTREEWRITES = 1)
-*      18-apr-1998     Don Brady               Call brelse on bread failure.
-*      17-Apr-1998     Pat Dirks               Fixed ReleaseBTreeBlock to not call brelse when bwrite or bdwrite is called.
-*      13-apr-1998     Don Brady               Add ExtendBTreeFile routine (from BTreeWrapper.c).
-*      26-mar-1998     Don Brady               SetBTreeBlockSize was incorrectly excluding 512 byte blockSize.
-*      18-feb-1998     Don Brady               Initially created file.
-*
-*/
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/buf.h>
+#include <sys/kernel.h>
 #include <sys/mount.h>
 #include <sys/vnode.h>
 
 
 #include "hfs.h"
+#include "hfs_cnode.h"
 #include "hfs_dbg.h"
 #include "hfs_endian.h"
 
 
 #define FORCESYNCBTREEWRITES 0
 
-static OSStatus FlushAlternate( ExtendedVCB *vcb );
 
 static int ClearBTNodes(struct vnode *vp, long blksize, off_t offset, off_t amount);
 
 
+__private_extern__
 OSStatus SetBTreeBlockSize(FileReference vp, ByteCount blockSize, ItemCount minBlockCount)
 {
        BTreeControlBlockPtr    bTreePtr;
        
        DBG_ASSERT(vp != NULL);
-       DBG_ASSERT(VTOFCB(vp) != NULL);
-       DBG_ASSERT(VTOFCB(vp)->fcbBTCBPtr != NULL);
        DBG_ASSERT(blockSize >= kMinNodeSize);
     if (blockSize > MAXBSIZE )
         return (fsBTBadNodeSize);
 
-    DBG_TREE(("SetBlockSizeProc: blockSize=%ld for file %ld\n", blockSize, H_FILEID(VTOH(vp))));
-
-       bTreePtr = (BTreeControlBlockPtr)(VTOH(vp)->fcbBTCBPtr);
+       bTreePtr = (BTreeControlBlockPtr)VTOF(vp)->fcbBTCBPtr;
        bTreePtr->nodeSize = blockSize;
        
     return (E_NONE);
 }
 
 
+__private_extern__
 OSStatus GetBTreeBlock(FileReference vp, UInt32 blockNum, GetBlockOptions options, BlockDescriptor *block)
 {
     OSStatus    retval = E_NONE;
     struct buf   *bp = NULL;
 
-    if (options & kGetEmptyBlock)
-        bp = getblk (vp,
-                    IOBLKNOFORBLK(blockNum, VTOHFS(vp)->hfs_phys_block_size),
-                    IOBYTECCNTFORBLK(blockNum, block->blockSize, VTOHFS(vp)->hfs_phys_block_size),
-                    0,
-                    0,
-                    BLK_META);
-    else
-        retval = meta_bread(vp,
-                        IOBLKNOFORBLK(blockNum, VTOHFS(vp)->hfs_phys_block_size),
-                        IOBYTECCNTFORBLK(blockNum, block->blockSize, VTOHFS(vp)->hfs_phys_block_size),
-                        NOCRED,
-                        &bp);
+       if (options & kGetEmptyBlock)
+               bp = getblk(vp, blockNum, block->blockSize, 0, 0, BLK_META);
+       else
+               retval = meta_bread(vp, blockNum, block->blockSize, NOCRED, &bp);
 
     DBG_ASSERT(bp != NULL);
     DBG_ASSERT(bp->b_data != NULL);
@@ -107,9 +83,12 @@ OSStatus GetBTreeBlock(FileReference vp, UInt32 blockNum, GetBlockOptions option
 
     if (retval == E_NONE) {
         block->blockHeader = bp;
-        block->buffer = bp->b_data + IOBYTEOFFSETFORBLK(bp->b_blkno, VTOHFS(vp)->hfs_phys_block_size);
+        block->buffer = bp->b_data;
         block->blockReadFromDisk = (bp->b_flags & B_CACHE) == 0;       /* not found in cache ==> came from disk */
 
+               // XXXdbg 
+               block->isModified = 0;
+
 #if BYTE_ORDER == LITTLE_ENDIAN
         /* Endian swap B-Tree node (only if it's a valid block) */
         if (!(options & kGetEmptyBlock)) {
@@ -119,11 +98,11 @@ OSStatus GetBTreeBlock(FileReference vp, UInt32 blockNum, GetBlockOptions option
                 (SWAP_BE16 (((BTHeaderRec *)((char *)block->buffer + 14))->nodeSize) != bp->b_bcount)) {
 
                 /* Don't swap the descriptors at all, we don't care (this block will be invalidated) */
-                SWAP_BT_NODE (block, ISHFSPLUS(VTOVCB(vp)), H_FILEID(VTOH(vp)), 3);
+                SWAP_BT_NODE (block, ISHFSPLUS(VTOVCB(vp)), VTOC(vp)->c_fileid, 3);
 
             /* The node needs swapping */
             } else if (*((UInt16 *)((char *)block->buffer + (block->blockSize - sizeof (UInt16)))) == 0x0e00) {
-                SWAP_BT_NODE (block, ISHFSPLUS(VTOVCB(vp)), H_FILEID(VTOH(vp)), 0);
+                SWAP_BT_NODE (block, ISHFSPLUS(VTOVCB(vp)), VTOC(vp)->c_fileid, 0);
 #if 0
             /* The node is not already in native byte order, hence corrupt */
             } else if (*((UInt16 *)((char *)block->buffer + (block->blockSize - sizeof (UInt16)))) != 0x000e) {
@@ -143,48 +122,115 @@ OSStatus GetBTreeBlock(FileReference vp, UInt32 blockNum, GetBlockOptions option
 }
 
 
+__private_extern__
+void ModifyBlockStart(FileReference vp, BlockDescPtr blockPtr)
+{
+       struct hfsmount *hfsmp = VTOHFS(vp);
+    struct buf *bp = NULL;
+
+       if (hfsmp->jnl == NULL) {
+               return;
+       }
+       
+    bp = (struct buf *) blockPtr->blockHeader;
+    if (bp == NULL) {
+               panic("ModifyBlockStart: null bp  for blockdescptr 0x%x?!?\n", blockPtr);
+               return;
+    }
+
+       journal_modify_block_start(hfsmp->jnl, bp);
+       blockPtr->isModified = 1;
+}
+
+
+__private_extern__
 OSStatus ReleaseBTreeBlock(FileReference vp, BlockDescPtr blockPtr, ReleaseBlockOptions options)
 {
+    struct hfsmount    *hfsmp = VTOHFS(vp);
+    extern int bdwrite_internal(struct buf *, int);
     OSStatus   retval = E_NONE;
     struct buf *bp = NULL;
 
     bp = (struct buf *) blockPtr->blockHeader;
 
     if (bp == NULL) {
-        DBG_TREE(("ReleaseBlockProc: blockHeader is zero!\n"));
         retval = -1;
         goto exit;
     }
 
     if (options & kTrashBlock) {
-        bp->b_flags |= B_INVAL;
-       brelse(bp);     /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
+               bp->b_flags |= B_INVAL;
+               if (hfsmp->jnl && (bp->b_flags & B_LOCKED)) {
+                       journal_kill_block(hfsmp->jnl, bp);
+               } else {
+                       brelse(bp);     /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
+               }
     } else {
         if (options & kForceWriteBlock) {
-            bp->b_flags |= B_DIRTY;
-            retval = VOP_BWRITE(bp);
+                       if (hfsmp->jnl) {
+                               if (blockPtr->isModified == 0) {
+                                       panic("hfs: releaseblock: modified is 0 but forcewrite set! bp 0x%x\n", bp);
+                               }
+                               retval = journal_modify_block_end(hfsmp->jnl, bp);
+                               blockPtr->isModified = 0;
+                       } else {
+                               retval = VOP_BWRITE(bp);
+                       }
         } else if (options & kMarkBlockDirty) {
-            bp->b_flags |= B_DIRTY;
-#if FORCESYNCBTREEWRITES
-            VOP_BWRITE(bp);
-#else
-                       if (options & kLockTransaction) {
-
-                   /*
-                    *
-                    * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
-                    * the buffer onto the LOCKED free list.  This is necessary, otherwise
-                    * getnewbuf() would try to reclaim the buffers using bawrite, which
-                    * isn't going to work.
-                    *
-                    */
-                   bp->b_flags |= B_LOCKED;
-               };
-            bdwrite(bp);
+            if ((options & kLockTransaction) && hfsmp->jnl == NULL) {
+                /*
+                 *
+                 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
+                 * the buffer onto the LOCKED free list.  This is necessary, otherwise
+                 * getnewbuf() would try to reclaim the buffers using bawrite, which
+                 * isn't going to work.
+                 *
+                 */
+                extern int count_lock_queue __P((void));
+                /* Don't hog all the buffers... */
+                if (count_lock_queue() > kMaxLockedMetaBuffers) {
+                     hfs_btsync(vp, HFS_SYNCTRANS);
+                     /* Rollback sync time to cause a sync on lock release... */
+                     (void) BTSetLastSync(VTOF(vp), time.tv_sec - (kMaxSecsForFsync + 1));
+                }
+
+                               bp->b_flags |= B_LOCKED;
+            }
 
-#endif
+            /* 
+             * Delay-write this block.
+             * If the maximum delayed buffers has been exceeded then
+             * free up some buffers and fall back to an asynchronous write.
+             */
+                       if (hfsmp->jnl) {
+                               if (blockPtr->isModified == 0) {
+                                       panic("hfs: releaseblock: modified is 0 but markdirty set! bp 0x%x\n", bp);
+                               }
+                               retval = journal_modify_block_end(hfsmp->jnl, bp);
+                               blockPtr->isModified = 0;
+                       } else if (bdwrite_internal(bp, 1) != 0) {
+                hfs_btsync(vp, 0);
+                /* Rollback sync time to cause a sync on lock release... */
+                (void) BTSetLastSync(VTOF(vp), time.tv_sec - (kMaxSecsForFsync + 1));
+                bp->b_flags &= ~B_LOCKED;
+                bawrite(bp);
+            }
         } else {
-               brelse(bp);     /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
+                       // check if we had previously called journal_modify_block_start() 
+                       // on this block and if so, abort it (which will call brelse()).
+                       if (hfsmp->jnl && blockPtr->isModified) {
+                               // XXXdbg - I don't want to call modify_block_abort()
+                               //          because I think it may be screwing up the
+                               //          journal and blowing away a block that has
+                               //          valid data in it.
+                               //   
+                               //    journal_modify_block_abort(hfsmp->jnl, bp);
+                               //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp);
+                               journal_modify_block_end(hfsmp->jnl, bp);
+                               blockPtr->isModified = 0;
+                       } else {
+                               brelse(bp);     /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
+                       }
         };
     };
 
@@ -193,19 +239,21 @@ exit:
 }
 
 
+__private_extern__
 OSStatus ExtendBTreeFile(FileReference vp, FSSize minEOF, FSSize maxEOF)
 {
 #pragma unused (maxEOF)
 
-       OSStatus        retval;
-       UInt64          actualBytesAdded;
+       OSStatus        retval, ret;
+       UInt64          actualBytesAdded, origSize;
        UInt64          bytesToAdd;
-    UInt32             extendFlags;
+       u_int32_t       startAllocation;
+       u_int32_t       fileblocks;
        BTreeInfoRec btInfo;
        ExtendedVCB     *vcb;
        FCB                     *filePtr;
     struct proc *p = NULL;
-
+       UInt64          trim = 0;       
 
        filePtr = GetFileControlBlock(vp);
 
@@ -213,16 +261,15 @@ OSStatus ExtendBTreeFile(FileReference vp, FSSize minEOF, FSSize maxEOF)
        {
                bytesToAdd = minEOF - filePtr->fcbEOF;
 
-               if (bytesToAdd < filePtr->fcbClmpSize)
-                       bytesToAdd = filePtr->fcbClmpSize;              //XXX why not always be a mutiple of clump size?
+               if (bytesToAdd < filePtr->ff_clumpsize)
+                       bytesToAdd = filePtr->ff_clumpsize;             //XXX why not always be a mutiple of clump size?
        }
        else
        {
-               DBG_TREE((" ExtendBTreeFile: minEOF is smaller than current size!"));
                return -1;
        }
 
-       vcb = FCBTOVCB(filePtr);
+       vcb = VTOVCB(vp);
        
        /*
         * The Extents B-tree can't have overflow extents. ExtendFileC will
@@ -230,94 +277,154 @@ OSStatus ExtendBTreeFile(FileReference vp, FSSize minEOF, FSSize maxEOF)
         * when the resident extents are exhausted.
         */
     /* XXX warning - this can leave the volume bitmap unprotected during ExtendFileC call */
-       if(H_FILEID(filePtr) != kHFSExtentsFileID)
+       if(VTOC(vp)->c_fileid != kHFSExtentsFileID)
        {
                p = current_proc();
                /* lock extents b-tree (also protects volume bitmap) */
-               retval = hfs_metafilelocking(VTOHFS(vp), kHFSExtentsFileID, LK_EXCLUSIVE, p);
+               retval = hfs_metafilelocking(VTOHFS(vp), kHFSExtentsFileID, LK_EXCLUSIVE, p);
                if (retval)
                        return (retval);
        }
 
     (void) BTGetInformation(filePtr, 0, &btInfo);
 
+#if 0  // XXXdbg
        /*
-     * The b-tree code expects nodes to be contiguous. So when
+        * The b-tree code expects nodes to be contiguous. So when
         * the allocation block size is less than the b-tree node
-     * size, we need to force disk allocations to be contiguous.
-     */
+        * size, we need to force disk allocations to be contiguous.
+        */
        if (vcb->blockSize >= btInfo.nodeSize) {
                extendFlags = 0;
        } else {
                /* Ensure that all b-tree nodes are contiguous on disk */
-               extendFlags = kEFAllMask | kEFContigMask;
+               extendFlags = kEFContigMask;
        }
+#endif
 
-    retval = ExtendFileC(vcb, filePtr, bytesToAdd, extendFlags, &actualBytesAdded );
-
-       if(H_FILEID(filePtr) != kHFSExtentsFileID)
-               (void) hfs_metafilelocking(VTOHFS(vp), kHFSExtentsFileID, LK_RELEASE, p);
-
-       if (retval)
-               return (retval);
-
-       if (actualBytesAdded < bytesToAdd)
-               DBG_TREE((" ExtendBTreeFile: actualBytesAdded < bytesToAdd!"));
-       
-       filePtr->fcbEOF = filePtr->fcbPLen;
-
-       retval = ClearBTNodes(vp, btInfo.nodeSize, filePtr->fcbEOF - actualBytesAdded, actualBytesAdded);       
-       if (retval)
-               return (retval);
+       origSize = filePtr->fcbEOF;
+       fileblocks = filePtr->ff_blocks;
+       startAllocation = vcb->nextAllocation;
+
+       // loop trying to get a contiguous chunk that's an integer multiple
+       // of the btree node size.  if we can't get a contiguous chunk that
+       // is at least the node size then we break out of the loop and let
+       // the error propagate back up.
+       do {
+               retval = ExtendFileC(vcb, filePtr, bytesToAdd, 0, kEFContigMask, &actualBytesAdded);
+               if (retval == dskFulErr && actualBytesAdded == 0) {
+
+                       if (bytesToAdd == btInfo.nodeSize || bytesToAdd < (minEOF - origSize)) {
+                               // if we're here there's nothing else to try, we're out
+                               // of space so we break and bail out.
+                               break;
+                       } else {
+                               bytesToAdd >>= 1;
+                               if (bytesToAdd < btInfo.nodeSize) {
+                                       bytesToAdd = btInfo.nodeSize;
+                               } else if ((bytesToAdd % btInfo.nodeSize) != 0) {
+                                       // make sure it's an integer multiple of the nodeSize
+                                       bytesToAdd -= (bytesToAdd % btInfo.nodeSize);
+                               }
+                       }
+               }
+       } while (retval == dskFulErr && actualBytesAdded == 0);
        
        /*
-        * Update the Alternate MDB or Alternate VolumeHeader
+        * If a new extent was added then move the roving allocator
+        * reference forward by the current b-tree file size so 
+        * there's plenty of room to grow.
         */
-       if ((H_FILEID(filePtr) == kHFSExtentsFileID)    ||
-           (H_FILEID(filePtr) == kHFSCatalogFileID)    ||
-           (H_FILEID(filePtr) == kHFSAttributesFileID)
-          ) {
-               MarkVCBDirty( vcb );
-               if (vcb->vcbSigWord == kHFSPlusSigWord) {
-                       retval = hfs_flushvolumeheader(VCBTOHFS(vcb), 0);
+       if ((retval == 0) &&
+           (vcb->nextAllocation > startAllocation) &&
+           ((vcb->nextAllocation + fileblocks) < vcb->totalBlocks)) {
+               vcb->nextAllocation += fileblocks;
+       }
+               
+       filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize;
+
+       // XXXdbg ExtendFileC() could have returned an error even though
+       // it grew the file to be big enough for our needs.  If this is
+       // the case, we don't care about retval so we blow it away.
+       //
+       if (filePtr->fcbEOF >= minEOF && retval != 0) {
+               retval = 0;
+       }
+
+       // XXXdbg if the file grew but isn't large enough or isn't an
+       // even multiple of the nodeSize then trim things back.  if
+       // the file isn't large enough we trim back to the original
+       // size.  otherwise we trim back to be an even multiple of the
+       // btree node size.
+       //
+       if ((filePtr->fcbEOF < minEOF) || (actualBytesAdded % btInfo.nodeSize) != 0) {
+
+               if (filePtr->fcbEOF < minEOF) {
+                       retval = dskFulErr;
+                       
+                       if (filePtr->fcbEOF < origSize) {
+                               panic("hfs: btree file eof %lld less than orig size %lld!\n",
+                                         filePtr->fcbEOF, origSize);
+                       }
+                       
+                       trim = filePtr->fcbEOF - origSize;
+                       if (trim != actualBytesAdded) {
+                               panic("hfs: trim == %lld but actualBytesAdded == %lld\n",
+                                         trim, actualBytesAdded);
+                       }
                } else {
-                       retval = hfs_flushMDB(VCBTOHFS(vcb), 0);
+                       trim = (actualBytesAdded % btInfo.nodeSize);
                }
-               if (retval == 0) {
-                       retval = FlushAlternate(vcb);
+
+               ret = TruncateFileC(vcb, filePtr, filePtr->fcbEOF - trim, 0);
+               filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize;
+
+               // XXXdbg - panic if the file didn't get trimmed back properly
+               if ((filePtr->fcbEOF % btInfo.nodeSize) != 0) {
+                       panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb 0x%x\n",
+                                 filePtr->fcbEOF, btInfo.nodeSize, filePtr);
                }
+
+               if (ret) {
+                       // XXXdbg - this probably doesn't need to be a panic()
+                       panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %d)\n",
+                                 filePtr->fcbEOF, trim, ret);
+                       return ret;
+               }
+               actualBytesAdded -= trim;
        }
-       
-       return retval;
-}
 
+       if(VTOC(vp)->c_fileid != kHFSExtentsFileID) {
+               /*
+                * Get any extents overflow b-tree changes to disk ASAP!
+                */
+               (void) BTFlushPath(VTOF(vcb->extentsRefNum));
+               (void) VOP_FSYNC(vcb->extentsRefNum, NOCRED, MNT_WAIT, p);
 
-static OSStatus
-FlushAlternate( ExtendedVCB *vcb )
-{
-       void *maindata;
-       void *altdata;
-       int result;
-
-       /* Get the main MDB/VolumeHeader block */
-       result = GetBlock_glue(gbDefault,
-                       (vcb->hfsPlusIOPosOffset / kHFSBlockSize) + kMasterDirectoryBlock,
-                       (Ptr *)&maindata, kNoFileReference, vcb);
-       if (result) return (result);
-       
-       /* Get the alternate MDB/VolumeHeader block */
-       result = GetBlock_glue( gbDefault, vcb->altIDSector,
-                       (Ptr *)&altdata, kNoFileReference, vcb );
+               (void) hfs_metafilelocking(VTOHFS(vp), kHFSExtentsFileID, LK_RELEASE, p);
+       }
 
-       if (result == 0) {
-               bcopy(maindata, altdata, kMDBSize);
+       if ((filePtr->fcbEOF % btInfo.nodeSize) != 0) {
+               panic("hfs: extendbtree: fcb 0x%x has eof 0x%llx not a multiple of 0x%x (trim %llx)\n",
+                         filePtr, filePtr->fcbEOF, btInfo.nodeSize, trim);
+       }
 
-               result = RelBlock_glue( (Ptr)altdata, rbWriteMask );
+       /*
+        * Update the Alternate MDB or Alternate VolumeHeader
+        */
+       if ((VTOC(vp)->c_fileid == kHFSExtentsFileID)   ||
+           (VTOC(vp)->c_fileid == kHFSCatalogFileID)   ||
+           (VTOC(vp)->c_fileid == kHFSAttributesFileID)
+          ) {
+               MarkVCBDirty( vcb );
+               ret = hfs_flushvolumeheader(VCBTOHFS(vcb), MNT_WAIT, HFS_ALTFLUSH);
        }
 
-       (void) RelBlock_glue( (Ptr)maindata, rbFreeMask );
+       ret = ClearBTNodes(vp, btInfo.nodeSize, filePtr->fcbEOF - actualBytesAdded, actualBytesAdded);
+       if (ret)
+               return (ret);
        
-       return (result);
+       return retval;
 }
 
 
@@ -327,6 +434,7 @@ FlushAlternate( ExtendedVCB *vcb )
 static int
 ClearBTNodes(struct vnode *vp, long blksize, off_t offset, off_t amount)
 {
+       struct hfsmount *hfsmp = VTOHFS(vp);
        struct buf *bp = NULL;
        daddr_t blk;
        daddr_t blkcnt;
@@ -338,14 +446,36 @@ ClearBTNodes(struct vnode *vp, long blksize, off_t offset, off_t amount)
                bp = getblk(vp, blk, blksize, 0, 0, BLK_META);
                if (bp == NULL)
                        continue;
-               bzero((char *)bp->b_data, blksize);
-               bp->b_flags |= (B_DIRTY | B_AGE);
 
-                /* wait/yield every 32 blocks so we don't hog all the buffers */
-               if ((blk % 32) == 0)
-                       VOP_BWRITE(bp);
-               else
-                       bawrite(bp);
+        // XXXdbg
+               if (hfsmp->jnl) {
+                       // XXXdbg -- skipping this for now since it makes a transaction
+                       //           become *way* too large
+                   //journal_modify_block_start(hfsmp->jnl, bp);
+               }
+
+               bzero((char *)bp->b_data, blksize);
+               bp->b_flags |= B_AGE;
+
+        // XXXdbg
+               if (hfsmp->jnl) {
+                       // XXXdbg -- skipping this for now since it makes a transaction
+                       //           become *way* too large
+                       //journal_modify_block_end(hfsmp->jnl, bp);
+
+                       // XXXdbg - remove this once we decide what to do with the
+                       //          writes to the journal
+                       if ((blk % 32) == 0)
+                           VOP_BWRITE(bp);
+                       else
+                           bawrite(bp);
+               } else {
+                       /* wait/yield every 32 blocks so we don't hog all the buffers */
+                       if ((blk % 32) == 0)
+                               VOP_BWRITE(bp);
+                       else
+                               bawrite(bp);
+               }
                --blkcnt;
                ++blk;
        }