更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
Disk 的协作图:

函数

INT32 los_disk_init (const CHAR *diskName, const struct block_operations *bops, VOID *priv, INT32 diskID, VOID *info)
 Disk driver initialization. 更多...
 
INT32 los_disk_deinit (INT32 diskID)
 Destroy a disk driver. 更多...
 
INT32 los_disk_read (INT32 drvID, VOID *buf, UINT64 sector, UINT32 count, BOOL useRead)
 Read data from disk driver. 更多...
 
INT32 los_disk_write (INT32 drvID, const VOID *buf, UINT64 sector, UINT32 count)
 Write data to a disk driver. 更多...
 
INT32 los_disk_ioctl (INT32 drvID, INT32 cmd, VOID *buf)
 Get information of disk driver. 更多...
 
INT32 los_disk_sync (INT32 drvID)
 Sync blib cache. 更多...
 
INT32 los_disk_set_bcache (INT32 drvID, UINT32 sectorPerBlock, UINT32 blockNum)
 Set blib cache for the disk driver. 更多...
 
INT32 los_part_read (INT32 pt, VOID *buf, UINT64 sector, UINT32 count, BOOL useRead)
 Read data from chosen partition. 更多...
 
INT32 los_part_write (INT32 pt, const VOID *buf, UINT64 sector, UINT32 count)
 Write data to chosen partition. 更多...
 
INT32 los_disk_cache_clear (INT32 drvID)
 Clear the bcache data 更多...
 
INT32 los_part_ioctl (INT32 pt, INT32 cmd, VOID *buf)
 Get information of chosen partition. 更多...
 
INT32 los_part_access (const CHAR *dev, mode_t mode)
 Decide the chosen partition is exist or not. 更多...
 
los_partlos_part_find (struct Vnode *blkDriver)
 Find disk partition. 更多...
 
los_diskget_disk (INT32 id)
 Find disk driver. 更多...
 
los_partget_part (INT32 id)
 Find disk partition. 更多...
 
VOID show_part (los_part *part)
 Print partition information. 更多...
 
INT32 add_mmc_partition (struct disk_divide_info *info, size_t sectorStart, size_t sectorCount)
 Add a new mmc partition. 更多...
 
INT32 los_alloc_diskid_byname (const CHAR *diskName)
 alloc a new UNUSED disk id. 更多...
 
INT32 los_get_diskid_byname (const CHAR *diskName)
 get the INUSED disk id. 更多...
 
VOID OsSetUsbStatus (UINT32 diskId)
 Set usb mode. 更多...
 
VOID OsClearUsbStatus (UINT32 diskId)
 Set usb mode. 更多...
 

详细描述

函数说明

◆ add_mmc_partition()

INT32 add_mmc_partition ( struct disk_divide_info info,
size_t  sectorStart,
size_t  sectorCount 
)

Add a new mmc partition.

Description:
Add a new mmc partition, users can set the start sector and size of the new partition.
注意
参数
info[IN] Type #struct disk_divide_info * Disk driver information structure pointer.
sectorStart[IN] Type size_t Start sector number of the new partition.
sectorCount[IN] Type size_t Sector count of the new partition.
返回值
#0Add partition success.
#-1Add partition failed.
Dependency:
参见
None

Add a new mmc partition.

参数
info
sectorStart
sectorCount
返回
INT32

在文件 disk.c1779 行定义.

1780{
1781 UINT32 index, i;
1782
1783 if (info == NULL) {
1784 return VFS_ERROR;
1785 }
1786 //磁盘判断
1787 if ((info->part_count >= MAX_DIVIDE_PART_PER_DISK) || (sectorCount == 0)) {
1788 return VFS_ERROR;
1789 }
1790 //扇区判断
1791 if ((sectorCount > info->sector_count) || ((info->sector_count - sectorCount) < sectorStart)) {
1792 return VFS_ERROR;
1793 }
1794
1795 index = info->part_count;
1796 for (i = 0; i < index; i++) {//验证目的是确保分区的顺序,扇区顺序从小到大排列
1797 if (sectorStart < (info->part[i].sector_start + info->part[i].sector_count)) {
1798 return VFS_ERROR;
1799 }
1800 }
1801
1802 info->part[index].sector_start = sectorStart;//开始扇区
1803 info->part[index].sector_count = sectorCount;//扇区总数
1804 info->part[index].type = EMMC;//分区类型
1805 info->part_count++;//分区数量增加,鸿蒙分区数量上限默认是80个.
1806
1807 return ENOERR;
1808}
unsigned int UINT32
Definition: los_typedef.h:57
UINT32 part_count
Definition: disk.h:227
UINT64 sector_count
Definition: disk.h:225
struct partition_info part[MAX_DIVIDE_PART_PER_DISK+MAX_PRIMARY_PART_PER_DISK]
Definition: disk.h:232
UINT8 type
Definition: disk.h:219
UINT64 sector_count
Definition: disk.h:221
UINT64 sector_start
Definition: disk.h:220
这是这个函数的调用关系图:

◆ get_disk()

los_disk * get_disk ( INT32  id)

Find disk driver.

Description:
By disk driver id number to find disk dirver.
注意
参数
id[IN] Type INT32 disk id number,less than the value defined by SYS_MAX_DISK.
返回值
#NULLCan't find chosen disk driver.
los_disk* This is disk structure pointer of chosen disk driver.
Dependency:
参见
None

在文件 disk.c263 行定义.

264{
265 if ((id >= 0) && (id < SYS_MAX_DISK)) {
266 return &g_sysDisk[id];
267 }
268
269 return NULL;
270}
los_disk g_sysDisk[SYS_MAX_DISK]
支持挂载的磁盘总数量 5个
Definition: disk.c:76
这是这个函数的调用关系图:

◆ get_part()

los_part * get_part ( INT32  id)

Find disk partition.

Description:
By driver partition id number to find disk partition.
注意
参数
id[IN] Type INT32 partition id number,less than the value defined by SYS_MAX_PART.
返回值
#NULLCan't find chosen disk partition.
los_part* This is partition structure pointer of chosen disk partition.
Dependency:
参见
None

Find disk partition.

在文件 disk.c272 行定义.

