更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
bcache.h 文件参考

浏览源代码.

结构体

struct  OsBcacheBlock
 
struct  tagOsBcache
 

类型定义

typedef INT32(* BcacheReadFun) (struct Vnode *, UINT8 *, UINT32, UINT64)
 
typedef INT32(* BcacheWriteFun) (struct Vnode *, const UINT8 *, UINT32, UINT64)
 
typedef VOID(* BcachePrereadFun) (struct tagOsBcache *, const OsBcacheBlock *)
 
typedef struct tagOsBcache OsBcache
 

函数

INT32 BlockCacheRead (OsBcache *bc, UINT8 *buf, UINT32 *len, UINT64 pos, BOOL useRead)
 读块设备缓存 更多...
 
INT32 BlockCacheWrite (OsBcache *bc, const UINT8 *buf, UINT32 *len, UINT64 pos)
 写块设备缓存 更多...
 
INT32 BlockCacheSync (OsBcache *bc)
 块缓存同步 更多...
 
OsBcacheBlockCacheInit (struct Vnode *devNode, UINT32 sectorSize, UINT32 sectorPerBlock, UINT32 blockNum, UINT64 blockCount)
 
VOID BlockCacheDeinit (OsBcache *bc)
 
INT32 BcacheClearCache (OsBcache *bc)
 
INT32 OsSdSync (INT32 id)
 
VOID BcacheSyncThreadInit (OsBcache *bc, INT32 id)
 块缓存同步任务初始化,开了个内核任务. 更多...
 
VOID BcacheSyncThreadDeinit (const OsBcache *bc)
 
UINT32 BcacheAsyncPrereadInit (OsBcache *bc)
 
VOID ResumeAsyncPreread (OsBcache *arg1, const OsBcacheBlock *arg2)
 
UINT32 BcacheAsyncPrereadDeinit (OsBcache *bc)
 

类型定义说明

◆ BcachePrereadFun

typedef VOID(* BcachePrereadFun) (struct tagOsBcache *, const OsBcacheBlock *)

在文件 bcache.h90 行定义.

◆ BcacheReadFun

typedef INT32(* BcacheReadFun) (struct Vnode *, UINT8 *, UINT32, UINT64)

在文件 bcache.h78 行定义.

◆ BcacheWriteFun

typedef INT32(* BcacheWriteFun) (struct Vnode *, const UINT8 *, UINT32, UINT64)

在文件 bcache.h83 行定义.

◆ OsBcache

typedef struct tagOsBcache OsBcache

函数说明

◆ BcacheAsyncPrereadDeinit()

UINT32 BcacheAsyncPrereadDeinit ( OsBcache bc)

在文件 bcache.c1210 行定义.

1211{
1212 UINT32 ret = LOS_NOK;
1213
1214 if (bc != NULL) {
1215 ret = LOS_TaskDelete(bc->prereadTaskId);
1216 if (ret != ENOERR) {
1217 PRINT_ERR("Bcache async task delete failed in %s, %d\n", __FUNCTION__, __LINE__);
1218 }
1219
1220 ret = LOS_EventDestroy(&bc->bcacheEvent);
1221 if (ret != ENOERR) {
1222 PRINT_ERR("Async event destroy failed in %s, %d\n", __FUNCTION__, __LINE__);
1223 return ret;
1224 }
1225 }
1226
1227 return ret;
1228}
LITE_OS_SEC_TEXT_INIT UINT32 LOS_EventDestroy(PEVENT_CB_S eventCB)
销毁指定的事件控制块
Definition: los_event.c:334
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskDelete(UINT32 taskID)
Delete a task.
Definition: los_task.c:968
unsigned int UINT32
Definition: los_typedef.h:57
UINT32 prereadTaskId
Definition: bcache.h:104
EVENT_CB_S bcacheEvent
Definition: bcache.h:112
函数调用图:
这是这个函数的调用关系图:

◆ BcacheAsyncPrereadInit()

