]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2008 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | ||
29 | ||
30 | #include "../../hfs.h" | |
31 | #include "../../hfs_format.h" | |
32 | #include "../../hfs_endian.h" | |
33 | ||
34 | #include "../headers/FileMgrInternal.h" | |
35 | #include "../headers/BTreesInternal.h" | |
36 | ||
37 | #include <sys/malloc.h> | |
38 | ||
39 | /* | |
40 | ============================================================ | |
41 | Public (Exported) Routines: | |
42 | ============================================================ | |
43 | ||
44 | ExtendFileC Allocate more space to a given file. | |
45 | ||
46 | CompareExtentKeys | |
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 | |
50 | volume. | |
51 | ||
52 | CompareExtentKeysPlus | |
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+ | |
56 | volume. | |
57 | ||
58 | MapFileBlockC Convert (map) an offset within a given file into a | |
59 | physical disk address. | |
60 | ||
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. | |
65 | ||
66 | FlushExtentFile | |
67 | Flush the extents file for a given volume. | |
68 | ||
69 | ||
70 | ||
71 | ||
72 | ============================================================ | |
73 | Internal Routines: | |
74 | ============================================================ | |
75 | FindExtentRecord | |
76 | Search the extents BTree for a particular extent record. | |
77 | SearchExtentFile | |
78 | Search the FCB and extents file for an extent record that | |
79 | contains a given file position (in bytes). | |
80 | SearchExtentRecord | |
81 | Search a given extent record to see if it contains a given | |
82 | file position (in bytes). Used by SearchExtentFile. | |
83 | ReleaseExtents | |
84 | Deallocate all allocation blocks in all extents of an extent | |
85 | data record. | |
86 | TruncateExtents | |
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 | |
90 | for the file. | |
91 | DeallocateFork | |
92 | Deallocate all allocation blocks belonging to a given fork. | |
93 | UpdateExtentRecord | |
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. | |
98 | */ | |
99 | ||
100 | static const int64_t kTwoGigabytes = 0x80000000LL; | |
101 | ||
102 | enum | |
103 | { | |
104 | kDataForkType = 0, | |
105 | kResourceForkType = 0xFF, | |
106 | ||
107 | kPreviousRecord = -1 | |
108 | }; | |
109 | ||
110 | ||
111 | static OSErr HFSPlusToHFSExtents( | |
112 | const HFSPlusExtentRecord oldExtents, | |
113 | HFSExtentRecord newExtents); | |
114 | ||
115 | static OSErr FindExtentRecord( | |
116 | const ExtendedVCB *vcb, | |
117 | u_int8_t forkType, | |
118 | u_int32_t fileID, | |
119 | u_int32_t startBlock, | |
120 | Boolean allowPrevious, | |
121 | HFSPlusExtentKey *foundKey, | |
122 | HFSPlusExtentRecord foundData, | |
123 | u_int32_t *foundHint); | |
124 | ||
125 | static OSErr DeleteExtentRecord( | |
126 | const ExtendedVCB *vcb, | |
127 | u_int8_t forkType, | |
128 | u_int32_t fileID, | |
129 | u_int32_t startBlock); | |
130 | ||
131 | static OSErr CreateExtentRecord( | |
132 | ExtendedVCB *vcb, | |
133 | HFSPlusExtentKey *key, | |
134 | HFSPlusExtentRecord extents, | |
135 | u_int32_t *hint); | |
136 | ||
137 | ||
138 | static OSErr GetFCBExtentRecord( | |
139 | const FCB *fcb, | |
140 | HFSPlusExtentRecord extents); | |
141 | ||
142 | static OSErr SearchExtentFile( | |
143 | ExtendedVCB *vcb, | |
144 | const FCB *fcb, | |
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 ); | |
151 | ||
152 | static OSErr SearchExtentRecord( | |
153 | ExtendedVCB *vcb, | |
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); | |
160 | ||
161 | static OSErr ReleaseExtents( | |
162 | ExtendedVCB *vcb, | |
163 | const HFSPlusExtentRecord extentRecord, | |
164 | u_int32_t *numReleasedAllocationBlocks, | |
165 | Boolean *releasedLastExtent); | |
166 | ||
167 | static OSErr DeallocateFork( | |
168 | ExtendedVCB *vcb, | |
169 | HFSCatalogNodeID fileID, | |
170 | u_int8_t forkType, | |
171 | HFSPlusExtentRecord catalogExtents, | |
172 | Boolean * recordDeleted); | |
173 | ||
174 | static OSErr TruncateExtents( | |
175 | ExtendedVCB *vcb, | |
176 | u_int8_t forkType, | |
177 | u_int32_t fileID, | |
178 | u_int32_t startBlock, | |
179 | Boolean * recordDeleted); | |
180 | ||
181 | static OSErr UpdateExtentRecord ( | |
182 | ExtendedVCB *vcb, | |
183 | FCB *fcb, | |
184 | int deleted, | |
185 | const HFSPlusExtentKey *extentFileKey, | |
186 | const HFSPlusExtentRecord extentData, | |
187 | u_int32_t extentBTreeHint); | |
188 | ||
189 | static Boolean ExtentsAreIntegral( | |
190 | const HFSPlusExtentRecord extentRecord, | |
191 | u_int32_t mask, | |
192 | u_int32_t *blocksChecked, | |
193 | Boolean *checkedLastExtent); | |
194 | ||
195 | //_________________________________________________________________________________ | |
196 | // | |
197 | // Routine: FindExtentRecord | |
198 | // | |
199 | // Purpose: Search the extents BTree for an extent record matching the given | |
200 | // FileID, fork, and starting file allocation block number. | |
201 | // | |
202 | // Inputs: | |
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. | |
210 | // | |
211 | // Outputs: | |
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, | |
219 | u_int8_t forkType, | |
220 | u_int32_t fileID, | |
221 | u_int32_t startBlock, | |
222 | Boolean allowPrevious, | |
223 | HFSPlusExtentKey *foundKey, | |
224 | HFSPlusExtentRecord foundData, | |
225 | u_int32_t *foundHint) | |
226 | { | |
227 | FCB * fcb; | |
228 | BTreeIterator btIterator; | |
229 | FSBufferDescriptor btRecord; | |
230 | OSErr err; | |
231 | u_int16_t btRecordSize; | |
232 | ||
233 | err = noErr; | |
234 | if (foundHint) | |
235 | *foundHint = 0; | |
236 | fcb = GetFileControlBlock(vcb->extentsRefNum); | |
237 | ||
238 | bzero(&btIterator, sizeof(btIterator)); | |
239 | ||
240 | if (vcb->vcbSigWord == kHFSSigWord) { | |
241 | HFSExtentKey * extentKeyPtr; | |
242 | HFSExtentRecord extentData; | |
243 | ||
244 | extentKeyPtr = (HFSExtentKey*) &btIterator.key; | |
245 | extentKeyPtr->keyLength = kHFSExtentKeyMaximumLength; | |
246 | extentKeyPtr->forkType = forkType; | |
247 | extentKeyPtr->fileID = fileID; | |
248 | extentKeyPtr->startBlock = startBlock; | |
249 | ||
250 | btRecord.bufferAddress = &extentData; | |
251 | btRecord.itemSize = sizeof(HFSExtentRecord); | |
252 | btRecord.itemCount = 1; | |
253 | ||
254 | err = BTSearchRecord(fcb, &btIterator, &btRecord, &btRecordSize, &btIterator); | |
255 | ||
256 | if (err == btNotFound && allowPrevious) { | |
257 | err = BTIterateRecord(fcb, kBTreePrevRecord, &btIterator, &btRecord, &btRecordSize); | |
258 | ||
259 | // A previous record may not exist, so just return btNotFound (like we would if | |
260 | // it was for the wrong file/fork). | |
261 |