273{
274 if ((id >= 0) && (id < SYS_MAX_PART)) {
275 return &g_sysPart[id];
276 }
277
278 return NULL;
279}
los_part g_sysPart[SYS_MAX_PART]
支持磁盘的分区总数量 5*16,每个磁盘最大分16个区
Definition: disk.c:77
这是这个函数的调用关系图:

◆ los_alloc_diskid_byname()

INT32 los_alloc_diskid_byname ( const CHAR diskName)

alloc a new UNUSED disk id.

Description:
Get a free disk id for new device.
注意
  • The parameter diskName must point a valid string, which end with the null byte ('\0')
  • The total length of parameter diskName must be less than the value defined by DISK_NAME
参数
diskName[IN] Type #const CHAR * device name.
返回值
INT32available disk id
#-1alloc disk id failed
Dependency:
参见
los_get_diskid_byname

alloc a new UNUSED disk id.

在文件 disk.c134 行定义.

135{
136 INT32 diskID;
137 los_disk *disk = NULL;
138 UINT32 intSave;
139 size_t nameLen;
140
141 if (diskName == NULL) {
142 PRINT_ERR("The parameter disk_name is NULL");
143 return VFS_ERROR;
144 }
145
146 nameLen = strlen(diskName);
147 if (nameLen > DISK_NAME) {
148 PRINT_ERR("diskName is too long!\n");
149 return VFS_ERROR;
150 }
151 spin_lock_irqsave(&g_diskSpinlock, intSave);//加锁
152
153 for (diskID = 0; diskID < SYS_MAX_DISK; diskID++) {//磁盘池中分配一个磁盘描述符
154 disk = get_disk(diskID);
155 if ((disk != NULL) && (disk->disk_status == STAT_UNUSED)) {
156 disk->disk_status = STAT_UNREADY;//初始状态,可理解为未格式化
157 break;
158 }
159 }
160
161 spin_unlock_irqrestore(&g_diskSpinlock, intSave);
162
163 if ((disk == NULL) || (diskID == SYS_MAX_DISK)) {
164 PRINT_ERR("los_alloc_diskid_byname failed %d!\n", diskID);
165 return VFS_ERROR;
166 }
167
168 if (disk->disk_name != NULL) {//分配的磁盘如果有名称,可能之前用过,先释放内核内存
170 disk->disk_name = NULL;
171 }
172
173 disk->disk_name = LOS_MemAlloc(m_aucSysMem0, (nameLen + 1));
174 if (disk->disk_name == NULL) {
175 PRINT_ERR("los_alloc_diskid_byname alloc disk name failed\n");
176 return VFS_ERROR;
177 }
178 //disk->disk_name 等于 参数 名称
179 if (strncpy_s(disk->disk_name, (nameLen + 1), diskName, nameLen) != EOK) {
180 PRINT_ERR("The strncpy_s failed.\n");
182 disk->disk_name = NULL;
183 return VFS_ERROR;
184 }
185
186 disk->disk_name[nameLen] = '\0';
187
188 return diskID;
189}
spinlock_t g_diskSpinlock
磁盘自锁锁
Definition: disk.c:82
@ STAT_UNUSED
Definition: disk.h:171
@ STAT_UNREADY
Definition: disk.h:173
los_disk * get_disk(INT32 id)
Find disk driver.
Definition: disk.c:263
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
Definition: los_memory.c:1123
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
UINT8 * m_aucSysMem0
异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
Definition: los_memory.c:107
signed int INT32
Definition: los_typedef.h:60
CHAR * disk_name
Definition: disk.h:189
UINT32 disk_status
Definition: disk.h:178
函数调用图:
这是这个函数的调用关系图:

◆ los_disk_cache_clear()

INT32 los_disk_cache_clear ( INT32  drvID)

Clear the bcache data

Description:
Flush the data and mark the block as unused.
注意
参数
drvID[IN] Type INT32 disk id
返回值
#0Write success.
#-1Write failed.
Dependency:
参见
los_part_read

在文件 disk.c1254 行定义.

1255{
1256 INT32 result = ENOERR;
1257#ifdef LOSCFG_FS_FAT_CACHE
1258 los_part *part = get_part(drvID);
1259 los_disk *disk = NULL;
1260
1261 if (part == NULL) {
1262 return VFS_ERROR;
1263 }
1264 result = OsSdSync(part->disk_id);
1265 if (result != ENOERR) {
1266 PRINTK("[ERROR]disk_cache_clear SD sync failed!\n");
1267 return result;
1268 }
1269
1270 disk = get_disk(part->disk_id);
1271 if (disk == NULL) {
1272 return VFS_ERROR;
1273 }
1274
1275 DISK_LOCK(&disk->disk_mutex);
1276 result = BcacheClearCache(disk->bcache);
1277 DISK_UNLOCK(&disk->disk_mutex);
1278#endif
1279 return result;
1280}
INT32 BcacheClearCache(OsBcache *bc)
Definition: bcache.c:706
INT32 OsSdSync(INT32 id)
Definition: bcache.c:956
los_part * get_part(INT32 id)
获取某个分区的描述符
Definition: disk.c:272
struct pthread_mutex disk_mutex
Definition: disk.h:191
OsBcache * bcache
Definition: disk.h:183
UINT32 disk_id
Definition: disk.h:198
函数调用图:

◆ los_disk_deinit()

INT32 los_disk_deinit ( INT32  diskID)

Destroy a disk driver.

Description:
Destroy a disk driver, free the dependent resource.
注意
参数
diskID[IN] Type INT32 disk driver id number, less than the value defined by SYS_MAX_DISK.
返回值
#0Destroy success.
#-1Destroy failed.
Dependency:
参见
los_disk_init

Destroy a disk driver.

在文件 disk.c1544 行定义.

