2 * Copyright (c) 1999-2002, 2005, 2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 Contains: Routines to map file positions to volume positions, and manipulate the extents B-Tree.
30 Written by: Dave Heller, Mark Day
32 Copyright: © 1996-1999 by Apple Computer, Inc., all rights reserved.
37 #include "Scavenger.h"
40 ============================================================
41 Public (Exported) Routines:
42 ============================================================
43 DeallocateFile Deallocate all disk space allocated to a specified file.
44 Both forks are deallocated.
46 ExtendFileC Allocate more space to a given file.
49 MapFileBlockC Convert (map) an offset within a given file into a
50 physical disk address.
52 TruncateFileC Truncates the disk space allocated to a file. The file
53 space is truncated to a specified new physical EOF, rounded
54 up to the next allocation block boundry. There is an option
55 to truncate to the end of the extent containing the new EOF.
58 Flush the extents file for a given volume.
61 Copy EOF, physical length, and extent records from one FCB
62 to all other FCBs for that fork. This is used when a file is
63 grown or shrunk as the result of a Write, SetEOF, or Allocate.
66 Map some position in a file to a volume block number. Also
67 returns the number of contiguous bytes that are mapped there.
68 This is a queued HFSDispatch call that does the equivalent of
69 MapFileBlockC, using a parameter block.
72 If the extent record came from the extents file, write out
73 the updated record; otherwise, copy the updated record into
74 the FCB resident extent record. If the record has no extents,
75 and was in the extents file, then delete the record instead.
78 Deallocate all allocation blocks in all extents of an extent
80 ============================================================
82 ============================================================
84 Search the extents BTree for a particular extent record.
86 Search the FCB and extents file for an extent record that
87 contains a given file position (in bytes).
89 Search a given extent record to see if it contains a given
90 file position (in bytes). Used by SearchExtentFile.
92 Deallocate blocks and delete extent records for all allocation
93 blocks beyond a certain point in a file. The starting point
94 must be the first file allocation block for some extent record
97 Deallocate all allocation blocks belonging to a given fork.
102 kTwoGigSectors
= 0x00400000,
105 kResourceForkType
= 0xFF,
107 kPreviousRecord
= -1,
109 kSectorSize
= 512 // Size of a physical sector
112 static OSErr
ExtentsToExtDataRec(
113 HFSPlusExtentRecord oldExtents
,
114 HFSExtentRecord newExtents
);
116 OSErr
FindExtentRecord(
121 Boolean allowPrevious
,
122 HFSPlusExtentKey
*foundKey
,
123 HFSPlusExtentRecord foundData
,
126 OSErr
DeleteExtentRecord(
132 static OSErr
CreateExtentRecord(
134 HFSPlusExtentKey
*key
,
135 HFSPlusExtentRecord extents
,
138 OSErr
GetFCBExtentRecord(
141 HFSPlusExtentRecord extents
);
143 static OSErr
SetFCBExtentRecord(
146 HFSPlusExtentRecord extents
);
148 static OSErr
SearchExtentFile(
152 HFSPlusExtentKey
*foundExtentKey
,
153 HFSPlusExtentRecord foundExtentData
,
154 UInt32
*foundExtentDataIndex
,
155 UInt32
*extentBTreeHint
,
156 UInt32
*endingFABNPlusOne
);
158 static OSErr
SearchExtentRecord(
161 const HFSPlusExtentRecord extentData
,
162 UInt32 extentDataStartFABN
,
163 UInt32
*foundExtentDataOffset
,
164 UInt32
*endingFABNPlusOne
,
165 Boolean
*noMoreExtents
);
168 static OSErr
DeallocateFork(
170 HFSCatalogNodeID fileID
,
172 HFSPlusExtentRecord catalogExtents
,
173 Boolean
* recordDeleted
);
175 static OSErr
TruncateExtents(
180 Boolean
* recordDeleted
);
183 static OSErr
MapFileBlockFromFCB(
186 UInt64 offset
, // Desired offset in bytes from start of file
187 UInt32
*firstFABN
, // FABN of first block of found extent
188 UInt32
*firstBlock
, // Corresponding allocation block number
189 UInt32
*nextFABN
); // FABN of block after end of extent
191 static Boolean
ExtentsAreIntegral(
192 const HFSPlusExtentRecord extentRecord
,
194 UInt32
*blocksChecked
,
195 Boolean
*checkedLastExtent
);
197 //_________________________________________________________________________________
199 // Routine: FindExtentRecord
201 // Purpose: Search the extents BTree for an extent record matching the given
202 // FileID, fork, and starting file allocation block number.
205 // vcb Volume to search
206 // forkType 0 = data fork, -1 = resource fork
207 // fileID File's FileID (HFSCatalogNodeID)
208 // startBlock Starting file allocation block number
209 // allowPrevious If the desired record isn't found and this flag is set,
210 // then see if the previous record belongs to the same fork.
211 // If so, then return it.
214 // foundKey The key data for the record actually found
215 // foundData The extent record actually found (NOTE: on an HFS volume, the
216 // fourth entry will be zeroes.
217 // foundHint The BTree hint to find the node again
218 //_________________________________________________________________________________
219 OSErr
FindExtentRecord(
224 Boolean allowPrevious
,
225 HFSPlusExtentKey
*foundKey
,
226 HFSPlusExtentRecord foundData
,
234 if (vcb
->vcbSignature
== kHFSSigWord
) {
236 HFSExtentKey extentKey
;
237 HFSExtentRecord extentData
;
239 key
.keyLength
= kHFSExtentKeyMaximumLength
;
240 key
.forkType
= forkType
;
242 key
.startBlock
= startBlock
;
244 err
= SearchBTreeRecord(vcb
->vcbExtentsFile
, &key
, kNoHint
, &extentKey
, &extentData
,
245 &foundSize
, foundHint
);
247 if (err
== btNotFound
&& allowPrevious
) {
248 err
= GetBTreeRecord(vcb
->vcbExtentsFile
, kPreviousRecord
, &extentKey
, &extentData
,
249 &foundSize
, foundHint
);
251 // A previous record may not exist, so just return btNotFound (like we would if
252 // it was for the wrong file/fork).
253 if (err
== (OSErr
) fsBTStartOfIterationErr
) //¥¥ fsBTStartOfIterationErr is type unsigned long
257 // Found a previous record. Does it belong to the same fork of the same file?
258 if (extentKey
.fileID
!= fileID
|| extentKey
.forkType
!= forkType
)
266 // Copy the found key back for the caller
267 foundKey
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
268 foundKey
->forkType
= extentKey
.forkType
;
270 foundKey
->fileID
= extentKey
.fileID
;
271 foundKey
->startBlock
= extentKey
.startBlock
;
273 // Copy the found data back for the caller
274 foundData
[0].startBlock
= extentData
[0].startBlock
;
275 foundData
[0].blockCount
= extentData
[0].blockCount
;
276 foundData
[1].startBlock
= extentData
[1].startBlock
;
277 foundData
[1].blockCount
= extentData
[1].blockCount
;
278 foundData
[2].startBlock
= extentData
[2].startBlock
;
279 foundData
[2].blockCount
= extentData
[2].blockCount
;
281 for (i
= 3; i
< kHFSPlusExtentDensity
; ++i
)
283 foundData
[i
].startBlock
= 0;
284 foundData
[i
].blockCount
= 0;
288 else { // HFS Plus volume
289 HFSPlusExtentKey key
;
290 HFSPlusExtentKey extentKey
;
291 HFSPlusExtentRecord extentData
;
293 key
.keyLength
= kHFSPlusExtentKeyMaximumLength
;
294 key
.forkType
= forkType
;
297 key
.startBlock
= startBlock
;
299 err
= SearchBTreeRecord(vcb
->vcbExtentsFile
, &key
, kNoHint
, &extentKey
, &extentData
,
300 &foundSize
, foundHint
);
302 if (err
== btNotFound
&& allowPrevious
) {
303 err
= GetBTreeRecord(vcb
->vcbExtentsFile
, kPreviousRecord
, &extentKey
, &extentData
,
304 &foundSize
, foundHint
);
306 // A previous record may not exist, so just return btNotFound (like we would if
307 // it was for the wrong file/fork).
308 if (err
== (OSErr
) fsBTStartOfIterationErr
) //¥¥ fsBTStartOfIterationErr is type unsigned long
312 // Found a previous record. Does it belong to the same fork of the same file?
313 if (extentKey
.fileID
!= fileID
|| extentKey
.forkType
!= forkType
)
319 // Copy the found key back for the caller
320 CopyMemory(&extentKey
, foundKey
, sizeof(HFSPlusExtentKey
));
321 // Copy the found data back for the caller
322 CopyMemory(&extentData
, foundData
, sizeof(HFSPlusExtentRecord
));
331 static OSErr
CreateExtentRecord(
333 HFSPlusExtentKey
*key
,
334 HFSPlusExtentRecord extents
,
341 if (vcb
->vcbSignature
== kHFSSigWord
) {
343 HFSExtentRecord data
;
345 hfsKey
.keyLength
= kHFSExtentKeyMaximumLength
;
346 hfsKey
.forkType
= key
->forkType
;
347 hfsKey
.fileID
= key
->fileID
;
348 hfsKey
.startBlock
= key
->startBlock
;
350 err
= ExtentsToExtDataRec(extents
, data
);
352 err
= InsertBTreeRecord(vcb
->vcbExtentsFile
, &hfsKey
, data
, sizeof(HFSExtentRecord
), hint
);
354 else { // HFS Plus volume
355 err
= InsertBTreeRecord(vcb
->vcbExtentsFile
, key
, extents
, sizeof(HFSPlusExtentRecord
), hint
);
362 OSErr
DeleteExtentRecord(
372 if (vcb
->vcbSignature
== kHFSSigWord
) {
375 key
.keyLength
= kHFSExtentKeyMaximumLength
;
376 key
.forkType
= forkType
;
378 key
.startBlock
= startBlock
;
380 err
= DeleteBTreeRecord( vcb
->vcbExtentsFile
, &key
);
382 else { // HFS Plus volume
383 HFSPlusExtentKey key
;
385 key
.keyLength
= kHFSPlusExtentKeyMaximumLength
;
386 key
.forkType
= forkType
;
389 key
.startBlock
= startBlock
;
391 err
= DeleteBTreeRecord( vcb
->vcbExtentsFile
, &key
);
399 //_________________________________________________________________________________
401 // Routine: MapFileBlock
403 // Function: Maps a file position into a physical disk address.
405 // Input: A2.L - VCB pointer
406 // (A1,D1.W) - FCB pointer
407 // D4.L - number of bytes desired
408 // D5.L - file position (byte address)
410 // Output: D3.L - physical start block
411 // D6.L - number of contiguous bytes available (up to D4 bytes)
412 // D0.L - result code <01Oct85>
414 // FXRangeErr = file position beyond mapped range <17Oct85>
415 // FXOvFlErr = extents file overflow <17Oct85>
416 // other = error <17Oct85>
418 // Called By: Log2Phys (read/write in place), Cache (map a file block).
419 //_________________________________________________________________________________
421 OSErr
MapFileBlockC (
422 SVCB
*vcb
, // volume that file resides on
423 SFCB
*fcb
, // FCB of file
424 UInt32 numberOfBytes
, // number of contiguous bytes desired
425 UInt64 sectorOffset
, // starting offset within file (in 512-byte sectors)
426 UInt64
*startSector
, // first 512-byte volume sector (NOT an allocation block)
427 UInt32
*availableBytes
) // number of contiguous bytes (up to numberOfBytes)
430 UInt32 allocBlockSize
; // Size of the volume's allocation block, in sectors
431 HFSPlusExtentKey foundKey
;
432 HFSPlusExtentRecord foundData
;
435 UInt32 firstFABN
= 0; // file allocation block of first block in found extent
436 UInt32 nextFABN
; // file allocation block of block after end of found extent
437 UInt64 dataEnd
; // (offset) end of range that is contiguous (in sectors)
438 UInt32 startBlock
= 0; // volume allocation block corresponding to firstFABN
442 // LogStartTime(kTraceMapFileBlock);
444 allocBlockSize
= vcb
->vcbBlockSize
>> kSectorShift
;
446 err
= MapFileBlockFromFCB(vcb
, fcb
, sectorOffset
, &firstFABN
, &startBlock
, &nextFABN
);
448 err
= SearchExtentFile(vcb
, fcb
, sectorOffset
, &foundKey
, foundData
, &foundIndex
, &hint
, &nextFABN
);
450 startBlock
= foundData
[foundIndex
].startBlock
;
451 firstFABN
= nextFABN
- foundData
[foundIndex
].blockCount
;
457 // LogEndTime(kTraceMapFileBlock, err);
463 // Determine the end of the available space. It will either be the end of the extent,
464 // or the file's PEOF, whichever is smaller.
467 // Get fork's physical size, in sectors
468 temp
= fcb
->fcbPhysicalSize
>> kSectorShift
;
469 dataEnd
= (UInt64
) nextFABN
* allocBlockSize
; // Assume valid data through end of this extent
470 if (temp
< dataEnd
) // Is PEOF shorter?
471 dataEnd
= temp
; // Yes, so only map up to PEOF
474 // Compute the absolute sector number that contains the offset of the given file
476 temp
= sectorOffset
- ((UInt64
) firstFABN
* allocBlockSize
); // offset in sectors from start of this extent
477 temp
+= (UInt64
)startBlock
* (UInt64
)allocBlockSize
; // offset in sectors from start of allocation block space
478 if (vcb
->vcbSignature
== kHFSPlusSigWord
)
479 temp
+= vcb
->vcbEmbeddedOffset
/512; // offset into the wrapper
481 temp
+= vcb
->vcbAlBlSt
; // offset in sectors from start of volume
483 // Return the desired sector for file position "offset"
487 // Determine the number of contiguous sectors until the end of the extent
488 // (or the amount they asked for, whichever comes first). In any case,
489 // we never map more than 2GB per call.
491 temp
= dataEnd
- sectorOffset
;
492 if (temp
>= kTwoGigSectors
)
493 temp
= kTwoGigSectors
-1; // never map more than 2GB per call
494 temp
<<= kSectorShift
; // convert sectors to bytes
495 if (temp
> numberOfBytes
)
496 *availableBytes
= numberOfBytes
; // more there than they asked for, so pin the output
498 *availableBytes
= (UInt32
)temp
;
500 // LogEndTime(kTraceMapFileBlock, noErr);
506 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
507 // Routine: ReleaseExtents
509 // Function: Release the extents of a single extent data record.
510 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
513 OSErr
ReleaseExtents(
515 const HFSPlusExtentRecord extentRecord
,
516 UInt32
*numReleasedAllocationBlocks
,
517 Boolean
*releasedLastExtent
)
520 UInt32 numberOfExtents
;
523 *numReleasedAllocationBlocks
= 0;
524 *releasedLastExtent
= false;
526 if (vcb
->vcbSignature
== kHFSPlusSigWord
)
527 numberOfExtents
= kHFSPlusExtentDensity
;
529 numberOfExtents
= kHFSExtentDensity
;
531 for( extentIndex
= 0; extentIndex
< numberOfExtents
; extentIndex
++)
533 UInt32 numAllocationBlocks
;
535 // Loop over the extent record and release the blocks associated with each extent.
537 numAllocationBlocks
= extentRecord
[extentIndex
].blockCount
;
538 if ( numAllocationBlocks
== 0 )
540 *releasedLastExtent
= true;
544 err
= ReleaseBitmapBits( extentRecord
[extentIndex
].startBlock
, numAllocationBlocks
);
548 *numReleasedAllocationBlocks
+= numAllocationBlocks
; // bump FABN to beg of next extent
556 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
557 // Routine: TruncateExtents
559 // Purpose: Delete extent records whose starting file allocation block number
560 // is greater than or equal to a given starting block number. The
561 // allocation blocks represented by the extents are deallocated.
564 // vcb Volume to operate on
565 // fileID Which file to operate on
566 // startBlock Starting file allocation block number for first extent
570 // recordDeleted Set to true if any extents B-tree record was deleted.
571 // Unchanged otherwise.
572 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
574 static OSErr
TruncateExtents(
579 Boolean
* recordDeleted
)
582 Boolean releasedLastExtent
;
583 UInt32 numberExtentsReleased
;
585 HFSPlusExtentKey key
;
586 HFSPlusExtentRecord extents
;
589 err
= FindExtentRecord(vcb
, forkType
, fileID
, startBlock
, false, &key
, extents
, &hint
);
591 if (err
== btNotFound
)
596 err
= ReleaseExtents( vcb
, extents
, &numberExtentsReleased
, &releasedLastExtent
);
597 if (err
!= noErr
) break;
599 err
= DeleteExtentRecord(vcb
, forkType
, fileID
, startBlock
);
600 if (err
!= noErr
) break;
602 *recordDeleted
= true; // We did delete a record
603 startBlock
+= numberExtentsReleased
;
610 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
611 // Routine: DeallocateFork
613 // Function: De-allocates all disk space allocated to a specified fork.
614 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
615 static OSErr
DeallocateFork(
617 HFSCatalogNodeID fileID
,
619 HFSPlusExtentRecord catalogExtents
,
620 Boolean
* recordDeleted
) // set to true if any record was deleted
623 UInt32 numReleasedAllocationBlocks
;
624 Boolean releasedLastExtent
;
626 // Release the catalog extents
627 err
= ReleaseExtents( vcb
, catalogExtents
, &numReleasedAllocationBlocks
, &releasedLastExtent
);
629 // Release the extra extents, if present
630 if (err
== noErr
&& !releasedLastExtent
)
631 err
= TruncateExtents(vcb
, forkType
, fileID
, numReleasedAllocationBlocks
, recordDeleted
);
637 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
638 // Routine: FlushExtentFile
640 // Function: Flushes the extent file for a specified volume
641 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
643 OSErr
FlushExtentFile( SVCB
*vcb
)
647 err
= BTFlushPath(vcb
->vcbExtentsFile
);
650 // If the FCB for the extent "file" is dirty, mark the VCB as dirty.
652 if( ( vcb
->vcbExtentsFile
->fcbFlags
& fcbModifiedMask
) != 0 )
654 (void) MarkVCBDirty( vcb
);
655 err
= FlushVolumeControlBlock( vcb
);
663 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
664 // Routine: DeallocateFile
666 // Function: De-allocates all disk space allocated to a specified file.
667 // The space occupied by both forks is deallocated.
669 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
671 OSErr
DeallocateFile(SVCB
*vcb
, CatalogRecord
* fileRec
)
675 Boolean recordDeleted
= false;
679 if (fileRec
->recordType
== kHFSFileRecord
) {
680 HFSPlusExtentRecord dataForkExtents
;
681 HFSPlusExtentRecord rsrcForkExtents
;
683 ClearMemory(&dataForkExtents
, sizeof(dataForkExtents
));
684 ClearMemory(&rsrcForkExtents
, sizeof(rsrcForkExtents
));
686 for (i
= 0; i
< kHFSExtentDensity
; ++i
) {
687 dataForkExtents
[i
].startBlock
=
688 (UInt32
) (fileRec
->hfsFile
.dataExtents
[i
].startBlock
);
689 dataForkExtents
[i
].blockCount
=
690 (UInt32
) (fileRec
->hfsFile
.dataExtents
[i
].blockCount
);
692 rsrcForkExtents
[i
].startBlock
=
693 (UInt32
) (fileRec
->hfsFile
.rsrcExtents
[i
].startBlock
);
694 rsrcForkExtents
[i
].blockCount
=
695 (UInt32
) (fileRec
->hfsFile
.rsrcExtents
[i
].blockCount
);
698 errDF
= DeallocateFork(vcb
, fileRec
->hfsFile
.fileID
, kDataForkType
,
699 dataForkExtents
, &recordDeleted
);
701 errRF
= DeallocateFork(vcb
, fileRec
->hfsFile
.fileID
, kResourceForkType
,
702 rsrcForkExtents
, &recordDeleted
);
704 else if (fileRec
->recordType
== kHFSPlusFileRecord
) {
705 errDF
= DeallocateFork(vcb
, fileRec
->hfsPlusFile
.fileID
, kDataForkType
,
706 fileRec
->hfsPlusFile
.dataFork
.extents
, &recordDeleted
);
708 errRF
= DeallocateFork(vcb
, fileRec
->hfsPlusFile
.fileID
, kResourceForkType
,
709 fileRec
->hfsPlusFile
.resourceFork
.extents
, &recordDeleted
);
713 (void) FlushExtentFile(vcb
);
717 return (errDF
? errDF
: errRF
);
721 //_________________________________________________________________________________
723 // Routine: Extendfile
725 // Function: Extends the disk space allocated to a file.
727 // Input: A2.L - VCB pointer
728 // A1.L - pointer to FCB array
729 // D1.W - file refnum
730 // D3.B - option flags
731 // kEFContigMask - force contiguous allocation
732 // kEFAllMask - allocate all requested bytes or none
733 // NOTE: You may not set both options.
734 // D4.L - number of additional bytes to allocate
736 // Output: D0.W - result code
739 // D6.L - number of bytes allocated
741 // Called by: FileAloc,FileWrite,SetEof
743 // Note: ExtendFile updates the PEOF in the FCB.
744 //_________________________________________________________________________________
747 SVCB
*vcb
, // volume that file resides on
748 SFCB
*fcb
, // FCB of file to truncate
749 UInt32 sectorsToAdd
, // number of sectors to allocate
750 UInt32 flags
, // EFContig and/or EFAll
751 UInt32
*actualSectorsAdded
)// number of bytes actually allocated
756 UInt32 sectorsPerBlock
;
757 UInt32 blocksToAdd
; // number of blocks we'd like to add
758 UInt32 blocksPerClump
; // number of blocks in clump size
759 UInt32 maxBlocksToAdd
; // max blocks we want to add
760 UInt32 eofBlocks
; // current EOF in blocks
761 HFSPlusExtentKey foundKey
; // from SearchExtentFile
762 HFSPlusExtentRecord foundData
;
763 UInt32 foundIndex
; // from SearchExtentFile
764 UInt32 hint
; // from SearchExtentFile
765 UInt32 nextBlock
; // from SearchExtentFile
767 UInt32 actualStartBlock
;
768 UInt32 actualNumBlocks
;
769 UInt32 numExtentsPerRecord
;
772 needsFlush
= false; // Assume the B-tree header doesn't need to be updated
774 *actualSectorsAdded
= 0;
776 if (vcb
->vcbSignature
== kHFSPlusSigWord
)
777 numExtentsPerRecord
= kHFSPlusExtentDensity
;
779 numExtentsPerRecord
= kHFSExtentDensity
;
782 // Round up the request to whole allocation blocks
784 sectorsPerBlock
= vcb
->vcbBlockSize
>> kSectorShift
;
785 blocksToAdd
= DivideAndRoundUp(sectorsToAdd
, sectorsPerBlock
);
788 // Determine the physical EOF in allocation blocks
790 eofBlocks
= (UInt32
)(fcb
->fcbPhysicalSize
/ vcb
->vcbBlockSize
);
793 // Make sure the request won't make the file too big (>=2GB).
794 // [2350148] Always limit HFS files.
795 // ¥¥ Shouldn't really fail if allOrNothing is false
796 // ¥¥ Adjust for clump size here?
798 if ( vcb
->vcbSignature
== kHFSPlusSigWord
)
800 // Allow it to grow beyond 2GB.
804 UInt32 maxFileBlocks
; // max legal EOF, in blocks
805 maxFileBlocks
= (kTwoGigSectors
-1) / sectorsPerBlock
;
806 if (blocksToAdd
> maxFileBlocks
|| (blocksToAdd
+ eofBlocks
) > maxFileBlocks
) {
813 // If allocation is all-or-nothing, then make sure there
814 // are enough free blocks. (A quick test)
816 if ((flags
& kEFAllMask
) && blocksToAdd
> vcb
->vcbFreeBlocks
) {
822 // There may be blocks allocated beyond the physical EOF
823 // (because we allocated the rest of the clump size, or
824 // because of a PBAllocate or PBAllocContig call).
825 // If these extra blocks exist, then use them to satisfy
826 // part or all of the request.
828 // ¥¥ What, if anything, would break if the physical EOF always
829 // ¥¥ represented ALL extents allocated to the file (including
830 // ¥¥ the clump size roundup)?
832 // Note: (blocks * sectorsPerBlock - 1) is the sector offset
833 // of the last sector in the last block.
835 err
= SearchExtentFile(vcb
, fcb
, (eofBlocks
+blocksToAdd
) * sectorsPerBlock
- 1, &foundKey
, foundData
, &foundIndex
, &hint
, &nextBlock
);
837 // Enough blocks are already allocated. Just update the FCB to reflect the new length.
838 eofBlocks
+= blocksToAdd
; // new EOF, in blocks
839 blocksAdded
+= blocksToAdd
;
842 if (err
!= fxRangeErr
) // Any real error?
843 goto ErrorExit
; // Yes, so exit immediately
846 // There wasn't enough already allocated. But there might have been
847 // a few allocated blocks beyond the physical EOF. So, set the physical
848 // EOF to match the end of the last extent.
850 if (nextBlock
> eofBlocks
) {
851 // There were (nextBlock - eofBlocks) extra blocks past physical EOF
852 blocksAdded
+= nextBlock
- eofBlocks
;
853 blocksToAdd
-= nextBlock
- eofBlocks
;
854 eofBlocks
= nextBlock
;
858 // We still need to allocate more blocks.
860 // First try a contiguous allocation (of the whole amount).
861 // If that fails, get whatever we can.
862 // If forceContig, then take whatever we got
863 // else, keep getting bits and pieces (non-contig)
865 // ¥¥ Need to do clump size calculations
867 blocksPerClump
= fcb
->fcbClumpSize
/ vcb
->vcbBlockSize
;
868 if (blocksPerClump
== 0)
874 // Make maxBlocksToAdd equal to blocksToAdd rounded up to a multiple
875 // of the file's clump size. This gives the file room to grow some
876 // more without fragmenting.
877 if (flags
& kEFNoClumpMask
) {
878 // Caller said not to round up, so only allocate what was asked for.
879 maxBlocksToAdd
= blocksToAdd
;
882 // Round up to multiple of clump size
883 maxBlocksToAdd
= DivideAndRoundUp(blocksToAdd
, blocksPerClump
);
884 maxBlocksToAdd
*= blocksPerClump
;
887 // Try to allocate the new space contiguous with the end of the previous
888 // extent. If this succeeds, the last extent grows and the file does not
889 // become any more fragmented.
890 startBlock
= foundData
[foundIndex
].startBlock
+ foundData
[foundIndex
].blockCount
;
891 err
= BlockAllocate(vcb
, startBlock
, blocksToAdd
, maxBlocksToAdd
, wantContig
, &actualStartBlock
, &actualNumBlocks
);
892 if (err
== dskFulErr
) {
893 if (flags
& kEFContigMask
)
894 break; // AllocContig failed because not enough contiguous space
896 // Couldn't get one big chunk, so get whatever we can.
901 if (actualNumBlocks
!= 0)
905 // Add the new extent to the existing extent record, or create a new one.
906 if (actualStartBlock
== startBlock
) {
907 // We grew the file's last extent, so just adjust the number of blocks.
908 foundData
[foundIndex
].blockCount
+= actualNumBlocks
;
909 err
= UpdateExtentRecord(vcb
, fcb
, &foundKey
, foundData
, hint
);
910 if (err
!= noErr
) break;
915 // Need to add a new extent. See if there is room in the current record.
916 if (foundData
[foundIndex
].blockCount
!= 0) // Is current extent free to use?
917 ++foundIndex
; // No, so use the next one.
918 if (foundIndex
== numExtentsPerRecord
) {
919 // This record is full. Need to create a new one.
920 if (fcb
->fcbFileID
== kHFSExtentsFileID
|| (flags
& kEFNoExtOvflwMask
)) {
921 (void) BlockDeallocate(vcb
, actualStartBlock
, actualNumBlocks
);
922 err
= fxOvFlErr
; // Oops. Can't extend extents file past first record.
926 foundKey
.keyLength
= kHFSPlusExtentKeyMaximumLength
;
927 if (fcb
->fcbFlags
& fcbResourceMask
)
928 foundKey
.forkType
= kResourceForkType
;
930 foundKey
.forkType
= kDataForkType
;
932 foundKey
.fileID
= fcb
->fcbFileID
;
933 foundKey
.startBlock
= nextBlock
;
935 foundData
[0].startBlock
= actualStartBlock
;
936 foundData
[0].blockCount
= actualNumBlocks
;
938 // zero out remaining extents...
939 for (i
= 1; i
< kHFSPlusExtentDensity
; ++i
)
941 foundData
[i
].startBlock
= 0;
942 foundData
[i
].blockCount
= 0;
947 err
= CreateExtentRecord(vcb
, &foundKey
, foundData
, &hint
);
948 if (err
== fxOvFlErr
) {
949 // We couldn't create an extent record because extents B-tree
950 // couldn't grow. Dellocate the extent just allocated and
951 // return a disk full error.
952 (void) BlockDeallocate(vcb
, actualStartBlock
, actualNumBlocks
);
955 if (err
!= noErr
) break;
957 needsFlush
= true; // We need to update the B-tree header
960 // Add a new extent into this record and update.
961 foundData
[foundIndex
].startBlock
= actualStartBlock
;
962 foundData
[foundIndex
].blockCount
= actualNumBlocks
;
963 err
= UpdateExtentRecord(vcb
, fcb
, &foundKey
, foundData
, hint
);
964 if (err
!= noErr
) break;
968 // Figure out how many bytes were actually allocated.
969 // NOTE: BlockAllocate could have allocated more than the minimum
970 // we asked for (up to our requested maximum).
971 // Don't set the PEOF beyond what our client asked for.
972 nextBlock
+= actualNumBlocks
;
973 if (actualNumBlocks
> blocksToAdd
) {
974 blocksAdded
+= blocksToAdd
;
975 eofBlocks
+= blocksToAdd
;
979 blocksAdded
+= actualNumBlocks
;
980 blocksToAdd
-= actualNumBlocks
;
981 eofBlocks
+= actualNumBlocks
;
984 // If contiguous allocation was requested, then we've already got one contiguous
985 // chunk. If we didn't get all we wanted, then adjust the error to disk full.
986 if (flags
& kEFContigMask
) {
987 if (blocksToAdd
!= 0)
989 break; // We've already got everything that's contiguous
992 } while (err
== noErr
&& blocksToAdd
);
996 *actualSectorsAdded
= blocksAdded
* sectorsPerBlock
;
998 fcb
->fcbPhysicalSize
= (UInt64
)eofBlocks
* (UInt64
)vcb
->vcbBlockSize
;
999 fcb
->fcbFlags
|= fcbModifiedMask
;
1002 // [2355121] If we created a new extent record, then update the B-tree header
1004 (void) FlushExtentFile(vcb
);
1011 //_________________________________________________________________________________
1013 // Routine: TruncateFileC
1015 // Function: Truncates the disk space allocated to a file. The file space is
1016 // truncated to a specified new PEOF rounded up to the next allocation
1017 // block boundry. If the 'TFTrunExt' option is specified, the file is
1018 // truncated to the end of the extent containing the new PEOF.
1020 // Input: A2.L - VCB pointer
1021 // A1.L - pointer to FCB array
1022 // D1.W - file refnum
1023 // D2.B - option flags
1024 // TFTrunExt - truncate to the extent containing new PEOF
1027 // Output: D0.W - result code
1031 // Note: TruncateFile updates the PEOF in the FCB.
1032 //_________________________________________________________________________________
1035 OSErr
TruncateFileC (
1036 SVCB
*vcb
, // volume that file resides on
1037 SFCB
*fcb
, // FCB of file to truncate
1038 UInt32 eofSectors
, // new physical size for file
1039 Boolean truncateToExtent
) // if true, truncate to end of extent containing newPEOF
1042 UInt32 nextBlock
; // next file allocation block to consider
1043 UInt32 startBlock
; // Physical (volume) allocation block number of start of a range
1044 UInt32 physNumBlocks
; // Number of allocation blocks in file (according to PEOF)
1046 HFSPlusExtentKey key
; // key for current extent record; key->keyLength == 0 if FCB's extent record
1047 UInt32 hint
; // BTree hint corresponding to key
1048 HFSPlusExtentRecord extentRecord
;
1050 UInt32 extentNextBlock
;
1051 UInt32 numExtentsPerRecord
;
1052 UInt32 sectorsPerBlock
;
1054 Boolean extentChanged
; // true if we actually changed an extent
1055 Boolean recordDeleted
; // true if an extent record got deleted
1057 recordDeleted
= false;
1058 sectorsPerBlock
= vcb
->vcbBlockSize
>> kSectorShift
;
1060 if (vcb
->vcbSignature
== kHFSPlusSigWord
)
1061 numExtentsPerRecord
= kHFSPlusExtentDensity
;
1063 numExtentsPerRecord
= kHFSExtentDensity
;
1065 if (fcb
->fcbFlags
& fcbResourceMask
)
1066 forkType
= kResourceForkType
;
1068 forkType
= kDataForkType
;
1070 // Compute number of allocation blocks currently in file
1071 physNumBlocks
= fcb
->fcbPhysicalSize
/ vcb
->vcbBlockSize
;
1074 // Round newPEOF up to a multiple of the allocation block size. If new size is
1075 // two gigabytes or more, then round down by one allocation block (??? really?
1076 // shouldn't that be an error?).
1078 nextBlock
= DivideAndRoundUp(eofSectors
, sectorsPerBlock
); // number of allocation blocks to remain in file
1079 eofSectors
= nextBlock
* sectorsPerBlock
; // rounded up to multiple of block size
1080 if ((fcb
->fcbFlags
& fcbLargeFileMask
) == 0 && eofSectors
>= kTwoGigSectors
) {
1082 DebugStr("\pHFS: Trying to truncate a file to 2GB or more");
1084 err
= fileBoundsErr
;
1089 // Update FCB's length
1091 fcb
->fcbPhysicalSize
= (UInt64
)nextBlock
* (UInt64
)vcb
->vcbBlockSize
;
1092 fcb
->fcbFlags
|= fcbModifiedMask
;
1095 // If the new PEOF is 0, then truncateToExtent has no meaning (we should always deallocate
1098 if (eofSectors
== 0) {
1101 // Find the catalog extent record
1102 err
= GetFCBExtentRecord(vcb
, fcb
, extentRecord
);
1103 if (err
!= noErr
) goto ErrorExit
; // got some error, so return it
1105 // Deallocate all the extents for this fork
1106 err
= DeallocateFork(vcb
, fcb
->fcbFileID
, forkType
, extentRecord
, &recordDeleted
);
1107 if (err
!= noErr
) goto ErrorExit
; // got some error, so return it
1109 // Update the catalog extent record (making sure it's zeroed out)
1111 for (i
=0; i
< numExtentsPerRecord
; i
++) {
1112 extentRecord
[i
].startBlock
= 0;
1113 extentRecord
[i
].blockCount
= 0;
1116 err
= SetFCBExtentRecord((VCB
*) vcb
, fcb
, extentRecord
);
1121 // Find the extent containing byte (peof-1). This is the last extent we'll keep.
1122 // (If truncateToExtent is true, we'll keep the whole extent; otherwise, we'll only
1123 // keep up through peof). The search will tell us how many allocation blocks exist
1124 // in the found extent plus all previous extents.
1126 err
= SearchExtentFile(vcb
, fcb
, eofSectors
-1, &key
, extentRecord
, &extentIndex
, &hint
, &extentNextBlock
);
1127 if (err
!= noErr
) goto ErrorExit
;
1129 extentChanged
= false; // haven't changed the extent yet
1131 if (!truncateToExtent
) {
1133 // Shorten this extent. It may be the case that the entire extent gets
1136 numBlocks
= extentNextBlock
- nextBlock
; // How many blocks in this extent to free up
1137 if (numBlocks
!= 0) {
1138 // Compute first volume allocation block to free
1139 startBlock
= extentRecord
[extentIndex
].startBlock
+ extentRecord
[extentIndex
].blockCount
- numBlocks
;
1140 // Free the blocks in bitmap
1141 err
= BlockDeallocate(vcb
, startBlock
, numBlocks
);
1142 if (err
!= noErr
) goto ErrorExit
;
1143 // Adjust length of this extent
1144 extentRecord
[extentIndex
].blockCount
-= numBlocks
;
1145 // If extent is empty, set start block to 0
1146 if (extentRecord
[extentIndex
].blockCount
== 0)
1147 extentRecord
[extentIndex
].startBlock
= 0;
1148 // Remember that we changed the extent record
1149 extentChanged
= true;
1154 // Now move to the next extent in the record, and set up the file allocation block number
1156 nextBlock
= extentNextBlock
; // Next file allocation block to free
1157 ++extentIndex
; // Its index within the extent record
1160 // Release all following extents in this extent record. Update the record.
1162 while (extentIndex
< numExtentsPerRecord
&& extentRecord
[extentIndex
].blockCount
!= 0) {
1163 numBlocks
= extentRecord
[extentIndex
].blockCount
;
1164 // Deallocate this extent
1165 err
= BlockDeallocate(vcb
, extentRecord
[extentIndex
].startBlock
, numBlocks
);
1166 if (err
!= noErr
) goto ErrorExit
;
1167 // Update next file allocation block number
1168 nextBlock
+= numBlocks
;
1169 // Zero out start and length of this extent to delete it from record
1170 extentRecord
[extentIndex
].startBlock
= 0;
1171 extentRecord
[extentIndex
].blockCount
= 0;
1172 // Remember that we changed an extent
1173 extentChanged
= true;
1174 // Move to next extent in record
1179 // If any of the extents in the current record were changed, then update that
1180 // record (in the FCB, or extents file).
1182 if (extentChanged
) {
1183 err
= UpdateExtentRecord(vcb
, fcb
, &key
, extentRecord
, hint
);
1184 if (err
!= noErr
) goto ErrorExit
;
1188 // If there are any following allocation blocks, then we need
1189 // to seach for their extent records and delete those allocation
1192 if (nextBlock
< physNumBlocks
)
1193 err
= TruncateExtents(vcb
, forkType
, fcb
->fcbFileID
, nextBlock
, &recordDeleted
);
1199 if (err
== fxRangeErr
)
1200 DebugStr("\pAbout to return fxRangeErr");
1203 // [2355121] If we actually deleted extent records, then update the B-tree header
1205 (void) FlushExtentFile(vcb
);
1212 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1213 // Routine: SearchExtentRecord (was XRSearch)
1215 // Function: Searches extent record for the extent mapping a given file
1216 // allocation block number (FABN).
1218 // Input: searchFABN - desired FABN
1219 // extentData - pointer to extent data record (xdr)
1220 // extentDataStartFABN - beginning FABN for extent record
1222 // Output: foundExtentDataOffset - offset to extent entry within xdr
1223 // result = noErr, offset to extent mapping desired FABN
1224 // result = FXRangeErr, offset to last extent in record
1225 // endingFABNPlusOne - ending FABN +1
1226 // noMoreExtents - True if the extent was not found, and the
1227 // extent record was not full (so don't bother
1228 // looking in subsequent records); false otherwise.
1230 // Result: noErr = ok
1231 // FXRangeErr = desired FABN > last mapped FABN in record
1232 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1234 static OSErr
SearchExtentRecord(
1237 const HFSPlusExtentRecord extentData
,
1238 UInt32 extentDataStartFABN
,
1239 UInt32
*foundExtentIndex
,
1240 UInt32
*endingFABNPlusOne
,
1241 Boolean
*noMoreExtents
)
1245 UInt32 numberOfExtents
;
1246 UInt32 numAllocationBlocks
;
1247 Boolean foundExtent
;
1249 *endingFABNPlusOne
= extentDataStartFABN
;
1250 *noMoreExtents
= false;
1251 foundExtent
= false;
1253 if (vcb
->vcbSignature
== kHFSPlusSigWord
)
1254 numberOfExtents
= kHFSPlusExtentDensity
;
1256 numberOfExtents
= kHFSExtentDensity
;
1258 for( extentIndex
= 0; extentIndex
< numberOfExtents
; ++extentIndex
)
1261 // Loop over the extent record and find the search FABN.
1263 numAllocationBlocks
= extentData
[extentIndex
].blockCount
;
1264 if ( numAllocationBlocks
== 0 )
1269 *endingFABNPlusOne
+= numAllocationBlocks
;
1271 if( searchFABN
< *endingFABNPlusOne
)
1273 // Found the extent.
1281 // Found the extent. Note the extent offset
1282 *foundExtentIndex
= extentIndex
;
1286 // Did not find the extent. Set foundExtentDataOffset accordingly
1287 if( extentIndex
> 0 )
1289 *foundExtentIndex
= extentIndex
- 1;
1293 *foundExtentIndex
= 0;
1296 // If we found an empty extent, then set noMoreExtents.
1297 if (extentIndex
< numberOfExtents
)
1298 *noMoreExtents
= true;
1300 // Finally, return an error to the caller
1307 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1308 // Routine: SearchExtentFile (was XFSearch)
1310 // Function: Searches extent file (including the FCB resident extent record)
1311 // for the extent mapping a given file position.
1313 // Input: vcb - VCB pointer
1314 // fcb - FCB pointer
1315 // filePosition - file position (byte address)
1317 // Output: foundExtentKey - extent key record (xkr)
1318 // If extent was found in the FCB's resident extent record,
1319 // then foundExtentKey->keyLength will be set to 0.
1320 // foundExtentData - extent data record(xdr)
1321 // foundExtentIndex - index to extent entry in xdr
1322 // result = 0, offset to extent mapping desired FABN
1323 // result = FXRangeErr, offset to last extent in record
1324 // (i.e., kNumExtentsPerRecord-1)
1325 // extentBTreeHint - BTree hint for extent record
1326 // kNoHint = Resident extent record
1327 // endingFABNPlusOne - ending FABN +1
1330 // noErr Found an extent that contains the given file position
1331 // FXRangeErr Given position is beyond the last allocated extent
1332 // (other) (some other internal I/O error)
1333 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1335 static OSErr
SearchExtentFile(
1338 UInt64 sectorOffset
,
1339 HFSPlusExtentKey
*foundExtentKey
,
1340 HFSPlusExtentRecord foundExtentData
,
1341 UInt32
*foundExtentIndex
,
1342 UInt32
*extentBTreeHint
,
1343 UInt32
*endingFABNPlusOne
)
1346 UInt32 filePositionBlock
;
1347 Boolean noMoreExtents
= true;
1349 filePositionBlock
= (UInt32
)(sectorOffset
/ (vcb
->vcbBlockSize
>> kSectorShift
));
1351 // Search the resident FCB first.
1352 err
= GetFCBExtentRecord(vcb
, fcb
, foundExtentData
);
1354 err
= SearchExtentRecord( vcb
, filePositionBlock
, foundExtentData
, 0,
1355 foundExtentIndex
, endingFABNPlusOne
, &noMoreExtents
);
1357 if( err
== noErr
) {
1358 // Found the extent. Set results accordingly
1359 *extentBTreeHint
= kNoHint
; // no hint, because not in the BTree
1360 foundExtentKey
->keyLength
= 0; // 0 = the FCB itself
1365 // Didn't find extent in FCB. If FCB's extent record wasn't full, there's no point
1366 // in searching the extents file. Note that SearchExtentRecord left us pointing at
1367 // the last valid extent (or the first one, if none were valid). This means we need
1368 // to fill in the hint and key outputs, just like the "if" statement above.
1369 if ( noMoreExtents
) {
1370 *extentBTreeHint
= kNoHint
; // no hint, because not in the BTree
1371 foundExtentKey
->keyLength
= 0; // 0 = the FCB itself
1372 err
= fxRangeErr
; // There are no more extents, so must be beyond PEOF
1377 // Find the desired record, or the previous record if it is the same fork
1379 err
= FindExtentRecord(vcb
, (fcb
->fcbFlags
& fcbResourceMask
) ? kResourceForkType
: kDataForkType
,
1380 fcb
->fcbFileID
, filePositionBlock
, true, foundExtentKey
, foundExtentData
, extentBTreeHint
);
1382 if (err
== btNotFound
) {
1384 // If we get here, the desired position is beyond the extents in the FCB, and there are no extents
1385 // in the extents file. Return the FCB's extents and a range error.
1387 *extentBTreeHint
= kNoHint
;
1388 foundExtentKey
->keyLength
= 0;
1389 err
= GetFCBExtentRecord(vcb
, fcb
, foundExtentData
);
1390 // Note: foundExtentIndex and endingFABNPlusOne have already been set as a result of the very
1391 // first SearchExtentRecord call in this function (when searching in the FCB's extents, and
1392 // we got a range error).
1398 // If we get here, there was either a BTree error, or we found an appropriate record.
1399 // If we found a record, then search it for the correct index into the extents.
1402 // Find appropriate index into extent record
1403 err
= SearchExtentRecord(vcb
, filePositionBlock
, foundExtentData
, foundExtentKey
->startBlock
,
1404 foundExtentIndex
, endingFABNPlusOne
, &noMoreExtents
);
1413 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1414 // Routine: UpdateExtentRecord
1416 // Function: Write new extent data to an existing extent record with a given key.
1417 // If all of the extents are empty, and the extent record is in the
1418 // extents file, then the record is deleted.
1420 // Input: vcb - the volume containing the extents
1421 // fcb - the file that owns the extents
1422 // extentFileKey - pointer to extent key record (xkr)
1423 // If the key length is 0, then the extents are actually part
1424 // of the catalog record, stored in the FCB.
1425 // extentData - pointer to extent data record (xdr)
1426 // extentBTreeHint - hint for given key, or kNoHint
1428 // Result: noErr = ok
1429 // (other) = error from BTree
1430 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1432 OSErr
UpdateExtentRecord (
1435 const HFSPlusExtentKey
*extentFileKey
,
1436 HFSPlusExtentRecord extentData
,
1437 UInt32 extentBTreeHint
)
1441 UInt16 foundDataSize
;
1443 if (extentFileKey
->keyLength
== 0) { // keyLength == 0 means the FCB's extent record
1444 err
= SetFCBExtentRecord(vcb
, fcb
, extentData
);
1445 fcb
->fcbFlags
|= fcbModifiedMask
;
1449 // Need to find and change a record in Extents BTree
1451 if (vcb
->vcbSignature
== kHFSSigWord
) {
1452 HFSExtentKey key
; // Actual extent key used on disk in HFS
1453 HFSExtentKey foundKey
; // The key actually found during search
1454 HFSExtentRecord foundData
; // The extent data actually found
1456 key
.keyLength
= kHFSExtentKeyMaximumLength
;
1457 key
.forkType
= extentFileKey
->forkType
;
1458 key
.fileID
= extentFileKey
->fileID
;
1459 key
.startBlock
= extentFileKey
->startBlock
;
1461 err
= SearchBTreeRecord(vcb
->vcbExtentsFile
, &key
, extentBTreeHint
,
1462 &foundKey
, &foundData
, &foundDataSize
, &foundHint
);
1465 err
= ExtentsToExtDataRec(extentData
, (HFSExtentDescriptor
*)&foundData
);
1468 err
= ReplaceBTreeRecord(vcb
->vcbExtentsFile
, &foundKey
, foundHint
, &foundData
, foundDataSize
, &foundHint
);
1470 else { // HFS Plus volume
1471 HFSPlusExtentKey foundKey
; // The key actually found during search
1472 HFSPlusExtentRecord foundData
; // The extent data actually found
1474 err
= SearchBTreeRecord(vcb
->vcbExtentsFile
, extentFileKey
, extentBTreeHint
,
1475 &foundKey
, &foundData
, &foundDataSize
, &foundHint
);
1478 CopyMemory(extentData
, &foundData
, sizeof(HFSPlusExtentRecord
));
1481 err
= ReplaceBTreeRecord(vcb
->vcbExtentsFile
, &foundKey
, foundHint
, &foundData
, foundDataSize
, &foundHint
);
1489 void ExtDataRecToExtents(
1490 const HFSExtentRecord oldExtents
,
1491 HFSPlusExtentRecord newExtents
)
1495 // copy the first 3 extents
1496 newExtents
[0].startBlock
= oldExtents
[0].startBlock
;
1497 newExtents
[0].blockCount
= oldExtents
[0].blockCount
;
1498 newExtents
[1].startBlock
= oldExtents
[1].startBlock
;
1499 newExtents
[1].blockCount
= oldExtents
[1].blockCount
;
1500 newExtents
[2].startBlock
= oldExtents
[2].startBlock
;
1501 newExtents
[2].blockCount
= oldExtents
[2].blockCount
;
1503 // zero out the remaining ones
1504 for (i
= 3; i
< kHFSPlusExtentDensity
; ++i
)
1506 newExtents
[i
].startBlock
= 0;
1507 newExtents
[i
].blockCount
= 0;
1513 static OSErr
ExtentsToExtDataRec(
1514 HFSPlusExtentRecord oldExtents
,
1515 HFSExtentRecord newExtents
)
1521 // copy the first 3 extents
1522 newExtents
[0].startBlock
= oldExtents
[0].startBlock
;
1523 newExtents
[0].blockCount
= oldExtents
[0].blockCount
;
1524 newExtents
[1].startBlock
= oldExtents
[1].startBlock
;
1525 newExtents
[1].blockCount
= oldExtents
[1].blockCount
;
1526 newExtents
[2].startBlock
= oldExtents
[2].startBlock
;
1527 newExtents
[2].blockCount
= oldExtents
[2].blockCount
;
1530 if (oldExtents
[3].startBlock
|| oldExtents
[3].blockCount
) {
1531 DebugStr("\pExtentRecord with > 3 extents is invalid for HFS");
1540 OSErr
GetFCBExtentRecord(
1543 HFSPlusExtentRecord extents
)
1545 if (vcb
->vcbSignature
== kHFSPlusSigWord
)
1546 CopyMemory(fcb
->fcbExtents32
, extents
, sizeof(HFSPlusExtentRecord
));
1548 ExtDataRecToExtents(fcb
->fcbExtents16
, extents
);
1554 static OSErr
SetFCBExtentRecord(
1557 HFSPlusExtentRecord extents
)
1561 if (fcb
->fcbVolume
!= vcb
)
1562 DebugStr("\pVCB does not match FCB");
1565 if (vcb
->vcbSignature
== kHFSPlusSigWord
)
1566 CopyMemory(extents
, fcb
->fcbExtents32
, sizeof(HFSPlusExtentRecord
));
1568 (void) ExtentsToExtDataRec(extents
, fcb
->fcbExtents16
);
1575 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1576 // Routine: MapFileBlockFromFCB
1578 // Function: Determine if the given file offset is within the set of extents
1579 // stored in the FCB. If so, return the file allocation
1580 // block number of the start of the extent, volume allocation block number
1581 // of the start of the extent, and file allocation block number immediately
1582 // following the extent.
1584 // Input: vcb - the volume containing the extents
1585 // fcb - the file that owns the extents
1586 // offset - desired offset in 512-byte sectors
1588 // Output: firstFABN - file alloc block number of start of extent
1589 // firstBlock - volume alloc block number of start of extent
1590 // nextFABN - file alloc block number of next extent
1592 // Result: noErr = ok
1593 // fxRangeErr = beyond FCB's extents
1594 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1595 static OSErr
MapFileBlockFromFCB(
1598 UInt64 sectorOffset
, // Desired offset in sectors from start of file
1599 UInt32
*firstFABN
, // FABN of first block of found extent
1600 UInt32
*firstBlock
, // Corresponding allocation block number
1601 UInt32
*nextFABN
) // FABN of block after end of extent
1604 UInt32 offsetBlocks
;
1606 offsetBlocks
= (UInt32
)(sectorOffset
/ (vcb
->vcbBlockSize
>> kSectorShift
));
1608 if (vcb
->vcbSignature
== kHFSSigWord
) {
1609 const HFSExtentDescriptor
*extent
;
1613 extent
= fcb
->fcbExtents16
;
1616 for (index
=0; index
<kHFSExtentDensity
; index
++) {
1618 blockCount
= extent
->blockCount
;
1620 if (blockCount
== 0)
1621 return fxRangeErr
; // ran out of extents!
1623 // Is it in this extent?
1624 if (offsetBlocks
< blockCount
) {
1625 *firstFABN
= currentFABN
;
1626 *firstBlock
= extent
->startBlock
;
1627 currentFABN
+= blockCount
; // faster to add these as UInt16 first, then extend to UInt32
1628 *nextFABN
= currentFABN
;
1629 return noErr
; // found the right extent
1632 // Not in current extent, so adjust counters and loop again
1633 offsetBlocks
-= blockCount
;
1634 currentFABN
+= blockCount
;
1639 const HFSPlusExtentDescriptor
*extent
;
1643 extent
= fcb
->fcbExtents32
;
1646 for (index
=0; index
<kHFSPlusExtentDensity
; index
++) {
1648 blockCount
= extent
->blockCount
;
1650 if (blockCount
== 0)
1651 return fxRangeErr
; // ran out of extents!
1653 // Is it in this extent?
1654 if (offsetBlocks
< blockCount
) {
1655 *firstFABN
= currentFABN
;
1656 *firstBlock
= extent
->startBlock
;
1657 *nextFABN
= currentFABN
+ blockCount
;
1658 return noErr
; // found the right extent
1661 // Not in current extent, so adjust counters and loop again
1662 offsetBlocks
-= blockCount
;
1663 currentFABN
+= blockCount
;
1668 // If we fall through here, the extent record was full, but the offset was
1669 // beyond those extents.
1675 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1676 // Routine: ZeroFileBlocks
1678 // Function: Write all zeros to a range of a file. Currently used when
1679 // extending a B-Tree, so that all the new allocation blocks
1680 // contain zeros (to prevent them from accidentally looking
1683 // Input: vcb - the volume
1685 // startingSector - the first 512-byte sector to write
1686 // numberOfSectors - the number of sectors to zero
1688 // Result: noErr = ok
1689 // fxRangeErr = beyond FCB's extents
1690 //ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
1691 #define FSBufferSize 32768
1693 OSErr
ZeroFileBlocks( SVCB
*vcb
, SFCB
*fcb
, UInt32 startingSector
, UInt32 numberOfSectors
)
1698 UInt32 requestedBytes
;
1699 UInt32 actualBytes
; // Bytes actually read by CacheReadInPlace
1700 UInt32 bufferSizeSectors
= FSBufferSize
>> kSectorShift
;
1701 UInt64 currentPosition
= startingSector
<< kSectorShift
;
1703 buffer
= AllocateMemory(FSBufferSize
);
1704 if ( buffer
== NULL
)
1705 return( fileBoundsErr
);
1707 ClearMemory( buffer
, FSBufferSize
); // Zero our buffer
1708 ClearMemory( &iopb
, sizeof(iopb
) ); // Zero our param block
1710 iopb
.ioRefNum
= ResolveFileRefNum( fcb
);
1711 iopb
.ioBuffer
= buffer
;
1712 iopb
.ioPosMode
|= noCacheMask
; // OR with the high byte
1716 if ( numberOfSectors
> bufferSizeSectors
)
1717 requestedBytes
= FSBufferSize
;
1719 requestedBytes
= numberOfSectors
<< kSectorShift
;
1721 err
= CacheWriteInPlace( vcb
, iopb
.ioRefNum
, &iopb
, currentPosition
, requestedBytes
, &actualBytes
);
1723 if ( err
|| actualBytes
== 0 )
1726 // Don't update ioActCount to force writing from beginning of zero buffer
1727 currentPosition
+= actualBytes
;
1728 numberOfSectors
-= (actualBytes
>> kSectorShift
);
1730 } while( numberOfSectors
> 0 );
1733 DisposeMemory(buffer
);
1735 if ( err
== noErr
&& numberOfSectors
!= 0 )
1741 //_________________________________________________________________________________
1743 // Routine: ExtentsAreIntegral
1745 // Purpose: Ensure that each extent can hold an integral number of nodes
1746 // Called by the NodesAreContiguous function
1747 //_________________________________________________________________________________
1749 static Boolean
ExtentsAreIntegral(
1750 const HFSPlusExtentRecord extentRecord
,
1752 UInt32
*blocksChecked
,
1753 Boolean
*checkedLastExtent
)
1759 *checkedLastExtent
= false;
1761 for(extentIndex
= 0; extentIndex
< kHFSPlusExtentDensity
; extentIndex
++)
1763 blocks
= extentRecord
[extentIndex
].blockCount
;
1767 *checkedLastExtent
= true;
1771 *blocksChecked
+= blocks
;
1780 //_________________________________________________________________________________
1782 // Routine: NodesAreContiguous
1784 // Purpose: Ensure that all b-tree nodes are contiguous on disk
1785 // Called by BTOpenPath during volume mount
1786 //_________________________________________________________________________________
1788 Boolean
NodesAreContiguous(
1795 UInt32 blocksChecked
;
1797 HFSPlusExtentKey key
;
1798 HFSPlusExtentRecord extents
;
1800 Boolean lastExtentReached
;
1803 vcb
= (SVCB
*)fcb
->fcbVolume
;
1805 if (vcb
->vcbBlockSize
>= nodeSize
)
1808 mask
= (nodeSize
/ vcb
->vcbBlockSize
) - 1;
1810 // check the local extents
1811 (void) GetFCBExtentRecord(vcb
, fcb
, extents
);
1812 if ( !ExtentsAreIntegral(extents
, mask
, &blocksChecked
, &lastExtentReached
) )
1815 if (lastExtentReached
|| ((UInt64
)blocksChecked
* (UInt64
)vcb
->vcbBlockSize
) >= fcb
->fcbPhysicalSize
)
1818 startBlock
= blocksChecked
;
1820 // check the overflow extents (if any)
1821 while ( !lastExtentReached
)
1823 result
= FindExtentRecord(vcb
, kDataForkType
, fcb
->fcbFileID
, startBlock
, false, &key
, extents
, &hint
);
1826 if ( !ExtentsAreIntegral(extents
, mask
, &blocksChecked
, &lastExtentReached
) )
1829 startBlock
+= blocksChecked
;