]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfscommon/headers/BTreesPrivate.h
805c86346b69ffbf7c10eb804171c3dda4ed8ece
[apple/xnu.git] / bsd / hfs / hfscommon / headers / BTreesPrivate.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 File: BTreesPrivate.h
24
25 Contains: Private interface file for the BTree Module.
26
27 Version: xxx put the technology version here xxx
28
29 Written by: Gordon Sheridan and Bill Bruffey
30
31 Copyright: © 1992-1999 by Apple Computer, Inc., all rights reserved.
32
33 File Ownership:
34
35 DRI: Don Brady
36
37 Other Contact: Mark Day
38
39 Technology: File Systems
40
41 Writers:
42
43 (msd) Mark Day
44 (DSH) Deric Horn
45 (djb) Don Brady
46 (ser) Scott Roberts
47 (dkh) Dave Heller
48
49 Change History (most recent first):
50 <MacOSX> 3/19/99 djb Disable MoveRecordsLeft/Right macros since bcopy is broken.
51
52 <MacOSX> 8/10/98 djb Removed unused BTreeIterator from BTreeControlBlock, fixed alignment.
53
54 <CS5> 9/4/97 djb Convert MoveRecordsLeft and GetLeftSiblingNode to macros.
55 <CS4> 7/24/97 djb Add macro for GetRecordAddress (was a function before).
56 <CS3> 7/21/97 msd GetRecordByIndex now returns an OSStatus.
57 <CS2> 7/16/97 DSH FilesInternal.i renamed FileMgrInternal.i to avoid name
58 collision
59 <CS1> 4/23/97 djb first checked in
60
61 <HFS6> 3/17/97 DSH Added a refCon field to BTreeControlBlock, for DFA use, to point
62 to additional data. Fixed Panic macros for use with SC.
63 <HFS5> 2/19/97 djb Add InsertKey struct. Moved on-disk definitions to
64 HFSBTreesPriv.h
65 <HFS4> 1/27/97 djb InsertTree and DeleteTree are now recursive and support variable
66 sized index keys.
67 <HFS3> 1/15/97 djb Move GetFileRefNumFromFCB macro to FilesInternal.h. Added
68 kBTVariableIndexKeysMask.
69 <HFS2> 1/3/97 djb Added support for large keys.
70 <HFS1> 12/19/96 djb first checked in
71
72 History applicable to original Scarecrow Design:
73
74 <7> 10/25/96 ser Changing for new VFPI
75 <6> 10/18/96 ser Converting over VFPI changes
76 <5> 9/17/96 dkh More BTree statistics
77 <4> 9/16/96 dkh Revised BTree statistics
78 <3> 6/20/96 dkh Radar #1358740. Switch from using Pools to debug MemAllocators.
79 <2> 12/7/95 dkh D10E2 build. Changed usage of Ref data type to LogicalAddress.
80 <1> 10/18/95 rst Moved from Scarecrow project.
81
82 <19> 11/22/94 djb Add prototype for GetMapNode
83 <18> 11/16/94 prp Add IsItAHint routine prototype.
84 <17> 9/30/94 prp Get in sync with D2 interface changes.
85 <16> 7/25/94 wjk Eliminate usage of BytePtr in favor of UInt8 *.
86 <15> 7/22/94 wjk Convert to the new set of header files.
87 <14> 5/31/94 srs Moved Btree types to public interface
88 <13> 12/9/93 wjk Add 68k alignment pragma's around persistent structures.
89 <12> 11/30/93 wjk Move from Makefiles to BuildFiles. Fit into the ModernOS and
90 NRCmds environments.
91 <11> 11/23/93 wjk Changes required to compile on the RS6000.
92 <10> 8/30/93 CH Removed the M_ExitOnError and M_ReturnErrorIf macros which were
93 already defined in FileSystemPriv.h (included here).
94 <9> 8/30/93 CH Added parens around the M_ReturnErrorIf macro.
95 <8> 5/21/93 gs Add kBadClose flag. Add some prototypes for internal routines.
96 <7> 5/10/93 gs Change Ptr to BytePtr. Move BTreeTypes to BTree.h. Add
97 DeleteTree prototype.
98 <6> 3/23/93 gs Remove mysterious "flags" field from HeaderRec structure. Move
99 prototypes of private functions to top of respective source
100 files.
101 <5> 2/8/93 gs Update to use FSAgent.h Get/Release/SetEOF/SetBlockSize
102 procPtrs. Add UpdateNode routine.
103 <4> 12/10/92 gs Add Key Descriptor function declarations.
104 <3> 12/8/92 gs Add HeaderRec structure and incorporate review feedback.
105 <2> 12/2/92 gs Add GetNode and ReleaseNode callback procptrs to BTree CB, and
106 add internal function declarations.
107 <1> 11/15/92 gs first checked in
108
109 */
110
111 #ifndef __BTREESPRIVATE__
112 #define __BTREESPRIVATE__
113
114 #include <sys/appleapiopts.h>
115
116 #ifdef KERNEL
117 #ifdef __APPLE_API_PRIVATE
118
119 #include "../../hfs_macos_defs.h"
120
121 #ifndef __FILEMGRINTERNAL__
122 #include "FileMgrInternal.h"
123 #endif
124
125 #ifndef __BTREESINTERNAL__
126 #include "BTreesInternal.h"
127 #endif
128
129
130 /////////////////////////////////// Constants ///////////////////////////////////
131
132 #define kBTreeVersion 1
133 #define kMaxTreeDepth 16
134
135
136 #define kHeaderNodeNum 0
137 #define kKeyDescRecord 1
138
139
140 // Header Node Record Offsets
141 enum {
142 kHeaderRecOffset = 0x000E,
143 kKeyDescRecOffset = 0x0078,
144 kHeaderMapRecOffset = 0x00F8
145 };
146
147 #define kMinNodeSize 512
148
149 #define kMinRecordSize 6
150 // where is minimum record size enforced?
151
152 // miscellaneous BTree constants
153 enum {
154 kOffsetSize = 2
155 };
156
157 // Insert Operations
158 typedef enum {
159 kInsertRecord = 0,
160 kReplaceRecord = 1
161 } InsertType;
162
163 // illegal string attribute bits set in mask
164 #define kBadStrAttribMask 0xCF
165
166
167
168 //////////////////////////////////// Macros /////////////////////////////////////
169
170 #define M_NodesInMap(mapSize) ((mapSize) << 3)
171
172 #define M_ClearBitNum(integer,bitNumber) ((integer) &= (~(1<<(bitNumber))))
173 #define M_SetBitNum(integer,bitNumber) ((integer) |= (1<<(bitNumber)))
174 #define M_IsOdd(integer) (((integer) & 1) != 0)
175 #define M_IsEven(integer) (((integer) & 1) == 0)
176 #define M_BTreeHeaderDirty(btreePtr) btreePtr->flags |= kBTHeaderDirty
177
178 #define M_MapRecordSize(nodeSize) (nodeSize - sizeof (BTNodeDescriptor) - 6)
179 #define M_HeaderMapRecordSize(nodeSize) (nodeSize - sizeof(BTNodeDescriptor) - sizeof(BTHeaderRec) - 128 - 8)
180
181 #define M_SWAP_BE16_ClearBitNum(integer,bitNumber) ((integer) &= SWAP_BE16(~(1<<(bitNumber))))
182 #define M_SWAP_BE16_SetBitNum(integer,bitNumber) ((integer) |= SWAP_BE16(1<<(bitNumber)))
183
184 ///////////////////////////////////// Types /////////////////////////////////////
185
186 typedef struct BTreeControlBlock { // fields specific to BTree CBs
187
188 UInt8 reserved1; // keep for alignment with old style fields
189 UInt8 btreeType;
190 UInt16 treeDepth;
191 FileReference fileRefNum; // refNum of btree file
192 KeyCompareProcPtr keyCompareProc;
193 UInt32 rootNode;
194 UInt32 leafRecords;
195 UInt32 firstLeafNode;
196 UInt32 lastLeafNode;
197 UInt16 nodeSize;
198 UInt16 maxKeyLength;
199 UInt32 totalNodes;
200 UInt32 freeNodes;
201
202 UInt16 reserved3; // 4-byte alignment
203
204 // new fields
205 SInt16 version;
206 UInt32 flags; // dynamic flags
207 UInt32 attributes; // persistent flags
208 UInt32 writeCount;
209 UInt32 lastfsync; /* Last time that this was fsynced */
210
211 GetBlockProcPtr getBlockProc;
212 ReleaseBlockProcPtr releaseBlockProc;
213 SetEndOfForkProcPtr setEndOfForkProc;
214
215 // statistical information
216 UInt32 numGetNodes;
217 UInt32 numGetNewNodes;
218 UInt32 numReleaseNodes;
219 UInt32 numUpdateNodes;
220 UInt32 numMapNodesRead; // map nodes beyond header node
221 UInt32 numHintChecks;
222 UInt32 numPossibleHints; // Looks like a formated hint
223 UInt32 numValidHints; // Hint used to find correct record.
224
225 } BTreeControlBlock, *BTreeControlBlockPtr;
226
227
228 UInt32 CalcKeySize(const BTreeControlBlock *btcb, const BTreeKey *key);
229 #define CalcKeySize(btcb, key) ( ((btcb)->attributes & kBTBigKeysMask) ? ((key)->length16 + 2) : ((key)->length8 + 1) )
230
231 UInt32 KeyLength(const BTreeControlBlock *btcb, const BTreeKey *key);
232 #define KeyLength(btcb, key) ( ((btcb)->attributes & kBTBigKeysMask) ? (key)->length16 : (key)->length8 )
233
234
235
236 typedef enum {
237 kBTHeaderDirty = 0x00000001
238 } BTreeFlags;
239
240
241 typedef SInt8 *NodeBuffer;
242 typedef BlockDescriptor NodeRec, *NodePtr; //\80\80 remove this someday...
243
244
245
246
247 //// Tree Path Table - constructed by SearchTree, used by InsertTree and DeleteTree
248
249 typedef struct {
250 UInt32 node; // node number
251 UInt16 index;
252 UInt16 reserved; // align size to a power of 2
253 } TreePathRecord, *TreePathRecordPtr;
254
255 typedef TreePathRecord TreePathTable [kMaxTreeDepth];
256
257
258 //// InsertKey - used by InsertTree, InsertLevel and InsertNode
259
260 struct InsertKey {
261 BTreeKeyPtr keyPtr;
262 UInt8 * recPtr;
263 UInt16 keyLength;
264 UInt16 recSize;
265 Boolean replacingKey;
266 Boolean skipRotate;
267 };
268
269 typedef struct InsertKey InsertKey;
270
271
272 //// For Notational Convenience
273
274 typedef BTNodeDescriptor* NodeDescPtr;
275 typedef UInt8 *RecordPtr;
276 typedef BTreeKeyPtr KeyPtr;
277
278
279 //////////////////////////////////// Globals ////////////////////////////////////
280
281
282 //////////////////////////////////// Macros /////////////////////////////////////
283
284 #if DEBUG_BUILD
285 #define Panic( message ) DebugStr( (ConstStr255Param) message )
286 #define PanicIf( condition, message ) if ( condition != 0 ) DebugStr( message )
287 #else
288 #define Panic( message )
289 #define PanicIf( condition, message )
290 #endif
291
292 // Exit function on error
293 #define M_ExitOnError( result ) if ( ( result ) != noErr ) goto ErrorExit; else ;
294
295 // Test for passed condition and return if true
296 #define M_ReturnErrorIf( condition, error ) if ( condition ) return( error )
297
298 //////////////////////////////// Key Operations /////////////////////////////////
299
300 SInt32 CompareKeys (BTreeControlBlockPtr btreePtr,
301 KeyPtr searchKey,
302 KeyPtr trialKey );
303
304 //////////////////////////////// Map Operations /////////////////////////////////
305
306 OSStatus AllocateNode (BTreeControlBlockPtr btreePtr,
307 UInt32 *nodeNum);
308
309 OSStatus FreeNode (BTreeControlBlockPtr btreePtr,
310 UInt32 nodeNum);
311
312 OSStatus ExtendBTree (BTreeControlBlockPtr btreePtr,
313 UInt32 nodes );
314
315 UInt32 CalcMapBits (BTreeControlBlockPtr btreePtr);
316
317
318 //////////////////////////////// Misc Operations ////////////////////////////////
319
320 UInt16 CalcKeyRecordSize (UInt16 keySize,
321 UInt16 recSize );
322
323 OSStatus VerifyHeader (FCB *filePtr,
324 BTHeaderRec *header );
325
326 OSStatus UpdateHeader (BTreeControlBlockPtr btreePtr,
327 Boolean forceWrite );
328
329 OSStatus FindIteratorPosition (BTreeControlBlockPtr btreePtr,
330 BTreeIteratorPtr iterator,
331 BlockDescriptor *left,
332 BlockDescriptor *middle,
333 BlockDescriptor *right,
334 UInt32 *nodeNum,
335 UInt16 *index,
336 Boolean *foundRecord );
337
338 OSStatus CheckInsertParams (FCB *filePtr,
339 BTreeIterator *iterator,
340 FSBufferDescriptor *record,
341 UInt16 recordLen );
342
343 OSStatus TrySimpleReplace (BTreeControlBlockPtr btreePtr,
344 NodeDescPtr nodePtr,
345 BTreeIterator *iterator,
346 FSBufferDescriptor *record,
347 UInt16 recordLen,
348 Boolean *recordInserted );
349
350 OSStatus IsItAHint (BTreeControlBlockPtr btreePtr,
351 BTreeIterator *iterator,
352 Boolean *answer );
353
354 //////////////////////////////// Node Operations ////////////////////////////////
355
356 //// Node Operations
357
358 OSStatus GetNode (BTreeControlBlockPtr btreePtr,
359 UInt32 nodeNum,
360 NodeRec *returnNodePtr );
361
362 OSStatus GetLeftSiblingNode (BTreeControlBlockPtr btreePtr,
363 NodeDescPtr node,
364 NodeRec *left );
365
366 #define GetLeftSiblingNode(btree,node,left) GetNode ((btree), ((NodeDescPtr)(node))->bLink, (left))
367
368 OSStatus GetRightSiblingNode (BTreeControlBlockPtr btreePtr,
369 NodeDescPtr node,
370 NodeRec *right );
371
372 #define GetRightSiblingNode(btree,node,right) GetNode ((btree), ((NodeDescPtr)(node))->fLink, (right))
373
374
375 OSStatus GetNewNode (BTreeControlBlockPtr btreePtr,
376 UInt32 nodeNum,
377 NodeRec *returnNodePtr );
378
379 OSStatus ReleaseNode (BTreeControlBlockPtr btreePtr,
380 NodePtr nodePtr );
381
382 OSStatus TrashNode (BTreeControlBlockPtr btreePtr,
383 NodePtr nodePtr );
384
385 // XXXdbg
386 void ModifyBlockStart(FileReference vp, BlockDescPtr blockPtr);
387 // XXXdbg
388
389 OSStatus UpdateNode (BTreeControlBlockPtr btreePtr,
390 NodePtr nodePtr,
391 UInt32 transactionID,
392 UInt32 flags );
393
394 OSStatus GetMapNode (BTreeControlBlockPtr btreePtr,
395 BlockDescriptor *nodePtr,
396 UInt16 **mapPtr,
397 UInt16 *mapSize );
398
399 //// Node Buffer Operations
400
401 OSStatus CheckNode (BTreeControlBlockPtr btreePtr,
402 NodeDescPtr node );
403
404 void ClearNode (BTreeControlBlockPtr btreePtr,
405 NodeDescPtr node );
406
407 UInt16 GetNodeDataSize (BTreeControlBlockPtr btreePtr,
408 NodeDescPtr node );
409
410 UInt16 GetNodeFreeSize (BTreeControlBlockPtr btreePtr,
411 NodeDescPtr node );
412
413
414 //// Record Operations
415
416 Boolean InsertRecord (BTreeControlBlockPtr btreePtr,
417 NodeDescPtr node,
418 UInt16 index,
419 RecordPtr recPtr,
420 UInt16 recSize );
421
422 Boolean InsertKeyRecord (BTreeControlBlockPtr btreePtr,
423 NodeDescPtr node,
424 UInt16 index,
425 KeyPtr keyPtr,
426 UInt16 keyLength,
427 RecordPtr recPtr,
428 UInt16 recSize );
429
430 void DeleteRecord (BTreeControlBlockPtr btree,
431 NodeDescPtr node,
432 UInt16 index );
433
434
435 Boolean SearchNode (BTreeControlBlockPtr btree,
436 NodeDescPtr node,
437 KeyPtr searchKey,
438 UInt16 *index );
439
440 OSStatus GetRecordByIndex (BTreeControlBlockPtr btree,
441 NodeDescPtr node,
442 UInt16 index,
443 KeyPtr *keyPtr,
444 UInt8 * *dataPtr,
445 UInt16 *dataSize );
446
447 UInt8 * GetRecordAddress (BTreeControlBlockPtr btree,
448 NodeDescPtr node,
449 UInt16 index );
450
451 #define GetRecordAddress(btreePtr,node,index) ((UInt8 *)(node) + (*(short *) ((UInt8 *)(node) + (btreePtr)->nodeSize - ((index) << 1) - kOffsetSize)))
452
453
454 UInt16 GetRecordSize (BTreeControlBlockPtr btree,
455 NodeDescPtr node,
456 UInt16 index );
457
458 UInt32 GetChildNodeNum (BTreeControlBlockPtr btreePtr,
459 NodeDescPtr nodePtr,
460 UInt16 index );
461
462 void MoveRecordsLeft (UInt8 * src,
463 UInt8 * dst,
464 UInt16 bytesToMove );
465
466 #define MoveRecordsLeft(src,dst,bytes) bcopy((src),(dst),(bytes))
467
468 void MoveRecordsRight (UInt8 * src,
469 UInt8 * dst,
470 UInt16 bytesToMove );
471
472 #define MoveRecordsRight(src,dst,bytes) bcopy((src),(dst),(bytes))
473
474
475 //////////////////////////////// Tree Operations ////////////////////////////////
476
477 OSStatus SearchTree (BTreeControlBlockPtr btreePtr,
478 BTreeKeyPtr keyPtr,
479 TreePathTable treePathTable,
480 UInt32 *nodeNum,
481 BlockDescriptor *nodePtr,
482 UInt16 *index );
483
484 OSStatus InsertTree (BTreeControlBlockPtr btreePtr,
485 TreePathTable treePathTable,
486 KeyPtr keyPtr,
487 UInt8 * recPtr,
488 UInt16 recSize,
489 BlockDescriptor *targetNode,
490 UInt16 index,
491 UInt16 level,
492 Boolean replacingKey,
493 UInt32 *insertNode );
494
495 OSStatus DeleteTree (BTreeControlBlockPtr btreePtr,
496 TreePathTable treePathTable,
497 BlockDescriptor *targetNode,
498 UInt16 index,
499 UInt16 level );
500
501 #endif /* __APPLE_API_PRIVATE */
502 #endif /* KERNEL */
503 #endif //__BTREESPRIVATE__