]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/hfs/hfscommon/Misc/VolumeAllocation.c
xnu-344.49.tar.gz
[apple/xnu.git] / bsd / hfs / hfscommon / Misc / VolumeAllocation.c
index 8f074d0b14212320a669acdb0389ba8eb18b2413..8908358e537359ad3e23d20bb942546d30335cf0 100644 (file)
@@ -1,21 +1,24 @@
 /*
- * Copyright (c) 2000-2001 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@
  */
@@ -230,11 +233,11 @@ OSErr BlockAllocate (
        //
        //      If the disk is already full, don't bother.
        //
-       if (vcb->freeBlocks == 0) {
+       if (hfs_freeblks(VCBTOHFS(vcb), 0) == 0) {
                err = dskFulErr;
                goto Exit;
        }
-       if (forceContiguous && vcb->freeBlocks < minBlocks) {
+       if (forceContiguous && hfs_freeblks(VCBTOHFS(vcb), 0) < minBlocks) {
                err = dskFulErr;
                goto Exit;
        }
@@ -256,6 +259,12 @@ OSErr BlockAllocate (
        //
        if (forceContiguous) {
                err = BlockAllocateContig(vcb, startingBlock, minBlocks, maxBlocks, actualStartBlock, actualNumBlocks);
+               /*
+                * If we allocated from a new position then
+                * also update the roving allocatior.
+                */
+               if ((err == noErr) && (*actualStartBlock > startingBlock))
+                       vcb->nextAllocation = *actualStartBlock;
        } else {
                /*
                 * Scan the bitmap once, gather the N largest free extents, then
@@ -347,6 +356,8 @@ OSErr BlockDeallocate (
        //
        VCB_LOCK(vcb);
        vcb->freeBlocks += numBlocks;
+       if (vcb->nextAllocation == (firstBlock + numBlocks))
+               vcb->nextAllocation -= numBlocks;
        VCB_UNLOCK(vcb);
        MarkVCBDirty(vcb);
 
@@ -468,8 +479,14 @@ static OSErr ReleaseBitmapBlock(
 
        if (bp) {
                if (dirty) {
-                       bp->b_flags |= B_DIRTY;
-                       bdwrite(bp);
+                       // XXXdbg
+                       struct hfsmount *hfsmp = VCBTOHFS(vcb);
+                       
+                       if (hfsmp->jnl) {
+                               journal_modify_block_end(hfsmp->jnl, bp);
+                       } else {
+                               bdwrite(bp);
+                       }
                } else {
                        brelse(bp);
                }
@@ -590,6 +607,7 @@ static OSErr BlockAllocateAny(
        UInt32  bitsPerBlock;
        UInt32  wordsPerBlock;
        Boolean dirty = false;
+       struct hfsmount *hfsmp = VCBTOHFS(vcb);
 
        //      Since this routine doesn't wrap around
        if (maxBlocks > (endingBlock - startingBlock)) {
@@ -671,6 +689,11 @@ static OSErr BlockAllocateAny(
                endingBlock = block + maxBlocks;        //      if we get this far, we've found enough
        }
        
+       // XXXdbg
+       if (hfsmp->jnl) {
+               journal_modify_block_start(hfsmp->jnl, (struct buf *)blockRef);
+       }
+
        //
        //      Allocate all of the consecutive blocks
        //
@@ -702,6 +725,11 @@ static OSErr BlockAllocateAny(
                                if (err != noErr) goto Exit;
                 buffer = currCache;
 
+                               // XXXdbg
+                               if (hfsmp->jnl) {
+                                       journal_modify_block_start(hfsmp->jnl, (struct buf *)blockRef);
+                               }
+                               
                                wordsLeft = wordsPerBlock;
                        }
                        
@@ -838,6 +866,8 @@ static OSErr BlockMarkAllocated(
        UInt32  blockRef;
        UInt32  bitsPerBlock;
        UInt32  wordsPerBlock;
+       // XXXdbg
+       struct hfsmount *hfsmp = VCBTOHFS(vcb);
 
        //
        //      Pre-read the bitmap block containing the first word of allocation
@@ -859,6 +889,11 @@ static OSErr BlockMarkAllocated(
                wordsLeft = wordsPerBlock - wordIndexInBlock;
        }
        
+       // XXXdbg
+       if (hfsmp->jnl) {
+               journal_modify_block_start(hfsmp->jnl, (struct buf *)blockRef);
+       }
+
        //
        //      If the first block to allocate doesn't start on a word
        //      boundary in the bitmap, then treat that first word
@@ -902,6 +937,11 @@ static OSErr BlockMarkAllocated(
                        err = ReadBitmapBlock(vcb, startingBlock, &buffer, &blockRef);
                        if (err != noErr) goto Exit;
 
+                       // XXXdbg
+                       if (hfsmp->jnl) {
+                               journal_modify_block_start(hfsmp->jnl, (struct buf *)blockRef);
+                       }
+
                        //      Readjust currentWord and wordsLeft
                        currentWord = buffer;
                        wordsLeft = wordsPerBlock;
@@ -935,6 +975,11 @@ static OSErr BlockMarkAllocated(
                        err = ReadBitmapBlock(vcb, startingBlock, &buffer, &blockRef);
                        if (err != noErr) goto Exit;
 
+                       // XXXdbg
+                       if (hfsmp->jnl) {
+                               journal_modify_block_start(hfsmp->jnl, (struct buf *)blockRef);
+                       }
+                       
                        //      Readjust currentWord and wordsLeft
                        currentWord = buffer;
                        wordsLeft = wordsPerBlock;
@@ -988,6 +1033,8 @@ static OSErr BlockMarkFree(
        UInt32  blockRef;
        UInt32  bitsPerBlock;
        UInt32  wordsPerBlock;
+    // XXXdbg
+       struct hfsmount *hfsmp = VCBTOHFS(vcb);
 
        //
        //      Pre-read the bitmap block containing the first word of allocation
@@ -995,6 +1042,11 @@ static OSErr BlockMarkFree(
 
        err = ReadBitmapBlock(vcb, startingBlock, &buffer, &blockRef);
        if (err != noErr) goto Exit;
+       // XXXdbg
+       if (hfsmp->jnl) {
+               journal_modify_block_start(hfsmp->jnl, (struct buf *)blockRef);
+       }
+
        //
        //      Initialize currentWord, and wordsLeft.
        //
@@ -1051,6 +1103,11 @@ static OSErr BlockMarkFree(
                        err = ReadBitmapBlock(vcb, startingBlock, &buffer, &blockRef);
                        if (err != noErr) goto Exit;
 
+                       // XXXdbg
+                       if (hfsmp->jnl) {
+                               journal_modify_block_start(hfsmp->jnl, (struct buf *)blockRef);
+                       }
+
                        //      Readjust currentWord and wordsLeft
                        currentWord = buffer;
                        wordsLeft = wordsPerBlock;
@@ -1085,6 +1142,11 @@ static OSErr BlockMarkFree(
                        err = ReadBitmapBlock(vcb, startingBlock, &buffer, &blockRef);
                        if (err != noErr) goto Exit;
 
+                       // XXXdbg
+                       if (hfsmp->jnl) {
+                               journal_modify_block_start(hfsmp->jnl, (struct buf *)blockRef);
+                       }
+                       
                        //      Readjust currentWord and wordsLeft
                        currentWord = buffer;
                        wordsLeft = wordsPerBlock;
@@ -1165,6 +1227,7 @@ static OSErr BlockFindContiguous(
 
        stopBlock = endingBlock - minBlocks + 1;
        currentBlock = startingBlock;
+       firstBlock = 0;
        
        //
        //      Pre-read the first bitmap block.
@@ -1195,7 +1258,7 @@ static OSErr BlockFindContiguous(
                bitMask = currentBlock & kBitsWithinWordMask;
                if (bitMask)
                {                       
-                       tempWord = *currentWord;                        //      Fetch the current word only once
+                       tempWord = SWAP_BE32(*currentWord);                     //      Fetch the current word only once
                        bitMask = kHighBitInWordMask >> bitMask;
                        while (tempWord & bitMask)
                        {
@@ -1232,7 +1295,7 @@ static OSErr BlockFindContiguous(
                        }
                        
                        //      See if any of the bits are clear
-                       if ((tempWord=*currentWord) + 1)        //      non-zero if any bits were clear
+                       if ((tempWord = SWAP_BE32(*currentWord)) + 1)   //      non-zero if any bits were clear
                        {
                                //      Figure out which bit is clear
                                bitMask = kHighBitInWordMask;
@@ -1271,7 +1334,7 @@ FoundUnused:
                bitMask = currentBlock & kBitsWithinWordMask;
                if (bitMask)
                {
-                       tempWord = *currentWord;                        //      Fetch the current word only once
+                       tempWord = SWAP_BE32(*currentWord);                     //      Fetch the current word only once
                        bitMask = kHighBitInWordMask >> bitMask;
                        while (bitMask && !(tempWord & bitMask))
                        {
@@ -1308,7 +1371,7 @@ FoundUnused:
                        }
                        
                        //      See if any of the bits are set
-                       if ((tempWord=*currentWord) != 0)
+                       if ((tempWord = SWAP_BE32(*currentWord)) != 0)
                        {
                                //      Figure out which bit is set
                                bitMask = kHighBitInWordMask;