2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 File: BTreesInternal.h
33 Contains: IPI to File Manager B-tree
37 Copyright: © 1996-1998 by Apple Computer, Inc., all rights reserved.
43 Other Contact: Mark Day
45 Technology: File Systems
53 Change History (most recent first):
55 <RHAP> 9/22/99 ser Added prototypes for BTGetLastSync and BTSetLastSync
56 <RHAP> 6/22/98 djb Add ERR_BASE to btree error codes to make them negative (for MacOS X only).
58 <CS7> 7/28/97 msd Add enum for fsBTTimeOutErr.
59 <CS6> 7/25/97 DSH Added heuristicHint as parameter to BTSearchRecord.
60 <CS5> 7/24/97 djb Add blockReadFromDisk flag to BlockDescriptor. Callbacks now use
61 a file refNum instead of an FCB.
62 <CS4> 7/16/97 DSH FilesInternal.i renamed FileMgrInternal.i to avoid name
64 <CS3> 6/2/97 DSH Added SetEndOfForkProc() prototype, so Attributes.c can call it
66 <CS2> 5/19/97 djb kMaxKeyLength is now 520.
67 <CS1> 4/28/97 djb first checked in
69 <HFS6> 3/17/97 DSH Remove Key Comparison prototype, already in FilesInternal.h.
70 <HFS5> 2/19/97 djb Add SetBlockSizeProcPtr. Add blockSize field to BlockDescriptor.
71 Remove E_ type error enums.
72 <HFS4> 1/27/97 djb Include Types.h and FilesInternal.h.
73 <HFS3> 1/13/97 djb Added kBTreeCurrentRecord for BTIterateRecord.
74 <HFS2> 1/3/97 djb Added support for large keys.
75 <HFS1> 12/19/96 djb first checked in
79 #ifndef __BTREESINTERNAL__
80 #define __BTREESINTERNAL__
82 #include <sys/appleapiopts.h>
85 #ifdef __APPLE_API_PRIVATE
87 #ifndef __FILEMGRINTERNAL__
88 #include "FileMgrInternal.h"
92 fsBTInvalidHeaderErr
= btBadHdr
,
93 fsBTBadRotateErr
= dsBadRotate
,
94 fsBTInvalidNodeErr
= btBadNode
,
95 fsBTRecordTooLargeErr
= btNoFit
,
96 fsBTRecordNotFoundErr
= btNotFound
,
97 fsBTDuplicateRecordErr
= btExists
,
98 fsBTFullErr
= btNoSpaceAvail
,
100 fsBTInvalidFileErr
= ERR_BASE
+ 0x0302, /* no BTreeCB has been allocated for fork*/
101 fsBTrFileAlreadyOpenErr
= ERR_BASE
+ 0x0303,
102 fsBTInvalidIteratorErr
= ERR_BASE
+ 0x0308,
103 fsBTEmptyErr
= ERR_BASE
+ 0x030A,
104 fsBTNoMoreMapNodesErr
= ERR_BASE
+ 0x030B,
105 fsBTBadNodeSize
= ERR_BASE
+ 0x030C,
106 fsBTBadNodeType
= ERR_BASE
+ 0x030D,
107 fsBTInvalidKeyLengthErr
= ERR_BASE
+ 0x030E,
108 fsBTStartOfIterationErr
= ERR_BASE
+ 0x0353,
109 fsBTEndOfIterationErr
= ERR_BASE
+ 0x0354,
110 fsBTUnknownVersionErr
= ERR_BASE
+ 0x0355,
111 fsBTTreeTooDeepErr
= ERR_BASE
+ 0x0357,
112 fsIteratorExitedScopeErr
= ERR_BASE
+ 0x0A02, /* iterator exited the scope*/
113 fsIteratorScopeExceptionErr
= ERR_BASE
+ 0x0A03, /* iterator is undefined due to error or movement of scope locality*/
114 fsUnknownIteratorMovementErr
= ERR_BASE
+ 0x0A04, /* iterator movement is not defined*/
115 fsInvalidIterationMovmentErr
= ERR_BASE
+ 0x0A05, /* iterator movement is invalid in current context*/
116 fsClientIDMismatchErr
= ERR_BASE
+ 0x0A06, /* wrong client process ID*/
117 fsEndOfIterationErr
= ERR_BASE
+ 0x0A07, /* there were no objects left to return on iteration*/
118 fsBTTimeOutErr
= ERR_BASE
+ 0x0A08 /* BTree scan interrupted -- no time left for physical I/O */
121 struct BlockDescriptor
{
124 daddr64_t blockNum
; /* logical block number (used by hfs_swap_BTNode) */
126 Boolean blockReadFromDisk
;
127 Byte isModified
; // XXXdbg - for journaling
130 typedef struct BlockDescriptor BlockDescriptor
;
131 typedef BlockDescriptor
*BlockDescPtr
;
134 struct FSBufferDescriptor
{
135 void * bufferAddress
;
139 typedef struct FSBufferDescriptor FSBufferDescriptor
;
141 typedef FSBufferDescriptor
*FSBufferDescriptorPtr
;
145 Fork Level Access Method Block get options
148 kGetBlock
= 0x00000000,
149 kForceReadBlock
= 0x00000002, //\80\80 how does this relate to Read/Verify? Do we need this?
150 kGetEmptyBlock
= 0x00000008
152 typedef OptionBits GetBlockOptions
;
155 Fork Level Access Method Block release options
158 kReleaseBlock
= 0x00000000,
159 kForceWriteBlock
= 0x00000001,
160 kMarkBlockDirty
= 0x00000002,
161 kTrashBlock
= 0x00000004,
162 kLockTransaction
= 0x00000100
164 typedef OptionBits ReleaseBlockOptions
;
166 typedef UInt64 FSSize
;
167 typedef UInt32 ForkBlockNumber
;
169 /*============================================================================
170 Fork Level Buffered I/O Access Method
171 ============================================================================*/
173 typedef OSStatus (* GetBlockProcPtr
) (FileReference fileRefNum
,
175 GetBlockOptions options
,
176 BlockDescriptor
*block
);
179 typedef OSStatus (* ReleaseBlockProcPtr
) (FileReference fileRefNum
,
180 BlockDescPtr blockPtr
,
181 ReleaseBlockOptions options
);
183 typedef OSStatus (* SetEndOfForkProcPtr
) (FileReference fileRefNum
,
187 typedef OSStatus (* SetBlockSizeProcPtr
) (FileReference fileRefNum
,
189 ItemCount minBlockCount
);
191 OSStatus
SetEndOfForkProc ( FileReference fileRefNum
, FSSize minEOF
, FSSize maxEOF
);
195 B*Tree Information Version
198 enum BTreeInformationVersion
{
199 kBTreeInfoVersion
= 0
203 B*Tree Iteration Operation Constants
206 enum BTreeIterationOperations
{
213 typedef UInt16 BTreeIterationOperation
;
217 Btree types: 0 is HFS CAT/EXT file, 1~127 are AppleShare B*Tree files, 128~254 unused
218 hfsBtreeType EQU 0 ; control file
219 validBTType EQU $80 ; user btree type starts from 128
220 userBT1Type EQU $FF ; 255 is our Btree type. Used by BTInit and BTPatch
224 kHFSBTreeType
= 0, // control file
225 kUserBTreeType
= 128, // user btree type starts from 128
226 kReservedBTreeType
= 255 //
229 #define kBTreeHeaderUserBytes 128
232 typedef BTreeKey
*BTreeKeyPtr
;
236 BTreeInfoRec Structure - for BTGetInformation
243 UInt32 lastfsync
; /* Last time that this was fsynced */
244 ItemCount numRecords
;
246 ItemCount numFreeNodes
;
247 UInt8 keyCompareType
;
250 typedef struct BTreeInfoRec BTreeInfoRec
;
251 typedef BTreeInfoRec
*BTreeInfoPtr
;
254 BTreeHint can never be exported to the outside. Use UInt32 BTreeHint[4],
255 UInt8 BTreeHint[16], etc.
258 ItemCount writeCount
;
259 UInt32 nodeNum
; // node the key was last seen in
260 UInt16 index
; // index then key was last seen at
264 typedef struct BTreeHint BTreeHint
;
265 typedef BTreeHint
*BTreeHintPtr
;
270 struct BTreeIterator
{
274 UInt32 hitCount
; // Total number of leaf records hit
275 UInt32 maxLeafRecs
; // Max leaf records over iteration
278 typedef struct BTreeIterator BTreeIterator
;
279 typedef BTreeIterator
*BTreeIteratorPtr
;
282 /*============================================================================
284 ============================================================================*/
287 Key Comparison Function ProcPtr Type - for BTOpenPath
289 //typedef SInt32 (* KeyCompareProcPtr)(BTreeKeyPtr a, BTreeKeyPtr b);
292 typedef SInt32 (* IterateCallBackProcPtr
)(BTreeKeyPtr key
, void * record
, void * state
);
295 extern OSStatus
BTOpenPath(FCB
*filePtr
, KeyCompareProcPtr keyCompareProc
);
297 extern OSStatus
BTClosePath (FCB
*filePtr
);
300 extern OSStatus
BTSearchRecord (FCB
*filePtr
,
301 BTreeIterator
*searchIterator
,
302 FSBufferDescriptor
*btRecord
,
304 BTreeIterator
*resultIterator
);
306 extern OSStatus
BTIterateRecord (FCB
*filePtr
,
307 BTreeIterationOperation operation
,
308 BTreeIterator
*iterator
,
309 FSBufferDescriptor
*btRecord
,
313 extern OSStatus
BTIterateRecords(FCB
*filePtr
, BTreeIterationOperation operation
, BTreeIterator
*iterator
,
314 IterateCallBackProcPtr callBackProc
, void * callBackState
);
316 extern OSStatus
BTInsertRecord (FCB
*filePtr
,
317 BTreeIterator
*iterator
,
318 FSBufferDescriptor
*btrecord
,
321 extern OSStatus
BTReplaceRecord (FCB
*filePtr
,
322 BTreeIterator
*iterator
,
323 FSBufferDescriptor
*btRecord
,
326 extern OSStatus
BTUpdateRecord (FCB
*filePtr
,
327 BTreeIterator
*iterator
,
328 IterateCallBackProcPtr callBackProc
,
329 void *callBackState
);
331 extern OSStatus
BTDeleteRecord (FCB
*filePtr
,
332 BTreeIterator
*iterator
);
334 extern OSStatus
BTGetInformation (FCB
*filePtr
,
336 BTreeInfoRec
*info
);
338 extern OSStatus
BTFlushPath (FCB
*filePtr
);
340 extern OSStatus
BTReloadData (FCB
*filePtr
);
342 extern OSStatus
BTInvalidateHint (BTreeIterator
*iterator
);
344 extern OSStatus
BTGetLastSync (FCB
*filePtr
,
347 extern OSStatus
BTSetLastSync (FCB
*filePtr
,
350 extern OSStatus
BTHasContiguousNodes(FCB
*filePtr
);
352 extern OSStatus
BTGetUserData(FCB
*filePtr
, void * dataPtr
, int dataSize
);
354 extern OSStatus
BTSetUserData(FCB
*filePtr
, void * dataPtr
, int dataSize
);
356 /* B-tree node reserve routines. */
357 extern void BTReserveSetup(void);
359 extern int BTReserveSpace(FCB
*file
, int operations
, void * data
);
361 extern int BTReleaseReserve(FCB
*file
, void * data
);
364 #endif /* __APPLE_API_PRIVATE */
366 #endif // __BTREESINTERNAL__