/* ---------------------------------------------------------------------------- * Copyright (c) @CompanyNameMagicTag 2015-2019. All rights reserved. * Description: LiteOS Bcache Module Headfile * Create: 2015-01-01 * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * 3. Neither the name of the copyright holder nor the names of its contributors may be used * to endorse or promote products derived from this software without specific prior written * permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * --------------------------------------------------------------------------- */ #ifndef _BCACHE_H #define _BCACHE_H #include "inode/inode.h" #include "rbtree.h" #ifdef __cplusplus #if __cplusplus extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ #define ALIGN_LIB(x) (((x) + (HALARC_ALIGNMENT - 1)) & ~(HALARC_ALIGNMENT - 1)) #define ALIGN_DISP(x) (HALARC_ALIGNMENT - ((x) & (HALARC_ALIGNMENT - 1))) #define BCACHE_PREREAD_PRIO 12 #define UNSIGNED_INTEGER_BITS 32 #define UNINT_MAX_SHIFT_BITS 31 #define UNINT_LOG2_SHIFT 5 #define PREREAD_BLOCK_NUM 2 #define EVEN_JUDGED 2 #define PERCENTAGE 100 #define PREREAD_EVENT_MASK 0xf #if CONFIG_FS_FAT_SECTOR_PER_BLOCK < UNSIGNED_INTEGER_BITS #error cache too small #else #define BCACHE_BLOCK_FLAGS (CONFIG_FS_FAT_SECTOR_PER_BLOCK / UNSIGNED_INTEGER_BITS) #endif #define FS_OS_TASK_PRIORITY_LOWEST 31 #define FS_LOS_WAIT_FOREVER 0xFFFFFFFF #define FS_LOS_TASK_STATUS_DETACHED NULL //0x0100U #define FS_LOS_WAITMODE_CLR 1U #define FS_LOS_WAITMODE_OR 2U typedef struct { dpal_dl_list_t listNode; /* list node */ struct rb_node rbNode; /* red-black tree node */ uint64_t num; /* block number */ BOOL modified; /* is this block data modified (needs write) */ BOOL readFlag; /* is the block data have read form sd(real data) */ uint32_t age; uint32_t pgHit; uint8_t *data; /* block data */ uint32_t flag[0]; } OsBcacheBlock; typedef int32_t (*BcacheReadFun)(struct inode *, /* private data */ uint8_t *, /* block buffer */ uint32_t, /* number of blocks to read */ uint64_t); /* starting block number */ typedef int32_t (*BcacheWriteFun)(struct inode *, /* private data */ const uint8_t *, /* block buffer */ uint32_t, /* number of blocks to write */ uint64_t); /* starting block number */ struct tagOsBcache; typedef void (*BcachePrereadFun)(struct tagOsBcache *, /* block cache instance space holder */ const OsBcacheBlock *); /* block data */ typedef struct tagOsBcache { void *priv; /* private data */ dpal_dl_list_t listHead; /* head of block list */ struct rb_root rbRoot; /* block red-black tree root */ uint32_t blockSize; /* block size in bytes */ uint32_t blockSizeLog2; /* block size log2 */ uint64_t blockCount; /* block count of the disk */ uint32_t sectorSize; /* device sector size in bytes */ uint32_t sectorPerBlock; /* sector count per block */ uint8_t *memStart; /* memory base */ uint32_t prereadTaskId; /* preread task id */ uint64_t curBlockNum; /* current preread block number */ dpal_dl_list_t freeListHead; /* list of free blocks */ BcacheReadFun breadFun; /* block read function */ BcacheWriteFun bwriteFun; /* block write function */ BcachePrereadFun prereadFun; /* block preread function */ uint8_t *rwBuffer; /* buffer for bcache block */ uint32_t bcacheMutex; /* mutex for bcache */ dpal_event_t bcacheEvent; /* event for bcache */ uint32_t modifiedBlock; /* number of modified blocks */ #ifdef CONFIG_FS_FAT_CACHE_SYNC_THREAD uint32_t syncTaskId; /* sync task id */ #endif } OsBcache; /** * @ingroup bcache * * @par Description: * The BlockCacheRead() function shall read data from the bcache, and if it doesn't hit, read the data from disk. * * @param bc [IN] block cache instance * @param buf [OUT] data buffer ptr * @param len [IN] number of bytes to read * @param num [IN] starting block number * @param pos [IN] starting position inside starting block * * @attention *