1545{
1546 int ret;
1547 los_disk *disk = get_disk(diskID);
1548 if (disk == NULL) {
1549 return -EINVAL;
1550 }
1551 ret = ForceUmountDev(disk->dev);
1552 PRINTK("warning: %s lost, force umount ret = %d\n", disk->disk_name, ret);
1553
1554 DISK_LOCK(&disk->disk_mutex);
1555
1556 if (disk->disk_status != STAT_INUSED) {
1557 DISK_UNLOCK(&disk->disk_mutex);
1558 return -EINVAL;
1559 }
1560
1561 disk->disk_status = STAT_UNREADY;//未格式化状态
1562 DISK_UNLOCK(&disk->disk_mutex);
1563
1564 return DiskDeinit(disk);
1565}
static INT32 DiskDeinit(los_disk *disk)
磁盘反初始化
Definition: disk.c:1400
@ STAT_INUSED
Definition: disk.h:172
int ForceUmountDev(struct Vnode *dev)
struct Vnode * dev
Definition: disk.h:181
函数调用图:
这是这个函数的调用关系图:

◆ los_disk_init()

INT32 los_disk_init ( const CHAR diskName,
const struct block_operations *  bops,
VOID *  priv,
INT32  diskID,
VOID *  info 
)

Disk driver initialization.

Description:
Initializate a disk dirver, and set the block cache.
注意
  • The parameter diskName must point a valid string, which end with the terminating null byte.
  • The total length of parameter diskName must be less than the value defined by PATH_MAX.
  • The parameter bops must pointed the right functions, otherwise the system will crash when the disk is being operated.
  • The parameter info can be null or point to struct disk_divide_info. when info is null, the disk will be divided base the information of MBR, otherwise, the disk will be divided base the information of parameter info.
参数
diskName[IN] Type #const CHAR * disk driver name.
bops[IN] Type #const struct block_operations * block driver control structure.
priv[IN] Type #VOID * private data of vnode.
diskID[IN] Type INT32 disk id number, less than SYS_MAX_DISK.
info[IN] Type #VOID * disk driver partition information.
返回值
#0Initialization success.
#-1Initialization failed.
Dependency:
参见
los_disk_deinit

Disk driver initialization.

在文件 disk.c1481 行定义.

1483{
1484 struct geometry diskInfo;
1485 struct Vnode *blkDriver = NULL;
1486 los_disk *disk = get_disk(diskID);
1487 INT32 ret;
1488 //参数检查
1489 if ((diskName == NULL) || (disk == NULL) ||
1490 (disk->disk_status != STAT_UNREADY) || (strlen(diskName) > DISK_NAME)) {
1491 return VFS_ERROR;
1492 }
1493 //注册块设备驱动,设备驱动程序由设备提供
1494 if (register_blockdriver(diskName, bops, RWE_RW_RW, priv) != 0) {
1495 PRINT_ERR("disk_init : register %s fail!\n", diskName);
1496 return VFS_ERROR;
1497 }
1498
1499 VnodeHold();
1500 ret = VnodeLookup(diskName, &blkDriver, 0);
1501 if (ret < 0) {
1502 VnodeDrop();
1503 ret = ENOENT;
1504 goto DISK_FIND_ERROR;
1505 }
1506 struct block_operations *bops2 = (struct block_operations *)((struct drv_data *)blkDriver->data)->ops;
1507 //块操作,块是文件系统层面的概念,块(Block)是文件系统存取数据的最小单位,一般大小是4KB
1508 if ((bops2 == NULL) || (bops2->geometry == NULL) || (bops2->geometry(blkDriver, &diskInfo) != 0)) {
1509 goto DISK_BLKDRIVER_ERROR;
1510 }
1511
1512 if (diskInfo.geo_sectorsize < DISK_MAX_SECTOR_SIZE) {
1513 goto DISK_BLKDRIVER_ERROR;
1514 }
1515
1516 ret = OsDiskInitSub(diskName, diskID, disk, &diskInfo, blkDriver);
1517 if (ret != ENOERR) {
1518 (VOID)DiskDeinit(disk);
1519 VnodeDrop();
1520 return VFS_ERROR;
1521 }
1522 VnodeDrop();
1523 if (DiskDivideAndPartitionRegister(info, disk) != ENOERR) {
1524 (VOID)DiskDeinit(disk);
1525 return VFS_ERROR;
1526 }
1527
1528 disk->disk_status = STAT_INUSED;//磁盘状态变成使用中
1529 if (info != NULL) {//https://www.huaweicloud.com/articles/bcdefd0d9da5de83d513123ef3aabcf0.html
1530 disk->type = EMMC;//eMMC 是 embedded MultiMediaCard 的简称
1531 } else {
1532 disk->type = OTHERS;
1533 }
1534 return ENOERR;
1535
1536DISK_BLKDRIVER_ERROR:
1537 PRINT_ERR("disk_init : register %s ok but get disk info fail!\n", diskName);
1538 VnodeDrop();
1539DISK_FIND_ERROR:
1540 (VOID)unregister_blockdriver(diskName);//注销块设备驱动
1541 return VFS_ERROR;
1542}
static UINT32 OsDiskInitSub(const CHAR *diskName, INT32 diskID, los_disk *disk, struct geometry *diskInfo, struct Vnode *blkDriver)
磁盘初始化
Definition: disk.c:1452
static INT32 DiskDivideAndPartitionRegister(struct disk_divide_info *info, los_disk *disk)
磁盘分区和注册分区
Definition: disk.c:1380
UINT8 type
Definition: disk.h:188
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
Definition: vnode.h:164
void * data
Definition: vnode.h:176
int VnodeDrop(void)
归还锁
Definition: vnode.c:292
int VnodeHold(void)
拿锁,封装互斥量
Definition: vnode.c:283
int VnodeLookup(const char *path, struct Vnode **vnode, uint32_t flags)
通过路径查询vnode节点
Definition: vnode.c:491
函数调用图:
这是这个函数的调用关系图:

◆ los_disk_ioctl()

INT32 los_disk_ioctl ( INT32  drvID,
INT32  cmd,
VOID *  buf 
)

Get information of disk driver.

Description:
Get information of disk driver.
注意
参数
drvID[IN] Type INT32 disk driver id number, less than the value defined by SYS_MAX_DISK.
cmd[IN] Type INT32 command to issu, currently support GET_SECTOR_COUNT, GET_SECTOR_SIZE, GET_BLOCK_SIZE, CTRL_SYNC.
buf[OUT] Type #VOID * memory to storage the information, the size must enough for data type(UINT64) when cmd type is DISK_GET_SECTOR_COUNT, others is size_t.
返回值
#0Get information success.
#-1Get information failed.
Dependency:
参见
None

