28#if !defined(BUILDING_IEEE1394_DISK) && !defined(BUILDING_USBHDFSD)
31#ifdef BUILDING_USBHDFSD
34#include "usbhd_common.h"
35#ifdef BUILDING_USBHDFSD
38#ifdef BUILDING_IEEE1394_DISK
45#ifdef BUILDING_USBHDFSD
46#define READ_SECTOR mass_stor_readSector
47#define WRITE_SECTOR mass_stor_writeSector
48#define DEV_ACCESSOR(d) ((d)->dev)
49#define DEVID_ACCESSOR(d) ((d)->dev->devId)
51#ifdef BUILDING_IEEE1394_DISK
52#define READ_SECTOR scsiReadSector
53#define WRITE_SECTOR scsiWriteSector
54#define DEV_ACCESSOR(d) ((d)->dev)
55#define DEVID_ACCESSOR(d) ((d)->dev->nodeID)
57#if !defined(BUILDING_IEEE1394_DISK) && !defined(BUILDING_USBHDFSD)
58#define READ_SECTOR(d, s, a, c) (d)->bd->read((d)->bd, s, a, c)
59#define WRITE_SECTOR(d, s, a, c) (d)->bd->write((d)->bd, s, a, c)
60#define DEV_ACCESSOR(d) (d)
61#define DEVID_ACCESSOR(d) ((d)->bd->devNr)
66#include "mass_debug.h"
68#ifdef BUILDING_USBHDFSD
72static int scache_flushSector(
cache_set *cache,
int index);
80 for (i = 0; i < CACHE_SIZE; i++) {
81 cache->rec[i].sector = 0xFFFFFFF0;
82 cache->rec[i].tax = 0;
83 cache->rec[i].writeDirty = 0;
93static int getSlot(
cache_set *cache,
unsigned int sector)
97 for (i = 0; i < CACHE_SIZE; i++) {
98 if (sector >= cache->rec[i].sector && sector < (cache->rec[i].sector + cache->indexLimit)) {
107static int getIndexRead(
cache_set *cache,
unsigned int sector)
112 for (i = 0; i < CACHE_SIZE; i++) {
113 if (sector >= cache->rec[i].sector && sector < (cache->rec[i].sector + cache->indexLimit)) {
114 if (cache->rec[i].tax < 0)
115 cache->rec[i].tax = 0;
116 cache->rec[i].tax += 2;
119 if (cache->rec[i].tax > 1)
127 return ((index * cache->indexLimit) + (sector - cache->rec[index].sector));
132static int getIndexWrite(
cache_set *cache,
unsigned int sector)
134 int minTax = 0x0FFFFFFF;
135 unsigned int i, index = 0;
137 for (i = 0; i < CACHE_SIZE; i++) {
138 if (cache->rec[i].tax < minTax) {
140 minTax = cache->rec[i].tax;
145#ifdef BUILDING_USBHDFSD
148 ret = scache_flushSector(cache, index);
153 if (cache->rec[index].writeDirty) {
156 XPRINTF(
"scache: getIndexWrite: sector is dirty : %d index=%d \n", cache->rec[index].sector, index);
157 ret = WRITE_SECTOR(DEV_ACCESSOR(cache), cache->rec[index].sector, cache->sectorBuf + (index * BLOCK_SIZE), BLOCK_SIZE / cache->sectorSize);
158 cache->rec[index].writeDirty = 0;
161 M_PRINTF(
"scache: ERROR writing sector to disk! sector=%u\n", sector);
165 cache->rec[index].tax += 2;
166 cache->rec[index].sector = sector;
168 return index * cache->indexLimit;
175#ifdef BUILDING_USBHDFSD
176static int scache_flushSector(
cache_set *cache,
int index)
178 if (cache->rec[index].writeDirty) {
181 XPRINTF(
"scache: flushSector dirty index=%d sector=%u \n", index, cache->rec[index].sector);
182 ret = WRITE_SECTOR(DEV_ACCESSOR(cache), cache->rec[index].sector, cache->sectorBuf + (index * BLOCK_SIZE), BLOCK_SIZE / cache->sectorSize);
184 M_PRINTF(
"scache: ERROR writing sector to disk! sector=%u\n", cache->rec[index].sector);
188 cache->rec[index].writeDirty = 0;
199 XPRINTF(
"cache: flushSectors devId = %i \n", DEVID_ACCESSOR(cache));
201 XPRINTF(
"scache: flushSectors writeFlag=%d\n", cache->writeFlag);
203 if (cache->writeFlag == 0) {
207 for (i = 0; i < CACHE_SIZE; i++) {
209#ifdef BUILDING_USBHDFSD
210 if ((ret = scache_flushSector(cache, i)) >= 0)
212 if (cache->rec[i].writeDirty)
215#ifndef BUILDING_USBHDFSD
216 XPRINTF(
"scache: flushSectors dirty index=%d sector=%u \n", i, cache->rec[i].sector);
217 ret = WRITE_SECTOR(DEV_ACCESSOR(cache), cache->rec[i].sector, cache->sectorBuf + (i * BLOCK_SIZE), BLOCK_SIZE);
218#if defined(BUILDING_IEEE1394_DISK) || defined(BUILDING_USBHDFSD)
219 cache->rec[i].writeDirty = 0;
223 M_PRINTF(
"scache: ERROR writing sector to disk! sector=%d\n", cache->rec[i].sector);
226#if !defined(BUILDING_IEEE1394_DISK) && !defined(BUILDING_USBHDFSD)
227 cache->rec[i].writeDirty = 0;
232#ifdef BUILDING_USBHDFSD
237 cache->writeFlag = 0;
242int scache_readSector(
cache_set *cache,
unsigned int sector,
void **buf)
246 unsigned int alignedSector;
249 XPRINTF(
"cache: readSector devId = %i %p sector = %u \n", DEVID_ACCESSOR(cache), cache, sector);
252 M_PRINTF(
"cache: devId cache not created \n");
256#ifdef SCACHE_RECORD_STATS
257 cache->cacheAccess++;
259 index = getIndexRead(cache, sector);
260 XPRINTF(
"cache: indexRead=%i \n", index);
262#ifdef SCACHE_RECORD_STATS
265 *buf = cache->sectorBuf + (index * cache->sectorSize);
266 XPRINTF(
"cache: hit and done reading sector \n");
268 return cache->sectorSize;
272 alignedSector = (sector / cache->indexLimit) * cache->indexLimit;
273 index = getIndexWrite(cache, alignedSector);
274 XPRINTF(
"cache: indexWrite=%i slot=%d alignedSector=%u\n", index, index / cache->indexLimit, alignedSector);
275 ret = READ_SECTOR(DEV_ACCESSOR(cache), alignedSector, cache->sectorBuf + (index * cache->sectorSize), BLOCK_SIZE / cache->sectorSize);
278 M_PRINTF(
"scache: ERROR reading sector from disk! sector=%u\n", alignedSector);
281 *buf = cache->sectorBuf + (index * cache->sectorSize) + ((sector % cache->indexLimit) * cache->sectorSize);
282 XPRINTF(
"cache: done reading physical sector \n");
290 return cache->sectorSize;
301int scache_allocSector(
cache_set *cache,
unsigned int sector,
void **buf)
305 unsigned int alignedSector;
307 XPRINTF(
"cache: allocSector devId = %i sector = %u \n", DEVID_ACCESSOR(cache), sector);
309 index = getIndexRead(cache, sector);
310 XPRINTF(
"cache: indexRead=%i \n", index);
312 *buf = cache->sectorBuf + (index * cache->sectorSize);
313 XPRINTF(
"cache: hit and done allocating sector \n");
314 return cache->sectorSize;
318 alignedSector = (sector / cache->indexLimit) * cache->indexLimit;
319 index = getIndexWrite(cache, alignedSector);
320 XPRINTF(
"cache: indexWrite=%i \n", index);
321 *buf = cache->sectorBuf + (index * cache->sectorSize) + ((sector % cache->indexLimit) * cache->sectorSize);
322 XPRINTF(
"cache: done allocating sector\n");
323 return cache->sectorSize;
328int scache_writeSector(
cache_set *cache,
unsigned int sector)
333 XPRINTF(
"cache: writeSector devId = %i sector = %u \n", DEVID_ACCESSOR(cache), sector);
335 index = getSlot(cache, sector);
337 M_PRINTF(
"cache: writeSector: ERROR! the sector is not allocated! \n");
340 XPRINTF(
"cache: slotFound=%i \n", index);
343 cache->rec[index].tax += 2;
346 cache->rec[index].writeDirty = 1;
349 XPRINTF(
"cache: done soft writing sector \n");
357 return cache->sectorSize;
360#ifdef BUILDING_USBHDFSD
361void scache_invalidate(
cache_set *cache,
unsigned int sector,
int count)
365 XPRINTF(
"cache: invalidate devId = %i sector = %u count = %d \n", DEVID_ACCESSOR(cache), sector,
count);
367 for (i = 0; i <
count; i++, sector++) {
370 index = getSlot(cache, sector);
372 scache_flushSector(cache, index);
374 cache->rec[index].sector = 0xFFFFFFF0;
375 cache->rec[index].tax = 0;
376 cache->rec[index].writeDirty = 0;
383#if defined(BUILDING_USBHDFSD)
385#elif defined(BUILDING_IEEE1394_DISK)
392#ifdef BUILDING_USBHDFSD
393 XPRINTF(
"cache: init devId = %i sectSize = %u \n", dev->devId, sectSize);
395#ifdef BUILDING_IEEE1394_DISK
396 XPRINTF(
"cache: init devId = %i sectSize = %i \n", dev->nodeID, sectSize);
401 M_PRINTF(
"scache init! Sector cache: can't alloate cache!\n");
405 XPRINTF(
"scache init! \n");
406#if !defined(BUILDING_IEEE1394_DISK) && !defined(BUILDING_USBHDFSD)
412 cache->sectorBuf = (
unsigned char *)malloc(BLOCK_SIZE * CACHE_SIZE);
413 if (cache->sectorBuf == NULL) {
414 M_PRINTF(
"Sector cache: can't alloate memory of size:%d \n", BLOCK_SIZE * CACHE_SIZE);
418 XPRINTF(
"Sector cache: allocated memory at:%p of size:%d \n", cache->sectorBuf, BLOCK_SIZE * CACHE_SIZE);
421#if !defined(BUILDING_IEEE1394_DISK) && !defined(BUILDING_USBHDFSD)
422 cache->sectorSize = bd->sectorSize;
424 cache->sectorSize = sectSize;
426 cache->indexLimit = BLOCK_SIZE / cache->sectorSize;
427#ifdef SCACHE_RECORD_STATS
428 cache->cacheAccess = 0;
429 cache->cacheHits = 0;
431 XPRINTF(
"sectorSize: 0x%x\n", cache->sectorSize);
436#ifdef SCACHE_RECORD_STATS
438void scache_getStat(
cache_set *cache,
unsigned int *access,
unsigned int *hits)
440 *access = cache->cacheAccess;
441 *hits = cache->cacheHits;
448 XPRINTF(
"cache: kill devId = %i \n", DEVID_ACCESSOR(cache));
449 if (cache->sectorBuf != NULL) {
450 free(cache->sectorBuf);
451 cache->sectorBuf = NULL;
458 XPRINTF(
"cache: close devId = %i \n", DEVID_ACCESSOR(cache));
459 scache_flushSectors(cache);
u32 count
start sector of fragmented bd/file