-
-/*************************************************************************************/
-
-/*************************************************************************************/
-/*
- * The following two routines work in tandem: StoreBufferMapping stores
- * successive buffer address -> buffer pointer mappings in a circular
- * match list, advancing the list index forward each time, while LookupBufferMapping
- * looks backwards through the list to look up a particular mapping (which is
- * typically the entry currently pointed to by gBufferAddress).
- *
- */
-static void StoreBufferMapping(caddr_t bufferAddress, struct buf *bp)
-{
- int i;
-
- DBG_ASSERT(gBufferListIndex >= 0);
- DBG_ASSERT(gBufferListIndex < BUFFERPTRLISTSIZE);
-
- simple_lock(&gBufferPtrListLock);
-
- /* We've got at most BUFFERPTRLISTSIZE tries at this... */
- for (i = BUFFERPTRLISTSIZE; i > 0; --i) {
- if (gBufferAddress[gBufferListIndex] == NULL) {
- gBufferAddress[gBufferListIndex] = bufferAddress;
- gBufferHeaderPtr[gBufferListIndex] = bp;
- break;
- }
- gBufferListIndex = (gBufferListIndex + 1) % BUFFERPTRLISTSIZE;
- };
-
- if (i == 0) {
- panic("StoreBufferMapping: couldn't find an empty slot in buffer list.");
- };
-
- DBG_ASSERT(gBufferListIndex >= 0);
- DBG_ASSERT(gBufferListIndex < BUFFERPTRLISTSIZE);
-
- simple_unlock(&gBufferPtrListLock);
-}
-
-
-/*static*/ OSErr LookupBufferMapping(caddr_t bufferAddress, struct buf **bpp, int *mappingIndexPtr)
-{
- OSErr err = E_NONE;
- int i;
- int listIndex = gBufferListIndex;
- struct buf *bp = NULL;
-
- DBG_ASSERT(gBufferListIndex >= 0);
- DBG_ASSERT(gBufferListIndex < BUFFERPTRLISTSIZE);
-
- simple_lock(&gBufferPtrListLock);
-
- /* We've got at most BUFFERPTRLISTSIZE tries at this... */
- for (i = BUFFERPTRLISTSIZE; i > 0; --i) {
- if (gBufferAddress[listIndex] == bufferAddress) {
- *mappingIndexPtr = listIndex;
- bp = gBufferHeaderPtr[listIndex];
- break;
- };
-
- listIndex = (listIndex - 1);
- if (listIndex < 0) {
- listIndex = BUFFERPTRLISTSIZE - 1;
- };
- };
-
- if (bp == NULL) {
- DEBUG_BREAK_MSG(("LookupBufferMapping: couldn't find buffer header for buffer in list.\n"));
- err = -1;
- };
-
- DBG_ASSERT(gBufferListIndex >= 0);
- DBG_ASSERT(gBufferListIndex < BUFFERPTRLISTSIZE);
-
- simple_unlock(&gBufferPtrListLock);
-
- *bpp = bp;
- return err;
-}
-
-
-static void ReleaseMappingEntry(int entryIndex) {
-
- DBG_ASSERT(gBufferListIndex >= 0);
- DBG_ASSERT(gBufferListIndex < BUFFERPTRLISTSIZE);
-
- simple_lock(&gBufferPtrListLock);
- gBufferAddress[entryIndex] = NULL;
- simple_unlock(&gBufferPtrListLock);
-};
-#if HFS_DIAGNOSTIC
-#define DBG_GETBLOCK 0
-#else
-#define DBG_GETBLOCK 0
-#endif
-
-OSErr GetBlock_glue (UInt16 options, UInt32 blockNum, Ptr *baddress, FileReference fileRefNum, ExtendedVCB * vcb)
-{
- int status;
- struct buf *bp = NULL;
- int readcount = 0;
-
-#if DBG_GETBLOCK
- DBG_IO(("Getting block %ld with options %d and a refnum of %x\n", blockNum, options, fileRefNum ));
-#endif
-
- if ((options & ~(gbReadMask | gbNoReadMask)) != 0) {
- DEBUG_BREAK_MSG(("GetBlock_glue: options = 0x%04X.\n", options));
- };
-
- *baddress = NULL;
-
- if (options & gbNoReadMask) {
- if (fileRefNum == NULL) {
- bp = getblk (VCBTOHFS(vcb)->hfs_devvp,
- IOBLKNOFORBLK(blockNum, VCBTOHFS(vcb)->hfs_phys_block_size),
- IOBYTECCNTFORBLK(blockNum, kHFSBlockSize, VCBTOHFS(vcb)->hfs_phys_block_size),
- 0,
- 0,
- BLK_META);
- } else {
- bp = getblk (fileRefNum,
- IOBLKNOFORBLK(blockNum, VCBTOHFS(vcb)->hfs_phys_block_size),
- IOBYTECCNTFORBLK(blockNum, kHFSBlockSize, VCBTOHFS(vcb)->hfs_phys_block_size),
- 0,
- 0,
- BLK_META);
- };
- status = E_NONE;
- } else {
- do {
- if (fileRefNum == NULL) {
- status = meta_bread (VCBTOHFS(vcb)->hfs_devvp,
- IOBLKNOFORBLK(blockNum, VCBTOHFS(vcb)->hfs_phys_block_size),
- IOBYTECCNTFORBLK(blockNum, kHFSBlockSize, VCBTOHFS(vcb)->hfs_phys_block_size),
- NOCRED,
- &bp);
- } else {
- status = meta_bread (fileRefNum,
- IOBLKNOFORBLK(blockNum, VCBTOHFS(vcb)->hfs_phys_block_size),
- IOBYTECCNTFORBLK(blockNum, kHFSBlockSize, VCBTOHFS(vcb)->hfs_phys_block_size),
- NOCRED,
- &bp);
- };
- if (status != E_NONE) {
- if (bp) brelse(bp);
- goto Error_Exit;
- };
-
- if (bp == NULL) {
- status = -1;
- goto Error_Exit;
- };
-
- ++readcount;
-
- if ((options & gbReadMask) && (bp->b_flags & B_CACHE)) {
- /* Rats! The block was found in the cache just when we really wanted a
- fresh copy off disk...
- */
- if (bp->b_flags & B_DIRTY) {
- DEBUG_BREAK_MSG(("GetBlock_glue: forced read for dirty block!\n"))
- };
- bp->b_flags |= B_INVAL;
- brelse(bp);
-
- /* Fall through and try again until we get a fresh copy from the disk... */
- };
- } while (((options & gbReadMask) != 0) && (readcount <= 1));
- };
-
- *baddress = bp->b_data + IOBYTEOFFSETFORBLK(bp->b_blkno, VCBTOHFS(vcb)->hfs_phys_block_size);
- StoreBufferMapping(*baddress, bp);
-
-Error_Exit: ;
- return status;
-}
-
-void MarkBlock_glue (Ptr address)
-{
- int err;
- struct buf *bp = NULL;
- int mappingEntry;
-
- if ((err = LookupBufferMapping(address, &bp, &mappingEntry))) {
- panic("Failed to find buffer pointer for buffer in MarkBlock_glue.");
- } else {
- bp->b_flags |= B_DIRTY;
- };
-}
-
-OSErr RelBlock_glue (Ptr address, UInt16 options )
-{
- int err;
- struct buf *bp;
- int mappingEntry;
-
- if (options & ~(rbTrashMask | rbDirtyMask | rbWriteMask) == 0) {
- DEBUG_BREAK_MSG(("RelBlock_glue: options = 0x%04X.\n", options));
- };
-
- if ((err = LookupBufferMapping(address, &bp, &mappingEntry))) {
- DEBUG_BREAK_MSG(("Failed to find buffer pointer for buffer in RelBlock_glue.\n"));
- } else {
- if (bp->b_flags & B_DIRTY) {
- /* The buffer was previously marked dirty (using MarkBlock_glue):
- now's the time to write it. */
- options |= rbDirtyMask;
- };
- ReleaseMappingEntry(mappingEntry);
- if (options & rbTrashMask) {
- bp->b_flags |= B_INVAL;
- brelse(bp);
- } else {
- if (options & (rbDirtyMask | rbWriteMask)) {
- bp->b_flags |= B_DIRTY;
- if (options & rbWriteMask) {
- bwrite(bp);
- } else {
- bdwrite(bp);
- }
- } else {
- brelse(bp);
- };
- };
- err = E_NONE;
- };
- return err;
-}
-
-/* */
-/* Creates a new vnode to hold a psuedo file like an extents tree file */
-/* */
-
-OSStatus GetInitializedVNode(struct hfsmount *hfsmp, struct vnode **tmpvnode, int init_ubc)
-{
-
- struct hfsnode *hp;
- struct vnode *vp = NULL;
- int rtn;
-
- DBG_ASSERT(hfsmp != NULL);
- DBG_ASSERT(tmpvnode != NULL);
-
- /* Allocate a new hfsnode. */
- /*
- * Must do malloc() before getnewvnode(), since malloc() can block
- * and could cause other part of the system to access v_data
- * which has not been initialized yet
- */
- MALLOC_ZONE(hp, struct hfsnode *, sizeof(struct hfsnode), M_HFSNODE, M_WAITOK);
- if(hp == NULL) {
- rtn = ENOMEM;
- goto Err_Exit;
- }
- bzero((caddr_t)hp, sizeof(struct hfsnode));
- lockinit(&hp->h_lock, PINOD, "hfsnode", 0, 0);
-
- MALLOC_ZONE(hp->h_meta, struct hfsfilemeta *,
- sizeof(struct hfsfilemeta), M_HFSFMETA, M_WAITOK);
- /* Allocate a new vnode. */
- if ((rtn = getnewvnode(VT_HFS, HFSTOVFS(hfsmp), hfs_vnodeop_p, &vp))) {
- FREE_ZONE(hp->h_meta, sizeof(struct hfsfilemeta), M_HFSFMETA);
- FREE_ZONE(hp, sizeof(struct hfsnode), M_HFSNODE);
- goto Err_Exit;
- }
-
- /* Init the structure */
- bzero(hp->h_meta, sizeof(struct hfsfilemeta));
-
- hp->h_vp = vp; /* Make HFSTOV work */
- hp->h_meta->h_devvp = hfsmp->hfs_devvp;
- hp->h_meta->h_dev = hfsmp->hfs_raw_dev;
- hp->h_meta->h_usecount++;
- hp->h_nodeflags |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
-#if HFS_DIAGNOSTIC
- hp->h_valid = HFS_VNODE_MAGIC;
-#endif
- vp->v_data = hp; /* Make VTOH work */
- vp->v_type = VREG;
- /*
- * Metadata files are VREG but not available for IO
- * through mapped IO as will as POSIX IO APIs.
- * Hence we do not initialize UBC for those files
- */
- if (init_ubc)
- ubc_info_init(vp);
- else
- vp->v_ubcinfo = UBC_NOINFO;
-
- *tmpvnode = vp;
-
- VREF(hp->h_meta->h_devvp);
-
- return noErr;
-
-Err_Exit:
-
- *tmpvnode = NULL;
-
- return rtn;
-}
-
-OSErr GetNewFCB(ExtendedVCB *vcb, FileReference* fRefPtr)
-{
- OSErr err;
-
- err = GetInitializedVNode( VCBTOHFS(vcb), fRefPtr, 0 );
- panic("This node is not completely initialized in GetNewFCB!"); /* XXX SER */
-
- return( err );
-}
-
-
-OSErr CheckVolumeOffLine( ExtendedVCB *vcb )
-{
-
- return( 0 );
-}
-
-
-OSErr C_FlushMDB( ExtendedVCB *volume)
-{
- short err;
-
- if (volume->vcbSigWord == kHFSPlusSigWord)
- err = hfs_flushvolumeheader(VCBTOHFS(volume), 0);
- else
- err = hfs_flushMDB(VCBTOHFS(volume), 0);
-
- return err;
-}
-
-