在文件 disk.c1027 行定义.

1028{
1029 struct geometry info;
1030 los_disk *disk = get_disk(drvID);
1031 if (disk == NULL) {
1032 return VFS_ERROR;
1033 }
1034
1035 DISK_LOCK(&disk->disk_mutex);
1036
1037 if ((disk->dev == NULL) || (disk->disk_status != STAT_INUSED)) {
1038 goto ERROR_HANDLE;
1039 }
1040
1041 if (cmd == DISK_CTRL_SYNC) {
1042 DISK_UNLOCK(&disk->disk_mutex);
1043 return ENOERR;
1044 }
1045
1046 if (buf == NULL) {
1047 goto ERROR_HANDLE;
1048 }
1049
1050 (VOID)memset_s(&info, sizeof(info), 0, sizeof(info));
1051
1052 struct block_operations *bops = (struct block_operations *)((struct drv_data *)disk->dev->data)->ops;
1053 if ((bops == NULL) || (bops->geometry == NULL) ||
1054 (bops->geometry(disk->dev, &info) != 0)) {
1055 goto ERROR_HANDLE;
1056 }
1057
1058 if (cmd == DISK_GET_SECTOR_COUNT) {
1059 *(UINT64 *)buf = info.geo_nsectors;
1060 if (info.geo_nsectors == 0) {
1061 goto ERROR_HANDLE;
1062 }
1063 } else if (cmd == DISK_GET_SECTOR_SIZE) {
1064 *(size_t *)buf = info.geo_sectorsize;
1065 } else if (cmd == DISK_GET_BLOCK_SIZE) { /* Get erase block size in unit of sectors (UINT32) */
1066 /* Block Num SDHC == 512, SD can be set to 512 or other */
1067 *(size_t *)buf = DISK_MAX_SECTOR_SIZE / info.geo_sectorsize;
1068 } else {
1069 goto ERROR_HANDLE;
1070 }
1071
1072 DISK_UNLOCK(&disk->disk_mutex);
1073 return ENOERR;
1074
1075ERROR_HANDLE:
1076 DISK_UNLOCK(&disk->disk_mutex);
1077 return VFS_ERROR;
1078}
long unsigned int UINT64
Definition: los_typedef.h:66
if(tv==NULL)
Definition: time.c:430
函数调用图:

◆ los_disk_read()

INT32 los_disk_read ( INT32  drvID,
VOID *  buf,
UINT64  sector,
UINT32  count,
BOOL  useRead 
)

Read data from disk driver.

Description:
Read data from disk driver.
注意
  • The sector size of the disk to be read should be acquired by los_part_ioctl before calling this function.
  • The parameter buf must point to a valid memory and the buf size is count * sector_size.
参数
drvID[IN] Type INT32 disk driver id number, less than the value defined by SYS_MAX_DISK.
buf[OUT] Type #VOID * memory which used to store read data.
sector[IN] Type UINT64 expected start sector number to read.
count[IN] Type UINT32 expected sector count to read.
useRead[IN] Type BOOL set FALSE to use the write block for optimization
返回值
#0Read success.
#-1Read failed.
Dependency:
参见
los_disk_write

Read data from disk driver.

在文件 disk.c911 行定义.

912{
913#ifdef LOSCFG_FS_FAT_CACHE
914 UINT32 len;
915#endif
916 INT32 result = VFS_ERROR;
917 los_disk *disk = get_disk(drvID);
918
919 if ((buf == NULL) || (count == 0)) { /* buff equal to NULL or count equal to 0 */
920 return result;
921 }
922
923 if (disk == NULL) {
924 return result;
925 }
926
927 DISK_LOCK(&disk->disk_mutex);
928
929 if (disk->disk_status != STAT_INUSED) {
930 goto ERROR_HANDLE;
931 }
932
933 if ((count > disk->sector_count) || ((disk->sector_count - count) < sector)) {
934 goto ERROR_HANDLE;
935 }
936
937#ifdef LOSCFG_FS_FAT_CACHE
938 if (disk->bcache != NULL) {
939 if (((UINT64)(disk->bcache->sectorSize) * count) > UINT_MAX) {
940 goto ERROR_HANDLE;
941 }
942 len = disk->bcache->sectorSize * count;
943 /* useRead should be FALSE when reading large contiguous data *///读取大量连续数据时,useRead 应为 FALSE
944 result = BlockCacheRead(disk->bcache, (UINT8 *)buf, &len, sector, useRead);//从缓存区里读
945 if (result != ENOERR) {
946 PRINT_ERR("los_disk_read read err = %d, sector = %llu, len = %u\n", result, sector, len);
947 }
948 } else {
949 result = VFS_ERROR;
950 }
951#else
952 if (disk->dev == NULL) {
953 goto ERROR_HANDLE;
954 }
955 result = disk_read_directly(disk, buf, sector, count);
956#endif
957
958 if (result != ENOERR) {
959 goto ERROR_HANDLE;
960 }
961
962 DISK_UNLOCK(&disk->disk_mutex);
963 return ENOERR;
964
965ERROR_HANDLE:
966 DISK_UNLOCK(&disk->disk_mutex);
967 return VFS_ERROR;
968}
INT32 BlockCacheRead(OsBcache *bc, UINT8 *buf, UINT32 *len, UINT64 sector, BOOL useRead)
读块设备缓存
Definition: bcache.c:824
static INT32 disk_read_directly(los_disk *disk, VOID *buf, UINT64 sector, UINT32 count)
Definition: disk.c:840
unsigned char UINT8
Definition: los_typedef.h:55
UINT64 sector_count
Definition: disk.h:187
UINT32 sectorSize
Definition: bcache.h:101
函数调用图:
这是这个函数的调用关系图:

◆ los_disk_set_bcache()

INT32 los_disk_set_bcache ( INT32  drvID,
UINT32  sectorPerBlock,
UINT32  blockNum 
)

Set blib cache for the disk driver.

Description:
Set blib cache for the disk driver, users can set the number of sectors of per block, and the number of blocks.
注意
参数
drvID[IN] Type INT32 disk driver id number, less than the value defined by SYS_MAX_DISK.
sectorPerBlock[IN] Type UINT32 sector number of per block, only can be 32 * (1, 2, ..., 8).
blockNum[IN] Type UINT32 block number of cache.
返回值
#0Set success.
INT32Set failed.
Dependency:
参见
None

