2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include "../../hfs.h"
31 #include "../../hfs_format.h"
32 #include "../../hfs_endian.h"
34 #include "../headers/FileMgrInternal.h"
35 #include "../headers/BTreesInternal.h"
37 #include <sys/malloc.h>
40 ============================================================
41 Public (Exported) Routines:
42 ============================================================
44 ExtendFileC Allocate more space to a given file.
47 Compare two extents file keys (a search key and a trial
48 key). Used by the BTree manager when searching for,
49 adding, or deleting keys in the extents file of an HFS
53 Compare two extents file keys (a search key and a trial
54 key). Used by the BTree manager when searching for,
55 adding, or deleting keys in the extents file of an HFS+
58 MapFileBlockC Convert (map) an offset within a given file into a
59 physical disk address.
61 TruncateFileC Truncates the disk space allocated to a file. The file
62 space is truncated to a specified new physical EOF, rounded
63 up to the next allocation block boundry. There is an option
64 to truncate to the end of the extent containing the new EOF.
67 Flush the extents file for a given volume.
72 ============================================================
74 ============================================================
76 Search the extents BTree for a particular extent record.
78 Search the FCB and extents file for an extent record that
79 contains a given file position (in bytes).
81 Search a given extent record to see if it contains a given
82 file position (in bytes). Used by SearchExtentFile.
84 Deallocate all allocation blocks in all extents of an extent
87 Deallocate blocks and delete extent records for all allocation
88 blocks beyond a certain point in a file. The starting point
89 must be the first file allocation block for some extent record
92 Deallocate all allocation blocks belonging to a given fork.
94 If the extent record came from the extents file, write out
95 the updated record; otherwise, copy the updated record into
96 the FCB resident extent record. If the record has no extents,
97 and was in the extents file, then delete the record instead.
100 static const int64_t kTwoGigabytes
= 0x80000000LL
;
105 kResourceForkType
= 0xFF,
111 static OSErr
HFSPlusToHFSExtents(
112 const HFSPlusExtentRecord oldExtents
,
113 HFSExtentRecord newExtents
);
115 static OSErr
FindExtentRecord(
116 const ExtendedVCB
*vcb
,
119 u_int32_t startBlock
,
120 Boolean allowPrevious
,
121 HFSPlusExtentKey
*foundKey
,
122 HFSPlusExtentRecord foundData
,
123 u_int32_t
*foundHint
);
125 static OSErr
DeleteExtentRecord(
126 const ExtendedVCB
*vcb
,
129 u_int32_t startBlock
);
131 static OSErr
CreateExtentRecord(
133 HFSPlusExtentKey
*key
,
134 HFSPlusExtentRecord extents
,
138 static OSErr
GetFCBExtentRecord(
140 HFSPlusExtentRecord extents
);
142 static OSErr
SearchExtentFile(
145 int64_t filePosition
,
146 HFSPlusExtentKey
*foundExtentKey
,
147 HFSPlusExtentRecord foundExtentData
,
148 u_int32_t
*foundExtentDataIndex
,
149 u_int32_t
*extentBTreeHint
,
150 u_int32_t
*endingFABNPlusOne
);
152 static OSErr
SearchExtentRecord(
154 u_int32_t searchFABN
,
155 const HFSPlusExtentRecord extentData
,
156 u_int32_t extentDataStartFABN
,
157 u_int32_t
*foundExtentDataOffset
,
158 u_int32_t
*endingFABNPlusOne
,
159 Boolean
*noMoreExtents
);
161 static OSErr
ReleaseExtents(
163 const HFSPlusExtentRecord extentRecord
,
164 u_int32_t
*numReleasedAllocationBlocks
,
165 Boolean
*releasedLastExtent
);
167 static OSErr
DeallocateFork(
169 HFSCatalogNodeID fileID
,
171 HFSPlusExtentRecord catalogExtents
,
172 Boolean
* recordDeleted
);
174 static OSErr
TruncateExtents(
178 u_int32_t startBlock
,
179 Boolean
* recordDeleted
);
181 static OSErr
UpdateExtentRecord (
184 const HFSPlusExtentKey
*extentFileKey
,
185 const HFSPlusExtentRecord extentData
,
186 u_int32_t extentBTreeHint
);
188 static Boolean
ExtentsAreIntegral(
189 const HFSPlusExtentRecord extentRecord
,
191 u_int32_t
*blocksChecked
,
192 Boolean
*checkedLastExtent
);
194 //_________________________________________________________________________________
196 // Routine: FindExtentRecord
198 // Purpose: Search the extents BTree for an extent record matching the given
199 // FileID, fork, and starting file allocation block number.
202 // vcb Volume to search
203 // forkType 0 = data fork, -1 = resource fork
204 // fileID File's FileID (CatalogNodeID)
205 // startBlock Starting file allocation block number
206 // allowPrevious If the desired record isn't found and this flag is set,
207 // then see if the previous record belongs to the same fork.
208 // If so, then return it.
211 // foundKey The key data for the record actually found
212 // foundData The extent record actually found (NOTE: on an HFS volume, the
213 // fourth entry will be zeroes.
214 // foundHint The BTree hint to find the node again
215 //_________________________________________________________________________________
216 static OSErr
FindExtentRecord(
217 const ExtendedVCB
*vcb
,
220 u_int32_t startBlock
,
221 Boolean allowPrevious
,
222 HFSPlusExtentKey
*foundKey
,
223 HFSPlusExtentRecord foundData
,
224 u_int32_t
*foundHint
)
227 BTreeIterator btIterator
;
228 FSBufferDescriptor btRecord
;
230 u_int16_t btRecordSize
;
235 fcb
= GetFileControlBlock(vcb
->extentsRefNum
);
237 bzero(&btIterator
, sizeof(btIterator
));
239 if (vcb
->vcbSigWord
== kHFSSigWord
) {
240 HFSExtentKey
* extentKeyPtr
;
241 HFSExtentRecord extentData
;
243 extentKeyPtr
= (HFSExtentKey
*) &btIterator
.key
;
244 extentKeyPtr
->keyLength
= kHFSExtentKeyMaximumLength
;
245 extentKeyPtr
->forkType
= forkType
;
246 extentKeyPtr
->fileID
= fileID
;
247 extentKeyPtr
->startBlock
= startBlock
;
249 btRecord
.bufferAddress
= &extentData
;
250 btRecord
.itemSize
= sizeof(HFSExtentRecord
);
251 btRecord
.itemCount
= 1;
253 err
= BTSearchRecord(fcb
, &btIterator
, &btRecord
, &btRecordSize
, &btIterator
);
255 if (err
== btNotFound
&& allowPrevious
) {
256 err
= BTIterateRecord(fcb
, kBTreePrevRecord
, &btIterator
, &btRecord
, &btRecordSize
);
258 // A previous record may not exist, so just return btNotFound (like we would if
259 // it was for the wrong file/fork).
260 if (err
== (OSErr
) fsBTStartOfIterationErr
) //¥¥ fsBTStartOfIterationErr is type unsigned long
264 // Found a previous record. Does it belong to the same fork of the same file?
265 if (extentKeyPtr
->fileID
!= fileID
|| extentKeyPtr
->forkType
!= forkType
)
273 // Copy the found key back for the caller
275 foundKey
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
276 foundKey
->forkType
= extentKeyPtr
->forkType
;
278 foundKey
->fileID
= extentKeyPtr
->fileID
;
279 foundKey
->startBlock
= extentKeyPtr
->startBlock
;
281 // Copy the found data back for the caller
282 foundData
[0].startBlock
= extentData
[0].startBlock
;
283 foundData
[0].blockCount
= extentData
[0].blockCount
;
284 foundData
[1].startBlock
= extentData
[1].startBlock
;
285 foundData
[1].blockCount
= extentData
[1].blockCount
;
286 foundData
[2].startBlock
= extentData
[2].startBlock
;
287 foundData
[2].blockCount
= extentData
[2].blockCount
;
289 for (i
= 3; i
< kHFSPlusExtentDensity
; ++i
)
291 foundData
[i
].startBlock
= 0;
292 foundData
[i
].blockCount
= 0;
296 else { // HFS Plus volume
297 HFSPlusExtentKey
* extentKeyPtr
;
298 HFSPlusExtentRecord extentData
;
300 extentKeyPtr
= (HFSPlusExtentKey
*) &btIterator
.key
;
301 extentKeyPtr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
302 extentKeyPtr
->forkType
= forkType
;
303 extentKeyPtr
->pad
= 0;
304 extentKeyPtr
->fileID
= fileID
;
305 extentKeyPtr
->startBlock
= startBlock
;
307 btRecord
.bufferAddress
= &extentData
;
308 btRecord
.itemSize
= sizeof(HFSPlusExtentRecord
);
309 btRecord
.itemCount
= 1;
311 err
= BTSearchRecord(fcb
, &btIterator
, &btRecord
, &btRecordSize
, &btIterator
);
313 if (err
== btNotFound
&& allowPrevious
) {
314 err
= BTIterateRecord(fcb
, kBTreePrevRecord
, &btIterator
, &btRecord
, &btRecordSize
);
316 // A previous record may not exist, so just return btNotFound (like we would if
317 // it was for the wrong file/fork).
318 if (err
== (OSErr
) fsBTStartOfIterationErr
) //¥¥ fsBTStartOfIterationErr is type unsigned long
322 // Found a previous record. Does it belong to the same fork of the same file?
323 if (extentKeyPtr
->fileID
!= fileID
|| extentKeyPtr
->forkType
!= forkType
)
329 // Copy the found key back for the caller
331 BlockMoveData(extentKeyPtr
, foundKey
, sizeof(HFSPlusExtentKey
));
332 // Copy the found data back for the caller
333 BlockMoveData(&extentData
, foundData
, sizeof(HFSPlusExtentRecord
));
338 *foundHint
= btIterator
.hint
.nodeNum
;
344 static OSErr
CreateExtentRecord(
346 HFSPlusExtentKey
*key
,
347 HFSPlusExtentRecord extents
,
350 BTreeIterator btIterator
;
351 FSBufferDescriptor btRecord
;
352 u_int16_t btRecordSize
;
359 bzero(&btIterator
, sizeof(btIterator
));
362 * The lock taken by callers of ExtendFileC is speculative and
363 * only occurs when the file already has overflow extents. So
364 * We need to make sure we have the lock here. The extents
365 * btree lock can be nested (its recursive) so we always take
368 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
370 if (vcb
->vcbSigWord
== kHFSSigWord
) {
371 HFSExtentKey
* keyPtr
;
372 HFSExtentRecord data
;
374 btRecordSize
= sizeof(HFSExtentRecord
);
375 btRecord
.bufferAddress
= &data
;
376 btRecord
.itemSize
= btRecordSize
;
377 btRecord
.itemCount
= 1;
379 keyPtr
= (HFSExtentKey
*) &btIterator
.key
;
380 keyPtr
->keyLength
= kHFSExtentKeyMaximumLength
;
381 keyPtr
->forkType
= key
->forkType
;
382 keyPtr
->fileID
= key
->fileID
;
383 keyPtr
->startBlock
= key
->startBlock
;
385 err
= HFSPlusToHFSExtents(extents
, data
);
387 else { // HFS Plus volume
388 btRecordSize
= sizeof(HFSPlusExtentRecord
);
389 btRecord
.bufferAddress
= extents
;
390 btRecord
.itemSize
= btRecordSize
;
391 btRecord
.itemCount
= 1;
393 BlockMoveData(key
, &btIterator
.key
, sizeof(HFSPlusExtentKey
));
397 err
= BTInsertRecord(GetFileControlBlock(vcb
->extentsRefNum
), &btIterator
, &btRecord
, btRecordSize
);
400 *hint
= btIterator
.hint
.nodeNum
;
402 (void) BTFlushPath(GetFileControlBlock(vcb
->extentsRefNum
));
404 hfs_systemfile_unlock(vcb
, lockflags
);
410 static OSErr
DeleteExtentRecord(
411 const ExtendedVCB
*vcb
,
414 u_int32_t startBlock
)
416 BTreeIterator btIterator
;
421 bzero(&btIterator
, sizeof(btIterator
));
423 if (vcb
->vcbSigWord
== kHFSSigWord
) {
424 HFSExtentKey
* keyPtr
;
426 keyPtr
= (HFSExtentKey
*) &btIterator
.key
;
427 keyPtr
->keyLength
= kHFSExtentKeyMaximumLength
;
428 keyPtr
->forkType
= forkType
;
429 keyPtr
->fileID
= fileID
;
430 keyPtr
->startBlock
= startBlock
;
432 else { // HFS Plus volume
433 HFSPlusExtentKey
* keyPtr
;
435 keyPtr
= (HFSPlusExtentKey
*) &btIterator
.key
;
436 keyPtr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
437 keyPtr
->forkType
= forkType
;
439 keyPtr
->fileID
= fileID
;
440 keyPtr
->startBlock
= startBlock
;
443 err
= BTDeleteRecord(GetFileControlBlock(vcb
->extentsRefNum
), &btIterator
);
444 (void) BTFlushPath(GetFileControlBlock(vcb
->extentsRefNum
));
451 //_________________________________________________________________________________
453 // Routine: MapFileBlock
455 // Function: Maps a file position into a physical disk address.
457 //_________________________________________________________________________________
460 OSErr
MapFileBlockC (
461 ExtendedVCB
*vcb
, // volume that file resides on
462 FCB
*fcb
, // FCB of file
463 size_t numberOfBytes
, // number of contiguous bytes desired
464 off_t offset
, // starting offset within file (in bytes)
465 daddr64_t
*startSector
, // first sector (NOT an allocation block)
466 size_t *availableBytes
) // number of contiguous bytes (up to numberOfBytes)
469 u_int32_t allocBlockSize
; // Size of the volume's allocation block
470 u_int32_t sectorSize
;
471 HFSPlusExtentKey foundKey
;
472 HFSPlusExtentRecord foundData
;
473 u_int32_t foundIndex
;
475 u_int32_t firstFABN
; // file allocation block of first block in found extent
476 u_int32_t nextFABN
; // file allocation block of block after end of found extent
477 off_t dataEnd
; // (offset) end of range that is contiguous
478 u_int32_t sectorsPerBlock
; // Number of sectors per allocation block
479 u_int32_t startBlock
; // volume allocation block corresponding to firstFABN
483 allocBlockSize
= vcb
->blockSize
;
484 sectorSize
= VCBTOHFS(vcb
)->hfs_logical_block_size
;
486 err
= SearchExtentFile(vcb
, fcb
, offset
, &foundKey
, foundData
, &foundIndex
, &hint
, &nextFABN
);
488 startBlock
= foundData
[foundIndex
].startBlock
;
489 firstFABN
= nextFABN
- foundData
[foundIndex
].blockCount
;
498 // Determine the end of the available space. It will either be the end of the extent,
499 // or the file's PEOF, whichever is smaller.
501 dataEnd
= (off_t
)((off_t
)(nextFABN
) * (off_t
)(allocBlockSize
)); // Assume valid data through end of this extent
502 if (((off_t
)fcb
->ff_blocks
* (off_t
)allocBlockSize
) < dataEnd
) // Is PEOF shorter?
503 dataEnd
= (off_t
)fcb
->ff_blocks
* (off_t
)allocBlockSize
; // Yes, so only map up to PEOF
505 // Compute the number of sectors in an allocation block
506 sectorsPerBlock
= allocBlockSize
/ sectorSize
; // sectors per allocation block
509 // Compute the absolute sector number that contains the offset of the given file
510 // offset in sectors from start of the extent +
511 // offset in sectors from start of allocation block space
513 temp
= (daddr64_t
)((offset
- (off_t
)((off_t
)(firstFABN
) * (off_t
)(allocBlockSize
)))/sectorSize
);
514 temp
+= (daddr64_t
)startBlock
* (daddr64_t
)sectorsPerBlock
;
516 /* Add in any volume offsets */
517 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
518 temp
+= vcb
->hfsPlusIOPosOffset
/ sectorSize
;
520 temp
+= vcb
->vcbAlBlSt
;
522 // Return the desired sector for file position "offset"
526 // Determine the number of contiguous bytes until the end of the extent
527 // (or the amount they asked for, whichever comes first).
531 tmpOff
= dataEnd
- offset
;
532 if (tmpOff
> (off_t
)(numberOfBytes
))
533 *availableBytes
= numberOfBytes
; // more there than they asked for, so pin the output
535 *availableBytes
= tmpOff
;
542 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
543 // Routine: ReleaseExtents
545 // Function: Release the extents of a single extent data record.
546 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
548 static OSErr
ReleaseExtents(
550 const HFSPlusExtentRecord extentRecord
,
551 u_int32_t
*numReleasedAllocationBlocks
,
552 Boolean
*releasedLastExtent
)
554 u_int32_t extentIndex
;
555 u_int32_t numberOfExtents
;
558 *numReleasedAllocationBlocks
= 0;
559 *releasedLastExtent
= false;
561 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
562 numberOfExtents
= kHFSPlusExtentDensity
;
564 numberOfExtents
= kHFSExtentDensity
;
566 for( extentIndex
= 0; extentIndex
< numberOfExtents
; extentIndex
++)
568 u_int32_t numAllocationBlocks
;
570 // Loop over the extent record and release the blocks associated with each extent.
572 numAllocationBlocks
= extentRecord
[extentIndex
].blockCount
;
573 if ( numAllocationBlocks
== 0 )
575 *releasedLastExtent
= true;
579 err
= BlockDeallocate( vcb
, extentRecord
[extentIndex
].startBlock
, numAllocationBlocks
, 0);
583 *numReleasedAllocationBlocks
+= numAllocationBlocks
; // bump FABN to beg of next extent
591 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
592 // Routine: TruncateExtents
594 // Purpose: Delete extent records whose starting file allocation block number
595 // is greater than or equal to a given starting block number. The
596 // allocation blocks represented by the extents are deallocated.
599 // vcb Volume to operate on
600 // fileID Which file to operate on
601 // startBlock Starting file allocation block number for first extent
603 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
605 static OSErr
TruncateExtents(
609 u_int32_t startBlock
,
610 Boolean
* recordDeleted
)
613 u_int32_t numberExtentsReleased
;
614 Boolean releasedLastExtent
;
616 HFSPlusExtentKey key
;
617 HFSPlusExtentRecord extents
;
621 * The lock taken by callers of TruncateFileC is speculative and
622 * only occurs when the file already has overflow extents. So
623 * We need to make sure we have the lock here. The extents
624 * btree lock can be nested (its recursive) so we always take
627 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
630 err
= FindExtentRecord(vcb
, forkType
, fileID
, startBlock
, false, &key
, extents
, &hint
);
632 if (err
== btNotFound
)
637 err
= ReleaseExtents( vcb
, extents
, &numberExtentsReleased
, &releasedLastExtent
);
638 if (err
!= noErr
) break;
640 err
= DeleteExtentRecord(vcb
, forkType
, fileID
, startBlock
);
641 if (err
!= noErr
) break;
643 *recordDeleted
= true;
644 startBlock
+= numberExtentsReleased
;
646 hfs_systemfile_unlock(vcb
, lockflags
);
653 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
654 // Routine: DeallocateFork
656 // Function: De-allocates all disk space allocated to a specified fork.
657 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
659 static OSErr
DeallocateFork(
661 HFSCatalogNodeID fileID
,
663 HFSPlusExtentRecord catalogExtents
,
664 Boolean
* recordDeleted
) /* true if a record was deleted */
667 u_int32_t numReleasedAllocationBlocks
;
668 Boolean releasedLastExtent
;
670 // Release the catalog extents
671 err
= ReleaseExtents( vcb
, catalogExtents
, &numReleasedAllocationBlocks
, &releasedLastExtent
);
672 // Release the extra extents, if present
673 if (err
== noErr
&& !releasedLastExtent
)
674 err
= TruncateExtents(vcb
, forkType
, fileID
, numReleasedAllocationBlocks
, recordDeleted
);
679 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
680 // Routine: FlushExtentFile
682 // Function: Flushes the extent file for a specified volume
683 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
686 OSErr
FlushExtentFile( ExtendedVCB
*vcb
)
692 fcb
= GetFileControlBlock(vcb
->extentsRefNum
);
694 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
695 err
= BTFlushPath(fcb
);
696 hfs_systemfile_unlock(vcb
, lockflags
);
700 // If the FCB for the extent "file" is dirty, mark the VCB as dirty.
702 if (FTOC(fcb
)->c_flag
& C_MODIFIED
)
705 // err = FlushVolumeControlBlock( vcb );
713 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
714 // Routine: CompareExtentKeys
716 // Function: Compares two extent file keys (a search key and a trial key) for
718 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
721 int32_t CompareExtentKeys( const HFSExtentKey
*searchKey
, const HFSExtentKey
*trialKey
)
723 int32_t result
; // ± 1
726 if (searchKey
->keyLength
!= kHFSExtentKeyMaximumLength
)
727 DebugStr("HFS: search Key is wrong length");
728 if (trialKey
->keyLength
!= kHFSExtentKeyMaximumLength
)
729 DebugStr("HFS: trial Key is wrong length");
732 result
= -1; // assume searchKey < trialKey
734 if (searchKey
->fileID
== trialKey
->fileID
) {
736 // FileNum's are equal; compare fork types
738 if (searchKey
->forkType
== trialKey
->forkType
) {
740 // Fork types are equal; compare allocation block number
742 if (searchKey
->startBlock
== trialKey
->startBlock
) {
744 // Everything is equal
750 // Allocation block numbers differ; determine sign
752 if (searchKey
->startBlock
> trialKey
->startBlock
)
758 // Fork types differ; determine sign
760 if (searchKey
->forkType
> trialKey
->forkType
)
766 // FileNums differ; determine sign
768 if (searchKey
->fileID
> trialKey
->fileID
)
777 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
778 // Routine: CompareExtentKeysPlus
780 // Function: Compares two extent file keys (a search key and a trial key) for
782 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
785 int32_t CompareExtentKeysPlus( const HFSPlusExtentKey
*searchKey
, const HFSPlusExtentKey
*trialKey
)
787 int32_t result
; // ± 1
790 if (searchKey
->keyLength
!= kHFSPlusExtentKeyMaximumLength
)
791 DebugStr("HFS: search Key is wrong length");
792 if (trialKey
->keyLength
!= kHFSPlusExtentKeyMaximumLength
)
793 DebugStr("HFS: trial Key is wrong length");
796 result
= -1; // assume searchKey < trialKey
798 if (searchKey
->fileID
== trialKey
->fileID
) {
800 // FileNum's are equal; compare fork types
802 if (searchKey
->forkType
== trialKey
->forkType
) {
804 // Fork types are equal; compare allocation block number
806 if (searchKey
->startBlock
== trialKey
->startBlock
) {
808 // Everything is equal
814 // Allocation block numbers differ; determine sign
816 if (searchKey
->startBlock
> trialKey
->startBlock
)
822 // Fork types differ; determine sign
824 if (searchKey
->forkType
> trialKey
->forkType
)
830 // FileNums differ; determine sign
832 if (searchKey
->fileID
> trialKey
->fileID
)
840 * Add a file extent to a file.
842 * Used by hfs_extendfs to extend the volume allocation bitmap file.
847 AddFileExtent(ExtendedVCB
*vcb
, FCB
*fcb
, u_int32_t startBlock
, u_int32_t blockCount
)
849 HFSPlusExtentKey foundKey
;
850 HFSPlusExtentRecord foundData
;
851 u_int32_t foundIndex
;
858 peof
= (int64_t)(fcb
->ff_blocks
+ blockCount
) * (int64_t)vcb
->blockSize
;
860 error
= SearchExtentFile(vcb
, fcb
, peof
-1, &foundKey
, foundData
, &foundIndex
, &hint
, &nextBlock
);
861 if (error
!= fxRangeErr
)
865 * Add new extent. See if there is room in the current record.
867 if (foundData
[foundIndex
].blockCount
!= 0)
869 if (foundIndex
== kHFSPlusExtentDensity
) {
871 * Existing record is full so create a new one.
873 foundKey
.keyLength
= kHFSPlusExtentKeyMaximumLength
;
874 foundKey
.forkType
= kDataForkType
;
876 foundKey
.fileID
= FTOC(fcb
)->c_fileid
;
877 foundKey
.startBlock
= nextBlock
;
879 foundData
[0].startBlock
= startBlock
;
880 foundData
[0].blockCount
= blockCount
;
882 /* zero out remaining extents. */
883 for (i
= 1; i
< kHFSPlusExtentDensity
; ++i
) {
884 foundData
[i
].startBlock
= 0;
885 foundData
[i
].blockCount
= 0;
890 error
= CreateExtentRecord(vcb
, &foundKey
, foundData
, &hint
);
891 if (error
== fxOvFlErr
)
895 * Add a new extent into existing record.
897 foundData
[foundIndex
].startBlock
= startBlock
;
898 foundData
[foundIndex
].blockCount
= blockCount
;
899 error
= UpdateExtentRecord(vcb
, fcb
, &foundKey
, foundData
, hint
);
901 (void) FlushExtentFile(vcb
);
907 //_________________________________________________________________________________
909 // Routine: Extendfile
911 // Function: Extends the disk space allocated to a file.
913 //_________________________________________________________________________________
917 ExtendedVCB
*vcb
, // volume that file resides on
918 FCB
*fcb
, // FCB of file to truncate
919 int64_t bytesToAdd
, // number of bytes to allocate
920 u_int32_t blockHint
, // desired starting allocation block
921 u_int32_t flags
, // EFContig and/or EFAll
922 int64_t *actualBytesAdded
) // number of bytes actually allocated
925 u_int32_t volumeBlockSize
;
927 int64_t bytesThisExtent
;
928 HFSPlusExtentKey foundKey
;
929 HFSPlusExtentRecord foundData
;
930 u_int32_t foundIndex
;
933 u_int32_t startBlock
;
934 Boolean allOrNothing
;
939 u_int32_t actualStartBlock
;
940 u_int32_t actualNumBlocks
;
941 u_int32_t numExtentsPerRecord
;
942 int64_t maximumBytes
;
945 u_int32_t prevblocks
;
949 *actualBytesAdded
= 0;
950 volumeBlockSize
= vcb
->blockSize
;
951 allOrNothing
= ((flags
& kEFAllMask
) != 0);
952 forceContig
= ((flags
& kEFContigMask
) != 0);
953 prevblocks
= fcb
->ff_blocks
;
955 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
956 numExtentsPerRecord
= kHFSPlusExtentDensity
;
958 numExtentsPerRecord
= kHFSExtentDensity
;
961 // Make sure the request and new PEOF are less than 2GB if HFS.
963 if (vcb
->vcbSigWord
== kHFSSigWord
) {
964 if (bytesToAdd
>= kTwoGigabytes
)
966 if ((((int64_t)fcb
->ff_blocks
* (int64_t)volumeBlockSize
) + bytesToAdd
) >= kTwoGigabytes
)
970 // Determine how many blocks need to be allocated.
971 // Round up the number of desired bytes to add.
973 blocksToAdd
= howmany(bytesToAdd
, volumeBlockSize
);
974 bytesToAdd
= (int64_t)((int64_t)blocksToAdd
* (int64_t)volumeBlockSize
);
977 * For deferred allocations just reserve the blocks.
979 if ((flags
& kEFDeferMask
)
980 && (vcb
->vcbSigWord
== kHFSPlusSigWord
)
981 && (bytesToAdd
< (int64_t)HFS_MAX_DEFERED_ALLOC
)
982 && (blocksToAdd
< hfs_freeblks(VCBTOHFS(vcb
), 1))) {
983 HFS_MOUNT_LOCK(vcb
, TRUE
);
984 vcb
->loanedBlocks
+= blocksToAdd
;
985 HFS_MOUNT_UNLOCK(vcb
, TRUE
);
987 fcb
->ff_unallocblocks
+= blocksToAdd
;
988 FTOC(fcb
)->c_blocks
+= blocksToAdd
;
989 fcb
->ff_blocks
+= blocksToAdd
;
991 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
992 *actualBytesAdded
= bytesToAdd
;
996 * Give back any unallocated blocks before doing real allocations.
998 if (fcb
->ff_unallocblocks
> 0) {
999 u_int32_t loanedBlocks
;
1001 loanedBlocks
= fcb
->ff_unallocblocks
;
1002 blocksToAdd
+= loanedBlocks
;
1003 bytesToAdd
= (int64_t)blocksToAdd
* (int64_t)volumeBlockSize
;
1004 FTOC(fcb
)->c_blocks
-= loanedBlocks
;
1005 fcb
->ff_blocks
-= loanedBlocks
;
1006 fcb
->ff_unallocblocks
= 0;
1008 HFS_MOUNT_LOCK(vcb
, TRUE
);
1009 vcb
->loanedBlocks
-= loanedBlocks
;
1010 HFS_MOUNT_UNLOCK(vcb
, TRUE
);
1014 // If the file's clump size is larger than the allocation block size,
1015 // then set the maximum number of bytes to the requested number of bytes
1016 // rounded up to a multiple of the clump size.
1018 if ((vcb
->vcbClpSiz
> (int32_t)volumeBlockSize
)
1019 && (bytesToAdd
< (int64_t)HFS_MAX_DEFERED_ALLOC
)
1020 && (flags
& kEFNoClumpMask
) == 0) {
1021 maximumBytes
= (int64_t)howmany(bytesToAdd
, vcb
->vcbClpSiz
);
1022 maximumBytes
*= vcb
->vcbClpSiz
;
1024 maximumBytes
= bytesToAdd
;
1028 // Compute new physical EOF, rounded up to a multiple of a block.
1030 if ( (vcb
->vcbSigWord
== kHFSSigWord
) && // Too big?
1031 ((((int64_t)fcb
->ff_blocks
* (int64_t)volumeBlockSize
) + bytesToAdd
) >= kTwoGigabytes
) ) {
1032 if (allOrNothing
) // Yes, must they have it all?
1033 goto Overflow
; // Yes, can't have it
1035 --blocksToAdd
; // No, give give 'em one block less
1036 bytesToAdd
-= volumeBlockSize
;
1041 // If allocation is all-or-nothing, make sure there are
1042 // enough free blocks on the volume (quick test).
1045 (blocksToAdd
> hfs_freeblks(VCBTOHFS(vcb
), flags
& kEFReserveMask
))) {
1051 // See if there are already enough blocks allocated to the file.
1053 peof
= ((int64_t)fcb
->ff_blocks
* (int64_t)volumeBlockSize
) + bytesToAdd
; // potential new PEOF
1054 err
= SearchExtentFile(vcb
, fcb
, peof
-1, &foundKey
, foundData
, &foundIndex
, &hint
, &nextBlock
);
1056 // Enough blocks are already allocated. Just update the FCB to reflect the new length.
1057 fcb
->ff_blocks
= peof
/ volumeBlockSize
;
1058 FTOC(fcb
)->c_blocks
+= (bytesToAdd
/ volumeBlockSize
);
1059 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
1062 if (err
!= fxRangeErr
) // Any real error?
1063 goto ErrorExit
; // Yes, so exit immediately
1066 // Adjust the PEOF to the end of the last extent.
1068 peof
= (int64_t)((int64_t)nextBlock
* (int64_t)volumeBlockSize
); // currently allocated PEOF
1069 bytesThisExtent
= (int64_t)(nextBlock
- fcb
->ff_blocks
) * (int64_t)volumeBlockSize
;
1070 if (bytesThisExtent
!= 0) {
1071 fcb
->ff_blocks
= nextBlock
;
1072 FTOC(fcb
)->c_blocks
+= (bytesThisExtent
/ volumeBlockSize
);
1073 FTOC(fcb
)->c_flag
|= C_MODIFIED
;
1074 bytesToAdd
-= bytesThisExtent
;
1078 // Allocate some more space.
1080 // First try a contiguous allocation (of the whole amount).
1081 // If that fails, get whatever we can.
1082 // If forceContig, then take whatever we got
1083 // else, keep getting bits and pieces (non-contig)
1086 * Note that for sparse devices (like sparse bundle dmgs), we
1087 * should only be aggressive with re-using once-allocated pieces
1088 * if we're not dealing with system files. If we're trying to operate
1089 * on behalf of a system file, we need the maximum contiguous amount
1093 if ( (vcb
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
)
1094 && (fcb
->ff_cp
->c_fileid
>= kHFSFirstUserCatalogNodeID
)
1095 && (flags
& kEFMetadataMask
) == 0) {
1096 if (vcb
->hfs_flags
& HFS_DID_CONTIG_SCAN
) {
1099 // we only want to do this once to scan the bitmap to
1100 // fill in the vcbFreeExt table of free blocks
1101 vcb
->hfs_flags
|= HFS_DID_CONTIG_SCAN
;
1107 useMetaZone
= flags
& kEFMetadataMask
;
1110 startBlock
= blockHint
;
1112 startBlock
= foundData
[foundIndex
].startBlock
+ foundData
[foundIndex
].blockCount
;
1114 actualNumBlocks
= 0;
1115 actualStartBlock
= 0;
1117 /* Find number of free blocks based on reserved block flag option */
1118 availbytes
= (int64_t)hfs_freeblks(VCBTOHFS(vcb
), flags
& kEFReserveMask
) *
1119 (int64_t)volumeBlockSize
;
1120 if (availbytes
<= 0) {
1123 if (wantContig
&& (availbytes
< bytesToAdd
))
1126 err
= BlockAllocate(
1129 howmany(MIN(bytesToAdd
, availbytes
), volumeBlockSize
),
1130 howmany(MIN(maximumBytes
, availbytes
), volumeBlockSize
),
1131 (wantContig
? HFS_ALLOC_FORCECONTIG
: 0) |
1132 (useMetaZone
? HFS_ALLOC_METAZONE
: 0),
1137 if (err
== dskFulErr
) {
1139 break; // AllocContig failed because not enough contiguous space
1141 // Couldn't get one big chunk, so get whatever we can.
1146 if (actualNumBlocks
!= 0)
1148 if (useMetaZone
== 0) {
1149 /* Couldn't get anything so dip into metadat zone */
1156 if (actualNumBlocks
!= 0) {
1157 // this catalog entry *must* get forced to disk when
1158 // hfs_update() is called
1159 FTOC(fcb
)->c_flag
|= C_FORCEUPDATE
;
1162 // Add the new extent to the existing extent record, or create a new one.
1163 if ((actualStartBlock
== startBlock
) && (blockHint
== 0)) {
1164 // We grew the file's last extent, so just adjust the number of blocks.
1165 foundData
[foundIndex
].blockCount
+= actualNumBlocks
;
1166 err
= UpdateExtentRecord(vcb
, fcb
, &foundKey
, foundData
, hint
);
1167 if (err
!= noErr
) break;
1172 // Need to add a new extent. See if there is room in the current record.
1173 if (foundData
[foundIndex
].blockCount
!= 0) // Is current extent free to use?
1174 ++foundIndex
; // No, so use the next one.
1175 if (foundIndex
== numExtentsPerRecord
) {
1176 // This record is full. Need to create a new one.
1177 if (FTOC(fcb
)->c_fileid
== kHFSExtentsFileID
) {
1178 (void) BlockDeallocate(vcb
, actualStartBlock
, actualNumBlocks
, 0);
1179 err
= dskFulErr
; // Oops. Can't extend extents file past first record.
1183 foundKey
.keyLength
= kHFSPlusExtentKeyMaximumLength
;
1184 if (FORK_IS_RSRC(fcb
))
1185 foundKey
.forkType
= kResourceForkType
;
1187 foundKey
.forkType
= kDataForkType
;
1189 foundKey
.fileID
= FTOC(fcb
)->c_fileid
;
1190 foundKey
.startBlock
= nextBlock
;
1192 foundData
[0].startBlock
= actualStartBlock
;
1193 foundData
[0].blockCount
= actualNumBlocks
;
1195 // zero out remaining extents...
1196 for (i
= 1; i
< kHFSPlusExtentDensity
; ++i
)
1198 foundData
[i
].startBlock
= 0;
1199 foundData
[i
].blockCount
= 0;
1204 err
= CreateExtentRecord(vcb
, &foundKey
, foundData
, &hint
);
1205 if (err
== fxOvFlErr
) {
1206 // We couldn't create an extent record because extents B-tree
1207 // couldn't grow. Dellocate the extent just allocated and
1208 // return a disk full error.
1209 (void) BlockDeallocate(vcb
, actualStartBlock
, actualNumBlocks
, 0);
1212 if (err
!= noErr
) break;
1214 needsFlush
= true; // We need to update the B-tree header
1217 // Add a new extent into this record and update.
1218 foundData
[foundIndex
].startBlock
= actualStartBlock
;
1219 foundData
[foundIndex
].blockCount
= actualNumBlocks
;
1220 err
= UpdateExtentRecord(vcb
, fcb
, &foundKey
, foundData
, hint
);
1221 if (err
!= noErr
) break;
1225 // Figure out how many bytes were actually allocated.
1226 // NOTE: BlockAllocate could have allocated more than we asked for.
1227 // Don't set the PEOF beyond what our client asked for.
1228 nextBlock
+= actualNumBlocks
;
1229 bytesThisExtent
= (int64_t)((int64_t)actualNumBlocks
* (int64_t)volumeBlockSize
);
1230 if (bytesThisExtent
> bytesToAdd
) {
1234 bytesToAdd
-= bytesThisExtent
;
1235 maximumBytes
-= bytesThisExtent
;
1237 fcb
->ff_blocks
+= (bytesThisExtent
/ volumeBlockSize
);
1238 FTOC(fcb
)->c_blocks
+= (bytesThisExtent
/ volumeBlockSize
);
1239 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
1241 // If contiguous allocation was requested, then we've already got one contiguous
1242 // chunk. If we didn't get all we wanted, then adjust the error to disk full.
1244 if (bytesToAdd
!= 0)
1246 break; // We've already got everything that's contiguous
1249 } while (err
== noErr
&& bytesToAdd
);
1253 if (VCBTOHFS(vcb
)->hfs_flags
& HFS_METADATA_ZONE
) {
1254 /* Keep the roving allocator out of the metadata zone. */
1255 if (vcb
->nextAllocation
>= VCBTOHFS(vcb
)->hfs_metazone_start
&&
1256 vcb
->nextAllocation
<= VCBTOHFS(vcb
)->hfs_metazone_end
) {
1257 HFS_MOUNT_LOCK(vcb
, TRUE
);
1258 HFS_UPDATE_NEXT_ALLOCATION(vcb
, VCBTOHFS(vcb
)->hfs_metazone_end
+ 1);
1260 HFS_MOUNT_UNLOCK(vcb
, TRUE
);
1263 if (prevblocks
< fcb
->ff_blocks
) {
1264 *actualBytesAdded
= (int64_t)(fcb
->ff_blocks
- prevblocks
) * (int64_t)volumeBlockSize
;
1266 *actualBytesAdded
= 0;
1270 (void) FlushExtentFile(vcb
);
1275 err
= fileBoundsErr
;
1281 //_________________________________________________________________________________
1283 // Routine: TruncateFileC
1285 // Function: Truncates the disk space allocated to a file. The file space is
1286 // truncated to a specified new PEOF rounded up to the next allocation
1287 // block boundry. If the 'TFTrunExt' option is specified, the file is
1288 // truncated to the end of the extent containing the new PEOF.
1290 //_________________________________________________________________________________
1293 OSErr
TruncateFileC (
1294 ExtendedVCB
*vcb
, // volume that file resides on
1295 FCB
*fcb
, // FCB of file to truncate
1296 int64_t peof
, // new physical size for file
1297 Boolean truncateToExtent
) // if true, truncate to end of extent containing newPEOF
1300 u_int32_t nextBlock
; // next file allocation block to consider
1301 u_int32_t startBlock
; // Physical (volume) allocation block number of start of a range
1302 u_int32_t physNumBlocks
; // Number of allocation blocks in file (according to PEOF)
1303 u_int32_t numBlocks
;
1304 HFSPlusExtentKey key
; // key for current extent record; key->keyLength == 0 if FCB's extent record
1305 u_int32_t hint
; // BTree hint corresponding to key
1306 HFSPlusExtentRecord extentRecord
;
1307 u_int32_t extentIndex
;
1308 u_int32_t extentNextBlock
;
1309 u_int32_t numExtentsPerRecord
;
1312 Boolean extentChanged
; // true if we actually changed an extent
1313 Boolean recordDeleted
; // true if an extent record got deleted
1315 recordDeleted
= false;
1317 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
1318 numExtentsPerRecord
= kHFSPlusExtentDensity
;
1320 numExtentsPerRecord
= kHFSExtentDensity
;
1322 if (FORK_IS_RSRC(fcb
))
1323 forkType
= kResourceForkType
;
1325 forkType
= kDataForkType
;
1327 temp64
= fcb
->ff_blocks
;
1328 physNumBlocks
= (u_int32_t
)temp64
;
1331 // Round newPEOF up to a multiple of the allocation block size. If new size is
1332 // two gigabytes or more, then round down by one allocation block (??? really?
1333 // shouldn't that be an error?).
1335 nextBlock
= howmany(peof
, vcb
->blockSize
); // number of allocation blocks to remain in file
1336 peof
= (int64_t)((int64_t)nextBlock
* (int64_t)vcb
->blockSize
); // number of bytes in those blocks
1337 if ((vcb
->vcbSigWord
== kHFSSigWord
) && (peof
>= kTwoGigabytes
)) {
1339 DebugStr("HFS: Trying to truncate a file to 2GB or more");
1341 err
= fileBoundsErr
;
1346 // Update FCB's length
1349 * XXX Any errors could cause ff_blocks and c_blocks to get out of sync...
1351 numBlocks
= peof
/ vcb
->blockSize
;
1352 FTOC(fcb
)->c_blocks
-= (fcb
->ff_blocks
- numBlocks
);
1353 fcb
->ff_blocks
= numBlocks
;
1355 // this catalog entry is modified and *must* get forced
1356 // to disk when hfs_update() is called
1357 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
1360 // If the new PEOF is 0, then truncateToExtent has no meaning (we should always deallocate
1366 // Deallocate all the extents for this fork
1367 err
= DeallocateFork(vcb
, FTOC(fcb
)->c_fileid
, forkType
, fcb
->fcbExtents
, &recordDeleted
);
1368 if (err
!= noErr
) goto ErrorExit
; // got some error, so return it
1370 // Update the catalog extent record (making sure it's zeroed out)
1372 for (i
=0; i
< kHFSPlusExtentDensity
; i
++) {
1373 fcb
->fcbExtents
[i
].startBlock
= 0;
1374 fcb
->fcbExtents
[i
].blockCount
= 0;
1381 // Find the extent containing byte (peof-1). This is the last extent we'll keep.
1382 // (If truncateToExtent is true, we'll keep the whole extent; otherwise, we'll only
1383 // keep up through peof). The search will tell us how many allocation blocks exist
1384 // in the found extent plus all previous extents.
1386 err
= SearchExtentFile(vcb
, fcb
, peof
-1, &key
, extentRecord
, &extentIndex
, &hint
, &extentNextBlock
);
1387 if (err
!= noErr
) goto ErrorExit
;
1389 extentChanged
= false; // haven't changed the extent yet
1391 if (!truncateToExtent
) {
1393 // Shorten this extent. It may be the case that the entire extent gets
1396 numBlocks
= extentNextBlock
- nextBlock
; // How many blocks in this extent to free up
1397 if (numBlocks
!= 0) {
1398 // Compute first volume allocation block to free
1399 startBlock
= extentRecord
[extentIndex
].startBlock
+ extentRecord
[extentIndex
].blockCount
- numBlocks
;
1400 // Free the blocks in bitmap
1401 err
= BlockDeallocate(vcb
, startBlock
, numBlocks
, 0);
1402 if (err
!= noErr
) goto ErrorExit
;
1403 // Adjust length of this extent
1404 extentRecord
[extentIndex
].blockCount
-= numBlocks
;
1405 // If extent is empty, set start block to 0
1406 if (extentRecord
[extentIndex
].blockCount
== 0)
1407 extentRecord
[extentIndex
].startBlock
= 0;
1408 // Remember that we changed the extent record
1409 extentChanged
= true;
1414 // Now move to the next extent in the record, and set up the file allocation block number
1416 nextBlock
= extentNextBlock
; // Next file allocation block to free
1417 ++extentIndex
; // Its index within the extent record
1420 // Release all following extents in this extent record. Update the record.
1422 while (extentIndex
< numExtentsPerRecord
&& extentRecord
[extentIndex
].blockCount
!= 0) {
1423 numBlocks
= extentRecord
[extentIndex
].blockCount
;
1424 // Deallocate this extent
1425 err
= BlockDeallocate(vcb
, extentRecord
[extentIndex
].startBlock
, numBlocks
, 0);
1426 if (err
!= noErr
) goto ErrorExit
;
1427 // Update next file allocation block number
1428 nextBlock
+= numBlocks
;
1429 // Zero out start and length of this extent to delete it from record
1430 extentRecord
[extentIndex
].startBlock
= 0;
1431 extentRecord
[extentIndex
].blockCount
= 0;
1432 // Remember that we changed an extent
1433 extentChanged
= true;
1434 // Move to next extent in record
1439 // If any of the extents in the current record were changed, then update that
1440 // record (in the FCB, or extents file).
1442 if (extentChanged
) {
1443 err
= UpdateExtentRecord(vcb
, fcb
, &key
, extentRecord
, hint
);
1444 if (err
!= noErr
) goto ErrorExit
;
1448 // If there are any following allocation blocks, then we need
1449 // to seach for their extent records and delete those allocation
1452 if (nextBlock
< physNumBlocks
)
1453 err
= TruncateExtents(vcb
, forkType
, FTOC(fcb
)->c_fileid
, nextBlock
, &recordDeleted
);
1458 (void) FlushExtentFile(vcb
);
1469 OSErr
HeadTruncateFile (
1474 HFSPlusExtentRecord extents
;
1475 HFSPlusExtentRecord tailExtents
;
1476 HFSCatalogNodeID fileID
;
1480 u_int32_t blksfreed
;
1486 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1489 forkType
= FORK_IS_RSRC(fcb
) ? kResourceForkType
: kDataForkType
;
1490 fileID
= FTOC(fcb
)->c_fileid
;
1491 bzero(tailExtents
, sizeof(tailExtents
));
1497 * Process catalog resident extents
1499 for (i
= 0, j
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1500 blkcnt
= fcb
->fcbExtents
[i
].blockCount
;
1502 break; /* end of extents */
1504 if (blksfreed
< headblks
) {
1505 error
= BlockDeallocate(vcb
, fcb
->fcbExtents
[i
].startBlock
, blkcnt
, 0);
1507 * Any errors after the first BlockDeallocate
1508 * must be ignored so we can put the file in
1513 goto ErrorExit
; /* uh oh */
1516 printf("hfs: HeadTruncateFile: problems deallocating %s (%d)\n",
1517 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1521 blksfreed
+= blkcnt
;
1522 fcb
->fcbExtents
[i
].startBlock
= 0;
1523 fcb
->fcbExtents
[i
].blockCount
= 0;
1525 tailExtents
[j
].startBlock
= fcb
->fcbExtents
[i
].startBlock
;
1526 tailExtents
[j
].blockCount
= blkcnt
;
1535 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1538 * Process overflow extents
1543 error
= FindExtentRecord(vcb
, forkType
, fileID
, startblk
, false, NULL
, extents
, NULL
);
1546 * Any errors after the first BlockDeallocate
1547 * must be ignored so we can put the file in
1550 if (error
!= btNotFound
)
1551 printf("hfs: HeadTruncateFile: problems finding extents %s (%d)\n",
1552 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1557 for(i
= 0, extblks
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1558 blkcnt
= extents
[i
].blockCount
;
1560 break; /* end of extents */
1562 if (blksfreed
< headblks
) {
1563 error
= BlockDeallocate(vcb
, extents
[i
].startBlock
, blkcnt
, 0);
1565 printf("hfs: HeadTruncateFile: problems deallocating %s (%d)\n",
1566 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1569 blksfreed
+= blkcnt
;
1571 tailExtents
[j
].startBlock
= extents
[i
].startBlock
;
1572 tailExtents
[j
].blockCount
= blkcnt
;
1578 error
= DeleteExtentRecord(vcb
, forkType
, fileID
, startblk
);
1580 printf("hfs: HeadTruncateFile: problems deallocating %s (%d)\n",
1581 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1586 break; /* all done */
1588 startblk
+= extblks
;
1590 hfs_systemfile_unlock(vcb
, lockflags
);
1594 bcopy(tailExtents
, fcb
->fcbExtents
, sizeof(tailExtents
));
1595 blkcnt
= fcb
->ff_blocks
- headblks
;
1596 FTOC(fcb
)->c_blocks
-= headblks
;
1597 fcb
->ff_blocks
= blkcnt
;
1599 FTOC(fcb
)->c_flag
|= C_FORCEUPDATE
;
1600 FTOC(fcb
)->c_touch_chgtime
= TRUE
;
1602 (void) FlushExtentFile(vcb
);
1606 return MacToVFSError(error
);
1611 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1612 // Routine: SearchExtentRecord (was XRSearch)
1614 // Function: Searches extent record for the extent mapping a given file
1615 // allocation block number (FABN).
1617 // Input: searchFABN - desired FABN
1618 // extentData - pointer to extent data record (xdr)
1619 // extentDataStartFABN - beginning FABN for extent record
1621 // Output: foundExtentDataOffset - offset to extent entry within xdr
1622 // result = noErr, offset to extent mapping desired FABN
1623 // result = FXRangeErr, offset to last extent in record
1624 // endingFABNPlusOne - ending FABN +1
1625 // noMoreExtents - True if the extent was not found, and the
1626 // extent record was not full (so don't bother
1627 // looking in subsequent records); false otherwise.
1629 // Result: noErr = ok
1630 // FXRangeErr = desired FABN > last mapped FABN in record
1631 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1633 static OSErr
SearchExtentRecord(
1635 u_int32_t searchFABN
,
1636 const HFSPlusExtentRecord extentData
,
1637 u_int32_t extentDataStartFABN
,
1638 u_int32_t
*foundExtentIndex
,
1639 u_int32_t
*endingFABNPlusOne
,
1640 Boolean
*noMoreExtents
)
1643 u_int32_t extentIndex
;
1644 u_int32_t numberOfExtents
;
1645 u_int32_t numAllocationBlocks
;
1646 Boolean foundExtent
;
1648 *endingFABNPlusOne
= extentDataStartFABN
;
1649 *noMoreExtents
= false;
1650 foundExtent
= false;
1652 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
1653 numberOfExtents
= kHFSPlusExtentDensity
;
1655 numberOfExtents
= kHFSExtentDensity
;
1657 for( extentIndex
= 0; extentIndex
< numberOfExtents
; ++extentIndex
)
1660 // Loop over the extent record and find the search FABN.
1662 numAllocationBlocks
= extentData
[extentIndex
].blockCount
;
1663 if ( numAllocationBlocks
== 0 )
1668 *endingFABNPlusOne
+= numAllocationBlocks
;
1670 if( searchFABN
< *endingFABNPlusOne
)
1672 // Found the extent.
1680 // Found the extent. Note the extent offset
1681 *foundExtentIndex
= extentIndex
;
1685 // Did not find the extent. Set foundExtentDataOffset accordingly
1686 if( extentIndex
> 0 )
1688 *foundExtentIndex
= extentIndex
- 1;
1692 *foundExtentIndex
= 0;
1695 // If we found an empty extent, then set noMoreExtents.
1696 if (extentIndex
< numberOfExtents
)
1697 *noMoreExtents
= true;
1699 // Finally, return an error to the caller
1706 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1707 // Routine: SearchExtentFile (was XFSearch)
1709 // Function: Searches extent file (including the FCB resident extent record)
1710 // for the extent mapping a given file position.
1712 // Input: vcb - VCB pointer
1713 // fcb - FCB pointer
1714 // filePosition - file position (byte address)
1716 // Output: foundExtentKey - extent key record (xkr)
1717 // If extent was found in the FCB's resident extent record,
1718 // then foundExtentKey->keyLength will be set to 0.
1719 // foundExtentData - extent data record(xdr)
1720 // foundExtentIndex - index to extent entry in xdr
1721 // result = 0, offset to extent mapping desired FABN
1722 // result = FXRangeErr, offset to last extent in record
1723 // (i.e., kNumExtentsPerRecord-1)
1724 // extentBTreeHint - BTree hint for extent record
1725 // kNoHint = Resident extent record
1726 // endingFABNPlusOne - ending FABN +1
1729 // noErr Found an extent that contains the given file position
1730 // FXRangeErr Given position is beyond the last allocated extent
1731 // (other) (some other internal I/O error)
1732 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1734 static OSErr
SearchExtentFile(
1737 int64_t filePosition
,
1738 HFSPlusExtentKey
*foundExtentKey
,
1739 HFSPlusExtentRecord foundExtentData
,
1740 u_int32_t
*foundExtentIndex
,
1741 u_int32_t
*extentBTreeHint
,
1742 u_int32_t
*endingFABNPlusOne
)
1745 u_int32_t filePositionBlock
;
1747 Boolean noMoreExtents
;
1750 temp64
= filePosition
/ (int64_t)vcb
->blockSize
;
1751 filePositionBlock
= (u_int32_t
)temp64
;
1753 bcopy ( fcb
->fcbExtents
, foundExtentData
, sizeof(HFSPlusExtentRecord
));
1755 // Search the resident FCB first.
1756 err
= SearchExtentRecord( vcb
, filePositionBlock
, foundExtentData
, 0,
1757 foundExtentIndex
, endingFABNPlusOne
, &noMoreExtents
);
1759 if( err
== noErr
) {
1760 // Found the extent. Set results accordingly
1761 *extentBTreeHint
= kNoHint
; // no hint, because not in the BTree
1762 foundExtentKey
->keyLength
= 0; // 0 = the FCB itself
1767 // Didn't find extent in FCB. If FCB's extent record wasn't full, there's no point
1768 // in searching the extents file. Note that SearchExtentRecord left us pointing at
1769 // the last valid extent (or the first one, if none were valid). This means we need
1770 // to fill in the hint and key outputs, just like the "if" statement above.
1771 if ( noMoreExtents
) {
1772 *extentBTreeHint
= kNoHint
; // no hint, because not in the BTree
1773 foundExtentKey
->keyLength
= 0; // 0 = the FCB itself
1774 err
= fxRangeErr
; // There are no more extents, so must be beyond PEOF
1779 // Find the desired record, or the previous record if it is the same fork
1781 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1783 err
= FindExtentRecord(vcb
, FORK_IS_RSRC(fcb
) ? kResourceForkType
: kDataForkType
,
1784 FTOC(fcb
)->c_fileid
, filePositionBlock
, true, foundExtentKey
, foundExtentData
, extentBTreeHint
);
1785 hfs_systemfile_unlock(vcb
, lockflags
);
1787 if (err
== btNotFound
) {
1789 // If we get here, the desired position is beyond the extents in the FCB, and there are no extents
1790 // in the extents file. Return the FCB's extents and a range error.
1792 *extentBTreeHint
= kNoHint
;
1793 foundExtentKey
->keyLength
= 0;
1794 err
= GetFCBExtentRecord(fcb
, foundExtentData
);
1795 // Note: foundExtentIndex and endingFABNPlusOne have already been set as a result of the very
1796 // first SearchExtentRecord call in this function (when searching in the FCB's extents, and
1797 // we got a range error).
1803 // If we get here, there was either a BTree error, or we found an appropriate record.
1804 // If we found a record, then search it for the correct index into the extents.
1807 // Find appropriate index into extent record
1808 err
= SearchExtentRecord(vcb
, filePositionBlock
, foundExtentData
, foundExtentKey
->startBlock
,
1809 foundExtentIndex
, endingFABNPlusOne
, &noMoreExtents
);
1818 //============================================================================
1819 // Routine: UpdateExtentRecord
1821 // Function: Write new extent data to an existing extent record with a given key.
1822 // If all of the extents are empty, and the extent record is in the
1823 // extents file, then the record is deleted.
1825 // Input: vcb - the volume containing the extents
1826 // fcb - the file that owns the extents
1827 // extentFileKey - pointer to extent key record (xkr)
1828 // If the key length is 0, then the extents are actually part
1829 // of the catalog record, stored in the FCB.
1830 // extentData - pointer to extent data record (xdr)
1831 // extentBTreeHint - hint for given key, or kNoHint
1833 // Result: noErr = ok
1834 // (other) = error from BTree
1835 //============================================================================
1837 static OSErr
UpdateExtentRecord (
1840 const HFSPlusExtentKey
*extentFileKey
,
1841 const HFSPlusExtentRecord extentData
,
1842 u_int32_t extentBTreeHint
)
1846 if (extentFileKey
->keyLength
== 0) { // keyLength == 0 means the FCB's extent record
1847 BlockMoveData(extentData
, fcb
->fcbExtents
, sizeof(HFSPlusExtentRecord
));
1848 FTOC(fcb
)->c_flag
|= C_MODIFIED
;
1851 BTreeIterator btIterator
;
1852 FSBufferDescriptor btRecord
;
1853 u_int16_t btRecordSize
;
1858 // Need to find and change a record in Extents BTree
1860 btFCB
= GetFileControlBlock(vcb
->extentsRefNum
);
1862 bzero(&btIterator
, sizeof(btIterator
));
1865 * The lock taken by callers of ExtendFileC/TruncateFileC is
1866 * speculative and only occurs when the file already has
1867 * overflow extents. So we need to make sure we have the lock
1868 * here. The extents btree lock can be nested (its recursive)
1869 * so we always take it here.
1871 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1873 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1874 HFSExtentKey
* key
; // Actual extent key used on disk in HFS
1875 HFSExtentRecord foundData
; // The extent data actually found
1877 key
= (HFSExtentKey
*) &btIterator
.key
;
1878 key
->keyLength
= kHFSExtentKeyMaximumLength
;
1879 key
->forkType
= extentFileKey
->forkType
;
1880 key
->fileID
= extentFileKey
->fileID
;
1881 key
->startBlock
= extentFileKey
->startBlock
;
1883 btIterator
.hint
.index
= 0;
1884 btIterator
.hint
.nodeNum
= extentBTreeHint
;
1886 btRecord
.bufferAddress
= &foundData
;
1887 btRecord
.itemSize
= sizeof(HFSExtentRecord
);
1888 btRecord
.itemCount
= 1;
1890 err
= BTSearchRecord(btFCB
, &btIterator
, &btRecord
, &btRecordSize
, &btIterator
);
1893 err
= HFSPlusToHFSExtents(extentData
, (HFSExtentDescriptor
*)&foundData
);
1896 err
= BTReplaceRecord(btFCB
, &btIterator
, &btRecord
, btRecordSize
);
1897 (void) BTFlushPath(btFCB
);
1899 else { // HFS Plus volume
1900 HFSPlusExtentRecord foundData
; // The extent data actually found
1902 BlockMoveData(extentFileKey
, &btIterator
.key
, sizeof(HFSPlusExtentKey
));
1904 btIterator
.hint
.index
= 0;
1905 btIterator
.hint
.nodeNum
= extentBTreeHint
;
1907 btRecord
.bufferAddress
= &foundData
;
1908 btRecord
.itemSize
= sizeof(HFSPlusExtentRecord
);
1909 btRecord
.itemCount
= 1;
1911 err
= BTSearchRecord(btFCB
, &btIterator
, &btRecord
, &btRecordSize
, &btIterator
);
1914 BlockMoveData(extentData
, &foundData
, sizeof(HFSPlusExtentRecord
));
1915 err
= BTReplaceRecord(btFCB
, &btIterator
, &btRecord
, btRecordSize
);
1917 (void) BTFlushPath(btFCB
);
1919 hfs_systemfile_unlock(vcb
, lockflags
);
1928 static OSErr
HFSPlusToHFSExtents(
1929 const HFSPlusExtentRecord oldExtents
,
1930 HFSExtentRecord newExtents
)
1936 // copy the first 3 extents
1937 newExtents
[0].startBlock
= oldExtents
[0].startBlock
;
1938 newExtents
[0].blockCount
= oldExtents
[0].blockCount
;
1939 newExtents
[1].startBlock
= oldExtents
[1].startBlock
;
1940 newExtents
[1].blockCount
= oldExtents
[1].blockCount
;
1941 newExtents
[2].startBlock
= oldExtents
[2].startBlock
;
1942 newExtents
[2].blockCount
= oldExtents
[2].blockCount
;
1945 if (oldExtents
[3].startBlock
|| oldExtents
[3].blockCount
) {
1946 DebugStr("ExtentRecord with > 3 extents is invalid for HFS");
1957 static OSErr
GetFCBExtentRecord(
1959 HFSPlusExtentRecord extents
)
1962 BlockMoveData(fcb
->fcbExtents
, extents
, sizeof(HFSPlusExtentRecord
));
1968 //_________________________________________________________________________________
1970 // Routine: ExtentsAreIntegral
1972 // Purpose: Ensure that each extent can hold an integral number of nodes
1973 // Called by the NodesAreContiguous function
1974 //_________________________________________________________________________________
1976 static Boolean
ExtentsAreIntegral(
1977 const HFSPlusExtentRecord extentRecord
,
1979 u_int32_t
*blocksChecked
,
1980 Boolean
*checkedLastExtent
)
1983 u_int32_t extentIndex
;
1986 *checkedLastExtent
= false;
1988 for(extentIndex
= 0; extentIndex
< kHFSPlusExtentDensity
; extentIndex
++)
1990 blocks
= extentRecord
[extentIndex
].blockCount
;
1994 *checkedLastExtent
= true;
1998 *blocksChecked
+= blocks
;
2008 //_________________________________________________________________________________
2010 // Routine: NodesAreContiguous
2012 // Purpose: Ensure that all b-tree nodes are contiguous on disk
2013 // Called by BTOpenPath during volume mount
2014 //_________________________________________________________________________________
2017 Boolean
NodesAreContiguous(
2023 u_int32_t startBlock
;
2024 u_int32_t blocksChecked
;
2026 HFSPlusExtentKey key
;
2027 HFSPlusExtentRecord extents
;
2029 Boolean lastExtentReached
;
2033 if (vcb
->blockSize
>= nodeSize
)
2036 mask
= (nodeSize
/ vcb
->blockSize
) - 1;
2038 // check the local extents
2039 (void) GetFCBExtentRecord(fcb
, extents
);
2040 if ( !ExtentsAreIntegral(extents
, mask
, &blocksChecked
, &lastExtentReached
) )
2043 if ( lastExtentReached
||
2044 (int64_t)((int64_t)blocksChecked
* (int64_t)vcb
->blockSize
) >= (int64_t)fcb
->ff_size
)
2047 startBlock
= blocksChecked
;
2049 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
2051 // check the overflow extents (if any)
2052 while ( !lastExtentReached
)
2054 result
= FindExtentRecord(vcb
, kDataForkType
, fcb
->ff_cp
->c_fileid
, startBlock
, FALSE
, &key
, extents
, &hint
);
2057 if ( !ExtentsAreIntegral(extents
, mask
, &blocksChecked
, &lastExtentReached
) ) {
2058 hfs_systemfile_unlock(vcb
, lockflags
);
2061 startBlock
+= blocksChecked
;
2063 hfs_systemfile_unlock(vcb
, lockflags
);