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 (
185 const HFSPlusExtentKey
*extentFileKey
,
186 const HFSPlusExtentRecord extentData
,
187 u_int32_t extentBTreeHint
);
189 static Boolean
ExtentsAreIntegral(
190 const HFSPlusExtentRecord extentRecord
,
192 u_int32_t
*blocksChecked
,
193 Boolean
*checkedLastExtent
);
195 //_________________________________________________________________________________
197 // Routine: FindExtentRecord
199 // Purpose: Search the extents BTree for an extent record matching the given
200 // FileID, fork, and starting file allocation block number.
203 // vcb Volume to search
204 // forkType 0 = data fork, -1 = resource fork
205 // fileID File's FileID (CatalogNodeID)
206 // startBlock Starting file allocation block number
207 // allowPrevious If the desired record isn't found and this flag is set,
208 // then see if the previous record belongs to the same fork.
209 // If so, then return it.
212 // foundKey The key data for the record actually found
213 // foundData The extent record actually found (NOTE: on an HFS volume, the
214 // fourth entry will be zeroes.
215 // foundHint The BTree hint to find the node again
216 //_________________________________________________________________________________
217 static OSErr
FindExtentRecord(
218 const ExtendedVCB
*vcb
,
221 u_int32_t startBlock
,
222 Boolean allowPrevious
,
223 HFSPlusExtentKey
*foundKey
,
224 HFSPlusExtentRecord foundData
,
225 u_int32_t
*foundHint
)
228 BTreeIterator btIterator
;
229 FSBufferDescriptor btRecord
;
231 u_int16_t btRecordSize
;
236 fcb
= GetFileControlBlock(vcb
->extentsRefNum
);
238 bzero(&btIterator
, sizeof(btIterator
));
240 if (vcb
->vcbSigWord
== kHFSSigWord
) {
241 HFSExtentKey
* extentKeyPtr
;
242 HFSExtentRecord extentData
;
244 extentKeyPtr
= (HFSExtentKey
*) &btIterator
.key
;
245 extentKeyPtr
->keyLength
= kHFSExtentKeyMaximumLength
;
246 extentKeyPtr
->forkType
= forkType
;
247 extentKeyPtr
->fileID
= fileID
;
248 extentKeyPtr
->startBlock
= startBlock
;
250 btRecord
.bufferAddress
= &extentData
;
251 btRecord
.itemSize
= sizeof(HFSExtentRecord
);
252 btRecord
.itemCount
= 1;
254 err
= BTSearchRecord(fcb
, &btIterator
, &btRecord
, &btRecordSize
, &btIterator
);
256 if (err
== btNotFound
&& allowPrevious
) {
257 err
= BTIterateRecord(fcb
, kBTreePrevRecord
, &btIterator
, &btRecord
, &btRecordSize
);
259 // A previous record may not exist, so just return btNotFound (like we would if
260 // it was for the wrong file/fork).
261 if (err
== (OSErr
) fsBTStartOfIterationErr
) //¥¥ fsBTStartOfIterationErr is type unsigned long
265 // Found a previous record. Does it belong to the same fork of the same file?
266 if (extentKeyPtr
->fileID
!= fileID
|| extentKeyPtr
->forkType
!= forkType
)
274 // Copy the found key back for the caller
276 foundKey
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
277 foundKey
->forkType
= extentKeyPtr
->forkType
;
279 foundKey
->fileID
= extentKeyPtr
->fileID
;
280 foundKey
->startBlock
= extentKeyPtr
->startBlock
;
282 // Copy the found data back for the caller
283 foundData
[0].startBlock
= extentData
[0].startBlock
;
284 foundData
[0].blockCount
= extentData
[0].blockCount
;
285 foundData
[1].startBlock
= extentData
[1].startBlock
;
286 foundData
[1].blockCount
= extentData
[1].blockCount
;
287 foundData
[2].startBlock
= extentData
[2].startBlock
;
288 foundData
[2].blockCount
= extentData
[2].blockCount
;
290 for (i
= 3; i
< kHFSPlusExtentDensity
; ++i
)
292 foundData
[i
].startBlock
= 0;
293 foundData
[i
].blockCount
= 0;
297 else { // HFS Plus volume
298 HFSPlusExtentKey
* extentKeyPtr
;
299 HFSPlusExtentRecord extentData
;
301 extentKeyPtr
= (HFSPlusExtentKey
*) &btIterator
.key
;
302 extentKeyPtr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
303 extentKeyPtr
->forkType
= forkType
;
304 extentKeyPtr
->pad
= 0;
305 extentKeyPtr
->fileID
= fileID
;
306 extentKeyPtr
->startBlock
= startBlock
;
308 btRecord
.bufferAddress
= &extentData
;
309 btRecord
.itemSize
= sizeof(HFSPlusExtentRecord
);
310 btRecord
.itemCount
= 1;
312 err
= BTSearchRecord(fcb
, &btIterator
, &btRecord
, &btRecordSize
, &btIterator
);
314 if (err
== btNotFound
&& allowPrevious
) {
315 err
= BTIterateRecord(fcb
, kBTreePrevRecord
, &btIterator
, &btRecord
, &btRecordSize
);
317 // A previous record may not exist, so just return btNotFound (like we would if
318 // it was for the wrong file/fork).
319 if (err
== (OSErr
) fsBTStartOfIterationErr
) //¥¥ fsBTStartOfIterationErr is type unsigned long
323 // Found a previous record. Does it belong to the same fork of the same file?
324 if (extentKeyPtr
->fileID
!= fileID
|| extentKeyPtr
->forkType
!= forkType
)
330 // Copy the found key back for the caller
332 BlockMoveData(extentKeyPtr
, foundKey
, sizeof(HFSPlusExtentKey
));
333 // Copy the found data back for the caller
334 BlockMoveData(&extentData
, foundData
, sizeof(HFSPlusExtentRecord
));
339 *foundHint
= btIterator
.hint
.nodeNum
;
345 static OSErr
CreateExtentRecord(
347 HFSPlusExtentKey
*key
,
348 HFSPlusExtentRecord extents
,
351 BTreeIterator btIterator
;
352 FSBufferDescriptor btRecord
;
353 u_int16_t btRecordSize
;
360 bzero(&btIterator
, sizeof(btIterator
));
363 * The lock taken by callers of ExtendFileC is speculative and
364 * only occurs when the file already has overflow extents. So
365 * We need to make sure we have the lock here. The extents
366 * btree lock can be nested (its recursive) so we always take
369 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
371 if (vcb
->vcbSigWord
== kHFSSigWord
) {
372 HFSExtentKey
* keyPtr
;
373 HFSExtentRecord data
;
375 btRecordSize
= sizeof(HFSExtentRecord
);
376 btRecord
.bufferAddress
= &data
;
377 btRecord
.itemSize
= btRecordSize
;
378 btRecord
.itemCount
= 1;
380 keyPtr
= (HFSExtentKey
*) &btIterator
.key
;
381 keyPtr
->keyLength
= kHFSExtentKeyMaximumLength
;
382 keyPtr
->forkType
= key
->forkType
;
383 keyPtr
->fileID
= key
->fileID
;
384 keyPtr
->startBlock
= key
->startBlock
;
386 err
= HFSPlusToHFSExtents(extents
, data
);
388 else { // HFS Plus volume
389 btRecordSize
= sizeof(HFSPlusExtentRecord
);
390 btRecord
.bufferAddress
= extents
;
391 btRecord
.itemSize
= btRecordSize
;
392 btRecord
.itemCount
= 1;
394 BlockMoveData(key
, &btIterator
.key
, sizeof(HFSPlusExtentKey
));
398 err
= BTInsertRecord(GetFileControlBlock(vcb
->extentsRefNum
), &btIterator
, &btRecord
, btRecordSize
);
401 *hint
= btIterator
.hint
.nodeNum
;
403 (void) BTFlushPath(GetFileControlBlock(vcb
->extentsRefNum
));
405 hfs_systemfile_unlock(vcb
, lockflags
);
411 static OSErr
DeleteExtentRecord(
412 const ExtendedVCB
*vcb
,
415 u_int32_t startBlock
)
417 BTreeIterator btIterator
;
422 bzero(&btIterator
, sizeof(btIterator
));
424 if (vcb
->vcbSigWord
== kHFSSigWord
) {
425 HFSExtentKey
* keyPtr
;
427 keyPtr
= (HFSExtentKey
*) &btIterator
.key
;
428 keyPtr
->keyLength
= kHFSExtentKeyMaximumLength
;
429 keyPtr
->forkType
= forkType
;
430 keyPtr
->fileID
= fileID
;
431 keyPtr
->startBlock
= startBlock
;
433 else { // HFS Plus volume
434 HFSPlusExtentKey
* keyPtr
;
436 keyPtr
= (HFSPlusExtentKey
*) &btIterator
.key
;
437 keyPtr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
438 keyPtr
->forkType
= forkType
;
440 keyPtr
->fileID
= fileID
;
441 keyPtr
->startBlock
= startBlock
;
444 err
= BTDeleteRecord(GetFileControlBlock(vcb
->extentsRefNum
), &btIterator
);
445 (void) BTFlushPath(GetFileControlBlock(vcb
->extentsRefNum
));
452 //_________________________________________________________________________________
454 // Routine: MapFileBlock
456 // Function: Maps a file position into a physical disk address.
458 //_________________________________________________________________________________
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.
502 dataEnd
= (off_t
)((off_t
)(nextFABN
) * (off_t
)(allocBlockSize
)); // Assume valid data through end of this extent
503 if (((off_t
)fcb
->ff_blocks
* (off_t
)allocBlockSize
) < dataEnd
) // Is PEOF shorter?
504 dataEnd
= (off_t
)fcb
->ff_blocks
* (off_t
)allocBlockSize
; // Yes, so only map up to PEOF
506 // Compute the number of sectors in an allocation block
507 sectorsPerBlock
= allocBlockSize
/ sectorSize
; // sectors per allocation block
510 // Compute the absolute sector number that contains the offset of the given file
511 // offset in sectors from start of the extent +
512 // offset in sectors from start of allocation block space
514 temp
= (daddr64_t
)((offset
- (off_t
)((off_t
)(firstFABN
) * (off_t
)(allocBlockSize
)))/sectorSize
);
515 temp
+= (daddr64_t
)startBlock
* (daddr64_t
)sectorsPerBlock
;
517 /* Add in any volume offsets */
518 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
519 temp
+= vcb
->hfsPlusIOPosOffset
/ sectorSize
;
521 temp
+= vcb
->vcbAlBlSt
;
523 // Return the desired sector for file position "offset"
527 // Determine the number of contiguous bytes until the end of the extent
528 // (or the amount they asked for, whichever comes first).
532 tmpOff
= dataEnd
- offset
;
534 * Disallow negative runs.
539 if (tmpOff
> (off_t
)(numberOfBytes
))
540 *availableBytes
= numberOfBytes
; // more there than they asked for, so pin the output
542 *availableBytes
= tmpOff
;
549 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
550 // Routine: ReleaseExtents
552 // Function: Release the extents of a single extent data record.
553 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
555 static OSErr
ReleaseExtents(
557 const HFSPlusExtentRecord extentRecord
,
558 u_int32_t
*numReleasedAllocationBlocks
,
559 Boolean
*releasedLastExtent
)
561 u_int32_t extentIndex
;
562 u_int32_t numberOfExtents
;
565 *numReleasedAllocationBlocks
= 0;
566 *releasedLastExtent
= false;
568 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
569 numberOfExtents
= kHFSPlusExtentDensity
;
571 numberOfExtents
= kHFSExtentDensity
;
573 for( extentIndex
= 0; extentIndex
< numberOfExtents
; extentIndex
++)
575 u_int32_t numAllocationBlocks
;
577 // Loop over the extent record and release the blocks associated with each extent.
579 numAllocationBlocks
= extentRecord
[extentIndex
].blockCount
;
580 if ( numAllocationBlocks
== 0 )
582 *releasedLastExtent
= true;
586 err
= BlockDeallocate( vcb
, extentRecord
[extentIndex
].startBlock
, numAllocationBlocks
, 0);
590 *numReleasedAllocationBlocks
+= numAllocationBlocks
; // bump FABN to beg of next extent
598 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
599 // Routine: TruncateExtents
601 // Purpose: Delete extent records whose starting file allocation block number
602 // is greater than or equal to a given starting block number. The
603 // allocation blocks represented by the extents are deallocated.
606 // vcb Volume to operate on
607 // fileID Which file to operate on
608 // startBlock Starting file allocation block number for first extent
610 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
612 static OSErr
TruncateExtents(
616 u_int32_t startBlock
,
617 Boolean
* recordDeleted
)
620 u_int32_t numberExtentsReleased
;
621 Boolean releasedLastExtent
;
623 HFSPlusExtentKey key
;
624 HFSPlusExtentRecord extents
;
628 * The lock taken by callers of TruncateFileC is speculative and
629 * only occurs when the file already has overflow extents. So
630 * We need to make sure we have the lock here. The extents
631 * btree lock can be nested (its recursive) so we always take
634 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
637 err
= FindExtentRecord(vcb
, forkType
, fileID
, startBlock
, false, &key
, extents
, &hint
);
639 if (err
== btNotFound
)
644 err
= ReleaseExtents( vcb
, extents
, &numberExtentsReleased
, &releasedLastExtent
);
645 if (err
!= noErr
) break;
647 err
= DeleteExtentRecord(vcb
, forkType
, fileID
, startBlock
);
648 if (err
!= noErr
) break;
650 *recordDeleted
= true;
651 startBlock
+= numberExtentsReleased
;
653 hfs_systemfile_unlock(vcb
, lockflags
);
660 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
661 // Routine: DeallocateFork
663 // Function: De-allocates all disk space allocated to a specified fork.
664 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
666 static OSErr
DeallocateFork(
668 HFSCatalogNodeID fileID
,
670 HFSPlusExtentRecord catalogExtents
,
671 Boolean
* recordDeleted
) /* true if a record was deleted */
674 u_int32_t numReleasedAllocationBlocks
;
675 Boolean releasedLastExtent
;
677 // Release the catalog extents
678 err
= ReleaseExtents( vcb
, catalogExtents
, &numReleasedAllocationBlocks
, &releasedLastExtent
);
679 // Release the extra extents, if present
680 if (err
== noErr
&& !releasedLastExtent
)
681 err
= TruncateExtents(vcb
, forkType
, fileID
, numReleasedAllocationBlocks
, recordDeleted
);
686 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
687 // Routine: FlushExtentFile
689 // Function: Flushes the extent file for a specified volume
690 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
692 OSErr
FlushExtentFile( ExtendedVCB
*vcb
)
698 fcb
= GetFileControlBlock(vcb
->extentsRefNum
);
700 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
701 err
= BTFlushPath(fcb
);
702 hfs_systemfile_unlock(vcb
, lockflags
);
706 // If the FCB for the extent "file" is dirty, mark the VCB as dirty.
708 if (FTOC(fcb
)->c_flag
& C_MODIFIED
)
711 // err = FlushVolumeControlBlock( vcb );
719 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
720 // Routine: CompareExtentKeys
722 // Function: Compares two extent file keys (a search key and a trial key) for
724 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
727 int32_t CompareExtentKeys( const HFSExtentKey
*searchKey
, const HFSExtentKey
*trialKey
)
729 int32_t result
; // ± 1
732 if (searchKey
->keyLength
!= kHFSExtentKeyMaximumLength
)
733 DebugStr("HFS: search Key is wrong length");
734 if (trialKey
->keyLength
!= kHFSExtentKeyMaximumLength
)
735 DebugStr("HFS: trial Key is wrong length");
738 result
= -1; // assume searchKey < trialKey
740 if (searchKey
->fileID
== trialKey
->fileID
) {
742 // FileNum's are equal; compare fork types
744 if (searchKey
->forkType
== trialKey
->forkType
) {
746 // Fork types are equal; compare allocation block number
748 if (searchKey
->startBlock
== trialKey
->startBlock
) {
750 // Everything is equal
756 // Allocation block numbers differ; determine sign
758 if (searchKey
->startBlock
> trialKey
->startBlock
)
764 // Fork types differ; determine sign
766 if (searchKey
->forkType
> trialKey
->forkType
)
772 // FileNums differ; determine sign
774 if (searchKey
->fileID
> trialKey
->fileID
)
783 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
784 // Routine: CompareExtentKeysPlus
786 // Function: Compares two extent file keys (a search key and a trial key) for
788 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
791 int32_t CompareExtentKeysPlus( const HFSPlusExtentKey
*searchKey
, const HFSPlusExtentKey
*trialKey
)
793 int32_t result
; // ± 1
796 if (searchKey
->keyLength
!= kHFSPlusExtentKeyMaximumLength
)
797 DebugStr("HFS: search Key is wrong length");
798 if (trialKey
->keyLength
!= kHFSPlusExtentKeyMaximumLength
)
799 DebugStr("HFS: trial Key is wrong length");
802 result
= -1; // assume searchKey < trialKey
804 if (searchKey
->fileID
== trialKey
->fileID
) {
806 // FileNum's are equal; compare fork types
808 if (searchKey
->forkType
== trialKey
->forkType
) {
810 // Fork types are equal; compare allocation block number
812 if (searchKey
->startBlock
== trialKey
->startBlock
) {
814 // Everything is equal
820 // Allocation block numbers differ; determine sign
822 if (searchKey
->startBlock
> trialKey
->startBlock
)
828 // Fork types differ; determine sign
830 if (searchKey
->forkType
> trialKey
->forkType
)
836 // FileNums differ; determine sign
838 if (searchKey
->fileID
> trialKey
->fileID
)
846 * Add a file extent to a file.
848 * Used by hfs_extendfs to extend the volume allocation bitmap file.
852 AddFileExtent(ExtendedVCB
*vcb
, FCB
*fcb
, u_int32_t startBlock
, u_int32_t blockCount
)
854 HFSPlusExtentKey foundKey
;
855 HFSPlusExtentRecord foundData
;
856 u_int32_t foundIndex
;
863 peof
= (int64_t)(fcb
->ff_blocks
+ blockCount
) * (int64_t)vcb
->blockSize
;
865 error
= SearchExtentFile(vcb
, fcb
, peof
-1, &foundKey
, foundData
, &foundIndex
, &hint
, &nextBlock
);
866 if (error
!= fxRangeErr
)
870 * Add new extent. See if there is room in the current record.
872 if (foundData
[foundIndex
].blockCount
!= 0)
874 if (foundIndex
== kHFSPlusExtentDensity
) {
876 * Existing record is full so create a new one.
878 foundKey
.keyLength
= kHFSPlusExtentKeyMaximumLength
;
879 foundKey
.forkType
= kDataForkType
;
881 foundKey
.fileID
= FTOC(fcb
)->c_fileid
;
882 foundKey
.startBlock
= nextBlock
;
884 foundData
[0].startBlock
= startBlock
;
885 foundData
[0].blockCount
= blockCount
;
887 /* zero out remaining extents. */
888 for (i
= 1; i
< kHFSPlusExtentDensity
; ++i
) {
889 foundData
[i
].startBlock
= 0;
890 foundData
[i
].blockCount
= 0;
895 error
= CreateExtentRecord(vcb
, &foundKey
, foundData
, &hint
);
896 if (error
== fxOvFlErr
)
900 * Add a new extent into existing record.
902 foundData
[foundIndex
].startBlock
= startBlock
;
903 foundData
[foundIndex
].blockCount
= blockCount
;
904 error
= UpdateExtentRecord(vcb
, fcb
, 0, &foundKey
, foundData
, hint
);
906 (void) FlushExtentFile(vcb
);
912 //_________________________________________________________________________________
914 // Routine: Extendfile
916 // Function: Extends the disk space allocated to a file.
918 //_________________________________________________________________________________
921 ExtendedVCB
*vcb
, // volume that file resides on
922 FCB
*fcb
, // FCB of file to truncate
923 int64_t bytesToAdd
, // number of bytes to allocate
924 u_int32_t blockHint
, // desired starting allocation block
925 u_int32_t flags
, // EFContig and/or EFAll
926 int64_t *actualBytesAdded
) // number of bytes actually allocated
929 u_int32_t volumeBlockSize
;
931 int64_t bytesThisExtent
;
932 HFSPlusExtentKey foundKey
;
933 HFSPlusExtentRecord foundData
;
934 u_int32_t foundIndex
;
937 u_int32_t startBlock
;
938 Boolean allOrNothing
;
943 u_int32_t actualStartBlock
;
944 u_int32_t actualNumBlocks
;
945 u_int32_t numExtentsPerRecord
;
946 int64_t maximumBytes
;
949 u_int32_t prevblocks
;
953 *actualBytesAdded
= 0;
954 volumeBlockSize
= vcb
->blockSize
;
955 allOrNothing
= ((flags
& kEFAllMask
) != 0);
956 forceContig
= ((flags
& kEFContigMask
) != 0);
957 prevblocks
= fcb
->ff_blocks
;
959 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
960 numExtentsPerRecord
= kHFSPlusExtentDensity
;
962 numExtentsPerRecord
= kHFSExtentDensity
;
965 // Make sure the request and new PEOF are less than 2GB if HFS.
967 if (vcb
->vcbSigWord
== kHFSSigWord
) {
968 if (bytesToAdd
>= kTwoGigabytes
)
970 if ((((int64_t)fcb
->ff_blocks
* (int64_t)volumeBlockSize
) + bytesToAdd
) >= kTwoGigabytes
)
974 // Determine how many blocks need to be allocated.
975 // Round up the number of desired bytes to add.
977 blocksToAdd
= howmany(bytesToAdd
, volumeBlockSize
);
978 bytesToAdd
= (int64_t)((int64_t)blocksToAdd
* (int64_t)volumeBlockSize
);
981 * For deferred allocations just reserve the blocks.
983 if ((flags
& kEFDeferMask
)
984 && (vcb
->vcbSigWord
== kHFSPlusSigWord
)
985 && (bytesToAdd
< (int64_t)HFS_MAX_DEFERED_ALLOC
)
986 && (blocksToAdd
< hfs_freeblks(VCBTOHFS(vcb
), 1))) {
987 HFS_MOUNT_LOCK(vcb
, TRUE
);
988 vcb
->loanedBlocks
+= blocksToAdd
;
989 HFS_MOUNT_UNLOCK(vcb
, TRUE
);
991 fcb
->ff_unallocblocks
+= blocksToAdd
;
992 FTOC(fcb
)->c_blocks
+= blocksToAdd
;
993 fcb
->ff_blocks
+= blocksToAdd
;
995 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
996 *actualBytesAdded
= bytesToAdd
;
1000 * Give back any unallocated blocks before doing real allocations.
1002 if (fcb
->ff_unallocblocks
> 0) {
1003 u_int32_t loanedBlocks
;
1005 loanedBlocks
= fcb
->ff_unallocblocks
;
1006 blocksToAdd
+= loanedBlocks
;
1007 bytesToAdd
= (int64_t)blocksToAdd
* (int64_t)volumeBlockSize
;
1008 FTOC(fcb
)->c_blocks
-= loanedBlocks
;
1009 fcb
->ff_blocks
-= loanedBlocks
;
1010 fcb
->ff_unallocblocks
= 0;
1012 HFS_MOUNT_LOCK(vcb
, TRUE
);
1013 vcb
->loanedBlocks
-= loanedBlocks
;
1014 HFS_MOUNT_UNLOCK(vcb
, TRUE
);
1018 // If the file's clump size is larger than the allocation block size,
1019 // then set the maximum number of bytes to the requested number of bytes
1020 // rounded up to a multiple of the clump size.
1022 if ((vcb
->vcbClpSiz
> (int32_t)volumeBlockSize
)
1023 && (bytesToAdd
< (int64_t)HFS_MAX_DEFERED_ALLOC
)
1024 && (flags
& kEFNoClumpMask
) == 0) {
1025 maximumBytes
= (int64_t)howmany(bytesToAdd
, vcb
->vcbClpSiz
);
1026 maximumBytes
*= vcb
->vcbClpSiz
;
1028 maximumBytes
= bytesToAdd
;
1032 // Compute new physical EOF, rounded up to a multiple of a block.
1034 if ( (vcb
->vcbSigWord
== kHFSSigWord
) && // Too big?
1035 ((((int64_t)fcb
->ff_blocks
* (int64_t)volumeBlockSize
) + bytesToAdd
) >= kTwoGigabytes
) ) {
1036 if (allOrNothing
) // Yes, must they have it all?
1037 goto Overflow
; // Yes, can't have it
1039 --blocksToAdd
; // No, give give 'em one block less
1040 bytesToAdd
-= volumeBlockSize
;
1045 // If allocation is all-or-nothing, make sure there are
1046 // enough free blocks on the volume (quick test).
1049 (blocksToAdd
> hfs_freeblks(VCBTOHFS(vcb
), flags
& kEFReserveMask
))) {
1055 // See if there are already enough blocks allocated to the file.
1057 peof
= ((int64_t)fcb
->ff_blocks
* (int64_t)volumeBlockSize
) + bytesToAdd
; // potential new PEOF
1058 err
= SearchExtentFile(vcb
, fcb
, peof
-1, &foundKey
, foundData
, &foundIndex
, &hint
, &nextBlock
);
1060 // Enough blocks are already allocated. Just update the FCB to reflect the new length.
1061 fcb
->ff_blocks
= peof
/ volumeBlockSize
;
1062 FTOC(fcb
)->c_blocks
+= (bytesToAdd
/ volumeBlockSize
);
1063 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
1066 if (err
!= fxRangeErr
) // Any real error?
1067 goto ErrorExit
; // Yes, so exit immediately
1070 // Adjust the PEOF to the end of the last extent.
1072 peof
= (int64_t)((int64_t)nextBlock
* (int64_t)volumeBlockSize
); // currently allocated PEOF
1073 bytesThisExtent
= (int64_t)(nextBlock
- fcb
->ff_blocks
) * (int64_t)volumeBlockSize
;
1074 if (bytesThisExtent
!= 0) {
1075 fcb
->ff_blocks
= nextBlock
;
1076 FTOC(fcb
)->c_blocks
+= (bytesThisExtent
/ volumeBlockSize
);
1077 FTOC(fcb
)->c_flag
|= C_MODIFIED
;
1078 bytesToAdd
-= bytesThisExtent
;
1082 // Allocate some more space.
1084 // First try a contiguous allocation (of the whole amount).
1085 // If that fails, get whatever we can.
1086 // If forceContig, then take whatever we got
1087 // else, keep getting bits and pieces (non-contig)
1090 * Note that for sparse devices (like sparse bundle dmgs), we
1091 * should only be aggressive with re-using once-allocated pieces
1092 * if we're not dealing with system files. If we're trying to operate
1093 * on behalf of a system file, we need the maximum contiguous amount
1094 * possible. For non-system files we favor locality and fragmentation over
1095 * contiguity as it can result in fewer blocks being needed from the underlying
1096 * filesystem that the sparse image resides upon.
1099 if ( (vcb
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
)
1100 && (fcb
->ff_cp
->c_fileid
>= kHFSFirstUserCatalogNodeID
)
1101 && (flags
& kEFMetadataMask
) == 0) {
1103 * We want locality over contiguity so by default we set wantContig to
1104 * false unless we hit one of the circumstances below.
1107 if (hfs_isrbtree_active(VCBTOHFS(vcb
))) {
1109 * If the red-black tree is acive, we can always find a suitable contiguous
1110 * chunk. So if the user specifically requests contiguous files, we should
1111 * honor that no matter what kind of device it is.
1119 * If the red-black tree is not active, then only set wantContig to true
1120 * if we have never done a contig scan on the device, which would populate
1121 * the free extent cache. Note that the caller may explicitly unset the
1122 * DID_CONTIG_SCAN bit in order to force us to vend a contiguous extent here
1123 * if the caller wants to get a contiguous chunk.
1125 if ((vcb
->hfs_flags
& HFS_DID_CONTIG_SCAN
) == 0) {
1126 vcb
->hfs_flags
|= HFS_DID_CONTIG_SCAN
;
1134 useMetaZone
= flags
& kEFMetadataMask
;
1137 startBlock
= blockHint
;
1139 startBlock
= foundData
[foundIndex
].startBlock
+ foundData
[foundIndex
].blockCount
;
1141 actualNumBlocks
= 0;
1142 actualStartBlock
= 0;
1144 /* Find number of free blocks based on reserved block flag option */
1145 availbytes
= (int64_t)hfs_freeblks(VCBTOHFS(vcb
), flags
& kEFReserveMask
) *
1146 (int64_t)volumeBlockSize
;
1147 if (availbytes
<= 0) {
1150 if (wantContig
&& (availbytes
< bytesToAdd
))
1153 err
= BlockAllocate(
1156 howmany(MIN(bytesToAdd
, availbytes
), volumeBlockSize
),
1157 howmany(MIN(maximumBytes
, availbytes
), volumeBlockSize
),
1158 (wantContig
? HFS_ALLOC_FORCECONTIG
: 0) |
1159 (useMetaZone
? HFS_ALLOC_METAZONE
: 0),
1164 if (err
== dskFulErr
) {
1166 break; // AllocContig failed because not enough contiguous space
1168 // Couldn't get one big chunk, so get whatever we can.
1173 if (actualNumBlocks
!= 0)
1175 if (useMetaZone
== 0) {
1176 /* Couldn't get anything so dip into metadat zone */
1183 if (actualNumBlocks
!= 0) {
1184 // this catalog entry *must* get forced to disk when
1185 // hfs_update() is called
1186 FTOC(fcb
)->c_flag
|= C_FORCEUPDATE
;
1189 // Add the new extent to the existing extent record, or create a new one.
1190 if ((actualStartBlock
== startBlock
) && (blockHint
== 0)) {
1191 // We grew the file's last extent, so just adjust the number of blocks.
1192 foundData
[foundIndex
].blockCount
+= actualNumBlocks
;
1193 err
= UpdateExtentRecord(vcb
, fcb
, 0, &foundKey
, foundData
, hint
);
1194 if (err
!= noErr
) break;
1199 // Need to add a new extent. See if there is room in the current record.
1200 if (foundData
[foundIndex
].blockCount
!= 0) // Is current extent free to use?
1201 ++foundIndex
; // No, so use the next one.
1202 if (foundIndex
== numExtentsPerRecord
) {
1203 // This record is full. Need to create a new one.
1204 if (FTOC(fcb
)->c_fileid
== kHFSExtentsFileID
) {
1205 (void) BlockDeallocate(vcb
, actualStartBlock
, actualNumBlocks
, 0);
1206 err
= dskFulErr
; // Oops. Can't extend extents file past first record.
1210 foundKey
.keyLength
= kHFSPlusExtentKeyMaximumLength
;
1211 if (FORK_IS_RSRC(fcb
))
1212 foundKey
.forkType
= kResourceForkType
;
1214 foundKey
.forkType
= kDataForkType
;
1216 foundKey
.fileID
= FTOC(fcb
)->c_fileid
;
1217 foundKey
.startBlock
= nextBlock
;
1219 foundData
[0].startBlock
= actualStartBlock
;
1220 foundData
[0].blockCount
= actualNumBlocks
;
1222 // zero out remaining extents...
1223 for (i
= 1; i
< kHFSPlusExtentDensity
; ++i
)
1225 foundData
[i
].startBlock
= 0;
1226 foundData
[i
].blockCount
= 0;
1231 err
= CreateExtentRecord(vcb
, &foundKey
, foundData
, &hint
);
1232 if (err
== fxOvFlErr
) {
1233 // We couldn't create an extent record because extents B-tree
1234 // couldn't grow. Dellocate the extent just allocated and
1235 // return a disk full error.
1236 (void) BlockDeallocate(vcb
, actualStartBlock
, actualNumBlocks
, 0);
1239 if (err
!= noErr
) break;
1241 needsFlush
= true; // We need to update the B-tree header
1244 // Add a new extent into this record and update.
1245 foundData
[foundIndex
].startBlock
= actualStartBlock
;
1246 foundData
[foundIndex
].blockCount
= actualNumBlocks
;
1247 err
= UpdateExtentRecord(vcb
, fcb
, 0, &foundKey
, foundData
, hint
);
1248 if (err
!= noErr
) break;
1252 // Figure out how many bytes were actually allocated.
1253 // NOTE: BlockAllocate could have allocated more than we asked for.
1254 // Don't set the PEOF beyond what our client asked for.
1255 nextBlock
+= actualNumBlocks
;
1256 bytesThisExtent
= (int64_t)((int64_t)actualNumBlocks
* (int64_t)volumeBlockSize
);
1257 if (bytesThisExtent
> bytesToAdd
) {
1261 bytesToAdd
-= bytesThisExtent
;
1262 maximumBytes
-= bytesThisExtent
;
1264 fcb
->ff_blocks
+= (bytesThisExtent
/ volumeBlockSize
);
1265 FTOC(fcb
)->c_blocks
+= (bytesThisExtent
/ volumeBlockSize
);
1266 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
1268 // If contiguous allocation was requested, then we've already got one contiguous
1269 // chunk. If we didn't get all we wanted, then adjust the error to disk full.
1271 if (bytesToAdd
!= 0)
1273 break; // We've already got everything that's contiguous
1276 } while (err
== noErr
&& bytesToAdd
);
1280 if (VCBTOHFS(vcb
)->hfs_flags
& HFS_METADATA_ZONE
) {
1281 /* Keep the roving allocator out of the metadata zone. */
1282 if (vcb
->nextAllocation
>= VCBTOHFS(vcb
)->hfs_metazone_start
&&
1283 vcb
->nextAllocation
<= VCBTOHFS(vcb
)->hfs_metazone_end
) {
1284 HFS_MOUNT_LOCK(vcb
, TRUE
);
1285 HFS_UPDATE_NEXT_ALLOCATION(vcb
, VCBTOHFS(vcb
)->hfs_metazone_end
+ 1);
1287 HFS_MOUNT_UNLOCK(vcb
, TRUE
);
1290 if (prevblocks
< fcb
->ff_blocks
) {
1291 *actualBytesAdded
= (int64_t)(fcb
->ff_blocks
- prevblocks
) * (int64_t)volumeBlockSize
;
1293 *actualBytesAdded
= 0;
1297 (void) FlushExtentFile(vcb
);
1302 err
= fileBoundsErr
;
1308 //_________________________________________________________________________________
1310 // Routine: TruncateFileC
1312 // Function: Truncates the disk space allocated to a file. The file space is
1313 // truncated to a specified new PEOF rounded up to the next allocation
1314 // block boundry. If the 'TFTrunExt' option is specified, the file is
1315 // truncated to the end of the extent containing the new PEOF.
1317 //_________________________________________________________________________________
1319 OSErr
TruncateFileC (
1320 ExtendedVCB
*vcb
, // volume that file resides on
1321 FCB
*fcb
, // FCB of file to truncate
1322 int64_t peof
, // new physical size for file
1323 int deleted
, // if nonzero, the file's catalog record has already been deleted.
1324 int rsrc
, // does this represent a resource fork or not?
1325 uint32_t fileid
, // the fileid of the file we're manipulating.
1326 Boolean truncateToExtent
) // if true, truncate to end of extent containing newPEOF
1330 u_int32_t nextBlock
; // next file allocation block to consider
1331 u_int32_t startBlock
; // Physical (volume) allocation block number of start of a range
1332 u_int32_t physNumBlocks
; // Number of allocation blocks in file (according to PEOF)
1333 u_int32_t numBlocks
;
1334 HFSPlusExtentKey key
; // key for current extent record; key->keyLength == 0 if FCB's extent record
1335 u_int32_t hint
; // BTree hint corresponding to key
1336 HFSPlusExtentRecord extentRecord
;
1337 u_int32_t extentIndex
;
1338 u_int32_t extentNextBlock
;
1339 u_int32_t numExtentsPerRecord
;
1342 Boolean extentChanged
; // true if we actually changed an extent
1343 Boolean recordDeleted
; // true if an extent record got deleted
1345 recordDeleted
= false;
1347 if (vcb
->vcbSigWord
== kHFSPlusSigWord
) {
1348 numExtentsPerRecord
= kHFSPlusExtentDensity
;
1351 numExtentsPerRecord
= kHFSExtentDensity
;
1355 forkType
= kResourceForkType
;
1358 forkType
= kDataForkType
;
1361 temp64
= fcb
->ff_blocks
;
1362 physNumBlocks
= (u_int32_t
)temp64
;
1365 // Round newPEOF up to a multiple of the allocation block size. If new size is
1366 // two gigabytes or more, then round down by one allocation block (??? really?
1367 // shouldn't that be an error?).
1369 nextBlock
= howmany(peof
, vcb
->blockSize
); // number of allocation blocks to remain in file
1370 peof
= (int64_t)((int64_t)nextBlock
* (int64_t)vcb
->blockSize
); // number of bytes in those blocks
1371 if ((vcb
->vcbSigWord
== kHFSSigWord
) && (peof
>= kTwoGigabytes
)) {
1373 DebugStr("HFS: Trying to truncate a file to 2GB or more");
1375 err
= fileBoundsErr
;
1380 // Update FCB's length
1383 * XXX Any errors could cause ff_blocks and c_blocks to get out of sync...
1385 numBlocks
= peof
/ vcb
->blockSize
;
1387 FTOC(fcb
)->c_blocks
-= (fcb
->ff_blocks
- numBlocks
);
1389 fcb
->ff_blocks
= numBlocks
;
1391 // this catalog entry is modified and *must* get forced
1392 // to disk when hfs_update() is called
1395 * If the file is already C_NOEXISTS, then the catalog record
1396 * has been removed from disk already. We wouldn't need to force
1399 FTOC(fcb
)->c_flag
|= (C_MODIFIED
| C_FORCEUPDATE
);
1402 // If the new PEOF is 0, then truncateToExtent has no meaning (we should always deallocate
1408 // Deallocate all the extents for this fork
1409 err
= DeallocateFork(vcb
, fileid
, forkType
, fcb
->fcbExtents
, &recordDeleted
);
1410 if (err
!= noErr
) goto ErrorExit
; // got some error, so return it
1412 // Update the catalog extent record (making sure it's zeroed out)
1414 for (i
=0; i
< kHFSPlusExtentDensity
; i
++) {
1415 fcb
->fcbExtents
[i
].startBlock
= 0;
1416 fcb
->fcbExtents
[i
].blockCount
= 0;
1423 // Find the extent containing byte (peof-1). This is the last extent we'll keep.
1424 // (If truncateToExtent is true, we'll keep the whole extent; otherwise, we'll only
1425 // keep up through peof). The search will tell us how many allocation blocks exist
1426 // in the found extent plus all previous extents.
1428 err
= SearchExtentFile(vcb
, fcb
, peof
-1, &key
, extentRecord
, &extentIndex
, &hint
, &extentNextBlock
);
1429 if (err
!= noErr
) goto ErrorExit
;
1431 extentChanged
= false; // haven't changed the extent yet
1433 if (!truncateToExtent
) {
1435 // Shorten this extent. It may be the case that the entire extent gets
1438 numBlocks
= extentNextBlock
- nextBlock
; // How many blocks in this extent to free up
1439 if (numBlocks
!= 0) {
1440 // Compute first volume allocation block to free
1441 startBlock
= extentRecord
[extentIndex
].startBlock
+ extentRecord
[extentIndex
].blockCount
- numBlocks
;
1442 // Free the blocks in bitmap
1443 err
= BlockDeallocate(vcb
, startBlock
, numBlocks
, 0);
1444 if (err
!= noErr
) goto ErrorExit
;
1445 // Adjust length of this extent
1446 extentRecord
[extentIndex
].blockCount
-= numBlocks
;
1447 // If extent is empty, set start block to 0
1448 if (extentRecord
[extentIndex
].blockCount
== 0)
1449 extentRecord
[extentIndex
].startBlock
= 0;
1450 // Remember that we changed the extent record
1451 extentChanged
= true;
1456 // Now move to the next extent in the record, and set up the file allocation block number
1458 nextBlock
= extentNextBlock
; // Next file allocation block to free
1459 ++extentIndex
; // Its index within the extent record
1462 // Release all following extents in this extent record. Update the record.
1464 while (extentIndex
< numExtentsPerRecord
&& extentRecord
[extentIndex
].blockCount
!= 0) {
1465 numBlocks
= extentRecord
[extentIndex
].blockCount
;
1466 // Deallocate this extent
1467 err
= BlockDeallocate(vcb
, extentRecord
[extentIndex
].startBlock
, numBlocks
, 0);
1468 if (err
!= noErr
) goto ErrorExit
;
1469 // Update next file allocation block number
1470 nextBlock
+= numBlocks
;
1471 // Zero out start and length of this extent to delete it from record
1472 extentRecord
[extentIndex
].startBlock
= 0;
1473 extentRecord
[extentIndex
].blockCount
= 0;
1474 // Remember that we changed an extent
1475 extentChanged
= true;
1476 // Move to next extent in record
1481 // If any of the extents in the current record were changed, then update that
1482 // record (in the FCB, or extents file).
1484 if (extentChanged
) {
1485 err
= UpdateExtentRecord(vcb
, fcb
, deleted
, &key
, extentRecord
, hint
);
1486 if (err
!= noErr
) goto ErrorExit
;
1490 // If there are any following allocation blocks, then we need
1491 // to seach for their extent records and delete those allocation
1494 if (nextBlock
< physNumBlocks
)
1495 err
= TruncateExtents(vcb
, forkType
, fileid
, nextBlock
, &recordDeleted
);
1500 (void) FlushExtentFile(vcb
);
1510 OSErr
HeadTruncateFile (
1515 HFSPlusExtentRecord extents
;
1516 HFSPlusExtentRecord tailExtents
;
1517 HFSCatalogNodeID fileID
;
1521 u_int32_t blksfreed
;
1527 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1530 forkType
= FORK_IS_RSRC(fcb
) ? kResourceForkType
: kDataForkType
;
1531 fileID
= FTOC(fcb
)->c_fileid
;
1532 bzero(tailExtents
, sizeof(tailExtents
));
1538 * Process catalog resident extents
1540 for (i
= 0, j
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1541 blkcnt
= fcb
->fcbExtents
[i
].blockCount
;
1543 break; /* end of extents */
1545 if (blksfreed
< headblks
) {
1546 error
= BlockDeallocate(vcb
, fcb
->fcbExtents
[i
].startBlock
, blkcnt
, 0);
1548 * Any errors after the first BlockDeallocate
1549 * must be ignored so we can put the file in
1554 goto ErrorExit
; /* uh oh */
1557 printf("hfs: HeadTruncateFile: problems deallocating %s (%d)\n",
1558 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1562 blksfreed
+= blkcnt
;
1563 fcb
->fcbExtents
[i
].startBlock
= 0;
1564 fcb
->fcbExtents
[i
].blockCount
= 0;
1566 tailExtents
[j
].startBlock
= fcb
->fcbExtents
[i
].startBlock
;
1567 tailExtents
[j
].blockCount
= blkcnt
;
1576 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1579 * Process overflow extents
1584 error
= FindExtentRecord(vcb
, forkType
, fileID
, startblk
, false, NULL
, extents
, NULL
);
1587 * Any errors after the first BlockDeallocate
1588 * must be ignored so we can put the file in
1591 if (error
!= btNotFound
)
1592 printf("hfs: HeadTruncateFile: problems finding extents %s (%d)\n",
1593 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1598 for(i
= 0, extblks
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1599 blkcnt
= extents
[i
].blockCount
;
1601 break; /* end of extents */
1603 if (blksfreed
< headblks
) {
1604 error
= BlockDeallocate(vcb
, extents
[i
].startBlock
, blkcnt
, 0);
1606 printf("hfs: HeadTruncateFile: problems deallocating %s (%d)\n",
1607 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1610 blksfreed
+= blkcnt
;
1612 tailExtents
[j
].startBlock
= extents
[i
].startBlock
;
1613 tailExtents
[j
].blockCount
= blkcnt
;
1619 error
= DeleteExtentRecord(vcb
, forkType
, fileID
, startblk
);
1621 printf("hfs: HeadTruncateFile: problems deallocating %s (%d)\n",
1622 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1627 break; /* all done */
1629 startblk
+= extblks
;
1631 hfs_systemfile_unlock(vcb
, lockflags
);
1635 bcopy(tailExtents
, fcb
->fcbExtents
, sizeof(tailExtents
));
1636 blkcnt
= fcb
->ff_blocks
- headblks
;
1637 FTOC(fcb
)->c_blocks
-= headblks
;
1638 fcb
->ff_blocks
= blkcnt
;
1640 FTOC(fcb
)->c_flag
|= C_FORCEUPDATE
;
1641 FTOC(fcb
)->c_touch_chgtime
= TRUE
;
1643 (void) FlushExtentFile(vcb
);
1647 return MacToVFSError(error
);
1652 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1653 // Routine: SearchExtentRecord (was XRSearch)
1655 // Function: Searches extent record for the extent mapping a given file
1656 // allocation block number (FABN).
1658 // Input: searchFABN - desired FABN
1659 // extentData - pointer to extent data record (xdr)
1660 // extentDataStartFABN - beginning FABN for extent record
1662 // Output: foundExtentDataOffset - offset to extent entry within xdr
1663 // result = noErr, offset to extent mapping desired FABN
1664 // result = FXRangeErr, offset to last extent in record
1665 // endingFABNPlusOne - ending FABN +1
1666 // noMoreExtents - True if the extent was not found, and the
1667 // extent record was not full (so don't bother
1668 // looking in subsequent records); false otherwise.
1670 // Result: noErr = ok
1671 // FXRangeErr = desired FABN > last mapped FABN in record
1672 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1674 static OSErr
SearchExtentRecord(
1676 u_int32_t searchFABN
,
1677 const HFSPlusExtentRecord extentData
,
1678 u_int32_t extentDataStartFABN
,
1679 u_int32_t
*foundExtentIndex
,
1680 u_int32_t
*endingFABNPlusOne
,
1681 Boolean
*noMoreExtents
)
1684 u_int32_t extentIndex
;
1685 u_int32_t numberOfExtents
;
1686 u_int32_t numAllocationBlocks
;
1687 Boolean foundExtent
;
1689 *endingFABNPlusOne
= extentDataStartFABN
;
1690 *noMoreExtents
= false;
1691 foundExtent
= false;
1693 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
1694 numberOfExtents
= kHFSPlusExtentDensity
;
1696 numberOfExtents
= kHFSExtentDensity
;
1698 for( extentIndex
= 0; extentIndex
< numberOfExtents
; ++extentIndex
)
1701 // Loop over the extent record and find the search FABN.
1703 numAllocationBlocks
= extentData
[extentIndex
].blockCount
;
1704 if ( numAllocationBlocks
== 0 )
1709 *endingFABNPlusOne
+= numAllocationBlocks
;
1711 if( searchFABN
< *endingFABNPlusOne
)
1713 // Found the extent.
1721 // Found the extent. Note the extent offset
1722 *foundExtentIndex
= extentIndex
;
1726 // Did not find the extent. Set foundExtentDataOffset accordingly
1727 if( extentIndex
> 0 )
1729 *foundExtentIndex
= extentIndex
- 1;
1733 *foundExtentIndex
= 0;
1736 // If we found an empty extent, then set noMoreExtents.
1737 if (extentIndex
< numberOfExtents
)
1738 *noMoreExtents
= true;
1740 // Finally, return an error to the caller
1747 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1748 // Routine: SearchExtentFile (was XFSearch)
1750 // Function: Searches extent file (including the FCB resident extent record)
1751 // for the extent mapping a given file position.
1753 // Input: vcb - VCB pointer
1754 // fcb - FCB pointer
1755 // filePosition - file position (byte address)
1757 // Output: foundExtentKey - extent key record (xkr)
1758 // If extent was found in the FCB's resident extent record,
1759 // then foundExtentKey->keyLength will be set to 0.
1760 // foundExtentData - extent data record(xdr)
1761 // foundExtentIndex - index to extent entry in xdr
1762 // result = 0, offset to extent mapping desired FABN
1763 // result = FXRangeErr, offset to last extent in record
1764 // (i.e., kNumExtentsPerRecord-1)
1765 // extentBTreeHint - BTree hint for extent record
1766 // kNoHint = Resident extent record
1767 // endingFABNPlusOne - ending FABN +1
1770 // noErr Found an extent that contains the given file position
1771 // FXRangeErr Given position is beyond the last allocated extent
1772 // (other) (some other internal I/O error)
1773 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1775 static OSErr
SearchExtentFile(
1778 int64_t filePosition
,
1779 HFSPlusExtentKey
*foundExtentKey
,
1780 HFSPlusExtentRecord foundExtentData
,
1781 u_int32_t
*foundExtentIndex
,
1782 u_int32_t
*extentBTreeHint
,
1783 u_int32_t
*endingFABNPlusOne
)
1786 u_int32_t filePositionBlock
;
1788 Boolean noMoreExtents
;
1791 temp64
= filePosition
/ (int64_t)vcb
->blockSize
;
1792 filePositionBlock
= (u_int32_t
)temp64
;
1794 bcopy ( fcb
->fcbExtents
, foundExtentData
, sizeof(HFSPlusExtentRecord
));
1796 // Search the resident FCB first.
1797 err
= SearchExtentRecord( vcb
, filePositionBlock
, foundExtentData
, 0,
1798 foundExtentIndex
, endingFABNPlusOne
, &noMoreExtents
);
1800 if( err
== noErr
) {
1801 // Found the extent. Set results accordingly
1802 *extentBTreeHint
= kNoHint
; // no hint, because not in the BTree
1803 foundExtentKey
->keyLength
= 0; // 0 = the FCB itself
1808 // Didn't find extent in FCB. If FCB's extent record wasn't full, there's no point
1809 // in searching the extents file. Note that SearchExtentRecord left us pointing at
1810 // the last valid extent (or the first one, if none were valid). This means we need
1811 // to fill in the hint and key outputs, just like the "if" statement above.
1812 if ( noMoreExtents
) {
1813 *extentBTreeHint
= kNoHint
; // no hint, because not in the BTree
1814 foundExtentKey
->keyLength
= 0; // 0 = the FCB itself
1815 err
= fxRangeErr
; // There are no more extents, so must be beyond PEOF
1820 // Find the desired record, or the previous record if it is the same fork
1822 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1824 err
= FindExtentRecord(vcb
, FORK_IS_RSRC(fcb
) ? kResourceForkType
: kDataForkType
,
1825 FTOC(fcb
)->c_fileid
, filePositionBlock
, true, foundExtentKey
, foundExtentData
, extentBTreeHint
);
1826 hfs_systemfile_unlock(vcb
, lockflags
);
1828 if (err
== btNotFound
) {
1830 // If we get here, the desired position is beyond the extents in the FCB, and there are no extents
1831 // in the extents file. Return the FCB's extents and a range error.
1833 *extentBTreeHint
= kNoHint
;
1834 foundExtentKey
->keyLength
= 0;
1835 err
= GetFCBExtentRecord(fcb
, foundExtentData
);
1836 // Note: foundExtentIndex and endingFABNPlusOne have already been set as a result of the very
1837 // first SearchExtentRecord call in this function (when searching in the FCB's extents, and
1838 // we got a range error).
1844 // If we get here, there was either a BTree error, or we found an appropriate record.
1845 // If we found a record, then search it for the correct index into the extents.
1848 // Find appropriate index into extent record
1849 err
= SearchExtentRecord(vcb
, filePositionBlock
, foundExtentData
, foundExtentKey
->startBlock
,
1850 foundExtentIndex
, endingFABNPlusOne
, &noMoreExtents
);
1859 //============================================================================
1860 // Routine: UpdateExtentRecord
1862 // Function: Write new extent data to an existing extent record with a given key.
1863 // If all of the extents are empty, and the extent record is in the
1864 // extents file, then the record is deleted.
1866 // Input: vcb - the volume containing the extents
1867 // fcb - the file that owns the extents
1868 // deleted - whether or not the file is already deleted
1869 // extentFileKey - pointer to extent key record (xkr)
1870 // If the key length is 0, then the extents are actually part
1871 // of the catalog record, stored in the FCB.
1872 // extentData - pointer to extent data record (xdr)
1873 // extentBTreeHint - hint for given key, or kNoHint
1875 // Result: noErr = ok
1876 // (other) = error from BTree
1877 //============================================================================
1879 static OSErr
UpdateExtentRecord (ExtendedVCB
*vcb
, FCB
*fcb
, int deleted
,
1880 const HFSPlusExtentKey
*extentFileKey
,
1881 const HFSPlusExtentRecord extentData
,
1882 u_int32_t extentBTreeHint
)
1886 if (extentFileKey
->keyLength
== 0) { // keyLength == 0 means the FCB's extent record
1887 BlockMoveData(extentData
, fcb
->fcbExtents
, sizeof(HFSPlusExtentRecord
));
1889 FTOC(fcb
)->c_flag
|= C_MODIFIED
;
1893 BTreeIterator btIterator
;
1894 FSBufferDescriptor btRecord
;
1895 u_int16_t btRecordSize
;
1900 // Need to find and change a record in Extents BTree
1902 btFCB
= GetFileControlBlock(vcb
->extentsRefNum
);
1904 bzero(&btIterator
, sizeof(btIterator
));
1907 * The lock taken by callers of ExtendFileC/TruncateFileC is
1908 * speculative and only occurs when the file already has
1909 * overflow extents. So we need to make sure we have the lock
1910 * here. The extents btree lock can be nested (its recursive)
1911 * so we always take it here.
1913 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1915 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1916 HFSExtentKey
* key
; // Actual extent key used on disk in HFS
1917 HFSExtentRecord foundData
; // The extent data actually found
1919 key
= (HFSExtentKey
*) &btIterator
.key
;
1920 key
->keyLength
= kHFSExtentKeyMaximumLength
;
1921 key
->forkType
= extentFileKey
->forkType
;
1922 key
->fileID
= extentFileKey
->fileID
;
1923 key
->startBlock
= extentFileKey
->startBlock
;
1925 btIterator
.hint
.index
= 0;
1926 btIterator
.hint
.nodeNum
= extentBTreeHint
;
1928 btRecord
.bufferAddress
= &foundData
;
1929 btRecord
.itemSize
= sizeof(HFSExtentRecord
);
1930 btRecord
.itemCount
= 1;
1932 err
= BTSearchRecord(btFCB
, &btIterator
, &btRecord
, &btRecordSize
, &btIterator
);
1935 err
= HFSPlusToHFSExtents(extentData
, (HFSExtentDescriptor
*)&foundData
);
1938 err
= BTReplaceRecord(btFCB
, &btIterator
, &btRecord
, btRecordSize
);
1939 (void) BTFlushPath(btFCB
);
1941 else { // HFS Plus volume
1942 HFSPlusExtentRecord foundData
; // The extent data actually found
1944 BlockMoveData(extentFileKey
, &btIterator
.key
, sizeof(HFSPlusExtentKey
));
1946 btIterator
.hint
.index
= 0;
1947 btIterator
.hint
.nodeNum
= extentBTreeHint
;
1949 btRecord
.bufferAddress
= &foundData
;
1950 btRecord
.itemSize
= sizeof(HFSPlusExtentRecord
);
1951 btRecord
.itemCount
= 1;
1953 err
= BTSearchRecord(btFCB
, &btIterator
, &btRecord
, &btRecordSize
, &btIterator
);
1956 BlockMoveData(extentData
, &foundData
, sizeof(HFSPlusExtentRecord
));
1957 err
= BTReplaceRecord(btFCB
, &btIterator
, &btRecord
, btRecordSize
);
1959 (void) BTFlushPath(btFCB
);
1961 hfs_systemfile_unlock(vcb
, lockflags
);
1970 static OSErr
HFSPlusToHFSExtents(
1971 const HFSPlusExtentRecord oldExtents
,
1972 HFSExtentRecord newExtents
)
1978 // copy the first 3 extents
1979 newExtents
[0].startBlock
= oldExtents
[0].startBlock
;
1980 newExtents
[0].blockCount
= oldExtents
[0].blockCount
;
1981 newExtents
[1].startBlock
= oldExtents
[1].startBlock
;
1982 newExtents
[1].blockCount
= oldExtents
[1].blockCount
;
1983 newExtents
[2].startBlock
= oldExtents
[2].startBlock
;
1984 newExtents
[2].blockCount
= oldExtents
[2].blockCount
;
1987 if (oldExtents
[3].startBlock
|| oldExtents
[3].blockCount
) {
1988 DebugStr("ExtentRecord with > 3 extents is invalid for HFS");
1999 static OSErr
GetFCBExtentRecord(
2001 HFSPlusExtentRecord extents
)
2004 BlockMoveData(fcb
->fcbExtents
, extents
, sizeof(HFSPlusExtentRecord
));
2010 //_________________________________________________________________________________
2012 // Routine: ExtentsAreIntegral
2014 // Purpose: Ensure that each extent can hold an integral number of nodes
2015 // Called by the NodesAreContiguous function
2016 //_________________________________________________________________________________
2018 static Boolean
ExtentsAreIntegral(
2019 const HFSPlusExtentRecord extentRecord
,
2021 u_int32_t
*blocksChecked
,
2022 Boolean
*checkedLastExtent
)
2025 u_int32_t extentIndex
;
2028 *checkedLastExtent
= false;
2030 for(extentIndex
= 0; extentIndex
< kHFSPlusExtentDensity
; extentIndex
++)
2032 blocks
= extentRecord
[extentIndex
].blockCount
;
2036 *checkedLastExtent
= true;
2040 *blocksChecked
+= blocks
;
2050 //_________________________________________________________________________________
2052 // Routine: NodesAreContiguous
2054 // Purpose: Ensure that all b-tree nodes are contiguous on disk
2055 // Called by BTOpenPath during volume mount
2056 //_________________________________________________________________________________
2058 Boolean
NodesAreContiguous(
2064 u_int32_t startBlock
;
2065 u_int32_t blocksChecked
;
2067 HFSPlusExtentKey key
;
2068 HFSPlusExtentRecord extents
;
2070 Boolean lastExtentReached
;
2074 if (vcb
->blockSize
>= nodeSize
)
2077 mask
= (nodeSize
/ vcb
->blockSize
) - 1;
2079 // check the local extents
2080 (void) GetFCBExtentRecord(fcb
, extents
);
2081 if ( !ExtentsAreIntegral(extents
, mask
, &blocksChecked
, &lastExtentReached
) )
2084 if ( lastExtentReached
||
2085 (int64_t)((int64_t)blocksChecked
* (int64_t)vcb
->blockSize
) >= (int64_t)fcb
->ff_size
)
2088 startBlock
= blocksChecked
;
2090 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
2092 // check the overflow extents (if any)
2093 while ( !lastExtentReached
)
2095 result
= FindExtentRecord(vcb
, kDataForkType
, fcb
->ff_cp
->c_fileid
, startBlock
, FALSE
, &key
, extents
, &hint
);
2098 if ( !ExtentsAreIntegral(extents
, mask
, &blocksChecked
, &lastExtentReached
) ) {
2099 hfs_systemfile_unlock(vcb
, lockflags
);
2102 startBlock
+= blocksChecked
;
2104 hfs_systemfile_unlock(vcb
, lockflags
);