Set blib cache for the disk driver.

在文件 disk.c1591 行定义.

1592{
1593#ifdef LOSCFG_FS_FAT_CACHE
1594
1595 INT32 ret;
1596 UINT32 intSave;
1597 OsBcache *bc = NULL;
1598 los_disk *disk = get_disk(drvID);
1599 if ((disk == NULL) || (sectorPerBlock == 0)) {
1600 return EINVAL;
1601 }
1602
1603 /*
1604 * Because we use UINT32 flag[BCACHE_BLOCK_FLAGS] in bcache for sectors bitmap tag, so it must
1605 * be less than 32 * BCACHE_BLOCK_FLAGS.
1606 */
1607 if (((sectorPerBlock % UNSIGNED_INTEGER_BITS) != 0) ||
1608 ((sectorPerBlock >> UNINT_LOG2_SHIFT) > BCACHE_BLOCK_FLAGS)) {
1609 return EINVAL;
1610 }
1611
1612 DISK_LOCK(&disk->disk_mutex);
1613
1614 if (disk->disk_status != STAT_INUSED) {
1615 goto ERROR_HANDLE;
1616 }
1617
1618 if (disk->bcache != NULL) {
1619 ret = BlockCacheSync(disk->bcache);
1620 if (ret != ENOERR) {
1621 DISK_UNLOCK(&disk->disk_mutex);
1622 return ret;
1623 }
1624 }
1625
1626 spin_lock_irqsave(&g_diskFatBlockSpinlock, intSave);
1627 DiskCacheDeinit(disk);
1628
1629 g_uwFatBlockNums = blockNum;
1630 g_uwFatSectorsPerBlock = sectorPerBlock;
1631
1632 bc = BlockCacheInit(disk->dev, disk->sector_size, sectorPerBlock, blockNum, disk->sector_count / sectorPerBlock);
1633 if ((bc == NULL) && (blockNum != 0)) {
1634 spin_unlock_irqrestore(&g_diskFatBlockSpinlock, intSave);
1635 DISK_UNLOCK(&disk->disk_mutex);
1636 return ENOMEM;
1637 }
1638
1639 if (bc != NULL) {
1640 DiskCacheThreadInit((UINT32)drvID, bc);
1641 }
1642
1643 disk->bcache = bc;
1644 spin_unlock_irqrestore(&g_diskFatBlockSpinlock, intSave);
1645 DISK_UNLOCK(&disk->disk_mutex);
1646 return ENOERR;
1647
1648ERROR_HANDLE:
1649 DISK_UNLOCK(&disk->disk_mutex);
1650 return EINVAL;
1651#else
1652 return VFS_ERROR;
1653#endif
1654}
OsBcache * BlockCacheInit(struct Vnode *devNode, UINT32 sectorSize, UINT32 sectorPerBlock, UINT32 blockNum, UINT64 blockCount)
Definition: bcache.c:1065
INT32 BlockCacheSync(OsBcache *bc)
块缓存同步
Definition: bcache.c:951
static VOID DiskCacheDeinit(los_disk *disk)
Definition: disk.c:1326
static VOID DiskCacheThreadInit(UINT32 diskID, OsBcache *bc)
Definition: disk.c:1283
UINT32 g_uwFatBlockNums
块数量 默认28
Definition: disk.c:80
spinlock_t g_diskFatBlockSpinlock
磁盘Fat块自旋锁
Definition: disk.c:83
UINT32 g_uwFatSectorsPerBlock
每块支持扇区数 默认64个扇区
Definition: disk.c:79
UINT32 sector_size
Definition: disk.h:185
函数调用图:

◆ los_disk_sync()

INT32 los_disk_sync ( INT32  drvID)

Sync blib cache.

Description:
Sync blib cache, write the valid data to disk driver.
注意
参数
drvID[IN] Type INT32 disk driver id number, less than the value defined by SYS_MAX_DISK.
返回值
#0Sync success.
INT32Sync failed.
Dependency:
参见
None

Sync blib cache.

在文件 disk.c1567 行定义.

1568{
1569 INT32 ret = ENOERR;
1570 los_disk *disk = get_disk(drvID);
1571 if (disk == NULL) {
1572 return EINVAL;
1573 }
1574
1575 DISK_LOCK(&disk->disk_mutex);
1576 if (disk->disk_status != STAT_INUSED) {
1577 DISK_UNLOCK(&disk->disk_mutex);
1578 return EINVAL;
1579 }
1580
1581#ifdef LOSCFG_FS_FAT_CACHE
1582 if (disk->bcache != NULL) {
1583 ret = BlockCacheSync(disk->bcache);
1584 }
1585#endif
1586
1587 DISK_UNLOCK(&disk->disk_mutex);
1588 return ret;
1589}
函数调用图:

◆ los_disk_write()

INT32 los_disk_write ( INT32  drvID,
const VOID *  buf,
UINT64  sector,
UINT32  count 
)

Write data to a disk driver.

Description:
Write data to a disk driver.
注意
  • The sector size of the disk to be read should be acquired by los_part_ioctl before calling this function.
  • The parameter buf must point to a valid memory and the buf size is count * sector_size.
参数
drvID[IN] Type INT32 disk driver id number, less than the value defined by SYS_MAX_DISK.
buf[IN] Type #const VOID * memory which used to storage write data.
sector[IN] Type UINT64 expected start sector number to read.
count[IN] Type UINT32 experted sector count of write.
返回值
#0Write success.
#-1Write failed.
Dependency:
参见
los_disk_read

Write data to a disk driver.

在文件 disk.c970 行定义.

