2 * Copyright (c) 2000 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: CatalogIterators.c
25 Contains: Catalog Iterator Implementation
29 Copyright: © 1997-1998 by Apple Computer, Inc., all rights reserved.
35 Other Contact: Mark Day
37 Technology: Mac OS File System
44 Change History (most recent first):
45 <MacOSX> 4/23/98 djb Re-enable InvalidateCatalogCache (was commented out).
46 <MacOSX> 4/6/98 djb Add locking for cache globals (list) and iterators.
47 <MacOSX> 4/2/98 djb Define gCatalogCacheGlobals here instead of FSVars.
48 <MacOSX> 3/31/98 djb Sync up with final HFSVolumes.h header file.
50 <CS3> 11/13/97 djb Radar #1683572 - Fix for indexed GetFileInfo.
51 <CS2> 10/17/97 msd Bug 1683506. Add support for long Unicode names in
52 CatalogIterators. Added a single global buffer for long Unicode
53 names; it is used by at most one CatalogIterator at a time.
54 <CS1> 10/1/97 djb first checked in
58 #include "../../hfs_macos_defs.h"
59 #include "../../hfs.h"
60 #include "../../hfs_dbg.h"
61 #include "../../hfs_format.h"
63 #include "../headers/FileMgrInternal.h"
64 #include "../headers/BTreesInternal.h"
65 #include "../headers/CatalogPrivate.h"
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <libkern/libkern.h>
73 static void InsertCatalogIteratorAsMRU( CatalogCacheGlobals
*cacheGlobals
, CatalogIterator
*iterator
);
75 static void InsertCatalogIteratorAsLRU( CatalogCacheGlobals
*cacheGlobals
, CatalogIterator
*iterator
);
77 static void PrepareForLongName( CatalogIterator
*iterator
);
80 #if TARGET_API_MACOS_X
81 CatalogCacheGlobals
*gCatalogCacheGlobals
;
83 #define GetCatalogCacheGlobals() (gCatalogCacheGlobals)
85 #define CATALOG_ITER_LIST_LOCK(g) simple_lock(&(g)->simplelock)
87 #define CATALOG_ITER_LIST_UNLOCK(g) simple_unlock(&(g)->simplelock)
89 #define CI_LOCK(i) lockmgr(&(i)->iterator_lock, LK_EXCLUSIVE, (simple_lock_t) 0, current_proc())
91 #define CI_UNLOCK(i) lockmgr(&(i)->iterator_lock, LK_RELEASE, (simple_lock_t) 0, current_proc())
93 #define CI_SLEEPLESS_LOCK(i) lockmgr(&(i)->iterator_lock, LK_EXCLUSIVE | LK_NOWAIT, (simple_lock_t) 0, current_proc())
95 #define CI_LOCK_FROM_LIST(g,i) lockmgr(&(i)->iterator_lock, LK_EXCLUSIVE | LK_INTERLOCK, &(g)->simplelock, current_proc())
97 #else /* TARGET_API_MACOS_X */
99 #define GetCatalogCacheGlobals() ((CatalogCacheGlobals*) ((FSVarsRec*) LMGetFSMVars()->gCatalogCacheGlobals))
101 #define CATALOG_ITER_LIST_LOCK(g)
103 #define CATALOG_ITER_LIST_UNLOCK(g)
107 #define CI_UNLOCK(i) 0
109 #define CI_SLEEPLESS_LOCK(i) 0
111 #define CI_LOCK_FROM_LIST(g,i) 0
116 //_______________________________________________________________________________
117 // Routine: InitCatalogCache
119 // Function: Allocates cache, and initializes all the cache structures.
121 //_______________________________________________________________________________
123 InitCatalogCache(void)
125 CatalogCacheGlobals
* cacheGlobals
;
126 CatalogIterator
* iterator
;
133 cacheSize
= sizeof(CatalogCacheGlobals
) + ( kCatalogIteratorCount
* sizeof(CatalogIterator
) );
134 cacheGlobals
= (CatalogCacheGlobals
*) NewPtrSysClear( cacheSize
);
136 cacheGlobals
->iteratorCount
= kCatalogIteratorCount
;
138 lastIterator
= kCatalogIteratorCount
- 1; // last iterator number, since they start at 0
140 // Initialize the MRU order for the cache
141 cacheGlobals
->mru
= (CatalogIterator
*) ( (Ptr
)cacheGlobals
+ sizeof(CatalogCacheGlobals
) );
143 // Initialize the LRU order for the cache
144 cacheGlobals
->lru
= (CatalogIterator
*) ( (Ptr
)(cacheGlobals
->mru
) + (lastIterator
* sizeof(CatalogIterator
)) );
147 // Traverse iterators, setting initial mru, lru, and default values
148 for ( i
= 0, iterator
= cacheGlobals
->mru
; i
< kCatalogIteratorCount
; i
++, iterator
= iterator
->nextMRU
)
150 if ( i
== lastIterator
)
151 iterator
->nextMRU
= nil
; // terminate the list
153 iterator
->nextMRU
= (CatalogIterator
*) ( (Ptr
)iterator
+ sizeof(CatalogIterator
) );
156 iterator
->nextLRU
= nil
; // terminate the list
158 iterator
->nextLRU
= (CatalogIterator
*) ( (Ptr
)iterator
- sizeof(CatalogIterator
) );
160 #if TARGET_API_MACOS_X
161 lockinit(&iterator
->iterator_lock
, PINOD
, "hfs_catalog_iterator", 0, 0);
165 #if TARGET_API_MAC_OS8
166 (FSVarsRec
*) LMGetFSMVars()->gCatalogCacheGlobals
= (Ptr
) cacheGlobals
;
169 #if TARGET_API_MACOS_X
170 gCatalogCacheGlobals
= cacheGlobals
;
171 simple_lock_init(&cacheGlobals
->simplelock
);
178 //_______________________________________________________________________________
179 // Routine: InvalidateCatalogCache
181 // Function: Trash any interators matching volume parameter
183 //_______________________________________________________________________________
184 void PrintCatalogIterator( void );
187 InvalidateCatalogCache( ExtendedVCB
*volume
)
189 TrashCatalogIterator( volume
, 0 );
193 //_______________________________________________________________________________
194 // Routine: PrintCatalogIterator
196 // Function: Prints all interators
198 //_______________________________________________________________________________
201 PrintCatalogIterator( void )
203 CatalogIterator
*iterator
;
204 CatalogCacheGlobals
*cacheGlobals
= GetCatalogCacheGlobals();
207 PRINTIT("CatalogCacheGlobals @ 0x%08lX are:\n", (unsigned long)cacheGlobals
);
208 PRINTIT("\titeratorCount: %ld \n", cacheGlobals
->iteratorCount
);
209 PRINTIT("\tmru: 0x%08lX \n", (unsigned long)cacheGlobals
->mru
);
210 PRINTIT("\tlru: 0x%08lX \n", (unsigned long)cacheGlobals
->lru
);
212 for ( iterator
= cacheGlobals
->mru
, i
=0 ; iterator
!= nil
&& i
<32 ; iterator
= iterator
->nextMRU
, i
++)
215 PRINTIT(" i: 0x%08lX", (unsigned long)iterator
);
216 PRINTIT(" M: 0x%08lX", (unsigned long)iterator
->nextMRU
);
217 PRINTIT(" L: 0x%08lX", (unsigned long)iterator
->nextLRU
);
223 //_______________________________________________________________________________
224 // Routine: TrashCatalogIterator
226 // Function: Trash any interators matching volume and folder parameters
228 //_______________________________________________________________________________
230 TrashCatalogIterator( const ExtendedVCB
*volume
, HFSCatalogNodeID folderID
)
232 CatalogIterator
*iterator
;
233 CatalogCacheGlobals
*cacheGlobals
= GetCatalogCacheGlobals();
235 CATALOG_ITER_LIST_LOCK(cacheGlobals
);
237 for ( iterator
= cacheGlobals
->mru
; iterator
!= nil
; iterator
= iterator
->nextMRU
)
241 // first match the volume
242 if ( iterator
->volume
!= volume
)
245 // now match the folder (or all folders if 0)
246 if ( (folderID
== 0) || (folderID
== iterator
->folderID
) )
248 CatalogIterator
*next
;
250 iterator
->volume
= 0; // trash it
251 iterator
->folderID
= 0;
253 next
= iterator
->nextMRU
; // remember the next iterator
255 // if iterator is not already last then make it last
258 InsertCatalogIteratorAsLRU( cacheGlobals
, iterator
);
260 // iterator->nextMRU will always be zero (since we moved it to the end)
261 // so set up the next iterator manually (we know its not nil)
263 goto top
; // process the next iterator
268 CATALOG_ITER_LIST_UNLOCK(cacheGlobals
);
272 //_______________________________________________________________________________
273 // Routine: AgeCatalogIterator
275 // Function: Move iterator to the end of the list...
277 //_______________________________________________________________________________
279 AgeCatalogIterator ( CatalogIterator
*catalogIterator
)
281 CatalogCacheGlobals
* cacheGlobals
= GetCatalogCacheGlobals();
283 CATALOG_ITER_LIST_LOCK(cacheGlobals
);
285 //PRINTIT(" AgeCatalogIterator: v=%d, d=%ld, i=%d\n", catalogIterator->volRefNum, catalogIterator->folderID, catalogIterator->currentIndex);
287 InsertCatalogIteratorAsLRU( cacheGlobals
, catalogIterator
);
289 CATALOG_ITER_LIST_UNLOCK(cacheGlobals
);
293 //_______________________________________________________________________________
294 // Routine: GetCatalogIterator
296 // Function: Release interest in Catalog iterator
298 //_______________________________________________________________________________
300 ReleaseCatalogIterator( CatalogIterator
* catalogIterator
)
302 #if TARGET_API_MACOS_X
303 //PRINTIT(" ReleaseCatalogIterator: v=%d, d=%ld, i=%d\n", catalogIterator->volRefNum, catalogIterator->folderID, catalogIterator->currentIndex);
304 return CI_UNLOCK(catalogIterator
);
311 //_______________________________________________________________________________
312 // Routine: GetCatalogIterator
314 // Function: Returns an iterator associated with the volume, folderID, index,
315 // and iterationType (kIterateFilesOnly or kIterateAll).
316 // Searches the cache in MRU order.
317 // Inserts the resulting iterator at the head of mru automatically
319 // Note: The returned iterator is locked and ReleaseCatalogIterator must
320 // be called to unlock it.
322 //_______________________________________________________________________________
325 GetCatalogIterator(ExtendedVCB
*volume
, HFSCatalogNodeID folderID
, UInt32 offset
)
327 CatalogCacheGlobals
*cacheGlobals
= GetCatalogCacheGlobals();
328 CatalogIterator
*iterator
;
329 CatalogIterator
*bestIterator
;
333 CATALOG_ITER_LIST_LOCK(cacheGlobals
);
335 for (iterator
= cacheGlobals
->mru
; iterator
!= nil
; iterator
= iterator
->nextMRU
) {
337 /* first make sure volume and folder id match */
338 if ((iterator
->volume
!= volume
) || (iterator
->folderID
!= folderID
)) {
342 /* ignore busy iterators */
343 if ( CI_SLEEPLESS_LOCK(iterator
) == EBUSY
) {
344 //PRINTIT(" GetCatalogIterator: busy v=%d, d=%ld, i=%d\n", volume, folderID, iterator->currentIndex);
348 /* we matched volume, folder id, now check the offset */
349 if ( iterator
->currentOffset
== offset
|| iterator
->nextOffset
== offset
) {
350 bestIterator
= iterator
; // we scored! - so get out of this loop
351 break; // break with iterator locked
354 (void) CI_UNLOCK(iterator
); // unlock iterator before moving to the next one
357 // check if we didn't get one or if the one we got is too far away...
358 if (bestIterator
== NULL
)
360 bestIterator
= cacheGlobals
->lru
; // start over with a new iterator
362 //PRINTIT(" GetCatalogIterator: recycle v=%d, d=%ld, i=%d\n", bestIterator->volume, bestIterator->folderID, bestIterator->currentIndex);
363 (void) CI_LOCK_FROM_LIST(cacheGlobals
, bestIterator
); // XXX we should not eat the error!
365 CATALOG_ITER_LIST_LOCK(cacheGlobals
); // grab the lock again for MRU Insert below...
367 bestIterator
->volume
= volume
; // update the iterator's volume
368 bestIterator
->folderID
= folderID
; // ... and folderID
369 bestIterator
->currentIndex
= 0xFFFFFFFF; // ... and offspring index marker
370 bestIterator
->currentOffset
= 0xFFFFFFFF;
371 bestIterator
->nextOffset
= 0xFFFFFFFF;
373 bestIterator
->btreeNodeHint
= 0;
374 bestIterator
->btreeIndexHint
= 0;
375 bestIterator
->parentID
= folderID
; // set key to folderID + empty name
376 bestIterator
->folderName
.unicodeName
.length
= 0; // clear pascal/unicode name
378 if ( volume
->vcbSigWord
== kHFSPlusSigWord
)
379 bestIterator
->nameType
= kShortUnicodeName
;
381 bestIterator
->nameType
= kShortPascalName
;
384 //PRINTIT(" GetCatalogIterator: found v=%d, d=%ld, i=%d\n", bestIterator->volume, bestIterator->folderID, bestIterator->currentIndex);
387 // put this iterator at the front of the list
388 InsertCatalogIteratorAsMRU( cacheGlobals
, bestIterator
);
390 CATALOG_ITER_LIST_UNLOCK(cacheGlobals
);
392 return bestIterator
; // return our best shot
394 } /* GetCatalogIterator */
397 //_______________________________________________________________________________
398 // Routine: UpdateBtreeIterator
400 // Function: Fills in a BTreeIterator from a CatalogIterator
402 // Assumes: catalogIterator->nameType is correctly initialized!
403 // catalogIterator is locked (MacOS X)
404 //_______________________________________________________________________________
406 UpdateBtreeIterator(const CatalogIterator
*catalogIterator
, BTreeIterator
*btreeIterator
)
408 CatalogName
* nodeName
;
412 btreeIterator
->hint
.writeCount
= 0;
413 btreeIterator
->hint
.nodeNum
= catalogIterator
->btreeNodeHint
;
414 btreeIterator
->hint
.index
= catalogIterator
->btreeIndexHint
;
416 switch (catalogIterator
->nameType
)
418 case kShortPascalName
:
419 if ( catalogIterator
->folderName
.pascalName
[0] > 0 )
420 nodeName
= (CatalogName
*) catalogIterator
->folderName
.pascalName
;
427 case kShortUnicodeName
:
428 if ( catalogIterator
->folderName
.unicodeName
.length
> 0 )
429 nodeName
= (CatalogName
*) &catalogIterator
->folderName
.unicodeName
;
436 case kLongUnicodeName
:
437 if ( catalogIterator
->folderName
.longNamePtr
->length
> 0 )
438 nodeName
= (CatalogName
*) catalogIterator
->folderName
.longNamePtr
;
449 BuildCatalogKey(catalogIterator
->parentID
, nodeName
, isHFSPlus
, (CatalogKey
*) &btreeIterator
->key
);
453 //_______________________________________________________________________________
454 // Routine: UpdateCatalogIterator
456 // Function: Updates a CatalogIterator from a BTreeIterator
458 // Assumes: catalogIterator->nameType is correctly initialized!
459 // catalogIterator is locked (MacOS X)
460 //_______________________________________________________________________________
462 UpdateCatalogIterator (const BTreeIterator
*btreeIterator
, CatalogIterator
*catalogIterator
)
467 CatalogKey
* catalogKey
;
470 catalogIterator
->btreeNodeHint
= btreeIterator
->hint
.nodeNum
;
471 catalogIterator
->btreeIndexHint
= btreeIterator
->hint
.index
;
473 catalogKey
= (CatalogKey
*) &btreeIterator
->key
;
475 switch (catalogIterator
->nameType
)
477 case kShortPascalName
:
478 catalogIterator
->parentID
= catalogKey
->hfs
.parentID
;
480 dstName
= catalogIterator
->folderName
.pascalName
;
481 srcName
= catalogKey
->hfs
.nodeName
;
482 nameSize
= catalogKey
->hfs
.nodeName
[0] + sizeof(UInt8
);
485 case kShortUnicodeName
:
486 catalogIterator
->parentID
= catalogKey
->hfsPlus
.parentID
;
488 dstName
= &catalogIterator
->folderName
.unicodeName
;
489 srcName
= &catalogKey
->hfsPlus
.nodeName
;
490 nameSize
= (catalogKey
->hfsPlus
.nodeName
.length
+ 1) * sizeof(UInt16
);
492 // See if we need to make this iterator use long names
493 if ( nameSize
> sizeof(catalogIterator
->folderName
.unicodeName
) )
495 PrepareForLongName(catalogIterator
); // Find a long name buffer to use
496 dstName
= catalogIterator
->folderName
.longNamePtr
;
500 case kLongUnicodeName
:
501 catalogIterator
->parentID
= catalogKey
->hfsPlus
.parentID
;
503 dstName
= catalogIterator
->folderName
.longNamePtr
;
504 srcName
= &catalogKey
->hfsPlus
.nodeName
;
505 nameSize
= (catalogKey
->hfsPlus
.nodeName
.length
+ 1) * sizeof(UInt16
);
512 if (catalogIterator
->parentID
!= catalogIterator
->folderID
)
513 catalogIterator
->nextOffset
= 0xFFFFFFFF;
515 BlockMoveData(srcName
, dstName
, nameSize
);
517 } // end UpdateCatalogIterator
520 //_______________________________________________________________________________
521 // Routine: InsertCatalogIteratorAsMRU
523 // Function: Moves catalog iterator to head of mru order in double linked list
525 // Assumes list simple lock is held
526 //_______________________________________________________________________________
528 InsertCatalogIteratorAsMRU ( CatalogCacheGlobals
*cacheGlobals
, CatalogIterator
*iterator
)
530 CatalogIterator
*swapIterator
;
532 if ( cacheGlobals
->mru
!= iterator
) // if it's not already the mru iterator
534 swapIterator
= cacheGlobals
->mru
; // put it in the front of the double queue
535 cacheGlobals
->mru
= iterator
;
536 iterator
->nextLRU
->nextMRU
= iterator
->nextMRU
;
537 if ( iterator
->nextMRU
!= nil
)
538 iterator
->nextMRU
->nextLRU
= iterator
->nextLRU
;
540 cacheGlobals
->lru
= iterator
->nextLRU
;
541 iterator
->nextMRU
= swapIterator
;
542 iterator
->nextLRU
= nil
;
543 swapIterator
->nextLRU
= iterator
;
548 //________________________________________________________________________________
549 // Routine: InsertCatalogIteratorAsLRU
551 // Function: Moves catalog iterator to head of lru order in double linked list
553 // Assumes list simple lock is held
554 //_______________________________________________________________________________
556 InsertCatalogIteratorAsLRU ( CatalogCacheGlobals
*cacheGlobals
, CatalogIterator
*iterator
)
558 CatalogIterator
*swapIterator
;
560 if ( cacheGlobals
->lru
!= iterator
)
562 swapIterator
= cacheGlobals
->lru
;
563 cacheGlobals
->lru
= iterator
;
564 iterator
->nextMRU
->nextLRU
= iterator
->nextLRU
;
565 if ( iterator
->nextLRU
!= nil
)
566 iterator
->nextLRU
->nextMRU
= iterator
->nextMRU
;
568 cacheGlobals
->mru
= iterator
->nextMRU
;
569 iterator
->nextLRU
= swapIterator
;
570 iterator
->nextMRU
= nil
;
571 swapIterator
->nextMRU
= iterator
;
577 //_______________________________________________________________________________
578 // Routine: PrepareForLongName
580 // Function: Takes a CatalogIterator whose nameType is kShortUnicodeName, and
581 // changes the nameType to kLongUnicodeName.
583 // Since long Unicode names aren't stored in the CatalogIterator itself, we have
584 // to point to an HFSUniStr255 for storage. In the current implementation, we have
585 // just one such global buffer in the cache globals. We'll set the iterator to
586 // point to the global buffer and invalidate the iterator that was using it
587 // (i.e. the iterator whose nameType is kLongUnicodeName).
589 // Eventually, we might want to have a list of long name buffers which we recycle
590 // using an LRU algorithm. Or perhaps, some other way....
592 // Assumes: catalogIterator is locked (MacOS X)
593 //_______________________________________________________________________________
595 PrepareForLongName ( CatalogIterator
*iterator
)
597 CatalogCacheGlobals
*cacheGlobals
= GetCatalogCacheGlobals();
598 CatalogIterator
*iter
;
600 if (DEBUG_BUILD
&& iterator
->nameType
!= kShortUnicodeName
)
601 DebugStr("\p PrepareForLongName: nameType is wrong!");
604 // Walk through all the iterators. The first iterator whose nameType
605 // is kLongUnicodeName is invalidated (because it is using the global
606 // long name buffer).
609 CATALOG_ITER_LIST_LOCK(cacheGlobals
);
611 for ( iter
= cacheGlobals
->mru
; iter
!= nil
; iter
= iter
->nextMRU
)
613 if (iter
->nameType
== kLongUnicodeName
)
615 // if iterator is not already last then make it last
616 if ( iter
->nextMRU
!= nil
)
617 InsertCatalogIteratorAsLRU( cacheGlobals
, iter
);
619 (void) CI_LOCK_FROM_LIST(cacheGlobals
,iter
);
620 iter
->volume
= 0; // trash it
622 (void) CI_UNLOCK(iter
);
624 #if TARGET_API_MACOS_X
631 * if iter is nil then none of the iterators was using the LongUnicodeName buffer
634 CATALOG_ITER_LIST_UNLOCK(cacheGlobals
);
637 // Change the nameType of this iterator and point to the global
638 // long name buffer. Note - this iterator is already locked
640 iterator
->nameType
= kLongUnicodeName
;
641 iterator
->folderName
.longNamePtr
= &cacheGlobals
->longName
;