更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
bcache.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _BCACHE_H
33#define _BCACHE_H
34
35#include "pthread.h"
36#include "linux/rbtree.h"
37#include "los_list.h"
38#include "vnode.h"
39
40#ifdef __cplusplus
41#if __cplusplus
42extern "C" {
43#endif /* __cplusplus */
44#endif /* __cplusplus */
45
46#define ALIGN_LIB(x) (((x) + (HALARC_ALIGNMENT - 1)) & ~(HALARC_ALIGNMENT - 1))
47#define ALIGN_DISP(x) (HALARC_ALIGNMENT - ((x) & (HALARC_ALIGNMENT - 1)))
48#define BCACHE_PREREAD_PRIO 12
49#define UNSIGNED_INTEGER_BITS 32
50#define UNINT_MAX_SHIFT_BITS 31
51#define UNINT_LOG2_SHIFT 5
52#define PREREAD_BLOCK_NUM 2
53#define EVEN_JUDGED 2
54#define PERCENTAGE 100
55#define PREREAD_EVENT_MASK 0xf
56
57#if CONFIG_FS_FAT_SECTOR_PER_BLOCK < UNSIGNED_INTEGER_BITS
58#error cache too small
59#else
60#define BCACHE_BLOCK_FLAGS (CONFIG_FS_FAT_SECTOR_PER_BLOCK / UNSIGNED_INTEGER_BITS)
61#endif
62
63typedef struct {
64 LOS_DL_LIST listNode; /* list node */
65 LOS_DL_LIST numNode; /* num node */
66 struct rb_node rbNode; /* red-black tree node */
67 UINT64 num; /* block number */
68 UINT32 flag[BCACHE_BLOCK_FLAGS];
70 UINT8 *data; /* block data */
71 BOOL modified; /* is this block data modified (needs write) */
72 BOOL readFlag; /* is the block data have read from sd(real data) */
73 BOOL readBuff; /* read write buffer */
74 BOOL used; /* used or free for write buf */
75 BOOL allDirty; /* the whole block is dirty */
77
78typedef INT32 (*BcacheReadFun)(struct Vnode *, /* private data */
79 UINT8 *, /* block buffer */
80 UINT32, /* number of blocks to read */
81 UINT64); /* starting block number */
82
83typedef INT32 (*BcacheWriteFun)(struct Vnode *, /* private data */
84 const UINT8 *, /* block buffer */
85 UINT32, /* number of blocks to write */
86 UINT64); /* starting block number */
87
88struct tagOsBcache;
89
90typedef VOID (*BcachePrereadFun)(struct tagOsBcache *, /* block cache instance space holder */
91 const OsBcacheBlock *); /* block data */
92
93typedef struct tagOsBcache {
94 VOID *priv; /* private data */
95 LOS_DL_LIST listHead; /* head of block list */
96 LOS_DL_LIST numHead; /* block num list */
97 struct rb_root rbRoot; /* block red-black tree root */
98 UINT32 blockSize; /* block size in bytes */
99 UINT32 blockSizeLog2; /* block size log2 */
100 UINT64 blockCount; /* block count of the disk */
101 UINT32 sectorSize; /* device sector size in bytes */
102 UINT32 sectorPerBlock; /* sector count per block */
103 UINT8 *memStart; /* memory base */
104 UINT32 prereadTaskId; /* preread task id */
105 UINT64 curBlockNum; /* current preread block number */
106 LOS_DL_LIST freeListHead; /* list of free blocks */
107 BcacheReadFun breadFun; /* block read function */
108 BcacheWriteFun bwriteFun; /* block write function */
109 BcachePrereadFun prereadFun; /* block preread function */
110 UINT8 *rwBuffer; /* buffer for bcache block */
111 pthread_mutex_t bcacheMutex; /* mutex for bcache */
112 EVENT_CB_S bcacheEvent; /* event for bcache */
113 UINT32 modifiedBlock; /* number of modified blocks */
114#ifdef LOSCFG_FS_FAT_CACHE_SYNC_THREAD
115 UINT32 syncTaskId; /* sync task id */
116#endif
117 OsBcacheBlock *wStart; /* write start block */
118 OsBcacheBlock *wEnd; /* write end block */
119 UINT64 sumNum; /* block num sum val */
120 UINT32 nBlock; /* current block count */
122
123/**
124 * @ingroup bcache
125 *
126 * @par Description:
127 * The BlockCacheRead() function shall read data from the bcache, and if it doesn't hit, read the data from disk.
128 *
129 * @param bc [IN] block cache instance
130 * @param buf [OUT] data buffer ptr
131 * @param len [IN] number of bytes to read
132 * @param num [IN] starting block number
133 * @param pos [IN] starting position inside starting block
134 * @param useRead [IN] whether use the read block or write block
135 *
136 * @attention
137 * <ul>
138 * <li>The block number is automatically adjusted if position is greater than block size.</li>
139 * </ul>
140 *
141 * @retval #0 read succeded
142 * @retval #INT32 read failed
143 *
144 * @par Dependency:
145 * <ul><li>bcache.h</li></ul>
146 *
147 */
149 UINT8 *buf,
150 UINT32 *len,
151 UINT64 pos,
152 BOOL useRead);
153
154/**
155 * @ingroup bcache
156 *
157 * @par Description:
158 * The BlockCacheWrite() function shall write data to the bcache.
159 *
160 * @param bc [IN] block cache instance
161 * @param buf [IN] data buffer ptr
162 * @param len [IN] number of bytes to write
163 * @param num [IN] starting block number
164 * @param pos [IN] starting position inside starting block
165 *
166 * @attention
167 * <ul>
168 * <li>The block number is automatically adjusted if position is greater than block size.</li>
169 * </ul>
170 *
171 * @retval #0 write succeded
172 * @retval #INT32 write failed
173 *
174 * @par Dependency:
175 * <ul><li>bcache.h</li></ul>
176 *
177 */
179 const UINT8 *buf,
180 UINT32 *len,
181 UINT64 pos);
182
183/**
184 * @ingroup bcache
185 *
186 * @par Description:
187 * The BlockCacheSync() function shall write-back all dirty data in the bcache into the disk.
188 *
189 * @param bc [IN] block cache instance
190 *
191 * @attention
192 * <ul>
193 * <li>None.</li>
194 * </ul>
195 *
196 * @retval #0 sync succeded
197 * @retval #INT32 sync failed
198 *
199 * @par Dependency:
200 * <ul><li>bcache.h</li></ul>
201 *
202 */
204
205/**
206 * @ingroup bcache
207 *
208 * @par Description:
209 * The BlockCacheInit() function shall alloc memory for bcache and init it.
210 *
211 * @param devNode [IN] dev node instance
212 * @param sectorSize [IN] size of a sector
213 * @param sectorPerBlock [IN] sector count per block in bcache
214 * @param blockNum [IN] block number of bcache
215 * @param blockCount [IN] block count of the disk
216 *
217 * @attention
218 * <ul>
219 * <li>None.</li>
220 * </ul>
221 *
222 * @retval #OsBcache * init succeded
223 * @retval #NULL init failed
224 *
225 * @par Dependency:
226 * <ul><li>bcache.h</li></ul>
227 *
228 */
229OsBcache *BlockCacheInit(struct Vnode *devNode,
230 UINT32 sectorSize,
231 UINT32 sectorPerBlock,
232 UINT32 blockNum,
233 UINT64 blockCount);
234
235/**
236 * @ingroup bcache
237 *
238 * @par Description:
239 * The BlockCacheDeinit() function shall deinit the bcache and release resources.
240 *
241 * @param bc [IN] block cache instance
242 *
243 * @attention
244 * <ul>
245 * <li>None.</li>
246 * </ul>
247 *
248 * @retval #VOID None.
249 *
250 * @par Dependency:
251 * <ul><li>bcache.h</li></ul>
252 *
253 */
254VOID BlockCacheDeinit(OsBcache *bc);
255
258
259#ifdef LOSCFG_FS_FAT_CACHE_SYNC_THREAD
261VOID BcacheSyncThreadDeinit(const OsBcache *bc);
262#endif
263
265
266VOID ResumeAsyncPreread(OsBcache *arg1, const OsBcacheBlock *arg2);
267
269
270#ifdef __cplusplus
271#if __cplusplus
272}
273#endif /* __cplusplus */
274#endif /* __cplusplus */
275#endif /* _BCACHE_H */
VOID ResumeAsyncPreread(OsBcache *arg1, const OsBcacheBlock *arg2)
Definition: bcache.c:1169
VOID(* BcachePrereadFun)(struct tagOsBcache *, const OsBcacheBlock *)
Definition: bcache.h:90
OsBcache * BlockCacheInit(struct Vnode *devNode, UINT32 sectorSize, UINT32 sectorPerBlock, UINT32 blockNum, UINT64 blockCount)
Definition: bcache.c:1065
VOID BlockCacheDeinit(OsBcache *bc)
Definition: bcache.c:1122
INT32 BcacheClearCache(OsBcache *bc)
Definition: bcache.c:706
INT32 OsSdSync(INT32 id)
Definition: bcache.c:956
INT32 BlockCacheRead(OsBcache *bc, UINT8 *buf, UINT32 *len, UINT64 pos, BOOL useRead)
读块设备缓存
Definition: bcache.c:824
INT32 BlockCacheSync(OsBcache *bc)
块缓存同步
Definition: bcache.c:951
VOID BcacheSyncThreadInit(OsBcache *bc, INT32 id)
块缓存同步任务初始化,开了个内核任务.
Definition: bcache.c:1037
INT32 BlockCacheWrite(OsBcache *bc, const UINT8 *buf, UINT32 *len, UINT64 pos)
写块设备缓存
Definition: bcache.c:892
INT32(* BcacheWriteFun)(struct Vnode *, const UINT8 *, UINT32, UINT64)
Definition: bcache.h:83
UINT32 BcacheAsyncPrereadInit(OsBcache *bc)
Definition: bcache.c:1184
VOID BcacheSyncThreadDeinit(const OsBcache *bc)
Definition: bcache.c:1055
struct tagOsBcache OsBcache
UINT32 BcacheAsyncPrereadDeinit(OsBcache *bc)
Definition: bcache.c:1210
INT32(* BcacheReadFun)(struct Vnode *, UINT8 *, UINT32, UINT64)
Definition: bcache.h:78
双向链表由内联函数实现 http://weharmonyos.com/openharmony/zh-cn/device-dev/kernel/kernel-small-apx-dll....
signed int INT32
Definition: los_typedef.h:60
long unsigned int UINT64
Definition: los_typedef.h:66
unsigned char UINT8
Definition: los_typedef.h:55
unsigned int UINT32
Definition: los_typedef.h:57
size_t BOOL
Definition: los_typedef.h:88
BOOL used
Definition: bcache.h:74
LOS_DL_LIST numNode
Definition: bcache.h:65
BOOL readBuff
Definition: bcache.h:73
BOOL readFlag
Definition: bcache.h:72
BOOL allDirty
Definition: bcache.h:75
LOS_DL_LIST listNode
Definition: bcache.h:64
UINT8 * data
Definition: bcache.h:70
UINT32 pgHit
Definition: bcache.h:69
UINT64 num
Definition: bcache.h:67
BOOL modified
Definition: bcache.h:71
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
Definition: vnode.h:164
UINT32 modifiedBlock
Definition: bcache.h:113
LOS_DL_LIST freeListHead
Definition: bcache.h:106
OsBcacheBlock * wEnd
Definition: bcache.h:118
LOS_DL_LIST listHead
Definition: bcache.h:95
UINT32 nBlock
Definition: bcache.h:120
pthread_mutex_t bcacheMutex
Definition: bcache.h:111
BcachePrereadFun prereadFun
Definition: bcache.h:109
UINT8 * memStart
Definition: bcache.h:103
BcacheWriteFun bwriteFun
Definition: bcache.h:108
UINT32 sectorPerBlock
Definition: bcache.h:102
UINT32 prereadTaskId
Definition: bcache.h:104
VOID * priv
Definition: bcache.h:94
struct rb_root rbRoot
Definition: bcache.h:97
UINT8 * rwBuffer
Definition: bcache.h:110
UINT64 sumNum
Definition: bcache.h:119
LOS_DL_LIST numHead
Definition: bcache.h:96
UINT64 blockCount
Definition: bcache.h:100
UINT32 syncTaskId
Definition: bcache.h:115
UINT32 blockSizeLog2
Definition: bcache.h:99
BcacheReadFun breadFun
Definition: bcache.h:107
UINT64 curBlockNum
Definition: bcache.h:105
UINT32 sectorSize
Definition: bcache.h:101
UINT32 blockSize
Definition: bcache.h:98
EVENT_CB_S bcacheEvent
Definition: bcache.h:112
OsBcacheBlock * wStart
Definition: bcache.h:117