971{
972#ifdef LOSCFG_FS_FAT_CACHE
973 UINT32 len;
974#endif
975 INT32 result = VFS_ERROR;
976 los_disk *disk = get_disk(drvID);
977 if (disk == NULL || disk->dev == NULL || disk->dev->data == NULL) {
978 return result;
979 }
980
981 if ((buf == NULL) || (count == 0)) { /* buff equal to NULL or count equal to 0 */
982 return result;
983 }
984
985 DISK_LOCK(&disk->disk_mutex);
986
987 if (disk->disk_status != STAT_INUSED) {
988 goto ERROR_HANDLE;
989 }
990
991 if ((count > disk->sector_count) || ((disk->sector_count - count) < sector)) {
992 goto ERROR_HANDLE;
993 }
994
995#ifdef LOSCFG_FS_FAT_CACHE
996 if (disk->bcache != NULL) {
997 if (((UINT64)(disk->bcache->sectorSize) * count) > UINT_MAX) {
998 goto ERROR_HANDLE;
999 }
1000 len = disk->bcache->sectorSize * count;
1001 result = BlockCacheWrite(disk->bcache, (const UINT8 *)buf, &len, sector);//写入缓存,后续由缓存同步至磁盘
1002 if (result != ENOERR) {
1003 PRINT_ERR("los_disk_write write err = %d, sector = %llu, len = %u\n", result, sector, len);
1004 }
1005 } else {
1006 result = VFS_ERROR;
1007 }
1008#else
1009 if (disk->dev == NULL) {
1010 goto ERROR_HANDLE;
1011 }
1012 result = disk_write_directly(disk, buf, sector, count);
1013#endif
1014
1015 if (result != ENOERR) {
1016 goto ERROR_HANDLE;
1017 }
1018
1019 DISK_UNLOCK(&disk->disk_mutex);
1020 return ENOERR;
1021
1022ERROR_HANDLE:
1023 DISK_UNLOCK(&disk->disk_mutex);
1024 return VFS_ERROR;
1025}
INT32 BlockCacheWrite(OsBcache *bc, const UINT8 *buf, UINT32 *len, UINT64 sector)
写块设备缓存
Definition: bcache.c:892
static INT32 disk_write_directly(los_disk *disk, const VOID *buf, UINT64 sector, UINT32 count)
Definition: disk.c:875
函数调用图:
这是这个函数的调用关系图:

◆ los_get_diskid_byname()

INT32 los_get_diskid_byname ( const CHAR diskName)

get the INUSED disk id.

Description:
Get the corresponding INUSED disk id by diskName.
注意
  • The parameter diskName must point a valid string, which end with the null byte ('\0')
  • The total length of parameter diskName must be less than the value defined by DISK_NAME
参数
diskName[IN] Type #const CHAR * device name.
返回值
INT32available disk id
#-1get disk id failed
Dependency:
参见
los_alloc_diskid_byname

get the INUSED disk id.

在文件 disk.c191 行定义.

192{
193 INT32 diskID;
194 los_disk *disk = NULL;
195 size_t diskNameLen;
196
197 if (diskName == NULL) {
198 PRINT_ERR("The parameter diskName is NULL");
199 return VFS_ERROR;
200 }
201
202 diskNameLen = strlen(diskName);
203 if (diskNameLen > DISK_NAME) {
204 PRINT_ERR("diskName is too long!\n");
205 return VFS_ERROR;
206 }
207
208 for (diskID = 0; diskID < SYS_MAX_DISK; diskID++) {
209 disk = get_disk(diskID);
210 if ((disk != NULL) && (disk->disk_name != NULL) && (disk->disk_status == STAT_INUSED)) {
211 if (strlen(disk->disk_name) != diskNameLen) {
212 continue;
213 }
214 if (strcmp(diskName, disk->disk_name) == 0) {
215 break;
216 }
217 }
218 }
219 if ((disk == NULL) || (diskID == SYS_MAX_DISK)) {
220 PRINT_ERR("los_get_diskid_byname failed!\n");
221 return VFS_ERROR;
222 }
223 return diskID;
224}
函数调用图:
这是这个函数的调用关系图:

◆ los_part_access()

INT32 los_part_access ( const CHAR dev,
mode_t  mode 
)

Decide the chosen partition is exist or not.

Description:
Decide the chosen partition is exist or not.
注意
  • The parameter dev is a full path, which begin with '/' and end with '/0'.
参数
dev[IN] Type #const CHAR * partition driver name.
mode[IN] Type mode_t access modd.
返回值
#0The chosen partition is exist.
#-1The chosen partition is not exist.
Dependency:
参见
None

Decide the chosen partition is exist or not.

在文件 disk.c1706 行定义.

1707{
1708 los_part *part = NULL;
1709 struct Vnode *node = NULL;
1710
1711 VnodeHold();
1712 if (VnodeLookup(dev, &node, 0) < 0) {
1713 VnodeDrop();
1714 return VFS_ERROR;
1715 }
1716
1717 part = los_part_find(node);//通过节点找到分区
1718 VnodeDrop();
1719 if (part == NULL) {
1720 return VFS_ERROR;
1721 }
1722
1723 return ENOERR;
1724}
los_part * los_part_find(struct Vnode *blkDriver)
通过索引节点找到分区信息
Definition: disk.c:1682
函数调用图:

◆ los_part_find()

los_part * los_part_find ( struct Vnode blkDriver)

Find disk partition.

Description:
By driver partition vnode to find disk partition.
注意
参数
blkDriver[IN] Type #struct Vnode * partition driver vnode.
返回值
#NULLCan't find chosen disk partition.
los_part* This is partition structure pointer of chosen disk partition.
Dependency:
参见
None

Find disk partition.

在文件 disk.c1682 行定义.

1683{
1684 INT32 i;
1685 los_disk *disk = NULL;
1686 los_part *part = NULL;
1687
1688 if (blkDriver == NULL) {
1689 return NULL;
1690 }
1691
1692 for (i = 0; i < SYS_MAX_DISK; i++) {//遍历所有磁盘
1693 disk = get_disk(i);
1694 if (disk == NULL) {
1695 continue;
1696 }
1697 part = OsPartFind(disk, blkDriver);//从磁盘中通过节点找分区信息
1698 if (part != NULL) {
1699 return part;
1700 }
1701 }
1702
1703 return NULL;
1704}
static los_part * OsPartFind(los_disk *disk, const struct Vnode *blkDriver)
通过索引节点从磁盘中找分区信息
Definition: disk.c:1656
函数调用图:
这是这个函数的调用关系图:

◆ los_part_ioctl()

INT32 los_part_ioctl ( INT32  pt,
INT32  cmd,
VOID *  buf 
)

