xref: /kernel/liteos_a/fs/vfs/include/bcache/bcache.h (revision 0d163575)
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];
69    UINT32 pgHit;
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 */
76} OsBcacheBlock;
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 */
121} OsBcache;
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 succeeded
142 * @retval #INT32       read failed
143 *
144 * @par Dependency:
145 * <ul><li>bcache.h</li></ul>
146 *
147 */
148INT32 BlockCacheRead(OsBcache *bc,
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 succeeded
172 * @retval #INT32       write failed
173 *
174 * @par Dependency:
175 * <ul><li>bcache.h</li></ul>
176 *
177 */
178INT32 BlockCacheWrite(OsBcache *bc,
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 succeeded
197 * @retval #INT32       sync failed
198 *
199 * @par Dependency:
200 * <ul><li>bcache.h</li></ul>
201 *
202 */
203INT32 BlockCacheSync(OsBcache *bc);
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 succeeded
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
256INT32 BcacheClearCache(OsBcache *bc);
257INT32 OsSdSync(INT32 id);
258
259#ifdef LOSCFG_FS_FAT_CACHE_SYNC_THREAD
260VOID BcacheSyncThreadInit(OsBcache *bc, INT32 id);
261VOID BcacheSyncThreadDeinit(const OsBcache *bc);
262#endif
263
264UINT32 BcacheAsyncPrereadInit(OsBcache *bc);
265
266VOID ResumeAsyncPreread(OsBcache *arg1, const OsBcacheBlock *arg2);
267
268UINT32 BcacheAsyncPrereadDeinit(OsBcache *bc);
269
270#ifdef __cplusplus
271#if __cplusplus
272}
273#endif  /* __cplusplus */
274#endif  /* __cplusplus */
275#endif  /* _BCACHE_H */
276