UINT32 BcacheAsyncPrereadInit ( OsBcache bc)

在文件 bcache.c1184 行定义.

1185{
1186 UINT32 ret;
1187 TSK_INIT_PARAM_S appTask;
1188
1189 ret = LOS_EventInit(&bc->bcacheEvent);
1190 if (ret != ENOERR) {
1191 PRINT_ERR("Async event init failed in %s, %d\n", __FUNCTION__, __LINE__);
1192 return ret;
1193 }
1194
1195 (VOID)memset_s(&appTask, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
1197 appTask.uwStackSize = BCACHE_STATCK_SIZE;
1198 appTask.pcName = "bcache_async_task";
1199 appTask.usTaskPrio = BCACHE_PREREAD_PRIO;
1200 appTask.auwArgs[0] = (UINTPTR)bc;
1201 appTask.uwResved = LOS_TASK_STATUS_DETACHED;
1202 ret = LOS_TaskCreate(&bc->prereadTaskId, &appTask);
1203 if (ret != ENOERR) {
1204 PRINT_ERR("Bcache async task create failed in %s, %d\n", __FUNCTION__, __LINE__);
1205 }
1206
1207 return ret;
1208}
static VOID BcacheAsyncPrereadThread(VOID *arg)
Definition: bcache.c:1134
LITE_OS_SEC_TEXT_INIT UINT32 LOS_EventInit(PEVENT_CB_S eventCB)
初始化一个事件控制块
Definition: los_event.c:95
struct tagTskInitParam TSK_INIT_PARAM_S
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskCreate(UINT32 *taskID, TSK_INIT_PARAM_S *initParam)
创建任务,并使该任务进入ready状态,如果就绪队列中没有更高优先级的任务,则运行该任务
Definition: los_task.c:718
VOID *(* TSK_ENTRY_FUNC)(UINTPTR param1, UINTPTR param2, UINTPTR param3, UINTPTR param4)
Define the type of a task entrance function.
Definition: los_task.h:480
unsigned long UINTPTR
Definition: los_typedef.h:68
UINT16 usTaskPrio
Definition: los_task.h:505
UINTPTR auwArgs[4]
Definition: los_task.h:507
UINT32 uwStackSize
Definition: los_task.h:508
CHAR * pcName
Definition: los_task.h:509
TSK_ENTRY_FUNC pfnTaskEntry
Definition: los_task.h:504
UINT32 uwResved
Definition: los_task.h:513
函数调用图:
这是这个函数的调用关系图:

◆ BcacheClearCache()

INT32 BcacheClearCache ( OsBcache bc)

在文件 bcache.c706 行定义.

707{
708 OsBcacheBlock *block = NULL;
709 OsBcacheBlock *next = NULL;
710 LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(block, next, &bc->listHead, OsBcacheBlock, listNode) {
711 DelBlock(bc, block);
712 }
713 return 0;
714}
static void DelBlock(OsBcache *bc, OsBcacheBlock *block)
Definition: bcache.c:404
LOS_DL_LIST listHead
Definition: bcache.h:95
函数调用图:
这是这个函数的调用关系图:

◆ BcacheSyncThreadDeinit()

VOID BcacheSyncThreadDeinit ( const OsBcache bc)

在文件 bcache.c1055 行定义.

1056{
1057 if (bc != NULL) {
1058 if (LOS_TaskDelete(bc->syncTaskId) != ENOERR) {
1059 PRINT_ERR("Bcache sync task delete failed in %s, %d\n", __FUNCTION__, __LINE__);
1060 }
1061 }
1062}
UINT32 syncTaskId
Definition: bcache.h:115
函数调用图:
这是这个函数的调用关系图:

◆ BcacheSyncThreadInit()

VOID BcacheSyncThreadInit ( OsBcache bc,
INT32  id 
)

块缓存同步任务初始化,开了个内核任务.

在文件 bcache.c1037 行定义.

1038{
1039 UINT32 ret;
1040 TSK_INIT_PARAM_S appTask;
1041
1042 (VOID)memset_s(&appTask, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
1044 appTask.uwStackSize = BCACHE_STATCK_SIZE;
1045 appTask.pcName = "bcache_sync_task";
1046 appTask.usTaskPrio = g_syncThreadPrio;//任务优先级
1047 appTask.auwArgs[0] = (UINTPTR)id;
1048 appTask.uwResved = LOS_TASK_STATUS_DETACHED;
1049 ret = LOS_TaskCreate(&bc->syncTaskId, &appTask);
1050 if (ret != ENOERR) {
1051 PRINT_ERR("Bcache sync task create failed in %s, %d\n", __FUNCTION__, __LINE__);
1052 }
1053}
UINT32 g_syncThreadPrio
Definition: bcache.c:81
static VOID BcacheSyncThread(UINT32 id)
Definition: bcache.c:1024
函数调用图:
这是这个函数的调用关系图:

◆ BlockCacheDeinit()

VOID BlockCacheDeinit ( OsBcache bc)
Description:
The BlockCacheDeinit() function shall deinit the bcache and release resources.
参数
bc[IN] block cache instance
注意
  • None.
返回值
#VOIDNone.
Dependency:

在文件 bcache.c1122 行定义.

1123{
1124 if (bcache != NULL) {
1125 (VOID)pthread_mutex_destroy(&bcache->bcacheMutex);
1126 free(bcache->memStart);
1127 bcache->memStart = NULL;
1128 free(bcache->rwBuffer);
1129 bcache->rwBuffer = NULL;
1130 free(bcache);
1131 }
1132}
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
int pthread_mutex_destroy(pthread_mutex_t *mutex)
销毁互斥锁
函数调用图:
这是这个函数的调用关系图:

◆ BlockCacheInit()

OsBcache * BlockCacheInit ( struct Vnode devNode,
UINT32  sectorSize,
UINT32  sectorPerBlock,
UINT32  blockNum,
UINT64  blockCount 
)
Description:
The BlockCacheInit() function shall alloc memory for bcache and init it.
参数
devNode[IN] dev node instance
sectorSize[IN] size of a sector
sectorPerBlock[IN] sector count per block in bcache
blockNum[IN] block number of bcache
blockCount[IN] block count of the disk
注意
  • None.
返回值
OsBcache* init succeded
#NULLinit failed
Dependency:

在文件 bcache.c1065 行定义.

1067{
1068 OsBcache *bcache = NULL;
1069 struct Vnode *blkDriver = devNode;
1070 UINT8 *bcacheMem = NULL;
1071 UINT8 *rwBuffer = NULL;
1072 UINT32 blockSize, memSize;
1073
1074 if ((blkDriver == NULL) || (sectorSize * sectorPerBlock * blockNum == 0) || (blockCount == 0)) {
1075 return NULL;
1076 }
1077
1078 blockSize = sectorSize * sectorPerBlock;
1079 if ((((UINT64)(sizeof(OsBcacheBlock) + blockSize) * blockNum) + DMA_ALLGN) > UINT_MAX) {
1080 return NULL;
1081 }
1082 memSize = ((sizeof(OsBcacheBlock) + blockSize) * blockNum) + DMA_ALLGN;
1083
1084 bcache = (OsBcache *)zalloc(sizeof(OsBcache));
1085 if (bcache == NULL) {
1086 PRINT_ERR("bcache_init : malloc %u Bytes failed!\n", sizeof(OsBcache));
1087 return NULL;
1088 }
1089
1090 bcacheMem = (UINT8 *)zalloc(memSize);
1091 if (bcacheMem == NULL) {
1092 PRINT_ERR("bcache_init : malloc %u Bytes failed!\n", memSize);
1093 goto ERROR_OUT_WITH_BCACHE;
1094 }
1095
1096 rwBuffer = (UINT8 *)memalign(DMA_ALLGN, blockSize);
1097 if (rwBuffer == NULL) {
1098 PRINT_ERR("bcache_init : malloc %u Bytes failed!\n", blockSize);
1099 goto ERROR_OUT_WITH_MEM;
1100 }
1101
1102 bcache->rwBuffer = rwBuffer;
1103 bcache->sectorSize = sectorSize;
1104 bcache->sectorPerBlock = sectorPerBlock;
1105 bcache->blockCount = blockCount;
1106
1107 if (BlockCacheDrvCreate(blkDriver, bcacheMem, memSize, blockSize, bcache) != ENOERR) {
1108 goto ERROR_OUT_WITH_BUFFER;
1109 }
1110
1111 return bcache;
1112
1113ERROR_OUT_WITH_BUFFER:
1114 free(rwBuffer);
1115ERROR_OUT_WITH_MEM:
1116 free(bcacheMem);
1117ERROR_OUT_WITH_BCACHE:
1118 free(bcache);
1119 return NULL;
1120}
INT32 BlockCacheDrvCreate(VOID *handle, UINT8 *memStart, UINT32 memSize, UINT32 blockSize, OsBcache *bc)
初始化块设备
Definition: bcache.c:800
long unsigned int UINT64
Definition: los_typedef.h:66
unsigned char UINT8
Definition: los_typedef.h:55
void * zalloc(size_t size)
Definition: malloc.c:91
void * memalign(size_t boundary, size_t size)
Definition: malloc.c:112
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
Definition: vnode.h:164
UINT32 sectorPerBlock
Definition: bcache.h:102
UINT8 * rwBuffer
Definition: bcache.h:110
UINT64 blockCount
Definition: bcache.h:100
UINT32 sectorSize
Definition: bcache.h:101
函数调用图:
这是这个函数的调用关系图:

◆ BlockCacheRead()

INT32 BlockCacheRead ( OsBcache bc,
UINT8 buf,
UINT32 len,
UINT64  pos,
BOOL  useRead 
)

读块设备缓存

Description:
The BlockCacheRead() function shall read data from the bcache, and if it doesn't hit, read the data from disk.
参数
bc[IN] block cache instance
buf[OUT] data buffer ptr
len[IN] number of bytes to read
num[IN] starting block number
pos[IN] starting position inside starting block
useRead[IN] whether use the read block or write block
注意
  • The block number is automatically adjusted if position is greater than block size.
返回值
#0read succeded
INT32read failed
Dependency:

在文件 bcache.c824 行定义.

825{
826 OsBcacheBlock *block = NULL;
827 UINT8 *tempBuf = buf;
828 UINT32 size;
829 UINT32 currentSize;
830 INT32 ret = ENOERR;
831 UINT64 pos;
832 UINT64 num;
833#ifdef BCACHE_ANALYSE
834 PRINTK("bcache read:\n");
835#endif
836
837 if (bc == NULL || buf == NULL || len == NULL) {
838 return -EPERM;
839 }
840
841 size = *len;
842 pos = sector * bc->sectorSize;
843 num = pos >> bc->blockSizeLog2;
844 pos = pos & (bc->blockSize - 1);
845
846 while (size > 0) {
847 if ((size + pos) > bc->blockSize) {
848 currentSize = bc->blockSize - (UINT32)pos;
849 } else {
850 currentSize = size;
851 }
852
854
855 /* useRead should be FALSE when reading large contiguous data */
856 ret = BcacheGetBlock(bc, num, useRead, &block);
857 if (ret != ENOERR) {
859 break;
860 }
861
862 if ((block->readFlag == FALSE) && (block->modified == TRUE)) {
863 ret = BcacheGetFlag(bc, block);
864 if (ret != ENOERR) {
866 return ret;
867 }
868 } else if ((block->readFlag == FALSE) && (block->modified == FALSE)) {
869 ret = BlockRead(bc, block, block->data);
870 if (ret != ENOERR) {
872 return ret;
873 }
874 }
875
876 if (LOS_CopyFromKernel((VOID *)tempBuf, size, (VOID *)(block->data + pos), currentSize) != EOK) {
878 return VFS_ERROR;
879 }
880
882
883 tempBuf += currentSize;
884 size -= currentSize;
885 pos = 0;
886 num++;
887 }
888 *len -= size;
889 return ret;
890}
static INT32 BlockRead(OsBcache *bc, OsBcacheBlock *block, UINT8 *buf)
Definition: bcache.c:246
static INT32 BcacheGetBlock(OsBcache *bc, UINT64 num, BOOL readData, OsBcacheBlock **dblock)
Definition: bcache.c:635
static INT32 BcacheGetFlag(OsBcache *bc, OsBcacheBlock *block)
Definition: bcache.c:266
signed int INT32
Definition: los_typedef.h:60
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
BOOL readFlag
Definition: bcache.h:72
UINT8 * data
Definition: bcache.h:70
BOOL modified
Definition: bcache.h:71
pthread_mutex_t bcacheMutex
Definition: bcache.h:111
UINT32 blockSizeLog2
Definition: bcache.h:99
UINT32 blockSize
Definition: bcache.h:98
INT32 LOS_CopyFromKernel(VOID *dest, UINT32 max, const VOID *src, UINT32 count)
将内核数据拷贝到用户空间
Definition: user_copy.c:88
函数调用图:
这是这个函数的调用关系图:

◆ BlockCacheSync()

INT32 BlockCacheSync ( OsBcache bc)

块缓存同步

Description:
The BlockCacheSync() function shall write-back all dirty data in the bcache into the disk.
参数
bc[IN] block cache instance
注意
  • None.
返回值
#0sync succeded
INT32sync failed
Dependency:

在文件 bcache.c951 行定义.

952{
953 return BcacheSync(bc);
954}
static INT32 BcacheSync(OsBcache *bc)
Definition: bcache.c:599
函数调用图:
这是这个函数的调用关系图:

◆ BlockCacheWrite()

INT32 BlockCacheWrite ( OsBcache bc,
const UINT8 buf,
UINT32 len,
UINT64  pos 
)

写块设备缓存

Description:
The BlockCacheWrite() function shall write data to the bcache.
参数
bc[IN] block cache instance
buf[IN] data buffer ptr
len[IN] number of bytes to write
num[IN] starting block number
pos[IN] starting position inside starting block
注意
  • The block number is automatically adjusted if position is greater than block size.
返回值
#0write succeded
INT32write failed
Dependency:

在文件 bcache.c892 行定义.

893{
894 OsBcacheBlock *block = NULL;
895 const UINT8 *tempBuf = buf;
896 UINT32 size = *len;
897 INT32 ret = ENOERR;
898 UINT32 currentSize;
899 UINT64 pos;
900 UINT64 num;
901#ifdef BCACHE_ANALYSE
902 PRINTK("bcache write:\n");
903#endif
904
905 pos = sector * bc->sectorSize;
906 num = pos >> bc->blockSizeLog2;
907 pos = pos & (bc->blockSize - 1);
908
909 D(("bcache write len = %u pos = %llu bnum = %llu\n", *len, pos, num));
910
911 while (size > 0) {
912 if ((size + pos) > bc->blockSize) {
913 currentSize = bc->blockSize - (UINT32)pos;
914 } else {
915 currentSize = size;
916 }
917
919 ret = BcacheGetBlock(bc, num, FALSE, &block);
920 if (ret != ENOERR) {
922 break;
923 }
924
925 if (LOS_CopyToKernel((VOID *)(block->data + pos), bc->blockSize - (UINT32)pos,
926 (VOID *)tempBuf, currentSize) != EOK) {
928 return VFS_ERROR;
929 }
930 if (block->modified == FALSE) {
931 block->modified = TRUE;
932 bc->modifiedBlock++;
933 }
934 if ((pos == 0) && (currentSize == bc->blockSize)) {
935 (void)memset_s(block->flag, sizeof(block->flag), 0xFF, sizeof(block->flag));
936 block->allDirty = TRUE;
937 } else {
938 BcacheSetFlag(bc, block, (UINT32)pos, currentSize);
939 }
941
942 tempBuf += currentSize;
943 size -= currentSize;
944 pos = 0;
945 num++;
946 }
947 *len -= size;
948 return ret;
949}
static VOID BcacheSetFlag(const OsBcache *bc, OsBcacheBlock *block, UINT32 pos, UINT32 size)
Definition: bcache.c:324
UINT32 flag[BCACHE_BLOCK_FLAGS]
Definition: bcache.h:68
BOOL allDirty
Definition: bcache.h:75
UINT32 modifiedBlock
Definition: bcache.h:113
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
INT32 LOS_CopyToKernel(VOID *dest, UINT32 max, const VOID *src, UINT32 count)
将用户空间的数据拷贝到内核空间
Definition: user_copy.c:101
函数调用图:
这是这个函数的调用关系图:

◆ OsSdSync()

INT32 OsSdSync ( INT32  id)

在文件 bcache.c956 行定义.

957{
958#ifdef LOSCFG_FS_FAT_CACHE
959 INT32 ret;
960 los_disk *disk = get_disk(id);
961 if ((disk == NULL) || (disk->disk_status == STAT_UNUSED)) {
962 return VFS_ERROR;
963 }
964 if (pthread_mutex_lock(&disk->disk_mutex) != ENOERR) {
965 PRINT_ERR("%s %d, mutex lock fail!\n", __FUNCTION__, __LINE__);
966 return VFS_ERROR;
967 }
968 if ((disk->disk_status == STAT_INUSED) && (disk->bcache != NULL)) {
969 ret = BcacheSync(disk->bcache);
970 } else {
971 ret = VFS_ERROR;
972 }
973 if (pthread_mutex_unlock(&disk->disk_mutex) != ENOERR) {
974 PRINT_ERR("%s %d, mutex unlock fail!\n", __FUNCTION__, __LINE__);
975 return VFS_ERROR;
976 }
977 return ret;
978#else
979 return VFS_ERROR;
980#endif
981}
@ STAT_UNUSED
Definition: disk.h:171
@ STAT_INUSED
Definition: disk.h:172
los_disk * get_disk(INT32 id)
Find disk driver.
Definition: disk.c:263
struct pthread_mutex disk_mutex
Definition: disk.h:191
OsBcache * bcache
Definition: disk.h:183
UINT32 disk_status
Definition: disk.h:178
函数调用图:
这是这个函数的调用关系图:

◆ ResumeAsyncPreread()

VOID ResumeAsyncPreread ( OsBcache arg1,
const OsBcacheBlock arg2 
)

在文件 bcache.c1169 行定义.

1170{
1171 UINT32 ret;
1172 OsBcache *bc = arg1;
1173 const OsBcacheBlock *block = arg2;
1174
1175 if (OsCurrTaskGet()->taskID != bc->prereadTaskId) {
1176 bc->curBlockNum = block->num;
1177 ret = LOS_EventWrite(&bc->bcacheEvent, ASYNC_EVENT_BIT);
1178 if (ret != ENOERR) {
1179 PRINT_ERR("Write event failed in %s, %d\n", __FUNCTION__, __LINE__);
1180 }
1181 }
1182}
LITE_OS_SEC_TEXT UINT32 LOS_EventWrite(PEVENT_CB_S eventCB, UINT32 events)
写指定的事件类型
Definition: los_event.c:318
STATIC INLINE LosTaskCB * OsCurrTaskGet(VOID)
UINT64 num
Definition: bcache.h:67
UINT64 curBlockNum
Definition: bcache.h:105
函数调用图:
这是这个函数的调用关系图: