//--------------------------------------------------------------------------- int scache_readSector(cache_set* cache, unsigned int sector, void** buf) { int index; //index is given in single sectors not octal sectors int ret; unsigned int alignedSector; XPRINTF("cache: readSector devId = %i %p sector = %u \n", cache->dev->devId, cache, sector); if (cache == NULL) { printf("cache: devId cache not created = %i \n", cache->dev->devId); return -1; } #ifdef SCACHE_RECORD_STATS cache->cacheAccess ++; #endif index = getIndexRead(cache, sector); XPRINTF("cache: indexRead=%i \n", index); if (index >= 0) { //sector found in cache #ifdef SCACHE_RECORD_STATS cache->cacheHits ++; #endif *buf = cache->sectorBuf + (index * cache->sectorSize); XPRINTF("cache: hit and done reading sector \n"); return cache->sectorSize; } //compute alignedSector - to prevent storage of duplicit sectors in slots alignedSector = (sector/cache->indexLimit)*cache->indexLimit; index = getIndexWrite(cache, alignedSector); XPRINTF("cache: indexWrite=%i slot=%d alignedSector=%u\n", index, index / cache->indexLimit, alignedSector); ret = READ_SECTOR(cache->dev, alignedSector, cache->sectorBuf + (index * cache->sectorSize), BLOCK_SIZE/cache->sectorSize); if (ret < 0) { printf("scache: ERROR reading sector from disk! sector=%u\n", alignedSector); return ret; } *buf = cache->sectorBuf + (index * cache->sectorSize) + ((sector%cache->indexLimit) * cache->sectorSize); XPRINTF("cache: done reading physical sector \n"); //write precaution //cache->flushCounter++; //if (cache->flushCounter == FLUSH_TRIGGER) { //scache_flushSectors(cache); //} return cache->sectorSize; }
//--------------------------------------------------------------------------- int scache_allocSector(cache_set* cache, unsigned int sector, void** buf) { int index; //index is given in single sectors not octal sectors //int ret; unsigned int alignedSector; XPRINTF("cache: allocSector devId = %i sector = %i \n", cache->dev->devId, sector); index = getIndexRead(cache, sector); XPRINTF("cache: indexRead=%i \n", index); if (index >= 0) { //sector found in cache *buf = cache->sectorBuf + (index * cache->sectorSize); XPRINTF("cache: hit and done allocating sector \n"); return cache->sectorSize; } //compute alignedSector - to prevent storage of duplicit sectors in slots alignedSector = (sector/cache->indexLimit)*cache->indexLimit; index = getIndexWrite(cache, alignedSector); XPRINTF("cache: indexWrite=%i \n", index); *buf = cache->sectorBuf + (index * cache->sectorSize) + ((sector%cache->indexLimit) * cache->sectorSize); XPRINTF("cache: done allocating sector\n"); return cache->sectorSize; }