2 * Copyright (c) 2000-2013 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,
112 static OSErr
HFSPlusToHFSExtents(
113 const HFSPlusExtentRecord oldExtents
,
114 HFSExtentRecord newExtents
);
117 static OSErr
FindExtentRecord(
118 const ExtendedVCB
*vcb
,
121 u_int32_t startBlock
,
122 Boolean allowPrevious
,
123 HFSPlusExtentKey
*foundKey
,
124 HFSPlusExtentRecord foundData
,
125 u_int32_t
*foundHint
);
127 static OSErr
DeleteExtentRecord(
128 const ExtendedVCB
*vcb
,
131 u_int32_t startBlock
);
133 static OSErr
CreateExtentRecord(
135 HFSPlusExtentKey
*key
,
136 HFSPlusExtentRecord extents
,
140 static OSErr
GetFCBExtentRecord(
142 HFSPlusExtentRecord extents
);
144 static OSErr
SearchExtentFile(
147 int64_t filePosition
,
148 HFSPlusExtentKey
*foundExtentKey
,
149 HFSPlusExtentRecord foundExtentData
,
150 u_int32_t
*foundExtentDataIndex
,
151 u_int32_t
*extentBTreeHint
,
152 u_int32_t
*endingFABNPlusOne
);
154 static OSErr
SearchExtentRecord(
156 u_int32_t searchFABN
,
157 const HFSPlusExtentRecord extentData
,
158 u_int32_t extentDataStartFABN
,
159 u_int32_t
*foundExtentDataOffset
,
160 u_int32_t
*endingFABNPlusOne
,
161 Boolean
*noMoreExtents
);
163 static OSErr
ReleaseExtents(
165 const HFSPlusExtentRecord extentRecord
,
166 u_int32_t
*numReleasedAllocationBlocks
,
167 Boolean
*releasedLastExtent
);
169 static OSErr
DeallocateFork(
171 HFSCatalogNodeID fileID
,
173 HFSPlusExtentRecord catalogExtents
,
174 Boolean
* recordDeleted
);
176 static OSErr
TruncateExtents(
180 u_int32_t startBlock
,
181 Boolean
* recordDeleted
);
183 static OSErr
UpdateExtentRecord (
187 const HFSPlusExtentKey
*extentFileKey
,
188 const HFSPlusExtentRecord extentData
,
189 u_int32_t extentBTreeHint
);
191 static Boolean
ExtentsAreIntegral(
192 const HFSPlusExtentRecord extentRecord
,
194 u_int32_t
*blocksChecked
,
195 Boolean
*checkedLastExtent
);
197 //_________________________________________________________________________________
199 // Routine: FindExtentRecord
201 // Purpose: Search the extents BTree for an extent record matching the given
202 // FileID, fork, and starting file allocation block number.
205 // vcb Volume to search
206 // forkType 0 = data fork, -1 = resource fork
207 // fileID File's FileID (CatalogNodeID)
208 // startBlock Starting file allocation block number
209 // allowPrevious If the desired record isn't found and this flag is set,
210 // then see if the previous record belongs to the same fork.
211 // If so, then return it.
214 // foundKey The key data for the record actually found
215 // foundData The extent record actually found (NOTE: on an HFS volume, the
216 // fourth entry will be zeroes.
217 // foundHint The BTree hint to find the node again
218 //_________________________________________________________________________________
219 static OSErr
FindExtentRecord(
220 const ExtendedVCB
*vcb
,
223 u_int32_t startBlock
,
224 Boolean allowPrevious
,
225 HFSPlusExtentKey
*foundKey
,
226 HFSPlusExtentRecord foundData
,
227 u_int32_t
*foundHint
)
230 struct BTreeIterator
*btIterator
= NULL
;
231 FSBufferDescriptor btRecord
;
233 u_int16_t btRecordSize
;
238 fcb
= GetFileControlBlock(vcb
->extentsRefNum
);
240 MALLOC (btIterator
, struct BTreeIterator
*, sizeof(struct BTreeIterator
), M_TEMP
, M_WAITOK
);
241 if (btIterator
== NULL
) {
242 return memFullErr
; // translates to ENOMEM
244 bzero(btIterator
, sizeof(*btIterator
));
246 /* HFS Plus / HFSX */
247 if (vcb
->vcbSigWord
!= kHFSSigWord
) {
248 HFSPlusExtentKey
* extentKeyPtr
;
249 HFSPlusExtentRecord extentData
;
251 extentKeyPtr
= (HFSPlusExtentKey
*) &btIterator
->key
;
252 extentKeyPtr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
253 extentKeyPtr
->forkType
= forkType
;
254 extentKeyPtr
->pad
= 0;
255 extentKeyPtr
->fileID
= fileID
;
256 extentKeyPtr
->startBlock
= startBlock
;
258 btRecord
.bufferAddress
= &extentData
;
259 btRecord
.itemSize
= sizeof(HFSPlusExtentRecord
);
260 btRecord
.itemCount
= 1;
262 err
= BTSearchRecord(fcb
, btIterator
, &btRecord
, &btRecordSize
, btIterator
);
264 if (err
== btNotFound
&& allowPrevious
) {
265 err
= BTIterateRecord(fcb
, kBTreePrevRecord
, btIterator
, &btRecord
, &btRecordSize
);
267 // A previous record may not exist, so just return btNotFound (like we would if
268 // it was for the wrong file/fork).
269 if (err
== (OSErr
) fsBTStartOfIterationErr
) //¥¥ fsBTStartOfIterationErr is type unsigned long
273 // Found a previous record. Does it belong to the same fork of the same file?
274 if (extentKeyPtr
->fileID
!= fileID
|| extentKeyPtr
->forkType
!= forkType
)
280 // Copy the found key back for the caller
282 BlockMoveData(extentKeyPtr
, foundKey
, sizeof(HFSPlusExtentKey
));
283 // Copy the found data back for the caller
284 BlockMoveData(&extentData
, foundData
, sizeof(HFSPlusExtentRecord
));
289 HFSExtentKey
* extentKeyPtr
;
290 HFSExtentRecord extentData
;
292 extentKeyPtr
= (HFSExtentKey
*) &btIterator
->key
;
293 extentKeyPtr
->keyLength
= kHFSExtentKeyMaximumLength
;
294 extentKeyPtr
->forkType
= forkType
;
295 extentKeyPtr
->fileID
= fileID
;
296 extentKeyPtr
->startBlock
= startBlock
;
298 btRecord
.bufferAddress
= &extentData
;
299 btRecord
.itemSize
= sizeof(HFSExtentRecord
);
300 btRecord
.itemCount
= 1;
302 err
= BTSearchRecord(fcb
, btIterator
, &btRecord
, &btRecordSize
, btIterator
);
304 if (err
== btNotFound
&& allowPrevious
) {
305 err
= BTIterateRecord(fcb
, kBTreePrevRecord
, btIterator
, &btRecord
, &btRecordSize
);
307 // A previous record may not exist, so just return btNotFound (like we would if
308 // it was for the wrong file/fork).
309 if (err
== (OSErr
) fsBTStartOfIterationErr
) //¥¥ fsBTStartOfIterationErr is type unsigned long
313 // Found a previous record. Does it belong to the same fork of the same file?
314 if (extentKeyPtr
->fileID
!= fileID
|| extentKeyPtr
->forkType
!= forkType
)
322 // Copy the found key back for the caller
324 foundKey
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
325 foundKey
->forkType
= extentKeyPtr
->forkType
;
327 foundKey
->fileID
= extentKeyPtr
->fileID
;
328 foundKey
->startBlock
= extentKeyPtr
->startBlock
;
330 // Copy the found data back for the caller
331 foundData
[0].startBlock
= extentData
[0].startBlock
;
332 foundData
[0].blockCount
= extentData
[0].blockCount
;
333 foundData
[1].startBlock
= extentData
[1].startBlock
;
334 foundData
[1].blockCount
= extentData
[1].blockCount
;
335 foundData
[2].startBlock
= extentData
[2].startBlock
;
336 foundData
[2].blockCount
= extentData
[2].blockCount
;
338 for (i
= 3; i
< kHFSPlusExtentDensity
; ++i
)
340 foundData
[i
].startBlock
= 0;
341 foundData
[i
].blockCount
= 0;
348 *foundHint
= btIterator
->hint
.nodeNum
;
350 FREE(btIterator
, M_TEMP
);
356 static OSErr
CreateExtentRecord(
358 HFSPlusExtentKey
*key
,
359 HFSPlusExtentRecord extents
,
362 struct BTreeIterator
*btIterator
= NULL
;
363 FSBufferDescriptor btRecord
;
364 u_int16_t btRecordSize
;
371 MALLOC (btIterator
, struct BTreeIterator
*, sizeof(struct BTreeIterator
), M_TEMP
, M_WAITOK
);
372 if (btIterator
== NULL
) {
373 return memFullErr
; // translates to ENOMEM
375 bzero(btIterator
, sizeof(*btIterator
));
378 * The lock taken by callers of ExtendFileC is speculative and
379 * only occurs when the file already has overflow extents. So
380 * We need to make sure we have the lock here. The extents
381 * btree lock can be nested (its recursive) so we always take
384 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
387 if (vcb
->vcbSigWord
!= kHFSSigWord
) {
388 btRecordSize
= sizeof(HFSPlusExtentRecord
);
389 btRecord
.bufferAddress
= extents
;
390 btRecord
.itemSize
= btRecordSize
;
391 btRecord
.itemCount
= 1;
393 BlockMoveData(key
, &btIterator
->key
, sizeof(HFSPlusExtentKey
));
398 HFSExtentKey
* keyPtr
;
399 HFSExtentRecord data
;
401 btRecordSize
= sizeof(HFSExtentRecord
);
402 btRecord
.bufferAddress
= &data
;
403 btRecord
.itemSize
= btRecordSize
;
404 btRecord
.itemCount
= 1;
406 keyPtr
= (HFSExtentKey
*) &btIterator
->key
;
407 keyPtr
->keyLength
= kHFSExtentKeyMaximumLength
;
408 keyPtr
->forkType
= key
->forkType
;
409 keyPtr
->fileID
= key
->fileID
;
410 keyPtr
->startBlock
= key
->startBlock
;
412 err
= HFSPlusToHFSExtents(extents
, data
);
417 err
= BTInsertRecord(GetFileControlBlock(vcb
->extentsRefNum
), btIterator
, &btRecord
, btRecordSize
);
420 *hint
= btIterator
->hint
.nodeNum
;
422 (void) BTFlushPath(GetFileControlBlock(vcb
->extentsRefNum
));
424 hfs_systemfile_unlock(vcb
, lockflags
);
426 FREE (btIterator
, M_TEMP
);
431 static OSErr
DeleteExtentRecord(
432 const ExtendedVCB
*vcb
,
435 u_int32_t startBlock
)
437 struct BTreeIterator
*btIterator
= NULL
;
442 MALLOC (btIterator
, struct BTreeIterator
*, sizeof(struct BTreeIterator
), M_TEMP
, M_WAITOK
);
443 if (btIterator
== NULL
) {
444 return memFullErr
; // translates to ENOMEM
446 bzero(btIterator
, sizeof(*btIterator
));
449 if (vcb
->vcbSigWord
!= kHFSSigWord
) { // HFS Plus volume
450 HFSPlusExtentKey
* keyPtr
;
452 keyPtr
= (HFSPlusExtentKey
*) &btIterator
->key
;
453 keyPtr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
454 keyPtr
->forkType
= forkType
;
456 keyPtr
->fileID
= fileID
;
457 keyPtr
->startBlock
= startBlock
;
462 HFSExtentKey
* keyPtr
;
464 keyPtr
= (HFSExtentKey
*) &btIterator
->key
;
465 keyPtr
->keyLength
= kHFSExtentKeyMaximumLength
;
466 keyPtr
->forkType
= forkType
;
467 keyPtr
->fileID
= fileID
;
468 keyPtr
->startBlock
= startBlock
;
472 err
= BTDeleteRecord(GetFileControlBlock(vcb
->extentsRefNum
), btIterator
);
473 (void) BTFlushPath(GetFileControlBlock(vcb
->extentsRefNum
));
476 FREE(btIterator
, M_TEMP
);
482 //_________________________________________________________________________________
484 // Routine: MapFileBlock
486 // Function: Maps a file position into a physical disk address.
488 //_________________________________________________________________________________
490 OSErr
MapFileBlockC (
491 ExtendedVCB
*vcb
, // volume that file resides on
492 FCB
*fcb
, // FCB of file
493 size_t numberOfBytes
, // number of contiguous bytes desired
494 off_t offset
, // starting offset within file (in bytes)
495 daddr64_t
*startSector
, // first sector (NOT an allocation block)
496 size_t *availableBytes
) // number of contiguous bytes (up to numberOfBytes)
499 u_int32_t allocBlockSize
; // Size of the volume's allocation block
500 u_int32_t sectorSize
;
501 HFSPlusExtentKey foundKey
;
502 HFSPlusExtentRecord foundData
;
503 u_int32_t foundIndex
;
505 u_int32_t firstFABN
; // file allocation block of first block in found extent
506 u_int32_t nextFABN
; // file allocation block of block after end of found extent
507 off_t dataEnd
; // (offset) end of range that is contiguous
508 u_int32_t sectorsPerBlock
; // Number of sectors per allocation block
509 u_int32_t startBlock
; // volume allocation block corresponding to firstFABN
513 allocBlockSize
= vcb
->blockSize
;
514 sectorSize
= VCBTOHFS(vcb
)->hfs_logical_block_size
;
516 err
= SearchExtentFile(vcb
, fcb
, offset
, &foundKey
, foundData
, &foundIndex
, &hint
, &nextFABN
);
518 startBlock
= foundData
[foundIndex
].startBlock
;
519 firstFABN
= nextFABN
- foundData
[foundIndex
].blockCount
;
528 // Determine the end of the available space. It will either be the end of the extent,
529 // or the file's PEOF, whichever is smaller.
531 dataEnd
= (off_t
)((off_t
)(nextFABN
) * (off_t
)(allocBlockSize
)); // Assume valid data through end of this extent
532 if (((off_t
)fcb
->ff_blocks
* (off_t
)allocBlockSize
) < dataEnd
) // Is PEOF shorter?
533 dataEnd
= (off_t
)fcb
->ff_blocks
* (off_t
)allocBlockSize
; // Yes, so only map up to PEOF
535 // Compute the number of sectors in an allocation block
536 sectorsPerBlock
= allocBlockSize
/ sectorSize
; // sectors per allocation block
539 // Compute the absolute sector number that contains the offset of the given file
540 // offset in sectors from start of the extent +
541 // offset in sectors from start of allocation block space
543 temp
= (daddr64_t
)((offset
- (off_t
)((off_t
)(firstFABN
) * (off_t
)(allocBlockSize
)))/sectorSize
);
544 temp
+= (daddr64_t
)startBlock
* (daddr64_t
)sectorsPerBlock
;
546 /* Add in any volume offsets */
547 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
548 temp
+= vcb
->hfsPlusIOPosOffset
/ sectorSize
;
550 temp
+= vcb
->vcbAlBlSt
;
552 // Return the desired sector for file position "offset"
556 // Determine the number of contiguous bytes until the end of the extent
557 // (or the amount they asked for, whichever comes first).
561 tmpOff
= dataEnd
- offset
;
563 * Disallow negative runs.
569 if (tmpOff
> (off_t
)(numberOfBytes
)) {
570 *availableBytes
= numberOfBytes
; // more there than they asked for, so pin the output
573 *availableBytes
= tmpOff
;
581 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
582 // Routine: ReleaseExtents
584 // Function: Release the extents of a single extent data record.
585 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
587 static OSErr
ReleaseExtents(
589 const HFSPlusExtentRecord extentRecord
,
590 u_int32_t
*numReleasedAllocationBlocks
,
591 Boolean
*releasedLastExtent
)
593 u_int32_t extentIndex
;
594 u_int32_t numberOfExtents
;
597 *numReleasedAllocationBlocks
= 0;
598 *releasedLastExtent
= false;
600 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
601 numberOfExtents
= kHFSPlusExtentDensity
;
603 numberOfExtents
= kHFSExtentDensity
;
605 for( extentIndex
= 0; extentIndex
< numberOfExtents
; extentIndex
++)
607 u_int32_t numAllocationBlocks
;
609 // Loop over the extent record and release the blocks associated with each extent.
611 numAllocationBlocks
= extentRecord
[extentIndex
].blockCount
;
612 if ( numAllocationBlocks
== 0 )
614 *releasedLastExtent
= true;
618 err
= BlockDeallocate( vcb
, extentRecord
[extentIndex
].startBlock
, numAllocationBlocks
, 0);
622 *numReleasedAllocationBlocks
+= numAllocationBlocks
; // bump FABN to beg of next extent
630 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
631 // Routine: TruncateExtents
633 // Purpose: Delete extent records whose starting file allocation block number
634 // is greater than or equal to a given starting block number. The
635 // allocation blocks represented by the extents are deallocated.
638 // vcb Volume to operate on
639 // fileID Which file to operate on
640 // startBlock Starting file allocation block number for first extent
642 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
644 static OSErr
TruncateExtents(
648 u_int32_t startBlock
,
649 Boolean
* recordDeleted
)
652 u_int32_t numberExtentsReleased
;
653 Boolean releasedLastExtent
;
655 HFSPlusExtentKey key
;
656 HFSPlusExtentRecord extents
;
660 * The lock taken by callers of TruncateFileC is speculative and
661 * only occurs when the file already has overflow extents. So
662 * We need to make sure we have the lock here. The extents
663 * btree lock can be nested (its recursive) so we always take
666 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
669 err
= FindExtentRecord(vcb
, forkType
, fileID
, startBlock
, false, &key
, extents
, &hint
);
671 if (err
== btNotFound
)
676 err
= ReleaseExtents( vcb
, extents
, &numberExtentsReleased
, &releasedLastExtent
);
677 if (err
!= noErr
) break;
679 err
= DeleteExtentRecord(vcb
, forkType
, fileID
, startBlock
);
680 if (err
!= noErr
) break;
682 *recordDeleted
= true;
683 startBlock
+= numberExtentsReleased
;
685 hfs_systemfile_unlock(vcb
, lockflags
);
692 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
693 // Routine: DeallocateFork
695 // Function: De-allocates all disk space allocated to a specified fork.
696 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
698 static OSErr
DeallocateFork(
700 HFSCatalogNodeID fileID
,
702 HFSPlusExtentRecord catalogExtents
,
703 Boolean
* recordDeleted
) /* true if a record was deleted */
706 u_int32_t numReleasedAllocationBlocks
;
707 Boolean releasedLastExtent
;
709 // Release the catalog extents
710 err
= ReleaseExtents( vcb
, catalogExtents
, &numReleasedAllocationBlocks
, &releasedLastExtent
);
711 // Release the extra extents, if present
712 if (err
== noErr
&& !releasedLastExtent
)
713 err
= TruncateExtents(vcb
, forkType
, fileID
, numReleasedAllocationBlocks
, recordDeleted
);
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
719 // Routine: FlushExtentFile
721 // Function: Flushes the extent file for a specified volume
722 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
724 OSErr
FlushExtentFile( ExtendedVCB
*vcb
)
730 fcb
= GetFileControlBlock(vcb
->extentsRefNum
);
732 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
733 err
= BTFlushPath(fcb
);
734 hfs_systemfile_unlock(vcb
, lockflags
);
738 // If the FCB for the extent "file" is dirty, mark the VCB as dirty.
740 if (FTOC(fcb
)->c_flag
& C_MODIFIED
)
743 // err = FlushVolumeControlBlock( vcb );
752 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
753 // Routine: CompareExtentKeys
755 // Function: Compares two extent file keys (a search key and a trial key) for
757 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
760 int32_t CompareExtentKeys( const HFSExtentKey
*searchKey
, const HFSExtentKey
*trialKey
)
762 int32_t result
; // ± 1
765 if (searchKey
->keyLength
!= kHFSExtentKeyMaximumLength
)
766 DebugStr("HFS: search Key is wrong length");
767 if (trialKey
->keyLength
!= kHFSExtentKeyMaximumLength
)
768 DebugStr("HFS: trial Key is wrong length");
771 result
= -1; // assume searchKey < trialKey
773 if (searchKey
->fileID
== trialKey
->fileID
) {
775 // FileNum's are equal; compare fork types
777 if (searchKey
->forkType
== trialKey
->forkType
) {
779 // Fork types are equal; compare allocation block number
781 if (searchKey
->startBlock
== trialKey
->startBlock
) {
783 // Everything is equal
789 // Allocation block numbers differ; determine sign
791 if (searchKey
->startBlock
> trialKey
->startBlock
)
797 // Fork types differ; determine sign
799 if (searchKey
->forkType
> trialKey
->forkType
)
805 // FileNums differ; determine sign
807 if (searchKey
->fileID
> trialKey
->fileID
)
816 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
817 // Routine: CompareExtentKeysPlus
819 // Function: Compares two extent file keys (a search key and a trial key) for
821 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
824 int32_t CompareExtentKeysPlus( const HFSPlusExtentKey
*searchKey
, const HFSPlusExtentKey
*trialKey
)
826 int32_t result
; // ± 1
829 if (searchKey
->keyLength
!= kHFSPlusExtentKeyMaximumLength
)
830 DebugStr("HFS: search Key is wrong length");
831 if (trialKey
->keyLength
!= kHFSPlusExtentKeyMaximumLength
)
832 DebugStr("HFS: trial Key is wrong length");
835 result
= -1; // assume searchKey < trialKey
837 if (searchKey
->fileID
== trialKey
->fileID
) {
839 // FileNum's are equal; compare fork types
841 if (searchKey
->forkType
== trialKey
->forkType
) {
843 // Fork types are equal; compare allocation block number
845 if (searchKey
->startBlock
== trialKey
->startBlock
) {
847 // Everything is equal
853 // Allocation block numbers differ; determine sign
855 if (searchKey
->startBlock
> trialKey
->startBlock
)
861 // Fork types differ; determine sign
863 if (searchKey
->forkType
> trialKey
->forkType
)
869 // FileNums differ; determine sign
871 if (searchKey
->fileID
> trialKey
->fileID
)
879 * Add a file extent to a file.
881 * Used by hfs_extendfs to extend the volume allocation bitmap file.
885 AddFileExtent(ExtendedVCB
*vcb
, FCB
*fcb
, u_int32_t startBlock
, u_int32_t blockCount
)
887 HFSPlusExtentKey foundKey
;
888 HFSPlusExtentRecord foundData
;
889 u_int32_t foundIndex
;
896 peof
= (int64_t)(fcb
->ff_blocks
+ blockCount
) * (int64_t)vcb
->blockSize
;
898 error
= SearchExtentFile(vcb
, fcb
, peof
-1, &foundKey
, foundData
, &foundIndex
, &hint
, &nextBlock
);
899 if (error
!= fxRangeErr
)
903 * Add new extent. See if there is room in the current record.
905 if (foundData
[foundIndex
].blockCount
!= 0)
907 if (foundIndex
== kHFSPlusExtentDensity
) {
909 * Existing record is full so create a new one.
911 foundKey
.keyLength
= kHFSPlusExtentKeyMaximumLength
;
912 foundKey
.forkType
= kDataForkType
;
914 foundKey
.fileID
= FTOC(fcb
)->c_fileid
;
915 foundKey
.startBlock
= nextBlock
;
917 foundData
[0].startBlock
= startBlock
;
918 foundData
[0].blockCount
= blockCount
;
920 /* zero out remaining extents. */
921 for (i
= 1; i
< kHFSPlusExtentDensity
; ++i
) {
922 foundData
[i
].startBlock
= 0;
923 foundData
[i
].blockCount
= 0;
928 error
= CreateExtentRecord(vcb
, &foundKey
, foundData
, &hint
);
929 if (error
== fxOvFlErr
)
933 * Add a new extent into existing record.
935 foundData
[foundIndex
].startBlock
= startBlock
;
936 foundData
[foundIndex
].blockCount
= blockCount
;
937 error
= UpdateExtentRecord(vcb
, fcb
, 0, &foundKey
, foundData
, hint
);
939 (void) FlushExtentFile(vcb
);
945 //_________________________________________________________________________________
947 // Routine: Extendfile
949 // Function: Extends the disk space allocated to a file.
951 //_________________________________________________________________________________
954 ExtendedVCB
*vcb
, // volume that file resides on
955 FCB
*fcb
, // FCB of file to truncate
956 int64_t bytesToAdd
, // number of bytes to allocate
957 u_int32_t blockHint
, // desired starting allocation block
958 u_int32_t flags
, // EFContig and/or EFAll
959 int64_t *actualBytesAdded
) // number of bytes actually allocated
962 u_int32_t volumeBlockSize
;
964 int64_t bytesThisExtent
;
965 HFSPlusExtentKey foundKey
;
966 HFSPlusExtentRecord foundData
;
967 u_int32_t foundIndex
;
970 u_int32_t startBlock
;
971 Boolean allOrNothing
;
977 u_int32_t actualStartBlock
;
978 u_int32_t actualNumBlocks
;
979 u_int32_t numExtentsPerRecord
;
980 int64_t maximumBytes
;
983 u_int32_t prevblocks
;
984 struct hfsmount
*hfsmp
= (struct hfsmount
*)vcb
;
987 *actualBytesAdded
= 0;
988 volumeBlockSize
= vcb
->blockSize
;
989 allOrNothing
= ((flags
& kEFAllMask
) != 0);
990 forceContig
= ((flags
& kEFContigMask
) != 0);
991 prevblocks
= fcb
->ff_blocks
;
993 if (vcb
->vcbSigWord
!= kHFSSigWord
) {
994 numExtentsPerRecord
= kHFSPlusExtentDensity
;
999 numExtentsPerRecord
= kHFSExtentDensity
;
1001 /* Make sure the request and new PEOF are less than 2GB if HFS std*/
1002 if (bytesToAdd
>= kTwoGigabytes
)
1003 goto HFS_Std_Overflow
;
1004 if ((((int64_t)fcb
->ff_blocks
* (int64_t)volumeBlockSize
) + bytesToAdd
) >= kTwoGigabytes
)
1005 goto HFS_Std_Overflow
;
1010 // Determine how many blocks need to be allocated.
1011 // Round up the number of desired bytes to add.
1013 blocksToAdd
= howmany(bytesToAdd
, volumeBlockSize
);
1014 bytesToAdd
= (int64_t)((int64_t)blocksToAdd
* (int64_t)volumeBlockSize
);
1017 * For deferred allocations just reserve the blocks.
1019 if ((flags
& kEFDeferMask
)
1020 && (vcb
->vcbSigWord
== kHFSPlusSigWord
)
1021 && (bytesToAdd
< (int64_t)HFS_MAX_DEFERED_ALLOC
)
1022 && (blocksToAdd
< hfs_freeblks(VCBTOHFS(vcb
), 1))) {
1023 hfs_lock_mount (hfsmp
);
1024 vcb
->loanedBlocks
+= blocksToAdd
;
1025 hfs_unlock_mount(hfsmp
);
1027 fcb
->ff_unallocblocks
+= blocksToAdd
;
1028 FTOC(fcb
)->c_blocks
+= blocksToAdd
;
1029 fcb
->ff_blocks
+= blocksToAdd
;
1031 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
1032 *actualBytesAdded
= bytesToAdd
;
1036 * Give back any unallocated blocks before doing real allocations.
1038 if (fcb
->ff_unallocblocks
> 0) {
1039 u_int32_t loanedBlocks
;
1041 loanedBlocks
= fcb
->ff_unallocblocks
;
1042 blocksToAdd
+= loanedBlocks
;
1043 bytesToAdd
= (int64_t)blocksToAdd
* (int64_t)volumeBlockSize
;
1044 FTOC(fcb
)->c_blocks
-= loanedBlocks
;
1045 fcb
->ff_blocks
-= loanedBlocks
;
1046 fcb
->ff_unallocblocks
= 0;
1048 hfs_lock_mount(hfsmp
);
1049 vcb
->loanedBlocks
-= loanedBlocks
;
1050 hfs_unlock_mount(hfsmp
);
1054 // If the file's clump size is larger than the allocation block size,
1055 // then set the maximum number of bytes to the requested number of bytes
1056 // rounded up to a multiple of the clump size.
1058 if ((vcb
->vcbClpSiz
> (int32_t)volumeBlockSize
)
1059 && (bytesToAdd
< (int64_t)HFS_MAX_DEFERED_ALLOC
)
1060 && (flags
& kEFNoClumpMask
) == 0) {
1061 maximumBytes
= (int64_t)howmany(bytesToAdd
, vcb
->vcbClpSiz
);
1062 maximumBytes
*= vcb
->vcbClpSiz
;
1064 maximumBytes
= bytesToAdd
;
1069 // Compute new physical EOF, rounded up to a multiple of a block.
1071 if ( (vcb
->vcbSigWord
== kHFSSigWord
) && // Too big?
1072 ((((int64_t)fcb
->ff_blocks
* (int64_t)volumeBlockSize
) + bytesToAdd
) >= kTwoGigabytes
) ) {
1073 if (allOrNothing
) // Yes, must they have it all?
1074 goto HFS_Std_Overflow
; // Yes, can't have it
1076 --blocksToAdd
; // No, give give 'em one block less
1077 bytesToAdd
-= volumeBlockSize
;
1083 // If allocation is all-or-nothing, make sure there are
1084 // enough free blocks on the volume (quick test).
1087 (blocksToAdd
> hfs_freeblks(VCBTOHFS(vcb
), flags
& kEFReserveMask
))) {
1093 // See if there are already enough blocks allocated to the file.
1095 peof
= ((int64_t)fcb
->ff_blocks
* (int64_t)volumeBlockSize
) + bytesToAdd
; // potential new PEOF
1096 err
= SearchExtentFile(vcb
, fcb
, peof
-1, &foundKey
, foundData
, &foundIndex
, &hint
, &nextBlock
);
1098 // Enough blocks are already allocated. Just update the FCB to reflect the new length.
1099 fcb
->ff_blocks
= peof
/ volumeBlockSize
;
1100 FTOC(fcb
)->c_blocks
+= (bytesToAdd
/ volumeBlockSize
);
1101 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
1104 if (err
!= fxRangeErr
) // Any real error?
1105 goto ErrorExit
; // Yes, so exit immediately
1108 // Adjust the PEOF to the end of the last extent.
1110 peof
= (int64_t)((int64_t)nextBlock
* (int64_t)volumeBlockSize
); // currently allocated PEOF
1111 bytesThisExtent
= (int64_t)(nextBlock
- fcb
->ff_blocks
) * (int64_t)volumeBlockSize
;
1112 if (bytesThisExtent
!= 0) {
1113 fcb
->ff_blocks
= nextBlock
;
1114 FTOC(fcb
)->c_blocks
+= (bytesThisExtent
/ volumeBlockSize
);
1115 FTOC(fcb
)->c_flag
|= C_MODIFIED
;
1116 bytesToAdd
-= bytesThisExtent
;
1120 // Allocate some more space.
1122 // First try a contiguous allocation (of the whole amount).
1123 // If that fails, get whatever we can.
1124 // If forceContig, then take whatever we got
1125 // else, keep getting bits and pieces (non-contig)
1128 * Note that for sparse devices (like sparse bundle dmgs), we
1129 * should only be aggressive with re-using once-allocated pieces
1130 * if we're not dealing with system files. If we're trying to operate
1131 * on behalf of a system file, we need the maximum contiguous amount
1132 * possible. For non-system files we favor locality and fragmentation over
1133 * contiguity as it can result in fewer blocks being needed from the underlying
1134 * filesystem that the sparse image resides upon.
1137 if ( (vcb
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
)
1138 && (fcb
->ff_cp
->c_fileid
>= kHFSFirstUserCatalogNodeID
)
1139 && (flags
& kEFMetadataMask
) == 0) {
1141 * We want locality over contiguity so by default we set wantContig to
1142 * false unless we hit one of the circumstances below.
1145 if (hfs_isrbtree_active(VCBTOHFS(vcb
))) {
1147 * If the red-black tree is acive, we can always find a suitable contiguous
1148 * chunk. So if the user specifically requests contiguous files, we should
1149 * honor that no matter what kind of device it is.
1157 * If the red-black tree is not active, then only set wantContig to true
1158 * if we have never done a contig scan on the device, which would populate
1159 * the free extent cache. Note that the caller may explicitly unset the
1160 * DID_CONTIG_SCAN bit in order to force us to vend a contiguous extent here
1161 * if the caller wants to get a contiguous chunk.
1163 if ((vcb
->hfs_flags
& HFS_DID_CONTIG_SCAN
) == 0) {
1164 vcb
->hfs_flags
|= HFS_DID_CONTIG_SCAN
;
1172 useMetaZone
= flags
& kEFMetadataMask
;
1175 startBlock
= blockHint
;
1177 startBlock
= foundData
[foundIndex
].startBlock
+ foundData
[foundIndex
].blockCount
;
1179 actualNumBlocks
= 0;
1180 actualStartBlock
= 0;
1182 /* Find number of free blocks based on reserved block flag option */
1183 availbytes
= (int64_t)hfs_freeblks(VCBTOHFS(vcb
), flags
& kEFReserveMask
) *
1184 (int64_t)volumeBlockSize
;
1185 if (availbytes
<= 0) {
1188 if (wantContig
&& (availbytes
< bytesToAdd
))
1191 uint32_t ba_flags
= 0;
1193 ba_flags
|= HFS_ALLOC_FORCECONTIG
;
1196 ba_flags
|= HFS_ALLOC_METAZONE
;
1198 if (allowFlushTxns
) {
1199 ba_flags
|= HFS_ALLOC_FLUSHTXN
;
1202 err
= BlockAllocate(
1205 howmany(MIN(bytesToAdd
, availbytes
), volumeBlockSize
),
1206 howmany(MIN(maximumBytes
, availbytes
), volumeBlockSize
),
1212 if (err
== dskFulErr
) {
1214 if (allowFlushTxns
== 0) {
1215 /* If we're forcing contiguity, re-try but allow plucking from recently freed regions */
1222 break; // AllocContig failed because not enough contiguous space
1226 // Couldn't get one big chunk, so get whatever we can.
1231 if (actualNumBlocks
!= 0)
1234 if (useMetaZone
== 0) {
1235 /* Couldn't get anything so dip into metadat zone */
1241 /* If we couldn't find what we needed without flushing the journal, then go ahead and do it now */
1242 if (allowFlushTxns
== 0) {
1250 if (actualNumBlocks
!= 0) {
1251 // this catalog entry *must* get forced to disk when
1252 // hfs_update() is called
1253 FTOC(fcb
)->c_flag
|= C_FORCEUPDATE
;
1256 // Add the new extent to the existing extent record, or create a new one.
1257 if ((actualStartBlock
== startBlock
) && (blockHint
== 0)) {
1258 // We grew the file's last extent, so just adjust the number of blocks.
1259 foundData
[foundIndex
].blockCount
+= actualNumBlocks
;
1260 err
= UpdateExtentRecord(vcb
, fcb
, 0, &foundKey
, foundData
, hint
);
1261 if (err
!= noErr
) break;
1266 // Need to add a new extent. See if there is room in the current record.
1267 if (foundData
[foundIndex
].blockCount
!= 0) // Is current extent free to use?
1268 ++foundIndex
; // No, so use the next one.
1269 if (foundIndex
== numExtentsPerRecord
) {
1270 // This record is full. Need to create a new one.
1271 if (FTOC(fcb
)->c_fileid
== kHFSExtentsFileID
) {
1272 (void) BlockDeallocate(vcb
, actualStartBlock
, actualNumBlocks
, 0);
1273 err
= dskFulErr
; // Oops. Can't extend extents file past first record.
1277 foundKey
.keyLength
= kHFSPlusExtentKeyMaximumLength
;
1278 if (FORK_IS_RSRC(fcb
))
1279 foundKey
.forkType
= kResourceForkType
;
1281 foundKey
.forkType
= kDataForkType
;
1283 foundKey
.fileID
= FTOC(fcb
)->c_fileid
;
1284 foundKey
.startBlock
= nextBlock
;
1286 foundData
[0].startBlock
= actualStartBlock
;
1287 foundData
[0].blockCount
= actualNumBlocks
;
1289 // zero out remaining extents...
1290 for (i
= 1; i
< kHFSPlusExtentDensity
; ++i
)
1292 foundData
[i
].startBlock
= 0;
1293 foundData
[i
].blockCount
= 0;
1298 err
= CreateExtentRecord(vcb
, &foundKey
, foundData
, &hint
);
1299 if (err
== fxOvFlErr
) {
1300 // We couldn't create an extent record because extents B-tree
1301 // couldn't grow. Dellocate the extent just allocated and
1302 // return a disk full error.
1303 (void) BlockDeallocate(vcb
, actualStartBlock
, actualNumBlocks
, 0);
1306 if (err
!= noErr
) break;
1308 needsFlush
= true; // We need to update the B-tree header
1311 // Add a new extent into this record and update.
1312 foundData
[foundIndex
].startBlock
= actualStartBlock
;
1313 foundData
[foundIndex
].blockCount
= actualNumBlocks
;
1314 err
= UpdateExtentRecord(vcb
, fcb
, 0, &foundKey
, foundData
, hint
);
1315 if (err
!= noErr
) break;
1319 // Figure out how many bytes were actually allocated.
1320 // NOTE: BlockAllocate could have allocated more than we asked for.
1321 // Don't set the PEOF beyond what our client asked for.
1322 nextBlock
+= actualNumBlocks
;
1323 bytesThisExtent
= (int64_t)((int64_t)actualNumBlocks
* (int64_t)volumeBlockSize
);
1324 if (bytesThisExtent
> bytesToAdd
) {
1328 bytesToAdd
-= bytesThisExtent
;
1329 maximumBytes
-= bytesThisExtent
;
1331 fcb
->ff_blocks
+= (bytesThisExtent
/ volumeBlockSize
);
1332 FTOC(fcb
)->c_blocks
+= (bytesThisExtent
/ volumeBlockSize
);
1333 FTOC(fcb
)->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
1335 // If contiguous allocation was requested, then we've already got one contiguous
1336 // chunk. If we didn't get all we wanted, then adjust the error to disk full.
1338 if (bytesToAdd
!= 0)
1340 break; // We've already got everything that's contiguous
1343 } while (err
== noErr
&& bytesToAdd
);
1347 if (VCBTOHFS(vcb
)->hfs_flags
& HFS_METADATA_ZONE
) {
1348 /* Keep the roving allocator out of the metadata zone. */
1349 if (vcb
->nextAllocation
>= VCBTOHFS(vcb
)->hfs_metazone_start
&&
1350 vcb
->nextAllocation
<= VCBTOHFS(vcb
)->hfs_metazone_end
) {
1351 hfs_lock_mount (hfsmp
);
1352 HFS_UPDATE_NEXT_ALLOCATION(vcb
, VCBTOHFS(vcb
)->hfs_metazone_end
+ 1);
1354 hfs_unlock_mount(hfsmp
);
1357 if (prevblocks
< fcb
->ff_blocks
) {
1358 *actualBytesAdded
= (int64_t)(fcb
->ff_blocks
- prevblocks
) * (int64_t)volumeBlockSize
;
1360 *actualBytesAdded
= 0;
1364 (void) FlushExtentFile(vcb
);
1371 err
= fileBoundsErr
;
1377 //_________________________________________________________________________________
1379 // Routine: TruncateFileC
1381 // Function: Truncates the disk space allocated to a file. The file space is
1382 // truncated to a specified new PEOF rounded up to the next allocation
1383 // block boundry. If the 'TFTrunExt' option is specified, the file is
1384 // truncated to the end of the extent containing the new PEOF.
1386 //_________________________________________________________________________________
1388 OSErr
TruncateFileC (
1389 ExtendedVCB
*vcb
, // volume that file resides on
1390 FCB
*fcb
, // FCB of file to truncate
1391 int64_t peof
, // new physical size for file
1392 int deleted
, // if nonzero, the file's catalog record has already been deleted.
1393 int rsrc
, // does this represent a resource fork or not?
1394 uint32_t fileid
, // the fileid of the file we're manipulating.
1395 Boolean truncateToExtent
) // if true, truncate to end of extent containing newPEOF
1399 u_int32_t nextBlock
; // next file allocation block to consider
1400 u_int32_t startBlock
; // Physical (volume) allocation block number of start of a range
1401 u_int32_t physNumBlocks
; // Number of allocation blocks in file (according to PEOF)
1402 u_int32_t numBlocks
;
1403 HFSPlusExtentKey key
; // key for current extent record; key->keyLength == 0 if FCB's extent record
1404 u_int32_t hint
; // BTree hint corresponding to key
1405 HFSPlusExtentRecord extentRecord
;
1406 u_int32_t extentIndex
;
1407 u_int32_t extentNextBlock
;
1408 u_int32_t numExtentsPerRecord
;
1411 Boolean extentChanged
; // true if we actually changed an extent
1412 Boolean recordDeleted
; // true if an extent record got deleted
1414 recordDeleted
= false;
1416 if (vcb
->vcbSigWord
== kHFSPlusSigWord
) {
1417 numExtentsPerRecord
= kHFSPlusExtentDensity
;
1420 numExtentsPerRecord
= kHFSExtentDensity
;
1424 forkType
= kResourceForkType
;
1427 forkType
= kDataForkType
;
1430 temp64
= fcb
->ff_blocks
;
1431 physNumBlocks
= (u_int32_t
)temp64
;
1434 // Round newPEOF up to a multiple of the allocation block size. If new size is
1435 // two gigabytes or more, then round down by one allocation block (??? really?
1436 // shouldn't that be an error?).
1438 nextBlock
= howmany(peof
, vcb
->blockSize
); // number of allocation blocks to remain in file
1439 peof
= (int64_t)((int64_t)nextBlock
* (int64_t)vcb
->blockSize
); // number of bytes in those blocks
1442 if ((vcb
->vcbSigWord
== kHFSSigWord
) && (peof
>= kTwoGigabytes
)) {
1444 DebugStr("HFS: Trying to truncate a file to 2GB or more");
1446 err
= fileBoundsErr
;
1452 // Update FCB's length
1455 * XXX Any errors could cause ff_blocks and c_blocks to get out of sync...
1457 numBlocks
= peof
/ vcb
->blockSize
;
1459 FTOC(fcb
)->c_blocks
-= (fcb
->ff_blocks
- numBlocks
);
1461 fcb
->ff_blocks
= numBlocks
;
1463 // this catalog entry is modified and *must* get forced
1464 // to disk when hfs_update() is called
1467 * If the file is already C_NOEXISTS, then the catalog record
1468 * has been removed from disk already. We wouldn't need to force
1471 FTOC(fcb
)->c_flag
|= (C_MODIFIED
| C_FORCEUPDATE
);
1474 // If the new PEOF is 0, then truncateToExtent has no meaning (we should always deallocate
1480 // Deallocate all the extents for this fork
1481 err
= DeallocateFork(vcb
, fileid
, forkType
, fcb
->fcbExtents
, &recordDeleted
);
1482 if (err
!= noErr
) goto ErrorExit
; // got some error, so return it
1484 // Update the catalog extent record (making sure it's zeroed out)
1486 for (i
=0; i
< kHFSPlusExtentDensity
; i
++) {
1487 fcb
->fcbExtents
[i
].startBlock
= 0;
1488 fcb
->fcbExtents
[i
].blockCount
= 0;
1495 // Find the extent containing byte (peof-1). This is the last extent we'll keep.
1496 // (If truncateToExtent is true, we'll keep the whole extent; otherwise, we'll only
1497 // keep up through peof). The search will tell us how many allocation blocks exist
1498 // in the found extent plus all previous extents.
1500 err
= SearchExtentFile(vcb
, fcb
, peof
-1, &key
, extentRecord
, &extentIndex
, &hint
, &extentNextBlock
);
1501 if (err
!= noErr
) goto ErrorExit
;
1503 extentChanged
= false; // haven't changed the extent yet
1505 if (!truncateToExtent
) {
1507 // Shorten this extent. It may be the case that the entire extent gets
1510 numBlocks
= extentNextBlock
- nextBlock
; // How many blocks in this extent to free up
1511 if (numBlocks
!= 0) {
1512 // Compute first volume allocation block to free
1513 startBlock
= extentRecord
[extentIndex
].startBlock
+ extentRecord
[extentIndex
].blockCount
- numBlocks
;
1514 // Free the blocks in bitmap
1515 err
= BlockDeallocate(vcb
, startBlock
, numBlocks
, 0);
1516 if (err
!= noErr
) goto ErrorExit
;
1517 // Adjust length of this extent
1518 extentRecord
[extentIndex
].blockCount
-= numBlocks
;
1519 // If extent is empty, set start block to 0
1520 if (extentRecord
[extentIndex
].blockCount
== 0)
1521 extentRecord
[extentIndex
].startBlock
= 0;
1522 // Remember that we changed the extent record
1523 extentChanged
= true;
1528 // Now move to the next extent in the record, and set up the file allocation block number
1530 nextBlock
= extentNextBlock
; // Next file allocation block to free
1531 ++extentIndex
; // Its index within the extent record
1534 // Release all following extents in this extent record. Update the record.
1536 while (extentIndex
< numExtentsPerRecord
&& extentRecord
[extentIndex
].blockCount
!= 0) {
1537 numBlocks
= extentRecord
[extentIndex
].blockCount
;
1538 // Deallocate this extent
1539 err
= BlockDeallocate(vcb
, extentRecord
[extentIndex
].startBlock
, numBlocks
, 0);
1540 if (err
!= noErr
) goto ErrorExit
;
1541 // Update next file allocation block number
1542 nextBlock
+= numBlocks
;
1543 // Zero out start and length of this extent to delete it from record
1544 extentRecord
[extentIndex
].startBlock
= 0;
1545 extentRecord
[extentIndex
].blockCount
= 0;
1546 // Remember that we changed an extent
1547 extentChanged
= true;
1548 // Move to next extent in record
1553 // If any of the extents in the current record were changed, then update that
1554 // record (in the FCB, or extents file).
1556 if (extentChanged
) {
1557 err
= UpdateExtentRecord(vcb
, fcb
, deleted
, &key
, extentRecord
, hint
);
1558 if (err
!= noErr
) goto ErrorExit
;
1562 // If there are any following allocation blocks, then we need
1563 // to seach for their extent records and delete those allocation
1566 if (nextBlock
< physNumBlocks
)
1567 err
= TruncateExtents(vcb
, forkType
, fileid
, nextBlock
, &recordDeleted
);
1572 (void) FlushExtentFile(vcb
);
1582 OSErr
HeadTruncateFile (
1587 HFSPlusExtentRecord extents
;
1588 HFSPlusExtentRecord tailExtents
;
1589 HFSCatalogNodeID fileID
;
1593 u_int32_t blksfreed
;
1599 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1602 forkType
= FORK_IS_RSRC(fcb
) ? kResourceForkType
: kDataForkType
;
1603 fileID
= FTOC(fcb
)->c_fileid
;
1604 bzero(tailExtents
, sizeof(tailExtents
));
1610 * Process catalog resident extents
1612 for (i
= 0, j
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1613 blkcnt
= fcb
->fcbExtents
[i
].blockCount
;
1615 break; /* end of extents */
1617 if (blksfreed
< headblks
) {
1618 error
= BlockDeallocate(vcb
, fcb
->fcbExtents
[i
].startBlock
, blkcnt
, 0);
1620 * Any errors after the first BlockDeallocate
1621 * must be ignored so we can put the file in
1626 goto ErrorExit
; /* uh oh */
1629 printf("hfs: HeadTruncateFile: problems deallocating %s (%d)\n",
1630 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1634 blksfreed
+= blkcnt
;
1635 fcb
->fcbExtents
[i
].startBlock
= 0;
1636 fcb
->fcbExtents
[i
].blockCount
= 0;
1638 tailExtents
[j
].startBlock
= fcb
->fcbExtents
[i
].startBlock
;
1639 tailExtents
[j
].blockCount
= blkcnt
;
1648 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1651 * Process overflow extents
1656 error
= FindExtentRecord(vcb
, forkType
, fileID
, startblk
, false, NULL
, extents
, NULL
);
1659 * Any errors after the first BlockDeallocate
1660 * must be ignored so we can put the file in
1663 if (error
!= btNotFound
)
1664 printf("hfs: HeadTruncateFile: problems finding extents %s (%d)\n",
1665 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1670 for(i
= 0, extblks
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1671 blkcnt
= extents
[i
].blockCount
;
1673 break; /* end of extents */
1675 if (blksfreed
< headblks
) {
1676 error
= BlockDeallocate(vcb
, extents
[i
].startBlock
, blkcnt
, 0);
1678 printf("hfs: HeadTruncateFile: problems deallocating %s (%d)\n",
1679 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1682 blksfreed
+= blkcnt
;
1684 tailExtents
[j
].startBlock
= extents
[i
].startBlock
;
1685 tailExtents
[j
].blockCount
= blkcnt
;
1691 error
= DeleteExtentRecord(vcb
, forkType
, fileID
, startblk
);
1693 printf("hfs: HeadTruncateFile: problems deallocating %s (%d)\n",
1694 FTOC(fcb
)->c_desc
.cd_nameptr
? (const char *)FTOC(fcb
)->c_desc
.cd_nameptr
: "", error
);
1699 break; /* all done */
1701 startblk
+= extblks
;
1703 hfs_systemfile_unlock(vcb
, lockflags
);
1707 bcopy(tailExtents
, fcb
->fcbExtents
, sizeof(tailExtents
));
1708 blkcnt
= fcb
->ff_blocks
- headblks
;
1709 FTOC(fcb
)->c_blocks
-= headblks
;
1710 fcb
->ff_blocks
= blkcnt
;
1712 FTOC(fcb
)->c_flag
|= C_FORCEUPDATE
;
1713 FTOC(fcb
)->c_touch_chgtime
= TRUE
;
1715 (void) FlushExtentFile(vcb
);
1719 return MacToVFSError(error
);
1724 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1725 // Routine: SearchExtentRecord (was XRSearch)
1727 // Function: Searches extent record for the extent mapping a given file
1728 // allocation block number (FABN).
1730 // Input: searchFABN - desired FABN
1731 // extentData - pointer to extent data record (xdr)
1732 // extentDataStartFABN - beginning FABN for extent record
1734 // Output: foundExtentDataOffset - offset to extent entry within xdr
1735 // result = noErr, offset to extent mapping desired FABN
1736 // result = FXRangeErr, offset to last extent in record
1737 // endingFABNPlusOne - ending FABN +1
1738 // noMoreExtents - True if the extent was not found, and the
1739 // extent record was not full (so don't bother
1740 // looking in subsequent records); false otherwise.
1742 // Result: noErr = ok
1743 // FXRangeErr = desired FABN > last mapped FABN in record
1744 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1746 static OSErr
SearchExtentRecord(
1748 u_int32_t searchFABN
,
1749 const HFSPlusExtentRecord extentData
,
1750 u_int32_t extentDataStartFABN
,
1751 u_int32_t
*foundExtentIndex
,
1752 u_int32_t
*endingFABNPlusOne
,
1753 Boolean
*noMoreExtents
)
1756 u_int32_t extentIndex
;
1757 /* Set it to the HFS std value */
1758 u_int32_t numberOfExtents
= kHFSExtentDensity
;
1759 u_int32_t numAllocationBlocks
;
1760 Boolean foundExtent
;
1762 *endingFABNPlusOne
= extentDataStartFABN
;
1763 *noMoreExtents
= false;
1764 foundExtent
= false;
1766 /* Override numberOfExtents for HFS+/HFSX */
1767 if (vcb
->vcbSigWord
!= kHFSSigWord
) {
1768 numberOfExtents
= kHFSPlusExtentDensity
;
1771 for( extentIndex
= 0; extentIndex
< numberOfExtents
; ++extentIndex
)
1774 // Loop over the extent record and find the search FABN.
1776 numAllocationBlocks
= extentData
[extentIndex
].blockCount
;
1777 if ( numAllocationBlocks
== 0 )
1782 *endingFABNPlusOne
+= numAllocationBlocks
;
1784 if( searchFABN
< *endingFABNPlusOne
)
1786 // Found the extent.
1794 // Found the extent. Note the extent offset
1795 *foundExtentIndex
= extentIndex
;
1799 // Did not find the extent. Set foundExtentDataOffset accordingly
1800 if( extentIndex
> 0 )
1802 *foundExtentIndex
= extentIndex
- 1;
1806 *foundExtentIndex
= 0;
1809 // If we found an empty extent, then set noMoreExtents.
1810 if (extentIndex
< numberOfExtents
)
1811 *noMoreExtents
= true;
1813 // Finally, return an error to the caller
1820 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1821 // Routine: SearchExtentFile (was XFSearch)
1823 // Function: Searches extent file (including the FCB resident extent record)
1824 // for the extent mapping a given file position.
1826 // Input: vcb - VCB pointer
1827 // fcb - FCB pointer
1828 // filePosition - file position (byte address)
1830 // Output: foundExtentKey - extent key record (xkr)
1831 // If extent was found in the FCB's resident extent record,
1832 // then foundExtentKey->keyLength will be set to 0.
1833 // foundExtentData - extent data record(xdr)
1834 // foundExtentIndex - index to extent entry in xdr
1835 // result = 0, offset to extent mapping desired FABN
1836 // result = FXRangeErr, offset to last extent in record
1837 // (i.e., kNumExtentsPerRecord-1)
1838 // extentBTreeHint - BTree hint for extent record
1839 // kNoHint = Resident extent record
1840 // endingFABNPlusOne - ending FABN +1
1843 // noErr Found an extent that contains the given file position
1844 // FXRangeErr Given position is beyond the last allocated extent
1845 // (other) (some other internal I/O error)
1846 //\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b\8b
1848 static OSErr
SearchExtentFile(
1851 int64_t filePosition
,
1852 HFSPlusExtentKey
*foundExtentKey
,
1853 HFSPlusExtentRecord foundExtentData
,
1854 u_int32_t
*foundExtentIndex
,
1855 u_int32_t
*extentBTreeHint
,
1856 u_int32_t
*endingFABNPlusOne
)
1859 u_int32_t filePositionBlock
;
1861 Boolean noMoreExtents
;
1864 temp64
= filePosition
/ (int64_t)vcb
->blockSize
;
1865 filePositionBlock
= (u_int32_t
)temp64
;
1867 bcopy ( fcb
->fcbExtents
, foundExtentData
, sizeof(HFSPlusExtentRecord
));
1869 // Search the resident FCB first.
1870 err
= SearchExtentRecord( vcb
, filePositionBlock
, foundExtentData
, 0,
1871 foundExtentIndex
, endingFABNPlusOne
, &noMoreExtents
);
1873 if( err
== noErr
) {
1874 // Found the extent. Set results accordingly
1875 *extentBTreeHint
= kNoHint
; // no hint, because not in the BTree
1876 foundExtentKey
->keyLength
= 0; // 0 = the FCB itself
1881 // Didn't find extent in FCB. If FCB's extent record wasn't full, there's no point
1882 // in searching the extents file. Note that SearchExtentRecord left us pointing at
1883 // the last valid extent (or the first one, if none were valid). This means we need
1884 // to fill in the hint and key outputs, just like the "if" statement above.
1885 if ( noMoreExtents
) {
1886 *extentBTreeHint
= kNoHint
; // no hint, because not in the BTree
1887 foundExtentKey
->keyLength
= 0; // 0 = the FCB itself
1888 err
= fxRangeErr
; // There are no more extents, so must be beyond PEOF
1893 // Find the desired record, or the previous record if it is the same fork
1895 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1897 err
= FindExtentRecord(vcb
, FORK_IS_RSRC(fcb
) ? kResourceForkType
: kDataForkType
,
1898 FTOC(fcb
)->c_fileid
, filePositionBlock
, true, foundExtentKey
, foundExtentData
, extentBTreeHint
);
1899 hfs_systemfile_unlock(vcb
, lockflags
);
1901 if (err
== btNotFound
) {
1903 // If we get here, the desired position is beyond the extents in the FCB, and there are no extents
1904 // in the extents file. Return the FCB's extents and a range error.
1906 *extentBTreeHint
= kNoHint
;
1907 foundExtentKey
->keyLength
= 0;
1908 err
= GetFCBExtentRecord(fcb
, foundExtentData
);
1909 // Note: foundExtentIndex and endingFABNPlusOne have already been set as a result of the very
1910 // first SearchExtentRecord call in this function (when searching in the FCB's extents, and
1911 // we got a range error).
1917 // If we get here, there was either a BTree error, or we found an appropriate record.
1918 // If we found a record, then search it for the correct index into the extents.
1921 // Find appropriate index into extent record
1922 err
= SearchExtentRecord(vcb
, filePositionBlock
, foundExtentData
, foundExtentKey
->startBlock
,
1923 foundExtentIndex
, endingFABNPlusOne
, &noMoreExtents
);
1932 //============================================================================
1933 // Routine: UpdateExtentRecord
1935 // Function: Write new extent data to an existing extent record with a given key.
1936 // If all of the extents are empty, and the extent record is in the
1937 // extents file, then the record is deleted.
1939 // Input: vcb - the volume containing the extents
1940 // fcb - the file that owns the extents
1941 // deleted - whether or not the file is already deleted
1942 // extentFileKey - pointer to extent key record (xkr)
1943 // If the key length is 0, then the extents are actually part
1944 // of the catalog record, stored in the FCB.
1945 // extentData - pointer to extent data record (xdr)
1946 // extentBTreeHint - hint for given key, or kNoHint
1948 // Result: noErr = ok
1949 // (other) = error from BTree
1950 //============================================================================
1952 static OSErr
UpdateExtentRecord (ExtendedVCB
*vcb
, FCB
*fcb
, int deleted
,
1953 const HFSPlusExtentKey
*extentFileKey
,
1954 const HFSPlusExtentRecord extentData
,
1955 u_int32_t extentBTreeHint
)
1959 if (extentFileKey
->keyLength
== 0) { // keyLength == 0 means the FCB's extent record
1960 BlockMoveData(extentData
, fcb
->fcbExtents
, sizeof(HFSPlusExtentRecord
));
1962 FTOC(fcb
)->c_flag
|= C_MODIFIED
;
1966 struct BTreeIterator
*btIterator
= NULL
;
1967 FSBufferDescriptor btRecord
;
1968 u_int16_t btRecordSize
;
1973 // Need to find and change a record in Extents BTree
1975 btFCB
= GetFileControlBlock(vcb
->extentsRefNum
);
1977 MALLOC (btIterator
, struct BTreeIterator
*, sizeof(struct BTreeIterator
), M_TEMP
, M_WAITOK
);
1978 if (btIterator
== NULL
) {
1979 return memFullErr
; // translates to ENOMEM
1981 bzero(btIterator
, sizeof(*btIterator
));
1984 * The lock taken by callers of ExtendFileC/TruncateFileC is
1985 * speculative and only occurs when the file already has
1986 * overflow extents. So we need to make sure we have the lock
1987 * here. The extents btree lock can be nested (its recursive)
1988 * so we always take it here.
1990 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1993 if (vcb
->vcbSigWord
!= kHFSSigWord
) { // HFS Plus volume
1994 HFSPlusExtentRecord foundData
; // The extent data actually found
1996 BlockMoveData(extentFileKey
, &btIterator
->key
, sizeof(HFSPlusExtentKey
));
1998 btIterator
->hint
.index
= 0;
1999 btIterator
->hint
.nodeNum
= extentBTreeHint
;
2001 btRecord
.bufferAddress
= &foundData
;
2002 btRecord
.itemSize
= sizeof(HFSPlusExtentRecord
);
2003 btRecord
.itemCount
= 1;
2005 err
= BTSearchRecord(btFCB
, btIterator
, &btRecord
, &btRecordSize
, btIterator
);
2008 BlockMoveData(extentData
, &foundData
, sizeof(HFSPlusExtentRecord
));
2009 err
= BTReplaceRecord(btFCB
, btIterator
, &btRecord
, btRecordSize
);
2011 (void) BTFlushPath(btFCB
);
2016 HFSExtentKey
* key
; // Actual extent key used on disk in HFS
2017 HFSExtentRecord foundData
; // The extent data actually found
2019 key
= (HFSExtentKey
*) &btIterator
->key
;
2020 key
->keyLength
= kHFSExtentKeyMaximumLength
;
2021 key
->forkType
= extentFileKey
->forkType
;
2022 key
->fileID
= extentFileKey
->fileID
;
2023 key
->startBlock
= extentFileKey
->startBlock
;
2025 btIterator
->hint
.index
= 0;
2026 btIterator
->hint
.nodeNum
= extentBTreeHint
;
2028 btRecord
.bufferAddress
= &foundData
;
2029 btRecord
.itemSize
= sizeof(HFSExtentRecord
);
2030 btRecord
.itemCount
= 1;
2032 err
= BTSearchRecord(btFCB
, btIterator
, &btRecord
, &btRecordSize
, btIterator
);
2035 err
= HFSPlusToHFSExtents(extentData
, (HFSExtentDescriptor
*)&foundData
);
2038 err
= BTReplaceRecord(btFCB
, btIterator
, &btRecord
, btRecordSize
);
2039 (void) BTFlushPath(btFCB
);
2044 hfs_systemfile_unlock(vcb
, lockflags
);
2046 FREE(btIterator
, M_TEMP
);
2055 static OSErr
HFSPlusToHFSExtents(
2056 const HFSPlusExtentRecord oldExtents
,
2057 HFSExtentRecord newExtents
)
2063 // copy the first 3 extents
2064 newExtents
[0].startBlock
= oldExtents
[0].startBlock
;
2065 newExtents
[0].blockCount
= oldExtents
[0].blockCount
;
2066 newExtents
[1].startBlock
= oldExtents
[1].startBlock
;
2067 newExtents
[1].blockCount
= oldExtents
[1].blockCount
;
2068 newExtents
[2].startBlock
= oldExtents
[2].startBlock
;
2069 newExtents
[2].blockCount
= oldExtents
[2].blockCount
;
2072 if (oldExtents
[3].startBlock
|| oldExtents
[3].blockCount
) {
2073 DebugStr("ExtentRecord with > 3 extents is invalid for HFS");
2084 static OSErr
GetFCBExtentRecord(
2086 HFSPlusExtentRecord extents
)
2089 BlockMoveData(fcb
->fcbExtents
, extents
, sizeof(HFSPlusExtentRecord
));
2095 //_________________________________________________________________________________
2097 // Routine: ExtentsAreIntegral
2099 // Purpose: Ensure that each extent can hold an integral number of nodes
2100 // Called by the NodesAreContiguous function
2101 //_________________________________________________________________________________
2103 static Boolean
ExtentsAreIntegral(
2104 const HFSPlusExtentRecord extentRecord
,
2106 u_int32_t
*blocksChecked
,
2107 Boolean
*checkedLastExtent
)
2110 u_int32_t extentIndex
;
2113 *checkedLastExtent
= false;
2115 for(extentIndex
= 0; extentIndex
< kHFSPlusExtentDensity
; extentIndex
++)
2117 blocks
= extentRecord
[extentIndex
].blockCount
;
2121 *checkedLastExtent
= true;
2125 *blocksChecked
+= blocks
;
2135 //_________________________________________________________________________________
2137 // Routine: NodesAreContiguous
2139 // Purpose: Ensure that all b-tree nodes are contiguous on disk
2140 // Called by BTOpenPath during volume mount
2141 //_________________________________________________________________________________
2143 Boolean
NodesAreContiguous(
2149 u_int32_t startBlock
;
2150 u_int32_t blocksChecked
;
2152 HFSPlusExtentKey key
;
2153 HFSPlusExtentRecord extents
;
2155 Boolean lastExtentReached
;
2159 if (vcb
->blockSize
>= nodeSize
)
2162 mask
= (nodeSize
/ vcb
->blockSize
) - 1;
2164 // check the local extents
2165 (void) GetFCBExtentRecord(fcb
, extents
);
2166 if ( !ExtentsAreIntegral(extents
, mask
, &blocksChecked
, &lastExtentReached
) )
2169 if ( lastExtentReached
||
2170 (int64_t)((int64_t)blocksChecked
* (int64_t)vcb
->blockSize
) >= (int64_t)fcb
->ff_size
)
2173 startBlock
= blocksChecked
;
2175 lockflags
= hfs_systemfile_lock(vcb
, SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
2177 // check the overflow extents (if any)
2178 while ( !lastExtentReached
)
2180 result
= FindExtentRecord(vcb
, kDataForkType
, fcb
->ff_cp
->c_fileid
, startBlock
, FALSE
, &key
, extents
, &hint
);
2183 if ( !ExtentsAreIntegral(extents
, mask
, &blocksChecked
, &lastExtentReached
) ) {
2184 hfs_systemfile_unlock(vcb
, lockflags
);
2187 startBlock
+= blocksChecked
;
2189 hfs_systemfile_unlock(vcb
, lockflags
);