Get information of chosen partition.

Description:
By passed command to get information of chosen partition.
注意
参数
pt[IN] Type INT32 partition number,less than the value defined by SYS_MAX_PART.
cmd[IN] Type INT32 command to issu, currently support GET_SECTOR_COUNT, GET_SECTOR_SIZE, GET_BLOCK_SIZE, CTRL_SYNC.
buf[OUT] Type #VOID * memory to store the information, the size must enough for data type (UINT64) when cmd type is DISK_GET_SECTOR_COUNT, others is size_t.
返回值
#0Get information success.
#-1Get information failed.
Dependency:
参见
None

在文件 disk.c1193 行定义.

1194{
1195 struct geometry info;
1196 los_part *part = get_part(pt);
1197 los_disk *disk = NULL;
1198
1199 if (part == NULL) {
1200 return VFS_ERROR;
1201 }
1202
1203 disk = get_disk((INT32)part->disk_id);
1204 if (disk == NULL) {
1205 return VFS_ERROR;
1206 }
1207
1208 DISK_LOCK(&disk->disk_mutex);
1209 if ((part->dev == NULL) || (disk->disk_status != STAT_INUSED)) {
1210 goto ERROR_HANDLE;
1211 }
1212
1213 if (cmd == DISK_CTRL_SYNC) {
1214 DISK_UNLOCK(&disk->disk_mutex);
1215 return ENOERR;
1216 }
1217
1218 if (buf == NULL) {
1219 goto ERROR_HANDLE;
1220 }
1221
1222 (VOID)memset_s(&info, sizeof(info), 0, sizeof(info));
1223
1224 struct block_operations *bops = (struct block_operations *)((struct drv_data *)part->dev->data)->ops;
1225 if ((bops == NULL) || (bops->geometry == NULL) ||
1226 (bops->geometry(part->dev, &info) != 0)) {
1227 goto ERROR_HANDLE;
1228 }
1229
1230 if (cmd == DISK_GET_SECTOR_COUNT) {
1231 *(UINT64 *)buf = part->sector_count;
1232 if (*(UINT64 *)buf == 0) {
1233 goto ERROR_HANDLE;
1234 }
1235 } else if (cmd == DISK_GET_SECTOR_SIZE) {
1236 *(size_t *)buf = info.geo_sectorsize;
1237 } else if (cmd == DISK_GET_BLOCK_SIZE) { /* Get erase block size in unit of sectors (UINT32) */
1238 if ((bops->ioctl == NULL) ||
1239 (bops->ioctl(part->dev, GET_ERASE_BLOCK_SIZE, (UINTPTR)buf) != 0)) {
1240 goto ERROR_HANDLE;
1241 }
1242 } else {
1243 goto ERROR_HANDLE;
1244 }
1245
1246 DISK_UNLOCK(&disk->disk_mutex);
1247 return ENOERR;
1248
1249ERROR_HANDLE:
1250 DISK_UNLOCK(&disk->disk_mutex);
1251 return VFS_ERROR;
1252}
unsigned long UINTPTR
Definition: los_typedef.h:68
函数调用图:

◆ los_part_read()

INT32 los_part_read ( INT32  pt,
VOID *  buf,
UINT64  sector,
UINT32  count,
BOOL  useRead 
)

Read data from chosen partition.

Description:
Read data from chosen partition.
注意
  • The sector size of the disk to be read should be acquired by los_part_ioctl before calling this function.
  • The parameter buf must point to valid memory and the buf size is count * sector_size.
参数
pt[IN] Type INT32 partition number, less than the value defined by SYS_MAX_PART.
buf[OUT] Type #VOID * memory which used to store the data to be read.
sector[IN] Type UINT64 start sector number of chosen partition.
count[IN] Type UINT32 the expected sector count for reading.
useRead[IN] Type BOOL FALSE when reading large contiguous data, TRUE for other situations
返回值
#0Read success.
#-1Read failed.
Dependency:
参见
los_part_read

Read data from chosen partition.

在文件 disk.c1080 行定义.

1081{
1082 const los_part *part = get_part(pt);//先拿到分区信息
1083 los_disk *disk = NULL;
1084 INT32 ret;
1085
1086 if (part == NULL) {
1087 return VFS_ERROR;
1088 }
1089
1090 disk = get_disk((INT32)part->disk_id);//再拿到磁盘信息
1091 if (disk == NULL) {
1092 return VFS_ERROR;
1093 }
1094
1095 DISK_LOCK(&disk->disk_mutex);//锁磁盘
1096 if ((part->dev == NULL) || (disk->disk_status != STAT_INUSED)) {
1097 goto ERROR_HANDLE;
1098 }
1099
1100 if (count > part->sector_count) {//不能超过分区总扇区数量
1101 PRINT_ERR("los_part_read failed, invalid count, count = %u\n", count);
1102 goto ERROR_HANDLE;
1103 }
1104
1105 /* Read from absolute sector. */
1106 if (part->type == EMMC) {//如果分区类型是磁盘介质
1107 if ((disk->sector_count - part->sector_start) > sector) {//从绝对扇区读取
1108 sector += part->sector_start;
1109 } else {
1110 PRINT_ERR("los_part_read failed, invalid sector, sector = %llu\n", sector);
1111 goto ERROR_HANDLE;
1112 }
1113 }
1114
1115 if ((sector >= GetFirstPartStart(part)) && //扇区范围判断
1116 (((sector + count) > (part->sector_start + part->sector_count)) || (sector < part->sector_start))) {
1117 PRINT_ERR("los_part_read error, sector = %llu, count = %u, part->sector_start = %llu, "
1118 "part->sector_count = %llu\n", sector, count, part->sector_start, part->sector_count);
1119 goto ERROR_HANDLE;
1120 }
1121 //读取大量连续数据时,useRead 应为 FALSE
1122 /* useRead should be FALSE when reading large contiguous data */
1123 ret = los_disk_read((INT32)part->disk_id, buf, sector, count, useRead);
1124 if (ret < 0) {
1125 goto ERROR_HANDLE;
1126 }
1127
1128 DISK_UNLOCK(&disk->disk_mutex);
1129 return ENOERR;
1130
1131ERROR_HANDLE:
1132 DISK_UNLOCK(&disk->disk_mutex);
1133 return VFS_ERROR;
1134}
static UINT64 GetFirstPartStart(const los_part *part)
Definition: disk.c:281
INT32 los_disk_read(INT32 drvID, VOID *buf, UINT64 sector, UINT32 count, BOOL useRead)
读磁盘数据
Definition: disk.c:911
struct Vnode * dev
Definition: disk.h:205
UINT64 sector_count
Definition: disk.h:211
UINT64 sector_start
Definition: disk.h:207
UINT8 type
Definition: disk.h:204
函数调用图:

