2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 File: BTreesInternal.h
25 Contains: IPI to File Manager B-tree
29 Copyright: © 1996-1998 by Apple Computer, Inc., all rights reserved.
35 Other Contact: Mark Day
37 Technology: File Systems
45 Change History (most recent first):
47 <RHAP> 9/22/99 ser Added prototypes for BTGetLastSync and BTSetLastSync
48 <RHAP> 6/22/98 djb Add ERR_BASE to btree error codes to make them negative (for MacOS X only).
50 <CS7> 7/28/97 msd Add enum for fsBTTimeOutErr.
51 <CS6> 7/25/97 DSH Added heuristicHint as parameter to BTSearchRecord.
52 <CS5> 7/24/97 djb Add blockReadFromDisk flag to BlockDescriptor. Callbacks now use
53 a file refNum instead of an FCB.
54 <CS4> 7/16/97 DSH FilesInternal.i renamed FileMgrInternal.i to avoid name
56 <CS3> 6/2/97 DSH Added SetEndOfForkProc() prototype, so Attributes.c can call it
58 <CS2> 5/19/97 djb kMaxKeyLength is now 520.
59 <CS1> 4/28/97 djb first checked in
61 <HFS6> 3/17/97 DSH Remove Key Comparison prototype, already in FilesInternal.h.
62 <HFS5> 2/19/97 djb Add SetBlockSizeProcPtr. Add blockSize field to BlockDescriptor.
63 Remove E_ type error enums.
64 <HFS4> 1/27/97 djb Include Types.h and FilesInternal.h.
65 <HFS3> 1/13/97 djb Added kBTreeCurrentRecord for BTIterateRecord.
66 <HFS2> 1/3/97 djb Added support for large keys.
67 <HFS1> 12/19/96 djb first checked in
71 #ifndef __BTREESINTERNAL__
72 #define __BTREESINTERNAL__
74 #include <sys/appleapiopts.h>
77 #ifdef __APPLE_API_PRIVATE
79 #ifndef __FILEMGRINTERNAL__
80 #include "FileMgrInternal.h"
84 fsBTInvalidHeaderErr
= btBadHdr
,
85 fsBTBadRotateErr
= dsBadRotate
,
86 fsBTInvalidNodeErr
= btBadNode
,
87 fsBTRecordTooLargeErr
= btNoFit
,
88 fsBTRecordNotFoundErr
= btNotFound
,
89 fsBTDuplicateRecordErr
= btExists
,
90 fsBTFullErr
= btNoSpaceAvail
,
92 fsBTInvalidFileErr
= ERR_BASE
+ 0x0302, /* no BTreeCB has been allocated for fork*/
93 fsBTrFileAlreadyOpenErr
= ERR_BASE
+ 0x0303,
94 fsBTInvalidIteratorErr
= ERR_BASE
+ 0x0308,
95 fsBTEmptyErr
= ERR_BASE
+ 0x030A,
96 fsBTNoMoreMapNodesErr
= ERR_BASE
+ 0x030B,
97 fsBTBadNodeSize
= ERR_BASE
+ 0x030C,
98 fsBTBadNodeType
= ERR_BASE
+ 0x030D,
99 fsBTInvalidKeyLengthErr
= ERR_BASE
+ 0x030E,
100 fsBTStartOfIterationErr
= ERR_BASE
+ 0x0353,
101 fsBTEndOfIterationErr
= ERR_BASE
+ 0x0354,
102 fsBTUnknownVersionErr
= ERR_BASE
+ 0x0355,
103 fsBTTreeTooDeepErr
= ERR_BASE
+ 0x0357,
104 fsIteratorExitedScopeErr
= ERR_BASE
+ 0x0A02, /* iterator exited the scope*/
105 fsIteratorScopeExceptionErr
= ERR_BASE
+ 0x0A03, /* iterator is undefined due to error or movement of scope locality*/
106 fsUnknownIteratorMovementErr
= ERR_BASE
+ 0x0A04, /* iterator movement is not defined*/
107 fsInvalidIterationMovmentErr
= ERR_BASE
+ 0x0A05, /* iterator movement is invalid in current context*/
108 fsClientIDMismatchErr
= ERR_BASE
+ 0x0A06, /* wrong client process ID*/
109 fsEndOfIterationErr
= ERR_BASE
+ 0x0A07, /* there were no objects left to return on iteration*/
110 fsBTTimeOutErr
= ERR_BASE
+ 0x0A08 /* BTree scan interrupted -- no time left for physical I/O */
113 struct BlockDescriptor
{
117 Boolean blockReadFromDisk
;
118 Byte isModified
; // XXXdbg - for journaling
121 typedef struct BlockDescriptor BlockDescriptor
;
122 typedef BlockDescriptor
*BlockDescPtr
;
125 struct FSBufferDescriptor
{
126 void * bufferAddress
;
130 typedef struct FSBufferDescriptor FSBufferDescriptor
;
132 typedef FSBufferDescriptor
*FSBufferDescriptorPtr
;
136 Fork Level Access Method Block get options
139 kGetBlock
= 0x00000000,
140 kForceReadBlock
= 0x00000002, //\80\80 how does this relate to Read/Verify? Do we need this?
141 kGetEmptyBlock
= 0x00000008
143 typedef OptionBits GetBlockOptions
;
146 Fork Level Access Method Block release options
149 kReleaseBlock
= 0x00000000,
150 kForceWriteBlock
= 0x00000001,
151 kMarkBlockDirty
= 0x00000002,
152 kTrashBlock
= 0x00000004,
153 kLockTransaction
= 0x00000100
155 typedef OptionBits ReleaseBlockOptions
;
157 typedef UInt64 FSSize
;
158 typedef UInt32 ForkBlockNumber
;
160 /*============================================================================
161 Fork Level Buffered I/O Access Method
162 ============================================================================*/
164 typedef OSStatus (* GetBlockProcPtr
) (FileReference fileRefNum
,
166 GetBlockOptions options
,
167 BlockDescriptor
*block
);
170 typedef OSStatus (* ReleaseBlockProcPtr
) (FileReference fileRefNum
,
171 BlockDescPtr blockPtr
,
172 ReleaseBlockOptions options
);
174 typedef OSStatus (* SetEndOfForkProcPtr
) (FileReference fileRefNum
,
178 typedef OSStatus (* SetBlockSizeProcPtr
) (FileReference fileRefNum
,
180 ItemCount minBlockCount
);
182 OSStatus
SetEndOfForkProc ( FileReference fileRefNum
, FSSize minEOF
, FSSize maxEOF
);
186 B*Tree Information Version
189 enum BTreeInformationVersion
{
190 kBTreeInfoVersion
= 0
194 B*Tree Iteration Operation Constants
197 enum BTreeIterationOperations
{
204 typedef UInt16 BTreeIterationOperation
;
208 Btree types: 0 is HFS CAT/EXT file, 1~127 are AppleShare B*Tree files, 128~254 unused
209 hfsBtreeType EQU 0 ; control file
210 validBTType EQU $80 ; user btree type starts from 128
211 userBT1Type EQU $FF ; 255 is our Btree type. Used by BTInit and BTPatch
215 kHFSBTreeType
= 0, // control file
216 kUserBTreeType
= 128, // user btree type starts from 128
217 kReservedBTreeType
= 255 //
220 #define kBTreeHeaderUserBytes 128
223 typedef BTreeKey
*BTreeKeyPtr
;
227 BTreeInfoRec Structure - for BTGetInformation
234 UInt32 lastfsync
; /* Last time that this was fsynced */
235 ItemCount numRecords
;
237 ItemCount numFreeNodes
;
238 UInt8 keyCompareType
;
241 typedef struct BTreeInfoRec BTreeInfoRec
;
242 typedef BTreeInfoRec
*BTreeInfoPtr
;
245 BTreeHint can never be exported to the outside. Use UInt32 BTreeHint[4],
246 UInt8 BTreeHint[16], etc.
249 ItemCount writeCount
;
250 UInt32 nodeNum
; // node the key was last seen in
251 UInt16 index
; // index then key was last seen at
255 typedef struct BTreeHint BTreeHint
;
256 typedef BTreeHint
*BTreeHintPtr
;
261 struct BTreeIterator
{
265 UInt32 hitCount
; // Total number of leaf records hit
266 UInt32 maxLeafRecs
; // Max leaf records over iteration
269 typedef struct BTreeIterator BTreeIterator
;
270 typedef BTreeIterator
*BTreeIteratorPtr
;
273 /*============================================================================
275 ============================================================================*/
278 Key Comparison Function ProcPtr Type - for BTOpenPath
280 //typedef SInt32 (* KeyCompareProcPtr)(BTreeKeyPtr a, BTreeKeyPtr b);
283 typedef SInt32 (* IterateCallBackProcPtr
)(BTreeKeyPtr key
, void * record
, void * state
);
286 extern OSStatus
BTOpenPath(FCB
*filePtr
, KeyCompareProcPtr keyCompareProc
);
288 extern OSStatus
BTClosePath (FCB
*filePtr
);
291 extern OSStatus
BTSearchRecord (FCB
*filePtr
,
292 BTreeIterator
*searchIterator
,
293 FSBufferDescriptor
*btRecord
,
295 BTreeIterator
*resultIterator
);
297 extern OSStatus
BTIterateRecord (FCB
*filePtr
,
298 BTreeIterationOperation operation
,
299 BTreeIterator
*iterator
,
300 FSBufferDescriptor
*btRecord
,
304 extern OSStatus
BTIterateRecords(FCB
*filePtr
, BTreeIterationOperation operation
, BTreeIterator
*iterator
,
305 IterateCallBackProcPtr callBackProc
, void * callBackState
);
307 extern OSStatus
BTInsertRecord (FCB
*filePtr
,
308 BTreeIterator
*iterator
,
309 FSBufferDescriptor
*btrecord
,
312 extern OSStatus
BTReplaceRecord (FCB
*filePtr
,
313 BTreeIterator
*iterator
,
314 FSBufferDescriptor
*btRecord
,
317 extern OSStatus
BTUpdateRecord (FCB
*filePtr
,
318 BTreeIterator
*iterator
,
319 IterateCallBackProcPtr callBackProc
,
320 void *callBackState
);
322 extern OSStatus
BTDeleteRecord (FCB
*filePtr
,
323 BTreeIterator
*iterator
);
325 extern OSStatus
BTGetInformation (FCB
*filePtr
,
327 BTreeInfoRec
*info
);
329 extern OSStatus
BTFlushPath (FCB
*filePtr
);
331 extern OSStatus
BTReloadData (FCB
*filePtr
);
333 extern OSStatus
BTInvalidateHint (BTreeIterator
*iterator
);
335 extern OSStatus
BTGetLastSync (FCB
*filePtr
,
338 extern OSStatus
BTSetLastSync (FCB
*filePtr
,
341 extern OSStatus
BTHasContiguousNodes(FCB
*filePtr
);
343 extern OSStatus
BTGetUserData(FCB
*filePtr
, void * dataPtr
, int dataSize
);
345 extern OSStatus
BTSetUserData(FCB
*filePtr
, void * dataPtr
, int dataSize
);
347 /* B-tree node reserve routines. */
348 extern void BTReserveSetup(void);
350 extern int BTReserveSpace(FCB
*file
, int operations
, void * data
);
352 extern int BTReleaseReserve(FCB
*file
, void * data
);
355 #endif /* __APPLE_API_PRIVATE */
357 #endif // __BTREESINTERNAL__