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