]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfscommon/Misc/VolumeAllocation.c
2 * Copyright (c) 2000-2001 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 File: VolumeAllocation.c
25 Contains: Routines for accessing and modifying the volume bitmap.
29 Copyright: © 1996-2001 by Apple Computer, Inc., all rights reserved.
36 Allocate space on a volume. Can allocate space contiguously.
37 If not contiguous, then allocation may be less than what was
38 asked for. Returns the starting block number, and number of
39 blocks. (Will only do a single extent???)
41 Deallocate a contiguous run of allocation blocks.
46 Mark a contiguous range of blocks as free. The corresponding
47 bits in the volume bitmap will be cleared.
49 Mark a contiguous range of blocks as allocated. The cor-
50 responding bits in the volume bitmap are set. Also tests to see
51 if any of the blocks were previously unallocated.
53 Find a contiguous range of blocks of a given size. The caller
54 specifies where to begin the search (by block number). The
55 block number of the first block in the range is returned.
57 Find and allocate a contiguous range of blocks up to a given size. The
58 first range of contiguous free blocks found are allocated, even if there
59 are fewer blocks than requested (and even if a contiguous range of blocks
60 of the given size exists elsewhere).
62 Find and allocate a contiguous range of blocks of a given size. If
63 a contiguous range of free blocks of the given size isn't found, then
64 the allocation fails (i.e. it is "all or nothing").
67 Try to allocate space from known free space in the volume's
71 Given an allocation block number, read the bitmap block that
72 contains that allocation block into a caller-supplied buffer.
75 Release a bitmap block back into the buffer cache.
78 #include "../../hfs_macos_defs.h"
80 #include <sys/types.h>
82 #include <sys/systm.h>
84 #include "../../hfs.h"
85 #include "../../hfs_dbg.h"
86 #include "../../hfs_format.h"
87 #include "../../hfs_endian.h"
89 #include "../headers/FileMgrInternal.h"
97 kBitsWithinWordMask
= kBitsPerWord
-1
100 #define kLowBitInWordMask 0x00000001ul
101 #define kHighBitInWordMask 0x80000000ul
102 #define kAllBitsSetInWord 0xFFFFFFFFul
105 static OSErr
ReadBitmapBlock(
111 static OSErr
ReleaseBitmapBlock(
116 static OSErr
BlockAllocateAny(
118 UInt32 startingBlock
,
121 UInt32
*actualStartBlock
,
122 UInt32
*actualNumBlocks
);
124 static OSErr
BlockAllocateContig(
126 UInt32 startingBlock
,
129 UInt32
*actualStartBlock
,
130 UInt32
*actualNumBlocks
);
132 static OSErr
BlockFindContiguous(
134 UInt32 startingBlock
,
138 UInt32
*actualStartBlock
,
139 UInt32
*actualNumBlocks
);
141 static OSErr
BlockMarkAllocated(
143 UInt32 startingBlock
,
146 static OSErr
BlockMarkFree(
148 UInt32 startingBlock
,
151 static OSErr
BlockAllocateKnown(
154 UInt32
*actualStartBlock
,
155 UInt32
*actualNumBlocks
);
159 ;________________________________________________________________________________
163 ; Function: Allocate space on a volume. If contiguous allocation is requested,
164 ; at least the requested number of bytes will be allocated or an
165 ; error will be returned. If contiguous allocation is not forced,
166 ; the space will be allocated at the first free fragment following
167 ; the requested starting allocation block. If there is not enough
168 ; room there, a block of less than the requested size will be
171 ; If the requested starting block is 0 (for new file allocations),
172 ; the volume's allocation block pointer will be used as a starting
175 ; All requests will be rounded up to the next highest clump size, as
176 ; indicated in the file's FCB.
179 ; vcb - Pointer to ExtendedVCB for the volume to allocate space on
180 ; fcb - Pointer to FCB for the file for which storage is being allocated
181 ; startingBlock - Preferred starting allocation block, 0 = no preference
182 ; forceContiguous - Force contiguous flag - if bit 0 set (NE), allocation is contiguous
183 ; or an error is returned
184 ; bytesRequested - Number of bytes requested. If the allocation is non-contiguous,
185 ; less than this may actually be allocated
186 ; bytesMaximum - The maximum number of bytes to allocate. If there is additional free
187 ; space after bytesRequested, then up to bytesMaximum bytes should really
188 ; be allocated. (Used by ExtendFileC to round up allocations to a multiple
189 ; of the file's clump size.)
192 ; (result) - Error code, zero for successful allocation
193 ; *startBlock - Actual starting allocation block
194 ; *actualBlocks - Actual number of allocation blocks allocated
197 ; The volume bitmap is read and updated; the volume bitmap cache may be changed.
198 ;________________________________________________________________________________
201 OSErr
BlockAllocate (
202 ExtendedVCB
*vcb
, /* which volume to allocate space on */
203 UInt32 startingBlock
, /* preferred starting block, or 0 for no preference */
204 SInt64 bytesRequested
, /* desired number of BYTES to allocate */
205 SInt64 bytesMaximum
, /* maximum number of bytes to allocate */
206 Boolean forceContiguous
, /* non-zero to force contiguous allocation and to force */
207 /* bytesRequested bytes to actually be allocated */
208 UInt32
*actualStartBlock
, /* actual first block of allocation */
209 UInt32
*actualNumBlocks
) /* number of blocks actually allocated; if forceContiguous */
210 /* was zero, then this may represent fewer than bytesRequested */
214 UInt32 minBlocks
; // minimum number of allocation blocks requested
215 UInt32 maxBlocks
; // number of allocation blocks requested, rounded to clump size
216 Boolean updateAllocPtr
= false; // true if nextAllocation needs to be updated
219 // Initialize outputs in case we get an error
221 *actualStartBlock
= 0;
222 *actualNumBlocks
= 0;
225 // Compute the number of allocation blocks requested, and maximum
227 minBlocks
= FileBytesToBlocks(bytesRequested
, vcb
->blockSize
);
228 maxBlocks
= FileBytesToBlocks(bytesMaximum
, vcb
->blockSize
);
231 // If the disk is already full, don't bother.
233 if (vcb
->freeBlocks
== 0) {
237 if (forceContiguous
&& vcb
->freeBlocks
< minBlocks
) {
243 // If caller didn't specify a starting block number, then use the volume's
244 // next block to allocate from.
246 if (startingBlock
== 0) {
248 startingBlock
= vcb
->nextAllocation
;
250 updateAllocPtr
= true;
254 // If the request must be contiguous, then find a sequence of free blocks
255 // that is long enough. Otherwise, find the first free block.
257 if (forceContiguous
) {
258 err
= BlockAllocateContig(vcb
, startingBlock
, minBlocks
, maxBlocks
, actualStartBlock
, actualNumBlocks
);
261 * Scan the bitmap once, gather the N largest free extents, then
262 * allocate from these largest extents. Repeat as needed until
263 * we get all the space we needed. We could probably build up
264 * that list when the higher level caller tried (and failed) a
265 * contiguous allocation first.
267 err
= BlockAllocateKnown(vcb
, maxBlocks
, actualStartBlock
, actualNumBlocks
);
268 if (err
== dskFulErr
)
269 err
= BlockAllocateAny(vcb
, startingBlock
, vcb
->totalBlocks
, maxBlocks
, actualStartBlock
, actualNumBlocks
);
270 if (err
== dskFulErr
)
271 err
= BlockAllocateAny(vcb
, 0, startingBlock
, maxBlocks
, actualStartBlock
, actualNumBlocks
);
276 // If we used the volume's roving allocation pointer, then we need to update it.
277 // Adding in the length of the current allocation might reduce the next allocate
278 // call by avoiding a re-scan of the already allocated space. However, the clump
279 // just allocated can quite conceivably end up being truncated or released when
280 // the file is closed or its EOF changed. Leaving the allocation pointer at the
281 // start of the last allocation will avoid unnecessary fragmentation in this case.
286 vcb
->nextAllocation
= *actualStartBlock
;
289 // Update the number of free blocks on the volume
291 vcb
->freeBlocks
-= *actualNumBlocks
;
304 ;________________________________________________________________________________
306 ; Routine: BlkDealloc
308 ; Function: Update the bitmap to deallocate a run of disk allocation blocks
311 ; vcb - Pointer to ExtendedVCB for the volume to free space on
312 ; firstBlock - First allocation block to be freed
313 ; numBlocks - Number of allocation blocks to free up (must be > 0!)
316 ; (result) - Result code
319 ; The volume bitmap is read and updated; the volume bitmap cache may be changed.
320 ;________________________________________________________________________________
323 OSErr
BlockDeallocate (
324 ExtendedVCB
*vcb
, // Which volume to deallocate space on
325 UInt32 firstBlock
, // First block in range to deallocate
326 UInt32 numBlocks
) // Number of contiguous blocks to deallocate
331 // If no blocks to deallocate, then exit early
333 if (numBlocks
== 0) {
339 // Call internal routine to free the sequence of blocks
341 err
= BlockMarkFree(vcb
, firstBlock
, numBlocks
);
346 // Update the volume's free block count, and mark the VCB as dirty.
349 vcb
->freeBlocks
+= numBlocks
;
360 ;_______________________________________________________________________
362 ; Routine: FileBytesToBlocks
364 ; Function: Divide numerator by denominator, rounding up the result if there
365 ; was a remainder. This is frequently used for computing the number
366 ; of whole and/or partial blocks used by some count of bytes.
367 ; Actuall divides a 64 bit by a 32 bit into a 32bit result
369 ; CAREFULL!!! THIS CAN CAUSE OVERFLOW....USER BEWARE!!!
370 ;_______________________________________________________________________
372 UInt32
FileBytesToBlocks(
378 quotient
= (UInt32
)(numerator
/ denominator
);
379 if (quotient
* denominator
!= numerator
)
388 ;_______________________________________________________________________
390 ; Routine: ReadBitmapBlock
392 ; Function: Read in a bitmap block corresponding to a given allocation
393 ; block (bit). Return a pointer to the bitmap block.
396 ; vcb -- Pointer to ExtendedVCB
397 ; bit -- Allocation block whose bitmap block is desired
400 ; buffer -- Pointer to bitmap block corresonding to "block"
402 ;_______________________________________________________________________
404 static OSErr
ReadBitmapBlock(
411 struct buf
*bp
= NULL
;
412 struct vnode
*vp
= NULL
;
417 * volume bitmap blocks are protected by the Extents B-tree lock
419 REQUIRE_FILE_LOCK(vcb
->extentsRefNum
, false);
421 blockSize
= (UInt32
)vcb
->vcbVBMIOSize
;
422 block
= bit
/ (blockSize
* kBitsPerByte
);
424 if (vcb
->vcbSigWord
== kHFSPlusSigWord
) {
425 vp
= vcb
->allocationsRefNum
; /* use allocation file vnode */
428 vp
= VCBTOHFS(vcb
)->hfs_devvp
; /* use device I/O vnode */
429 block
+= vcb
->vcbVBMSt
; /* map to physical block */
432 err
= meta_bread(vp
, block
, blockSize
, NOCRED
, &bp
);
440 *blockRef
= (UInt32
)bp
;
441 *buffer
= (UInt32
*)bp
->b_data
;
450 ;_______________________________________________________________________
452 ; Routine: ReleaseBitmapBlock
454 ; Function: Relase a bitmap block.
460 ;_______________________________________________________________________
462 static OSErr
ReleaseBitmapBlock(
467 struct buf
*bp
= (struct buf
*)blockRef
;
471 bp
->b_flags
|= B_DIRTY
;
483 _______________________________________________________________________
485 Routine: BlockAllocateContig
487 Function: Allocate a contiguous group of allocation blocks. The
488 allocation is all-or-nothing. The caller guarantees that
489 there are enough free blocks (though they may not be
490 contiguous, in which case this call will fail).
493 vcb Pointer to volume where space is to be allocated
494 startingBlock Preferred first block for allocation
495 minBlocks Minimum number of contiguous blocks to allocate
496 maxBlocks Maximum number of contiguous blocks to allocate
499 actualStartBlock First block of range allocated, or 0 if error
500 actualNumBlocks Number of blocks allocated, or 0 if error
501 _______________________________________________________________________
503 static OSErr
BlockAllocateContig(
505 UInt32 startingBlock
,
508 UInt32
*actualStartBlock
,
509 UInt32
*actualNumBlocks
)
514 // Find a contiguous group of blocks at least minBlocks long.
515 // Determine the number of contiguous blocks available (up
520 * NOTE: If the only contiguous free extent of at least minBlocks
521 * crosses startingBlock (i.e. starts before, ends after), then we
522 * won't find it. Earlier versions *did* find this case by letting
523 * the second search look past startingBlock by minBlocks. But
524 * with the free extent cache, this can lead to duplicate entries
525 * in the cache, causing the same blocks to be allocated twice.
527 err
= BlockFindContiguous(vcb
, startingBlock
, vcb
->totalBlocks
, minBlocks
, maxBlocks
,
528 actualStartBlock
, actualNumBlocks
);
529 if (err
== dskFulErr
&& startingBlock
!= 0) {
531 * Constrain the endingBlock so we don't bother looking for ranges
532 * that would overlap those found in the previous call.
534 err
= BlockFindContiguous(vcb
, 0, startingBlock
, minBlocks
, maxBlocks
,
535 actualStartBlock
, actualNumBlocks
);
537 if (err
!= noErr
) goto Exit
;
540 // Now mark those blocks allocated.
542 err
= BlockMarkAllocated(vcb
, *actualStartBlock
, *actualNumBlocks
);
546 *actualStartBlock
= 0;
547 *actualNumBlocks
= 0;
554 _______________________________________________________________________
556 Routine: BlockAllocateAny
558 Function: Allocate one or more allocation blocks. If there are fewer
559 free blocks than requested, all free blocks will be
560 allocated. The caller guarantees that there is at least
564 vcb Pointer to volume where space is to be allocated
565 startingBlock Preferred first block for allocation
566 endingBlock Last block to check + 1
567 maxBlocks Maximum number of contiguous blocks to allocate
570 actualStartBlock First block of range allocated, or 0 if error
571 actualNumBlocks Number of blocks allocated, or 0 if error
572 _______________________________________________________________________
574 static OSErr
BlockAllocateAny(
576 UInt32 startingBlock
,
577 register UInt32 endingBlock
,
579 UInt32
*actualStartBlock
,
580 UInt32
*actualNumBlocks
)
583 register UInt32 block
; // current block number
584 register UInt32 currentWord
; // Pointer to current word within bitmap block
585 register UInt32 bitMask
; // Word with given bits already set (ready to OR in)
586 register UInt32 wordsLeft
; // Number of words left in this bitmap block
587 UInt32
*buffer
= NULL
;
588 UInt32
*currCache
= NULL
;
591 UInt32 wordsPerBlock
;
592 Boolean dirty
= false;
594 // Since this routine doesn't wrap around
595 if (maxBlocks
> (endingBlock
- startingBlock
)) {
596 maxBlocks
= endingBlock
- startingBlock
;
600 // Pre-read the first bitmap block
602 err
= ReadBitmapBlock(vcb
, startingBlock
, &currCache
, &blockRef
);
603 if (err
!= noErr
) goto Exit
;
607 // Set up the current position within the block
610 UInt32 wordIndexInBlock
;
612 bitsPerBlock
= vcb
->vcbVBMIOSize
* kBitsPerByte
;
613 wordsPerBlock
= vcb
->vcbVBMIOSize
/ kBytesPerWord
;
615 wordIndexInBlock
= (startingBlock
& (bitsPerBlock
-1)) / kBitsPerWord
;
616 buffer
+= wordIndexInBlock
;
617 wordsLeft
= wordsPerBlock
- wordIndexInBlock
;
618 currentWord
= SWAP_BE32 (*buffer
);
619 bitMask
= kHighBitInWordMask
>> (startingBlock
& kBitsWithinWordMask
);
623 // Find the first unallocated block
626 while (block
< endingBlock
) {
627 if ((currentWord
& bitMask
) == 0)
635 bitMask
= kHighBitInWordMask
;
638 if (--wordsLeft
== 0) {
640 buffer
= currCache
= NULL
;
641 err
= ReleaseBitmapBlock(vcb
, blockRef
, false);
642 if (err
!= noErr
) goto Exit
;
644 err
= ReadBitmapBlock(vcb
, block
, &currCache
, &blockRef
);
645 if (err
!= noErr
) goto Exit
;
648 wordsLeft
= wordsPerBlock
;
651 currentWord
= SWAP_BE32 (*buffer
);
655 // Did we get to the end of the bitmap before finding a free block?
656 // If so, then couldn't allocate anything.
657 if (block
== endingBlock
) {
662 // Return the first block in the allocated range
663 *actualStartBlock
= block
;
666 // If we could get the desired number of blocks before hitting endingBlock,
667 // then adjust endingBlock so we won't keep looking. Ideally, the comparison
668 // would be (block + maxBlocks) < endingBlock, but that could overflow. The
669 // comparison below yields identical results, but without overflow.
670 if (block
< (endingBlock
-maxBlocks
)) {
671 endingBlock
= block
+ maxBlocks
; // if we get this far, we've found enough
675 // Allocate all of the consecutive blocks
677 while ((currentWord
& bitMask
) == 0) {
678 // Allocate this block
679 currentWord
|= bitMask
;
681 // Move to the next block. If no more, then exit.
683 if (block
== endingBlock
)
689 *buffer
= SWAP_BE32 (currentWord
); // update value in bitmap
692 bitMask
= kHighBitInWordMask
;
695 if (--wordsLeft
== 0) {
697 buffer
= currCache
= NULL
;
698 err
= ReleaseBitmapBlock(vcb
, blockRef
, true);
699 if (err
!= noErr
) goto Exit
;
701 err
= ReadBitmapBlock(vcb
, block
, &currCache
, &blockRef
);
702 if (err
!= noErr
) goto Exit
;
705 wordsLeft
= wordsPerBlock
;
708 currentWord
= SWAP_BE32 (*buffer
);
711 *buffer
= SWAP_BE32 (currentWord
); // update the last change
715 *actualNumBlocks
= block
- *actualStartBlock
;
718 *actualStartBlock
= 0;
719 *actualNumBlocks
= 0;
723 (void) ReleaseBitmapBlock(vcb
, blockRef
, dirty
);
730 _______________________________________________________________________
732 Routine: BlockAllocateKnown
734 Function: Try to allocate space from known free space in the free
738 vcb Pointer to volume where space is to be allocated
739 maxBlocks Maximum number of contiguous blocks to allocate
742 actualStartBlock First block of range allocated, or 0 if error
743 actualNumBlocks Number of blocks allocated, or 0 if error
746 dskFulErr Free extent cache is empty
747 _______________________________________________________________________
749 static OSErr
BlockAllocateKnown(
752 UInt32
*actualStartBlock
,
753 UInt32
*actualNumBlocks
)
757 UInt32 newStartBlock
, newBlockCount
;
759 if (vcb
->vcbFreeExtCnt
== 0)
762 // Just grab up to maxBlocks of the first (largest) free exent.
763 *actualStartBlock
= vcb
->vcbFreeExt
[0].startBlock
;
764 foundBlocks
= vcb
->vcbFreeExt
[0].blockCount
;
765 if (foundBlocks
> maxBlocks
)
766 foundBlocks
= maxBlocks
;
767 *actualNumBlocks
= foundBlocks
;
769 // Adjust the start and length of that extent.
770 newStartBlock
= vcb
->vcbFreeExt
[0].startBlock
+ foundBlocks
;
771 newBlockCount
= vcb
->vcbFreeExt
[0].blockCount
- foundBlocks
;
773 // The first extent might not be the largest anymore. Bubble up any
774 // (now larger) extents to the top of the list.
775 for (i
=1; i
<vcb
->vcbFreeExtCnt
; ++i
)
777 if (vcb
->vcbFreeExt
[i
].blockCount
> newBlockCount
)
779 vcb
->vcbFreeExt
[i
-1].startBlock
= vcb
->vcbFreeExt
[i
].startBlock
;
780 vcb
->vcbFreeExt
[i
-1].blockCount
= vcb
->vcbFreeExt
[i
].blockCount
;
788 // If this is now the smallest known free extent, then it might be smaller than
789 // other extents we didn't keep track of. So, just forget about this extent.
790 // After the previous loop, (i-1) is the index of the extent we just allocated from.
791 if (i
== vcb
->vcbFreeExtCnt
)
793 // It's now the smallest extent, so forget about it
794 --vcb
->vcbFreeExtCnt
;
798 // It's not the smallest, so store it in its proper place
799 vcb
->vcbFreeExt
[i
-1].startBlock
= newStartBlock
;
800 vcb
->vcbFreeExt
[i
-1].blockCount
= newBlockCount
;
804 // Now mark the found extent in the bitmap
806 return BlockMarkAllocated(vcb
, *actualStartBlock
, *actualNumBlocks
);
812 _______________________________________________________________________
814 Routine: BlockMarkAllocated
816 Function: Mark a contiguous group of blocks as allocated (set in the
817 bitmap). It assumes those bits are currently marked
818 deallocated (clear in the bitmap).
821 vcb Pointer to volume where space is to be allocated
822 startingBlock First block number to mark as allocated
823 numBlocks Number of blocks to mark as allocated
824 _______________________________________________________________________
826 static OSErr
BlockMarkAllocated(
828 UInt32 startingBlock
,
829 register UInt32 numBlocks
)
832 register UInt32
*currentWord
; // Pointer to current word within bitmap block
833 register UInt32 wordsLeft
; // Number of words left in this bitmap block
834 register UInt32 bitMask
; // Word with given bits already set (ready to OR in)
835 UInt32 firstBit
; // Bit index within word of first bit to allocate
836 UInt32 numBits
; // Number of bits in word to allocate
837 UInt32
*buffer
= NULL
;
840 UInt32 wordsPerBlock
;
843 // Pre-read the bitmap block containing the first word of allocation
846 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
847 if (err
!= noErr
) goto Exit
;
849 // Initialize currentWord, and wordsLeft.
852 UInt32 wordIndexInBlock
;
854 bitsPerBlock
= vcb
->vcbVBMIOSize
* kBitsPerByte
;
855 wordsPerBlock
= vcb
->vcbVBMIOSize
/ kBytesPerWord
;
857 wordIndexInBlock
= (startingBlock
& (bitsPerBlock
-1)) / kBitsPerWord
;
858 currentWord
= buffer
+ wordIndexInBlock
;
859 wordsLeft
= wordsPerBlock
- wordIndexInBlock
;
863 // If the first block to allocate doesn't start on a word
864 // boundary in the bitmap, then treat that first word
868 firstBit
= startingBlock
% kBitsPerWord
;
870 bitMask
= kAllBitsSetInWord
>> firstBit
; // turn off all bits before firstBit
871 numBits
= kBitsPerWord
- firstBit
; // number of remaining bits in this word
872 if (numBits
> numBlocks
) {
873 numBits
= numBlocks
; // entire allocation is inside this one word
874 bitMask
&= ~(kAllBitsSetInWord
>> (firstBit
+ numBits
)); // turn off bits after last
877 if ((*currentWord
& SWAP_BE32 (bitMask
)) != 0) {
878 panic("BlockMarkAllocated: blocks already allocated!");
881 *currentWord
|= SWAP_BE32 (bitMask
); // set the bits in the bitmap
882 numBlocks
-= numBits
; // adjust number of blocks left to allocate
884 ++currentWord
; // move to next word
885 --wordsLeft
; // one less word left in this block
889 // Allocate whole words (32 blocks) at a time.
892 bitMask
= kAllBitsSetInWord
; // put this in a register for 68K
893 while (numBlocks
>= kBitsPerWord
) {
894 if (wordsLeft
== 0) {
895 // Read in the next bitmap block
896 startingBlock
+= bitsPerBlock
; // generate a block number in the next bitmap block
899 err
= ReleaseBitmapBlock(vcb
, blockRef
, true);
900 if (err
!= noErr
) goto Exit
;
902 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
903 if (err
!= noErr
) goto Exit
;
905 // Readjust currentWord and wordsLeft
906 currentWord
= buffer
;
907 wordsLeft
= wordsPerBlock
;
910 if (*currentWord
!= 0) {
911 panic("BlockMarkAllocated: blocks already allocated!");
914 *currentWord
= SWAP_BE32 (bitMask
);
915 numBlocks
-= kBitsPerWord
;
917 ++currentWord
; // move to next word
918 --wordsLeft
; // one less word left in this block
922 // Allocate any remaining blocks.
925 if (numBlocks
!= 0) {
926 bitMask
= ~(kAllBitsSetInWord
>> numBlocks
); // set first numBlocks bits
927 if (wordsLeft
== 0) {
928 // Read in the next bitmap block
929 startingBlock
+= bitsPerBlock
; // generate a block number in the next bitmap block
932 err
= ReleaseBitmapBlock(vcb
, blockRef
, true);
933 if (err
!= noErr
) goto Exit
;
935 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
936 if (err
!= noErr
) goto Exit
;
938 // Readjust currentWord and wordsLeft
939 currentWord
= buffer
;
940 wordsLeft
= wordsPerBlock
;
943 if ((*currentWord
& SWAP_BE32 (bitMask
)) != 0) {
944 panic("BlockMarkAllocated: blocks already allocated!");
947 *currentWord
|= SWAP_BE32 (bitMask
); // set the bits in the bitmap
949 // No need to update currentWord or wordsLeft
955 (void)ReleaseBitmapBlock(vcb
, blockRef
, true);
962 _______________________________________________________________________
964 Routine: BlockMarkFree
966 Function: Mark a contiguous group of blocks as free (clear in the
967 bitmap). It assumes those bits are currently marked
968 allocated (set in the bitmap).
971 vcb Pointer to volume where space is to be freed
972 startingBlock First block number to mark as freed
973 numBlocks Number of blocks to mark as freed
974 _______________________________________________________________________
976 static OSErr
BlockMarkFree(
978 UInt32 startingBlock
,
979 register UInt32 numBlocks
)
982 register UInt32
*currentWord
; // Pointer to current word within bitmap block
983 register UInt32 wordsLeft
; // Number of words left in this bitmap block
984 register UInt32 bitMask
; // Word with given bits already set (ready to OR in)
985 UInt32 firstBit
; // Bit index within word of first bit to allocate
986 UInt32 numBits
; // Number of bits in word to allocate
987 UInt32
*buffer
= NULL
;
990 UInt32 wordsPerBlock
;
993 // Pre-read the bitmap block containing the first word of allocation
996 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
997 if (err
!= noErr
) goto Exit
;
999 // Initialize currentWord, and wordsLeft.
1002 UInt32 wordIndexInBlock
;
1004 bitsPerBlock
= vcb
->vcbVBMIOSize
* kBitsPerByte
;
1005 wordsPerBlock
= vcb
->vcbVBMIOSize
/ kBytesPerWord
;
1007 wordIndexInBlock
= (startingBlock
& (bitsPerBlock
-1)) / kBitsPerWord
;
1008 currentWord
= buffer
+ wordIndexInBlock
;
1009 wordsLeft
= wordsPerBlock
- wordIndexInBlock
;
1013 // If the first block to free doesn't start on a word
1014 // boundary in the bitmap, then treat that first word
1018 firstBit
= startingBlock
% kBitsPerWord
;
1019 if (firstBit
!= 0) {
1020 bitMask
= kAllBitsSetInWord
>> firstBit
; // turn off all bits before firstBit
1021 numBits
= kBitsPerWord
- firstBit
; // number of remaining bits in this word
1022 if (numBits
> numBlocks
) {
1023 numBits
= numBlocks
; // entire allocation is inside this one word
1024 bitMask
&= ~(kAllBitsSetInWord
>> (firstBit
+ numBits
)); // turn off bits after last
1027 if ((*currentWord
& SWAP_BE32 (bitMask
)) != SWAP_BE32 (bitMask
)) {
1028 panic("BlockMarkFree: blocks not allocated!");
1031 *currentWord
&= SWAP_BE32 (~bitMask
); // clear the bits in the bitmap
1032 numBlocks
-= numBits
; // adjust number of blocks left to free
1034 ++currentWord
; // move to next word
1035 --wordsLeft
; // one less word left in this block
1039 // Free whole words (32 blocks) at a time.
1042 while (numBlocks
>= kBitsPerWord
) {
1043 if (wordsLeft
== 0) {
1044 // Read in the next bitmap block
1045 startingBlock
+= bitsPerBlock
; // generate a block number in the next bitmap block
1048 err
= ReleaseBitmapBlock(vcb
, blockRef
, true);
1049 if (err
!= noErr
) goto Exit
;
1051 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
1052 if (err
!= noErr
) goto Exit
;
1054 // Readjust currentWord and wordsLeft
1055 currentWord
= buffer
;
1056 wordsLeft
= wordsPerBlock
;
1060 if (*currentWord
!= SWAP_BE32 (kAllBitsSetInWord
)) {
1061 panic("BlockMarkFree: blocks not allocated!");
1064 *currentWord
= 0; // clear the entire word
1065 numBlocks
-= kBitsPerWord
;
1067 ++currentWord
; // move to next word
1068 --wordsLeft
; // one less word left in this block
1072 // Free any remaining blocks.
1075 if (numBlocks
!= 0) {
1076 bitMask
= ~(kAllBitsSetInWord
>> numBlocks
); // set first numBlocks bits
1077 if (wordsLeft
== 0) {
1078 // Read in the next bitmap block
1079 startingBlock
+= bitsPerBlock
; // generate a block number in the next bitmap block
1082 err
= ReleaseBitmapBlock(vcb
, blockRef
, true);
1083 if (err
!= noErr
) goto Exit
;
1085 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
1086 if (err
!= noErr
) goto Exit
;
1088 // Readjust currentWord and wordsLeft
1089 currentWord
= buffer
;
1090 wordsLeft
= wordsPerBlock
;
1093 if ((*currentWord
& SWAP_BE32 (bitMask
)) != SWAP_BE32 (bitMask
)) {
1094 panic("BlockMarkFree: blocks not allocated!");
1097 *currentWord
&= SWAP_BE32 (~bitMask
); // clear the bits in the bitmap
1099 // No need to update currentWord or wordsLeft
1105 (void)ReleaseBitmapBlock(vcb
, blockRef
, true);
1112 _______________________________________________________________________
1114 Routine: BlockFindContiguous
1116 Function: Find a contiguous range of blocks that are free (bits
1117 clear in the bitmap). If a contiguous range of the
1118 minimum size can't be found, an error will be returned.
1121 vcb Pointer to volume where space is to be allocated
1122 startingBlock Preferred first block of range
1123 endingBlock Last possible block in range + 1
1124 minBlocks Minimum number of blocks needed. Must be > 0.
1125 maxBlocks Maximum (ideal) number of blocks desired
1128 actualStartBlock First block of range found, or 0 if error
1129 actualNumBlocks Number of blocks found, or 0 if error
1132 noErr Found at least minBlocks contiguous
1133 dskFulErr No contiguous space found, or all less than minBlocks
1134 _______________________________________________________________________
1137 static OSErr
BlockFindContiguous(
1139 UInt32 startingBlock
,
1143 UInt32
*actualStartBlock
,
1144 UInt32
*actualNumBlocks
)
1147 register UInt32 currentBlock
; // Block we're currently looking at.
1148 UInt32 firstBlock
; // First free block in current extent.
1149 UInt32 stopBlock
; // If we get to this block, stop searching for first free block.
1150 UInt32 foundBlocks
; // Number of contiguous free blocks in current extent.
1151 UInt32
*buffer
= NULL
;
1152 register UInt32
*currentWord
;
1153 register UInt32 bitMask
;
1154 register UInt32 wordsLeft
;
1155 register UInt32 tempWord
;
1157 UInt32 wordsPerBlock
;
1159 if ((endingBlock
- startingBlock
) < minBlocks
)
1161 // The set of blocks we're checking is smaller than the minimum number
1162 // of blocks, so we couldn't possibly find a good range.
1166 stopBlock
= endingBlock
- minBlocks
+ 1;
1167 currentBlock
= startingBlock
;
1170 // Pre-read the first bitmap block.
1172 err
= ReadBitmapBlock(vcb
, currentBlock
, &buffer
, &blockRef
);
1173 if ( err
!= noErr
) goto ErrorExit
;
1176 // Figure out where currentBlock is within the buffer.
1178 wordsPerBlock
= vcb
->vcbVBMIOSize
/ kBytesPerWord
;
1180 wordsLeft
= (startingBlock
/ kBitsPerWord
) & (wordsPerBlock
-1); // Current index into buffer
1181 currentWord
= buffer
+ wordsLeft
;
1182 wordsLeft
= wordsPerBlock
- wordsLeft
;
1188 //============================================================
1189 // Look for a free block, skipping over allocated blocks.
1190 //============================================================
1193 // Check an initial partial word (if any)
1195 bitMask
= currentBlock
& kBitsWithinWordMask
;
1198 tempWord
= *currentWord
; // Fetch the current word only once
1199 bitMask
= kHighBitInWordMask
>> bitMask
;
1200 while (tempWord
& bitMask
)
1206 // Did we find an unused bit (bitMask != 0), or run out of bits (bitMask == 0)?
1210 // Didn't find any unused bits, so we're done with this word.
1216 // Check whole words
1218 while (currentBlock
< stopBlock
)
1220 // See if it's time to read another block.
1224 err
= ReleaseBitmapBlock(vcb
, blockRef
, false);
1225 if (err
!= noErr
) goto ErrorExit
;
1227 err
= ReadBitmapBlock(vcb
, currentBlock
, &buffer
, &blockRef
);
1228 if ( err
!= noErr
) goto ErrorExit
;
1230 currentWord
= buffer
;
1231 wordsLeft
= wordsPerBlock
;
1234 // See if any of the bits are clear
1235 if ((tempWord
=*currentWord
) + 1) // non-zero if any bits were clear
1237 // Figure out which bit is clear
1238 bitMask
= kHighBitInWordMask
;
1239 while (tempWord
& bitMask
)
1245 break; // Found the free bit; break out to FoundUnused.
1248 // Keep looking at the next word
1249 currentBlock
+= kBitsPerWord
;
1255 // Make sure the unused bit is early enough to use
1256 if (currentBlock
>= stopBlock
)
1261 // Remember the start of the extent
1262 firstBlock
= currentBlock
;
1264 //============================================================
1265 // Count the number of contiguous free blocks.
1266 //============================================================
1269 // Check an initial partial word (if any)
1271 bitMask
= currentBlock
& kBitsWithinWordMask
;
1274 tempWord
= *currentWord
; // Fetch the current word only once
1275 bitMask
= kHighBitInWordMask
>> bitMask
;
1276 while (bitMask
&& !(tempWord
& bitMask
))
1282 // Did we find a used bit (bitMask != 0), or run out of bits (bitMask == 0)?
1286 // Didn't find any used bits, so we're done with this word.
1292 // Check whole words
1294 while (currentBlock
< endingBlock
)
1296 // See if it's time to read another block.
1300 err
= ReleaseBitmapBlock(vcb
, blockRef
, false);
1301 if (err
!= noErr
) goto ErrorExit
;
1303 err
= ReadBitmapBlock(vcb
, currentBlock
, &buffer
, &blockRef
);
1304 if ( err
!= noErr
) goto ErrorExit
;
1306 currentWord
= buffer
;
1307 wordsLeft
= wordsPerBlock
;
1310 // See if any of the bits are set
1311 if ((tempWord
=*currentWord
) != 0)
1313 // Figure out which bit is set
1314 bitMask
= kHighBitInWordMask
;
1315 while (!(tempWord
& bitMask
))
1321 break; // Found the used bit; break out to FoundUsed.
1324 // Keep looking at the next word
1325 currentBlock
+= kBitsPerWord
;
1329 // If we found at least maxBlocks, we can quit early.
1330 if ((currentBlock
- firstBlock
) >= maxBlocks
)
1335 // Make sure we didn't run out of bitmap looking for a used block.
1336 // If so, pin to the end of the bitmap.
1337 if (currentBlock
> endingBlock
)
1338 currentBlock
= endingBlock
;
1340 // Figure out how many contiguous free blocks there were.
1341 // Pin the answer to maxBlocks.
1342 foundBlocks
= currentBlock
- firstBlock
;
1343 if (foundBlocks
> maxBlocks
)
1344 foundBlocks
= maxBlocks
;
1345 if (foundBlocks
>= minBlocks
)
1346 break; // Found what we needed!
1348 // This free chunk wasn't big enough. Try inserting it into the free extent cache in case
1349 // the allocation wasn't forced contiguous.
1350 tempWord
= vcb
->vcbFreeExtCnt
;
1351 if (tempWord
== kMaxFreeExtents
&& vcb
->vcbFreeExt
[kMaxFreeExtents
-1].blockCount
< foundBlocks
)
1353 if (tempWord
< kMaxFreeExtents
)
1355 // We're going to add this extent. Bubble any smaller extents down in the list.
1356 while (tempWord
&& vcb
->vcbFreeExt
[tempWord
-1].blockCount
< foundBlocks
)
1358 vcb
->vcbFreeExt
[tempWord
] = vcb
->vcbFreeExt
[tempWord
-1];
1361 vcb
->vcbFreeExt
[tempWord
].startBlock
= firstBlock
;
1362 vcb
->vcbFreeExt
[tempWord
].blockCount
= foundBlocks
;
1364 if (vcb
->vcbFreeExtCnt
< kMaxFreeExtents
)
1365 ++vcb
->vcbFreeExtCnt
;
1367 } while (currentBlock
< stopBlock
);
1370 // Return the outputs.
1371 if (foundBlocks
< minBlocks
)
1376 *actualStartBlock
= 0;
1377 *actualNumBlocks
= 0;
1382 *actualStartBlock
= firstBlock
;
1383 *actualNumBlocks
= foundBlocks
;
1387 (void) ReleaseBitmapBlock(vcb
, blockRef
, false);