◆ los_part_write()

INT32 los_part_write ( INT32  pt,
const VOID *  buf,
UINT64  sector,
UINT32  count 
)

Write data to chosen partition.

Description:
Write data to chosen partition.
注意
  • The sector size of the disk to be write should be acquired by los_part_ioctl before calling this function.
  • The parameter buf must point to valid memory and the buf size is count * sector_size.
参数
pt[IN] Type INT32 partition number,less than the value defined by SYS_MAX_PART.
buf[IN] Type #VOID * memory which used to storage the written data.
sector[IN] Type UINT64 start sector number of chosen partition.
count[IN] Type UINT32 the expected sector count for write.
返回值
#0Write success.
#-1Write failed.
Dependency:
参见
los_part_read

Write data to chosen partition.

在文件 disk.c1136 行定义.

1137{
1138 const los_part *part = get_part(pt);
1139 los_disk *disk = NULL;
1140 INT32 ret;
1141
1142 if (part == NULL) {
1143 return VFS_ERROR;
1144 }
1145
1146 disk = get_disk((INT32)part->disk_id);
1147 if (disk == NULL) {
1148 return VFS_ERROR;
1149 }
1150
1151 DISK_LOCK(&disk->disk_mutex);
1152 if ((part->dev == NULL) || (disk->disk_status != STAT_INUSED)) {
1153 goto ERROR_HANDLE;
1154 }
1155
1156 if (count > part->sector_count) {
1157 PRINT_ERR("los_part_write failed, invalid count, count = %u\n", count);
1158 goto ERROR_HANDLE;
1159 }
1160
1161 /* Write to absolute sector. */
1162 if (part->type == EMMC) {
1163 if ((disk->sector_count - part->sector_start) > sector) {
1164 sector += part->sector_start;
1165 } else {
1166 PRINT_ERR("los_part_write failed, invalid sector, sector = %llu\n", sector);
1167 goto ERROR_HANDLE;
1168 }
1169 }
1170
1171 if ((sector >= GetFirstPartStart(part)) &&
1172 (((sector + count) > (part->sector_start + part->sector_count)) || (sector < part->sector_start))) {
1173 PRINT_ERR("los_part_write, sector = %llu, count = %u, part->sector_start = %llu, "
1174 "part->sector_count = %llu\n", sector, count, part->sector_start, part->sector_count);
1175 goto ERROR_HANDLE;
1176 }
1177 //sector已变成磁盘绝对扇区
1178 ret = los_disk_write((INT32)part->disk_id, buf, sector, count);//直接写入磁盘,
1179 if (ret < 0) {
1180 goto ERROR_HANDLE;
1181 }
1182
1183 DISK_UNLOCK(&disk->disk_mutex);
1184 return ENOERR;
1185
1186ERROR_HANDLE:
1187 DISK_UNLOCK(&disk->disk_mutex);
1188 return VFS_ERROR;
1189}
INT32 los_disk_write(INT32 drvID, const VOID *buf, UINT64 sector, UINT32 count)
将buf内容写到指定扇区,
Definition: disk.c:970
函数调用图:

◆ OsClearUsbStatus()

VOID OsClearUsbStatus ( UINT32  diskId)

Set usb mode.

Description:
Clear the corresponding bit of g_usbMode as usb host mode.
注意
  • diskId should be [0,SYS_MAX_DISK)
参数
diskId[IN] Type # unsigned int disk id.
Dependency:
  • driver.h
参见
OsSetUsbStatus

在文件 disk.c249 行定义.

250{
251 if (diskID < SYS_MAX_DISK) {
252 g_usbMode &= ~((1u << diskID) & UINT_MAX);
253 }
254}
UINT32 g_usbMode
Definition: disk.c:85

◆ OsSetUsbStatus()

VOID OsSetUsbStatus ( UINT32  diskId)

Set usb mode.

Description:
Set the corresponding bit of g_usbMode as usb host mode.
注意
  • diskId should be [0,SYS_MAX_DISK)
参数
diskId[IN] Type # unsigned int disk id.
Dependency:
  • driver.h
参见
OsClearUsbStatus

在文件 disk.c242 行定义.

243{
244 if (diskID < SYS_MAX_DISK) {
245 g_usbMode |= (1u << diskID) & UINT_MAX;
246 }
247}

◆ show_part()

VOID show_part ( los_part part)

Print partition information.

Description:
Print partition information.
注意
参数
part[IN] Type los_part * partition control structure pointer
Dependency:
参见
None

Print partition information.

在文件 disk.c1810 行定义.

1811{
1812 if ((part == NULL) || (part->dev == NULL)) {
1813 PRINT_ERR("part is NULL\n");
1814 return;
1815 }
1816
1817 PRINTK("\npart info :\n");
1818 PRINTK("disk id : %u\n", part->disk_id); //磁盘ID
1819 PRINTK("part_id in system: %u\n", part->part_id); //在整个系统的分区ID
1820 PRINTK("part no in disk : %u\n", part->part_no_disk);//在磁盘分区的ID
1821 PRINTK("part no in mbr : %u\n", part->part_no_mbr);//主分区ID
1822 PRINTK("part filesystem : %02X\n", part->filesystem_type);//文件系统(FAT | NTFS)
1823 PRINTK("part sec start : %llu\n", part->sector_start);//开始扇区
1824 PRINTK("part sec count : %llu\n", part->sector_count);//扇区大小
1825}
UINT32 part_id
Definition: disk.h:199
UINT32 part_no_disk
Definition: disk.h:200
UINT8 filesystem_type
Definition: disk.h:203
UINT32 part_no_mbr
Definition: disk.h:201
这是这个函数的调用关系图: