⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 findvsb.c

📁 intel的一个高效率FLASH文件系统。思路很好
💻 C
📖 第 1 页 / 共 2 页
字号:
   else
   {
      pLastLogicalVSB = (LAST_VSB *)&(lastLogicalVSB[ComponentID][0]);
      /* search mru cache to determine if cache holds useful info */
      for (cnt = 0; cnt < MRU_VSB_CACHE_SIZE; cnt++)
      {
         /* set up to look at cache */
         BlockNumber = (BYTE) pLastLogicalVSB->block;
         vi =   pLastLogicalVSB->vsbIndex;

         if ((BlockNumber == DEFAULT_BYTE) && (vi == DEFAULT_WORD))
         {
            cnt = MRU_VSB_CACHE_SIZE;
            break;
         }

         /* read the VAT entry indicated */
         /* set physical address of beginning of block */
         FlashAddr = CalculateFlashAddr( ComponentID, BlockNumber,
                                         (vi * sizeof(vi)) );

         VATEntry = (WORD *)VSBbuffer;
         if ((RetVal = FlashDevRead( FlashAddr, sizeof(vi), (BYTE *)VATEntry))
             != ERR_NONE)
            return (RetVal);

         /* ignore if discarded */
         if( (*VATEntry & VSB_DISCARD_MASK) )
         {
            /* right one? */
            if( *VATEntry == VSBToFind )
            {
               /* if so, we are done */
               break;
            }
         }

         pLastLogicalVSB++;

      }
      if (cnt < MRU_VSB_CACHE_SIZE)
      {
         /* cache hit occurred so fix up cache and return */
         VSBFound = TRUE;

         /* set VSB's component */
         CurrentComponent = ComponentID;

         /* set current VSB address */
         /*E2.3.41, Added (vi+(VAT_SIZE_VSB-1) instead of vi*/
         CurrentVSB[CurrentComponent] = ((vi+(VAT_SIZE_VSB-1)) * VSB_SIZE);
         /* set VSB's block number */
         CurrentBlock[CurrentComponent] = BlockNumber;

         /* set vsb's index */
         CurrentIndex[CurrentComponent] = vi;

         CurrentByte[CurrentComponent] = 0;

         /* update cache */
         /* determine how much of buffer to shift */
         BytesToRead = (WORD)(sizeof(LAST_VSB) * (cnt));

         /* read in most recently used */
         memcpy (VSBbuffer,
                      (BYTE *)&(lastLogicalVSB[CurrentComponent][0]),
                      BytesToRead);

         /* bump oldest out of cache */
         memcpy ((BYTE *)&(lastLogicalVSB[CurrentComponent][1]),
                       VSBbuffer, BytesToRead);

         /* place newest into cache */
         lastLogicalVSB[CurrentComponent][0].block = BlockNumber;
         lastLogicalVSB[CurrentComponent][0].vsbIndex = vi;

         if (ReclaimThresholdOverride == TRUE)
            ReclaimThresholdOverride = FALSE;

         return( ERR_NONE );
      }

      /* cache miss occurred so set up for searching block */
      BlockNumber = lastLogicalVSB[ComponentID][0].block;
      if (BlockNumber == DEFAULT_BYTE)
         BlockNumber = 0;
      vi = lastLogicalVSB[ComponentID][0].vsbIndex;
      if (vi == DEFAULT_WORD)
         vi = 1;

      if ( vi < ((LOGICAL_VSB_BUFFER_SIZE / 2) + 1))
         vi = 1;
      else
         vi = vi - (LOGICAL_VSB_BUFFER_SIZE / 2);

      VatBufferSize = LOGICAL_VSB_BUFFER_SIZE;
   }

   /* set up how many vat's to read in first scan */
  /*E2.3.41, Added -(VAT_SIZE_VSB-1) after VSB_PER_BLOCK*/
   if ((vi + VatBufferSize) < (VSB_PER_BLOCK-(VAT_SIZE_VSB-1)))
      BytesToRead = (WORD) (VatBufferSize * sizeof(vi));
   else
  /*E2.3.41, Added -(VAT_SIZE_VSB-1) after VSB_PER_BLOCK*/
      BytesToRead = (WORD) (((VSB_PER_BLOCK-(VAT_SIZE_VSB-1)) - vi) * sizeof(vi));

   /* init block count */
   BlockCount = 0;
   /*Comment- Loop for each Block*/
   do
   {
      /* don't search spare block */
      if ( BlockNumber != SpareBlock[ComponentID] )
      {
         /* set physical address of beginning of block */
         FlashAddr = CalculateFlashAddr( ComponentID, BlockNumber, 0 );
         VATEntry = (WORD *)VSBbuffer;
         if ((RetVal = FlashDevRead(FlashAddr, sizeof(vi), (BYTE *)VATEntry ))
             != ERR_NONE)
            return (RetVal);


         if( ( (VSBToFind == VSB_FREE) &&
               ( ( *VATEntry & 0x00ff ) == (WORD)VSB_FB_FULL ) ) ||
               ( (VSBToFind != VSB_FREE) && (*VATEntry == VSB_FB_UNUSED) ) )
         {
            /* do next block */
            ++BlockNumber;

            /* end of component? */
            if( BlockNumber == BLOCK_COUNT )
            {
               BlockNumber = 0;
            }
            continue;
         }

         /*Comment- Loop for each vat entry of each block*/ 
         /*E2.3.41, Added -(VAT_SIZE_VSB-1)*/  
         while (vi < (VSB_PER_BLOCK-(VAT_SIZE_VSB-1)))
         {

            VATEntry = (WORD *)VSBbuffer;

            /* set physical address of beginning of block */
            FlashAddr = CalculateFlashAddr( ComponentID,
                                                          BlockNumber,
                                                          vi * sizeof(vi) );

            /* read a portion of the VAT */
            if ((RetVal = FlashDevRead( FlashAddr, BytesToRead, (BYTE *)VATEntry ))
                != ERR_NONE)
               return (RetVal);

            for (cnt = 1; cnt <= (BytesToRead / sizeof(vi)); cnt++)
            {
               /* ignore if discarded */
               tempVAT = *VATEntry;
               if( (tempVAT & VSB_DISCARD_MASK) )
               {
                  /* right one? */
                  if( tempVAT == VSBToFind )
                  {
                     /* if so, we are done */
                     break;
                  }
               }
               VATEntry++;
               vi++;
            }
            if( tempVAT == VSBToFind )
            {
               /* if so, we are done */
               break;
            }
           /*E2.3.41, Added -VAT_SIZE_VSB instead if -1 after VSB_PER_BLOCK*/
           if ((BytesToRead / sizeof(vi)) < (VSB_PER_BLOCK - VAT_SIZE_VSB))
            {
              /*E2.3.41, Added -VAT_SIZE_VSB instead if -1 after VSB_PER_BLOCK*/
              BytesToRead = (VSB_PER_BLOCK - VAT_SIZE_VSB) * sizeof(vi);
              vi = 1;
            }
         }
         if( tempVAT == VSBToFind )
         {
            /* found it... */
            break;
         }
      }
      ++BlockNumber;

      /* if looking for a free vsb, wrap to first vat index on next block */
      vi = 1;

      /* end of component? If so, wrap */
      if( BlockNumber == BLOCK_COUNT )
      {
         BlockNumber = 0;
      }
   } while( ++BlockCount < BLOCK_COUNT );

   /* gone through blocks? */
   if( BlockCount == BLOCK_COUNT )
   {
      /* return not found */
      ReclaimThresholdOverride = FALSE;
      return( ERR_NOT_FOUND );
   }
   else
   {
      VSBFound = TRUE;
 
        /* set VSB's component */
      CurrentComponent = ComponentID;

       /* set current VSB address */
       /*E2.3.41, vi replaced with (vi+(VAT_SIZE_VSB-1)*/
       CurrentVSB[CurrentComponent] = ((vi+(VAT_SIZE_VSB-1)) * VSB_SIZE);
       /* set VSB's block number */
      CurrentBlock[CurrentComponent] = BlockNumber;   
       /*Comment- set vsb's index in the VAT*/
      CurrentIndex[CurrentComponent] = vi;
      CurrentByte[CurrentComponent] = 0;

      if(VSBToFind == VSB_FREE)
      {
         /* if looking for a free, track where we left off */
         FreeVSB[CurrentComponent] = vi;
      }

      else
      {
         /* if looking for logical track where it was found to assist in */
         /* the next search for a logical */

         /* determine how much of buffer to shift */
         BytesToRead = sizeof(LAST_VSB) * (MRU_VSB_CACHE_SIZE - 1);

         /* read in most recently used */
         memcpy (VSBbuffer,
                      (BYTE *)&(lastLogicalVSB[CurrentComponent][0]),
                      BytesToRead);

         /* bump oldest out of cache */
         memcpy ((BYTE *)&(lastLogicalVSB[CurrentComponent][1]),
                       VSBbuffer, BytesToRead);

         /* place newest into cache */
         lastLogicalVSB[CurrentComponent][0].block = BlockNumber;
         lastLogicalVSB[CurrentComponent][0].vsbIndex = vi;
      }
      if (ReclaimThresholdOverride == TRUE)
         ReclaimThresholdOverride = FALSE;

      return( ERR_NONE );
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -