2 * Copyright (c) 2000-2003 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
,
122 UInt32
*actualStartBlock
,
123 UInt32
*actualNumBlocks
);
125 static OSErr
BlockAllocateContig(
127 UInt32 startingBlock
,
131 UInt32
*actualStartBlock
,
132 UInt32
*actualNumBlocks
);
134 static OSErr
BlockFindContiguous(
136 UInt32 startingBlock
,
141 UInt32
*actualStartBlock
,
142 UInt32
*actualNumBlocks
);
144 static OSErr
BlockAllocateKnown(
147 UInt32
*actualStartBlock
,
148 UInt32
*actualNumBlocks
);
152 ;________________________________________________________________________________
156 ; Function: Allocate space on a volume. If contiguous allocation is requested,
157 ; at least the requested number of bytes will be allocated or an
158 ; error will be returned. If contiguous allocation is not forced,
159 ; the space will be allocated at the first free fragment following
160 ; the requested starting allocation block. If there is not enough
161 ; room there, a block of less than the requested size will be
164 ; If the requested starting block is 0 (for new file allocations),
165 ; the volume's allocation block pointer will be used as a starting
169 ; vcb - Pointer to ExtendedVCB for the volume to allocate space on
170 ; fcb - Pointer to FCB for the file for which storage is being allocated
171 ; startingBlock - Preferred starting allocation block, 0 = no preference
172 ; forceContiguous - Force contiguous flag - if bit 0 set (NE), allocation is contiguous
173 ; or an error is returned
175 ; minBlocks - Number of blocks requested. If the allocation is non-contiguous,
176 ; less than this may actually be allocated
177 ; maxBlocks - The maximum number of blocks to allocate. If there is additional free
178 ; space after bytesRequested, then up to maxBlocks bytes should really
179 ; be allocated. (Used by ExtendFileC to round up allocations to a multiple
180 ; of the file's clump size.)
183 ; (result) - Error code, zero for successful allocation
184 ; *startBlock - Actual starting allocation block
185 ; *actualBlocks - Actual number of allocation blocks allocated
188 ; The volume bitmap is read and updated; the volume bitmap cache may be changed.
189 ;________________________________________________________________________________
193 OSErr
BlockAllocate (
194 ExtendedVCB
*vcb
, /* which volume to allocate space on */
195 UInt32 startingBlock
, /* preferred starting block, or 0 for no preference */
196 UInt32 minBlocks
, /* desired number of blocks to allocate */
197 UInt32 maxBlocks
, /* maximum number of blocks to allocate */
198 Boolean forceContiguous
, /* non-zero to force contiguous allocation and to force */
199 /* minBlocks bytes to actually be allocated */
202 UInt32
*actualStartBlock
, /* actual first block of allocation */
203 UInt32
*actualNumBlocks
) /* number of blocks actually allocated; if forceContiguous */
204 /* was zero, then this may represent fewer than minBlocks */
208 Boolean updateAllocPtr
= false; // true if nextAllocation needs to be updated
211 // Initialize outputs in case we get an error
213 *actualStartBlock
= 0;
214 *actualNumBlocks
= 0;
215 freeBlocks
= hfs_freeblks(VCBTOHFS(vcb
), 0);
218 // If the disk is already full, don't bother.
220 if (freeBlocks
== 0) {
224 if (forceContiguous
&& freeBlocks
< minBlocks
) {
229 * Clip if necessary so we don't over-subscribe the free blocks.
231 if (minBlocks
> freeBlocks
) {
232 minBlocks
= freeBlocks
;
234 if (maxBlocks
> freeBlocks
) {
235 maxBlocks
= freeBlocks
;
239 // If caller didn't specify a starting block number, then use the volume's
240 // next block to allocate from.
242 if (startingBlock
== 0) {
244 startingBlock
= vcb
->nextAllocation
;
246 updateAllocPtr
= true;
248 if (startingBlock
>= vcb
->totalBlocks
) {
249 startingBlock
= 0; /* overflow so start at beginning */
253 // If the request must be contiguous, then find a sequence of free blocks
254 // that is long enough. Otherwise, find the first free block.
256 if (forceContiguous
) {
257 err
= BlockAllocateContig(vcb
, startingBlock
, minBlocks
, maxBlocks
,
258 useMetaZone
, actualStartBlock
, actualNumBlocks
);
260 * If we allocated from a new position then
261 * also update the roving allocatior.
263 if ((err
== noErr
) &&
264 (*actualStartBlock
> startingBlock
) &&
265 ((*actualStartBlock
< VCBTOHFS(vcb
)->hfs_metazone_start
) ||
266 (*actualStartBlock
> VCBTOHFS(vcb
)->hfs_metazone_end
))) {
267 vcb
->nextAllocation
= *actualStartBlock
; /* XXX */
271 * Scan the bitmap once, gather the N largest free extents, then
272 * allocate from these largest extents. Repeat as needed until
273 * we get all the space we needed. We could probably build up
274 * that list when the higher level caller tried (and failed) a
275 * contiguous allocation first.
277 err
= BlockAllocateKnown(vcb
, maxBlocks
, actualStartBlock
, actualNumBlocks
);
278 if (err
== dskFulErr
)
279 err
= BlockAllocateAny(vcb
, startingBlock
, vcb
->totalBlocks
,
280 maxBlocks
, useMetaZone
, actualStartBlock
,
282 if (err
== dskFulErr
)
283 err
= BlockAllocateAny(vcb
, 1, startingBlock
, maxBlocks
,
284 useMetaZone
, actualStartBlock
,
290 // If we used the volume's roving allocation pointer, then we need to update it.
291 // Adding in the length of the current allocation might reduce the next allocate
292 // call by avoiding a re-scan of the already allocated space. However, the clump
293 // just allocated can quite conceivably end up being truncated or released when
294 // the file is closed or its EOF changed. Leaving the allocation pointer at the
295 // start of the last allocation will avoid unnecessary fragmentation in this case.
299 if (updateAllocPtr
&&
300 ((*actualStartBlock
< VCBTOHFS(vcb
)->hfs_metazone_start
) ||
301 (*actualStartBlock
> VCBTOHFS(vcb
)->hfs_metazone_end
))) {
302 vcb
->nextAllocation
= *actualStartBlock
;
305 // Update the number of free blocks on the volume
307 vcb
->freeBlocks
-= *actualNumBlocks
;
308 hfs_generate_volume_notifications(VCBTOHFS(vcb
));
321 ;________________________________________________________________________________
323 ; Routine: BlkDealloc
325 ; Function: Update the bitmap to deallocate a run of disk allocation blocks
328 ; vcb - Pointer to ExtendedVCB for the volume to free space on
329 ; firstBlock - First allocation block to be freed
330 ; numBlocks - Number of allocation blocks to free up (must be > 0!)
333 ; (result) - Result code
336 ; The volume bitmap is read and updated; the volume bitmap cache may be changed.
337 ;________________________________________________________________________________
341 OSErr
BlockDeallocate (
342 ExtendedVCB
*vcb
, // Which volume to deallocate space on
343 UInt32 firstBlock
, // First block in range to deallocate
344 UInt32 numBlocks
) // Number of contiguous blocks to deallocate
349 // If no blocks to deallocate, then exit early
351 if (numBlocks
== 0) {
357 // Call internal routine to free the sequence of blocks
359 err
= BlockMarkFree(vcb
, firstBlock
, numBlocks
);
364 // Update the volume's free block count, and mark the VCB as dirty.
367 vcb
->freeBlocks
+= numBlocks
;
368 hfs_generate_volume_notifications(VCBTOHFS(vcb
));
369 if (vcb
->nextAllocation
== (firstBlock
+ numBlocks
))
370 vcb
->nextAllocation
-= numBlocks
;
380 UInt8 freebitcount
[16] = {
381 4, 3, 3, 2, 3, 2, 2, 1, /* 0 1 2 3 4 5 6 7 */
382 3, 2, 2, 1, 2, 1, 1, 0, /* 8 9 A B C D E F */
387 MetaZoneFreeBlocks(ExtendedVCB
*vcb
)
399 bytesleft
= freeblocks
= 0;
400 bit
= VCBTOHFS(vcb
)->hfs_metazone_start
;
404 lastbit
= VCBTOHFS(vcb
)->hfs_metazone_end
;
405 bytesperblock
= vcb
->vcbVBMIOSize
;
408 * Count all the bits from bit to lastbit.
410 while (bit
< lastbit
) {
412 * Get next bitmap block.
414 if (bytesleft
== 0) {
416 (void) ReleaseBitmapBlock(vcb
, blockRef
, false);
419 if (ReadBitmapBlock(vcb
, bit
, &currCache
, &blockRef
) != 0) {
422 buffer
= (UInt8
*)currCache
;
423 bytesleft
= bytesperblock
;
426 freeblocks
+= freebitcount
[byte
& 0x0F];
427 freeblocks
+= freebitcount
[(byte
>> 4) & 0x0F];
432 (void) ReleaseBitmapBlock(vcb
, blockRef
, false);
439 * Obtain the next allocation block (bit) that's
440 * outside the metadata allocation zone.
442 static UInt32
NextBitmapBlock(
446 struct hfsmount
*hfsmp
= VCBTOHFS(vcb
);
448 if ((hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) == 0)
451 * Skip over metadata allocation zone.
453 if ((bit
>= hfsmp
->hfs_metazone_start
) &&
454 (bit
<= hfsmp
->hfs_metazone_end
)) {
455 bit
= hfsmp
->hfs_metazone_end
+ 1;
462 ;_______________________________________________________________________
464 ; Routine: ReadBitmapBlock
466 ; Function: Read in a bitmap block corresponding to a given allocation
467 ; block (bit). Return a pointer to the bitmap block.
470 ; vcb -- Pointer to ExtendedVCB
471 ; bit -- Allocation block whose bitmap block is desired
474 ; buffer -- Pointer to bitmap block corresonding to "block"
476 ;_______________________________________________________________________
478 static OSErr
ReadBitmapBlock(
485 struct buf
*bp
= NULL
;
486 struct vnode
*vp
= NULL
;
491 * volume bitmap blocks are protected by the Extents B-tree lock
493 REQUIRE_FILE_LOCK(vcb
->extentsRefNum
, false);
495 blockSize
= (UInt32
)vcb
->vcbVBMIOSize
;
496 block
= bit
/ (blockSize
* kBitsPerByte
);
498 if (vcb
->vcbSigWord
== kHFSPlusSigWord
) {
499 vp
= vcb
->allocationsRefNum
; /* use allocation file vnode */
502 vp
= VCBTOHFS(vcb
)->hfs_devvp
; /* use device I/O vnode */
503 block
+= vcb
->vcbVBMSt
; /* map to physical block */
506 err
= meta_bread(vp
, block
, blockSize
, NOCRED
, &bp
);
514 *blockRef
= (UInt32
)bp
;
515 *buffer
= (UInt32
*)bp
->b_data
;
524 ;_______________________________________________________________________
526 ; Routine: ReleaseBitmapBlock
528 ; Function: Relase a bitmap block.
534 ;_______________________________________________________________________
536 static OSErr
ReleaseBitmapBlock(
541 struct buf
*bp
= (struct buf
*)blockRef
;
545 panic("ReleaseBitmapBlock: missing bp");
552 struct hfsmount
*hfsmp
= VCBTOHFS(vcb
);
555 journal_modify_block_end(hfsmp
->jnl
, bp
);
569 _______________________________________________________________________
571 Routine: BlockAllocateContig
573 Function: Allocate a contiguous group of allocation blocks. The
574 allocation is all-or-nothing. The caller guarantees that
575 there are enough free blocks (though they may not be
576 contiguous, in which case this call will fail).
579 vcb Pointer to volume where space is to be allocated
580 startingBlock Preferred first block for allocation
581 minBlocks Minimum number of contiguous blocks to allocate
582 maxBlocks Maximum number of contiguous blocks to allocate
586 actualStartBlock First block of range allocated, or 0 if error
587 actualNumBlocks Number of blocks allocated, or 0 if error
588 _______________________________________________________________________
590 static OSErr
BlockAllocateContig(
592 UInt32 startingBlock
,
596 UInt32
*actualStartBlock
,
597 UInt32
*actualNumBlocks
)
602 // Find a contiguous group of blocks at least minBlocks long.
603 // Determine the number of contiguous blocks available (up
608 * NOTE: If the only contiguous free extent of at least minBlocks
609 * crosses startingBlock (i.e. starts before, ends after), then we
610 * won't find it. Earlier versions *did* find this case by letting
611 * the second search look past startingBlock by minBlocks. But
612 * with the free extent cache, this can lead to duplicate entries
613 * in the cache, causing the same blocks to be allocated twice.
615 err
= BlockFindContiguous(vcb
, startingBlock
, vcb
->totalBlocks
, minBlocks
,
616 maxBlocks
, useMetaZone
, actualStartBlock
, actualNumBlocks
);
617 if (err
== dskFulErr
&& startingBlock
!= 0) {
619 * Constrain the endingBlock so we don't bother looking for ranges
620 * that would overlap those found in the previous call.
622 err
= BlockFindContiguous(vcb
, 1, startingBlock
, minBlocks
, maxBlocks
,
623 useMetaZone
, actualStartBlock
, actualNumBlocks
);
625 if (err
!= noErr
) goto Exit
;
628 if ((*actualStartBlock
+ *actualNumBlocks
) > vcb
->totalBlocks
)
629 panic("BlockAllocateContig: allocation overflow on \"%s\"", vcb
->vcbVN
);
632 // Now mark those blocks allocated.
634 err
= BlockMarkAllocated(vcb
, *actualStartBlock
, *actualNumBlocks
);
638 *actualStartBlock
= 0;
639 *actualNumBlocks
= 0;
646 _______________________________________________________________________
648 Routine: BlockAllocateAny
650 Function: Allocate one or more allocation blocks. If there are fewer
651 free blocks than requested, all free blocks will be
652 allocated. The caller guarantees that there is at least
656 vcb Pointer to volume where space is to be allocated
657 startingBlock Preferred first block for allocation
658 endingBlock Last block to check + 1
659 maxBlocks Maximum number of contiguous blocks to allocate
663 actualStartBlock First block of range allocated, or 0 if error
664 actualNumBlocks Number of blocks allocated, or 0 if error
665 _______________________________________________________________________
667 static OSErr
BlockAllocateAny(
669 UInt32 startingBlock
,
670 register UInt32 endingBlock
,
673 UInt32
*actualStartBlock
,
674 UInt32
*actualNumBlocks
)
677 register UInt32 block
; // current block number
678 register UInt32 currentWord
; // Pointer to current word within bitmap block
679 register UInt32 bitMask
; // Word with given bits already set (ready to OR in)
680 register UInt32 wordsLeft
; // Number of words left in this bitmap block
681 UInt32
*buffer
= NULL
;
682 UInt32
*currCache
= NULL
;
685 UInt32 wordsPerBlock
;
686 Boolean dirty
= false;
687 struct hfsmount
*hfsmp
= VCBTOHFS(vcb
);
689 // Since this routine doesn't wrap around
690 if (maxBlocks
> (endingBlock
- startingBlock
)) {
691 maxBlocks
= endingBlock
- startingBlock
;
695 * Skip over metadata blocks.
698 startingBlock
= NextBitmapBlock(vcb
, startingBlock
);
701 // Pre-read the first bitmap block
703 err
= ReadBitmapBlock(vcb
, startingBlock
, &currCache
, &blockRef
);
704 if (err
!= noErr
) goto Exit
;
708 // Set up the current position within the block
711 UInt32 wordIndexInBlock
;
713 bitsPerBlock
= vcb
->vcbVBMIOSize
* kBitsPerByte
;
714 wordsPerBlock
= vcb
->vcbVBMIOSize
/ kBytesPerWord
;
716 wordIndexInBlock
= (startingBlock
& (bitsPerBlock
-1)) / kBitsPerWord
;
717 buffer
+= wordIndexInBlock
;
718 wordsLeft
= wordsPerBlock
- wordIndexInBlock
;
719 currentWord
= SWAP_BE32 (*buffer
);
720 bitMask
= kHighBitInWordMask
>> (startingBlock
& kBitsWithinWordMask
);
724 // Find the first unallocated block
727 while (block
< endingBlock
) {
728 if ((currentWord
& bitMask
) == 0)
736 bitMask
= kHighBitInWordMask
;
739 if (--wordsLeft
== 0) {
741 buffer
= currCache
= NULL
;
742 err
= ReleaseBitmapBlock(vcb
, blockRef
, false);
743 if (err
!= noErr
) goto Exit
;
746 * Skip over metadata blocks.
749 block
= NextBitmapBlock(vcb
, block
);
750 if (block
>= endingBlock
) {
755 err
= ReadBitmapBlock(vcb
, block
, &currCache
, &blockRef
);
756 if (err
!= noErr
) goto Exit
;
759 wordsLeft
= wordsPerBlock
;
761 currentWord
= SWAP_BE32 (*buffer
);
765 // Did we get to the end of the bitmap before finding a free block?
766 // If so, then couldn't allocate anything.
767 if (block
>= endingBlock
) {
772 // Return the first block in the allocated range
773 *actualStartBlock
= block
;
776 // If we could get the desired number of blocks before hitting endingBlock,
777 // then adjust endingBlock so we won't keep looking. Ideally, the comparison
778 // would be (block + maxBlocks) < endingBlock, but that could overflow. The
779 // comparison below yields identical results, but without overflow.
780 if (block
< (endingBlock
-maxBlocks
)) {
781 endingBlock
= block
+ maxBlocks
; // if we get this far, we've found enough
786 journal_modify_block_start(hfsmp
->jnl
, (struct buf
*)blockRef
);
790 // Allocate all of the consecutive blocks
792 while ((currentWord
& bitMask
) == 0) {
793 // Allocate this block
794 currentWord
|= bitMask
;
796 // Move to the next block. If no more, then exit.
798 if (block
== endingBlock
)
804 *buffer
= SWAP_BE32 (currentWord
); // update value in bitmap
807 bitMask
= kHighBitInWordMask
;
810 if (--wordsLeft
== 0) {
812 buffer
= currCache
= NULL
;
813 err
= ReleaseBitmapBlock(vcb
, blockRef
, true);
814 if (err
!= noErr
) goto Exit
;
817 * Skip over metadata blocks.
822 nextBlock
= NextBitmapBlock(vcb
, block
);
823 if (nextBlock
!= block
) {
824 goto Exit
; /* allocation gap, so stop */
828 err
= ReadBitmapBlock(vcb
, block
, &currCache
, &blockRef
);
829 if (err
!= noErr
) goto Exit
;
834 journal_modify_block_start(hfsmp
->jnl
, (struct buf
*)blockRef
);
837 wordsLeft
= wordsPerBlock
;
840 currentWord
= SWAP_BE32 (*buffer
);
843 *buffer
= SWAP_BE32 (currentWord
); // update the last change
847 *actualNumBlocks
= block
- *actualStartBlock
;
850 if ((*actualStartBlock
+ *actualNumBlocks
) > vcb
->totalBlocks
)
851 panic("BlockAllocateAny: allocation overflow on \"%s\"", vcb
->vcbVN
);
854 *actualStartBlock
= 0;
855 *actualNumBlocks
= 0;
859 (void) ReleaseBitmapBlock(vcb
, blockRef
, dirty
);
866 _______________________________________________________________________
868 Routine: BlockAllocateKnown
870 Function: Try to allocate space from known free space in the free
874 vcb Pointer to volume where space is to be allocated
875 maxBlocks Maximum number of contiguous blocks to allocate
878 actualStartBlock First block of range allocated, or 0 if error
879 actualNumBlocks Number of blocks allocated, or 0 if error
882 dskFulErr Free extent cache is empty
883 _______________________________________________________________________
885 static OSErr
BlockAllocateKnown(
888 UInt32
*actualStartBlock
,
889 UInt32
*actualNumBlocks
)
893 UInt32 newStartBlock
, newBlockCount
;
895 if (vcb
->vcbFreeExtCnt
== 0)
898 // Just grab up to maxBlocks of the first (largest) free exent.
899 *actualStartBlock
= vcb
->vcbFreeExt
[0].startBlock
;
900 foundBlocks
= vcb
->vcbFreeExt
[0].blockCount
;
901 if (foundBlocks
> maxBlocks
)
902 foundBlocks
= maxBlocks
;
903 *actualNumBlocks
= foundBlocks
;
905 // Adjust the start and length of that extent.
906 newStartBlock
= vcb
->vcbFreeExt
[0].startBlock
+ foundBlocks
;
907 newBlockCount
= vcb
->vcbFreeExt
[0].blockCount
- foundBlocks
;
909 // The first extent might not be the largest anymore. Bubble up any
910 // (now larger) extents to the top of the list.
911 for (i
=1; i
<vcb
->vcbFreeExtCnt
; ++i
)
913 if (vcb
->vcbFreeExt
[i
].blockCount
> newBlockCount
)
915 vcb
->vcbFreeExt
[i
-1].startBlock
= vcb
->vcbFreeExt
[i
].startBlock
;
916 vcb
->vcbFreeExt
[i
-1].blockCount
= vcb
->vcbFreeExt
[i
].blockCount
;
924 // If this is now the smallest known free extent, then it might be smaller than
925 // other extents we didn't keep track of. So, just forget about this extent.
926 // After the previous loop, (i-1) is the index of the extent we just allocated from.
927 if (i
== vcb
->vcbFreeExtCnt
)
929 // It's now the smallest extent, so forget about it
930 --vcb
->vcbFreeExtCnt
;
934 // It's not the smallest, so store it in its proper place
935 vcb
->vcbFreeExt
[i
-1].startBlock
= newStartBlock
;
936 vcb
->vcbFreeExt
[i
-1].blockCount
= newBlockCount
;
940 if ((*actualStartBlock
+ *actualNumBlocks
) > vcb
->totalBlocks
)
941 panic("BlockAllocateKnown: allocation overflow on \"%s\"", vcb
->vcbVN
);
944 // Now mark the found extent in the bitmap
946 return BlockMarkAllocated(vcb
, *actualStartBlock
, *actualNumBlocks
);
952 _______________________________________________________________________
954 Routine: BlockMarkAllocated
956 Function: Mark a contiguous group of blocks as allocated (set in the
957 bitmap). It assumes those bits are currently marked
958 deallocated (clear in the bitmap).
961 vcb Pointer to volume where space is to be allocated
962 startingBlock First block number to mark as allocated
963 numBlocks Number of blocks to mark as allocated
964 _______________________________________________________________________
967 OSErr
BlockMarkAllocated(
969 UInt32 startingBlock
,
970 register UInt32 numBlocks
)
973 register UInt32
*currentWord
; // Pointer to current word within bitmap block
974 register UInt32 wordsLeft
; // Number of words left in this bitmap block
975 register UInt32 bitMask
; // Word with given bits already set (ready to OR in)
976 UInt32 firstBit
; // Bit index within word of first bit to allocate
977 UInt32 numBits
; // Number of bits in word to allocate
978 UInt32
*buffer
= NULL
;
981 UInt32 wordsPerBlock
;
983 struct hfsmount
*hfsmp
= VCBTOHFS(vcb
);
987 // Pre-read the bitmap block containing the first word of allocation
990 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
991 if (err
!= noErr
) goto Exit
;
993 // Initialize currentWord, and wordsLeft.
996 UInt32 wordIndexInBlock
;
998 bitsPerBlock
= vcb
->vcbVBMIOSize
* kBitsPerByte
;
999 wordsPerBlock
= vcb
->vcbVBMIOSize
/ kBytesPerWord
;
1001 wordIndexInBlock
= (startingBlock
& (bitsPerBlock
-1)) / kBitsPerWord
;
1002 currentWord
= buffer
+ wordIndexInBlock
;
1003 wordsLeft
= wordsPerBlock
- wordIndexInBlock
;
1008 journal_modify_block_start(hfsmp
->jnl
, (struct buf
*)blockRef
);
1012 // If the first block to allocate doesn't start on a word
1013 // boundary in the bitmap, then treat that first word
1017 firstBit
= startingBlock
% kBitsPerWord
;
1018 if (firstBit
!= 0) {
1019 bitMask
= kAllBitsSetInWord
>> firstBit
; // turn off all bits before firstBit
1020 numBits
= kBitsPerWord
- firstBit
; // number of remaining bits in this word
1021 if (numBits
> numBlocks
) {
1022 numBits
= numBlocks
; // entire allocation is inside this one word
1023 bitMask
&= ~(kAllBitsSetInWord
>> (firstBit
+ numBits
)); // turn off bits after last
1026 if ((*currentWord
& SWAP_BE32 (bitMask
)) != 0) {
1027 panic("BlockMarkAllocated: blocks already allocated!");
1030 *currentWord
|= SWAP_BE32 (bitMask
); // set the bits in the bitmap
1031 numBlocks
-= numBits
; // adjust number of blocks left to allocate
1033 ++currentWord
; // move to next word
1034 --wordsLeft
; // one less word left in this block
1038 // Allocate whole words (32 blocks) at a time.
1041 bitMask
= kAllBitsSetInWord
; // put this in a register for 68K
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
;
1056 journal_modify_block_start(hfsmp
->jnl
, (struct buf
*)blockRef
);
1059 // Readjust currentWord and wordsLeft
1060 currentWord
= buffer
;
1061 wordsLeft
= wordsPerBlock
;
1064 if (*currentWord
!= 0) {
1065 panic("BlockMarkAllocated: blocks already allocated!");
1068 *currentWord
= SWAP_BE32 (bitMask
);
1069 numBlocks
-= kBitsPerWord
;
1071 ++currentWord
; // move to next word
1072 --wordsLeft
; // one less word left in this block
1076 // Allocate any remaining blocks.
1079 if (numBlocks
!= 0) {
1080 bitMask
= ~(kAllBitsSetInWord
>> numBlocks
); // set first numBlocks bits
1081 if (wordsLeft
== 0) {
1082 // Read in the next bitmap block
1083 startingBlock
+= bitsPerBlock
; // generate a block number in the next bitmap block
1086 err
= ReleaseBitmapBlock(vcb
, blockRef
, true);
1087 if (err
!= noErr
) goto Exit
;
1089 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
1090 if (err
!= noErr
) goto Exit
;
1094 journal_modify_block_start(hfsmp
->jnl
, (struct buf
*)blockRef
);
1097 // Readjust currentWord and wordsLeft
1098 currentWord
= buffer
;
1099 wordsLeft
= wordsPerBlock
;
1102 if ((*currentWord
& SWAP_BE32 (bitMask
)) != 0) {
1103 panic("BlockMarkAllocated: blocks already allocated!");
1106 *currentWord
|= SWAP_BE32 (bitMask
); // set the bits in the bitmap
1108 // No need to update currentWord or wordsLeft
1114 (void)ReleaseBitmapBlock(vcb
, blockRef
, true);
1121 _______________________________________________________________________
1123 Routine: BlockMarkFree
1125 Function: Mark a contiguous group of blocks as free (clear in the
1126 bitmap). It assumes those bits are currently marked
1127 allocated (set in the bitmap).
1130 vcb Pointer to volume where space is to be freed
1131 startingBlock First block number to mark as freed
1132 numBlocks Number of blocks to mark as freed
1133 _______________________________________________________________________
1136 OSErr
BlockMarkFree(
1138 UInt32 startingBlock
,
1139 register UInt32 numBlocks
)
1142 register UInt32
*currentWord
; // Pointer to current word within bitmap block
1143 register UInt32 wordsLeft
; // Number of words left in this bitmap block
1144 register UInt32 bitMask
; // Word with given bits already set (ready to OR in)
1145 UInt32 firstBit
; // Bit index within word of first bit to allocate
1146 UInt32 numBits
; // Number of bits in word to allocate
1147 UInt32
*buffer
= NULL
;
1149 UInt32 bitsPerBlock
;
1150 UInt32 wordsPerBlock
;
1152 struct hfsmount
*hfsmp
= VCBTOHFS(vcb
);
1154 if (startingBlock
+ numBlocks
> vcb
->totalBlocks
) {
1155 panic("hfs: block mark free: trying to free non-existent blocks (%d %d %d)\n",
1156 startingBlock
, numBlocks
, vcb
->totalBlocks
);
1161 // Pre-read the bitmap block containing the first word of allocation
1164 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
1165 if (err
!= noErr
) goto Exit
;
1168 journal_modify_block_start(hfsmp
->jnl
, (struct buf
*)blockRef
);
1172 // Initialize currentWord, and wordsLeft.
1175 UInt32 wordIndexInBlock
;
1177 bitsPerBlock
= vcb
->vcbVBMIOSize
* kBitsPerByte
;
1178 wordsPerBlock
= vcb
->vcbVBMIOSize
/ kBytesPerWord
;
1180 wordIndexInBlock
= (startingBlock
& (bitsPerBlock
-1)) / kBitsPerWord
;
1181 currentWord
= buffer
+ wordIndexInBlock
;
1182 wordsLeft
= wordsPerBlock
- wordIndexInBlock
;
1186 // If the first block to free doesn't start on a word
1187 // boundary in the bitmap, then treat that first word
1191 firstBit
= startingBlock
% kBitsPerWord
;
1192 if (firstBit
!= 0) {
1193 bitMask
= kAllBitsSetInWord
>> firstBit
; // turn off all bits before firstBit
1194 numBits
= kBitsPerWord
- firstBit
; // number of remaining bits in this word
1195 if (numBits
> numBlocks
) {
1196 numBits
= numBlocks
; // entire allocation is inside this one word
1197 bitMask
&= ~(kAllBitsSetInWord
>> (firstBit
+ numBits
)); // turn off bits after last
1199 if ((*currentWord
& SWAP_BE32 (bitMask
)) != SWAP_BE32 (bitMask
)) {
1202 *currentWord
&= SWAP_BE32 (~bitMask
); // clear the bits in the bitmap
1203 numBlocks
-= numBits
; // adjust number of blocks left to free
1205 ++currentWord
; // move to next word
1206 --wordsLeft
; // one less word left in this block
1210 // Free whole words (32 blocks) at a time.
1213 while (numBlocks
>= kBitsPerWord
) {
1214 if (wordsLeft
== 0) {
1215 // Read in the next bitmap block
1216 startingBlock
+= bitsPerBlock
; // generate a block number in the next bitmap block
1219 err
= ReleaseBitmapBlock(vcb
, blockRef
, true);
1220 if (err
!= noErr
) goto Exit
;
1222 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
1223 if (err
!= noErr
) goto Exit
;
1227 journal_modify_block_start(hfsmp
->jnl
, (struct buf
*)blockRef
);
1230 // Readjust currentWord and wordsLeft
1231 currentWord
= buffer
;
1232 wordsLeft
= wordsPerBlock
;
1234 if (*currentWord
!= SWAP_BE32 (kAllBitsSetInWord
)) {
1237 *currentWord
= 0; // clear the entire word
1238 numBlocks
-= kBitsPerWord
;
1240 ++currentWord
; // move to next word
1241 --wordsLeft
; // one less word left in this block
1245 // Free any remaining blocks.
1248 if (numBlocks
!= 0) {
1249 bitMask
= ~(kAllBitsSetInWord
>> numBlocks
); // set first numBlocks bits
1250 if (wordsLeft
== 0) {
1251 // Read in the next bitmap block
1252 startingBlock
+= bitsPerBlock
; // generate a block number in the next bitmap block
1255 err
= ReleaseBitmapBlock(vcb
, blockRef
, true);
1256 if (err
!= noErr
) goto Exit
;
1258 err
= ReadBitmapBlock(vcb
, startingBlock
, &buffer
, &blockRef
);
1259 if (err
!= noErr
) goto Exit
;
1263 journal_modify_block_start(hfsmp
->jnl
, (struct buf
*)blockRef
);
1266 // Readjust currentWord and wordsLeft
1267 currentWord
= buffer
;
1268 wordsLeft
= wordsPerBlock
;
1270 if ((*currentWord
& SWAP_BE32 (bitMask
)) != SWAP_BE32 (bitMask
)) {
1273 *currentWord
&= SWAP_BE32 (~bitMask
); // clear the bits in the bitmap
1275 // No need to update currentWord or wordsLeft
1281 (void)ReleaseBitmapBlock(vcb
, blockRef
, true);
1287 panic("BlockMarkFree: blocks not allocated!");
1289 printf("hfs: WARNING - blocks on volume %s not allocated!\n", vcb
->vcbVN
);
1290 vcb
->vcbAtrb
|= kHFSVolumeInconsistentMask
;
1299 _______________________________________________________________________
1301 Routine: BlockFindContiguous
1303 Function: Find a contiguous range of blocks that are free (bits
1304 clear in the bitmap). If a contiguous range of the
1305 minimum size can't be found, an error will be returned.
1308 vcb Pointer to volume where space is to be allocated
1309 startingBlock Preferred first block of range
1310 endingBlock Last possible block in range + 1
1311 minBlocks Minimum number of blocks needed. Must be > 0.
1312 maxBlocks Maximum (ideal) number of blocks desired
1313 useMetaZone OK to dip into metadata allocation zone
1316 actualStartBlock First block of range found, or 0 if error
1317 actualNumBlocks Number of blocks found, or 0 if error
1320 noErr Found at least minBlocks contiguous
1321 dskFulErr No contiguous space found, or all less than minBlocks
1322 _______________________________________________________________________
1325 static OSErr
BlockFindContiguous(
1327 UInt32 startingBlock
,
1331 Boolean useMetaZone
,
1332 UInt32
*actualStartBlock
,
1333 UInt32
*actualNumBlocks
)
1336 register UInt32 currentBlock
; // Block we're currently looking at.
1337 UInt32 firstBlock
; // First free block in current extent.
1338 UInt32 stopBlock
; // If we get to this block, stop searching for first free block.
1339 UInt32 foundBlocks
; // Number of contiguous free blocks in current extent.
1340 UInt32
*buffer
= NULL
;
1341 register UInt32
*currentWord
;
1342 register UInt32 bitMask
;
1343 register UInt32 wordsLeft
;
1344 register UInt32 tempWord
;
1346 UInt32 wordsPerBlock
;
1349 struct hfsmount
*hfsmp
= VCBTOHFS(vcb
);
1352 if ((hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) &&
1353 (startingBlock
<= hfsmp
->hfs_metazone_end
))
1354 startingBlock
= hfsmp
->hfs_metazone_end
+ 1;
1357 if ((endingBlock
- startingBlock
) < minBlocks
)
1359 // The set of blocks we're checking is smaller than the minimum number
1360 // of blocks, so we couldn't possibly find a good range.
1364 stopBlock
= endingBlock
- minBlocks
+ 1;
1365 currentBlock
= startingBlock
;
1369 * Skip over metadata blocks.
1372 currentBlock
= NextBitmapBlock(vcb
, currentBlock
);
1375 // Pre-read the first bitmap block.
1377 err
= ReadBitmapBlock(vcb
, currentBlock
, &buffer
, &blockRef
);
1378 if ( err
!= noErr
) goto ErrorExit
;
1381 // Figure out where currentBlock is within the buffer.
1383 wordsPerBlock
= vcb
->vcbVBMIOSize
/ kBytesPerWord
;
1385 wordsLeft
= (currentBlock
/ kBitsPerWord
) & (wordsPerBlock
-1); // Current index into buffer
1386 currentWord
= buffer
+ wordsLeft
;
1387 wordsLeft
= wordsPerBlock
- wordsLeft
;
1393 //============================================================
1394 // Look for a free block, skipping over allocated blocks.
1395 //============================================================
1398 // Check an initial partial word (if any)
1400 bitMask
= currentBlock
& kBitsWithinWordMask
;
1403 tempWord
= SWAP_BE32(*currentWord
); // Fetch the current word only once
1404 bitMask
= kHighBitInWordMask
>> bitMask
;
1405 while (tempWord
& bitMask
)
1411 // Did we find an unused bit (bitMask != 0), or run out of bits (bitMask == 0)?
1415 // Didn't find any unused bits, so we're done with this word.
1421 // Check whole words
1423 while (currentBlock
< stopBlock
)
1425 // See if it's time to read another block.
1429 err
= ReleaseBitmapBlock(vcb
, blockRef
, false);
1430 if (err
!= noErr
) goto ErrorExit
;
1433 * Skip over metadata blocks.
1436 currentBlock
= NextBitmapBlock(vcb
, currentBlock
);
1437 if (currentBlock
>= stopBlock
) {
1442 err
= ReadBitmapBlock(vcb
, currentBlock
, &buffer
, &blockRef
);
1443 if ( err
!= noErr
) goto ErrorExit
;
1445 currentWord
= buffer
;
1446 wordsLeft
= wordsPerBlock
;
1449 // See if any of the bits are clear
1450 if ((tempWord
= SWAP_BE32(*currentWord
)) + 1) // non-zero if any bits were clear
1452 // Figure out which bit is clear
1453 bitMask
= kHighBitInWordMask
;
1454 while (tempWord
& bitMask
)
1460 break; // Found the free bit; break out to FoundUnused.
1463 // Keep looking at the next word
1464 currentBlock
+= kBitsPerWord
;
1470 // Make sure the unused bit is early enough to use
1471 if (currentBlock
>= stopBlock
)
1476 // Remember the start of the extent
1477 firstBlock
= currentBlock
;
1479 //============================================================
1480 // Count the number of contiguous free blocks.
1481 //============================================================
1484 // Check an initial partial word (if any)
1486 bitMask
= currentBlock
& kBitsWithinWordMask
;
1489 tempWord
= SWAP_BE32(*currentWord
); // Fetch the current word only once
1490 bitMask
= kHighBitInWordMask
>> bitMask
;
1491 while (bitMask
&& !(tempWord
& bitMask
))
1497 // Did we find a used bit (bitMask != 0), or run out of bits (bitMask == 0)?
1501 // Didn't find any used bits, so we're done with this word.
1507 // Check whole words
1509 while (currentBlock
< endingBlock
)
1511 // See if it's time to read another block.
1515 err
= ReleaseBitmapBlock(vcb
, blockRef
, false);
1516 if (err
!= noErr
) goto ErrorExit
;
1519 * Skip over metadata blocks.
1524 nextBlock
= NextBitmapBlock(vcb
, currentBlock
);
1525 if (nextBlock
!= currentBlock
) {
1526 goto LoopExit
; /* allocation gap, so stop */
1530 err
= ReadBitmapBlock(vcb
, currentBlock
, &buffer
, &blockRef
);
1531 if ( err
!= noErr
) goto ErrorExit
;
1533 currentWord
= buffer
;
1534 wordsLeft
= wordsPerBlock
;
1537 // See if any of the bits are set
1538 if ((tempWord
= SWAP_BE32(*currentWord
)) != 0)
1540 // Figure out which bit is set
1541 bitMask
= kHighBitInWordMask
;
1542 while (!(tempWord
& bitMask
))
1548 break; // Found the used bit; break out to FoundUsed.
1551 // Keep looking at the next word
1552 currentBlock
+= kBitsPerWord
;
1556 // If we found at least maxBlocks, we can quit early.
1557 if ((currentBlock
- firstBlock
) >= maxBlocks
)
1562 // Make sure we didn't run out of bitmap looking for a used block.
1563 // If so, pin to the end of the bitmap.
1564 if (currentBlock
> endingBlock
)
1565 currentBlock
= endingBlock
;
1567 // Figure out how many contiguous free blocks there were.
1568 // Pin the answer to maxBlocks.
1569 foundBlocks
= currentBlock
- firstBlock
;
1570 if (foundBlocks
> maxBlocks
)
1571 foundBlocks
= maxBlocks
;
1572 if (foundBlocks
>= minBlocks
)
1573 break; // Found what we needed!
1575 // This free chunk wasn't big enough. Try inserting it into the free extent cache in case
1576 // the allocation wasn't forced contiguous.
1577 tempWord
= vcb
->vcbFreeExtCnt
;
1578 if (tempWord
== kMaxFreeExtents
&& vcb
->vcbFreeExt
[kMaxFreeExtents
-1].blockCount
< foundBlocks
)
1580 if (tempWord
< kMaxFreeExtents
)
1582 // We're going to add this extent. Bubble any smaller extents down in the list.
1583 while (tempWord
&& vcb
->vcbFreeExt
[tempWord
-1].blockCount
< foundBlocks
)
1585 vcb
->vcbFreeExt
[tempWord
] = vcb
->vcbFreeExt
[tempWord
-1];
1588 vcb
->vcbFreeExt
[tempWord
].startBlock
= firstBlock
;
1589 vcb
->vcbFreeExt
[tempWord
].blockCount
= foundBlocks
;
1591 if (vcb
->vcbFreeExtCnt
< kMaxFreeExtents
)
1592 ++vcb
->vcbFreeExtCnt
;
1594 } while (currentBlock
< stopBlock
);
1597 // Return the outputs.
1598 if (foundBlocks
< minBlocks
)
1603 *actualStartBlock
= 0;
1604 *actualNumBlocks
= 0;
1609 *actualStartBlock
= firstBlock
;
1610 *actualNumBlocks
= foundBlocks
;
1614 (void) ReleaseBitmapBlock(vcb
, blockRef
, false);