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

浏览源代码.

函数

void los_vfs_init (void)
 Initializes the vfs filesystem 更多...
 
void CloseOnExec (struct files_struct *files)
 
void SetCloexecFlag (int procFd)
 
bool CheckCloexecFlag (int procFd)
 
void ClearCloexecFlag (int procFd)
 
void clear_fd (int fd)
 
char * rindex (const char *s, int c)
 locate character in string. 更多...
 
void set_label (const char *name)
 
int format (const char *dev, int sectors, int option)
 formatting sd card 更多...
 
void ls (const char *pathname)
 list directory contents. 更多...
 
int los_set_systime_status (BOOL b_status)
 set current system time is valid or invalid for FAT file system. 更多...
 
int chattr (const char *pathname, struct IATTR *attr)
 
int VfsFcntl (int fd, int cmd,...)
 
INT32 LOS_BcacheSyncByName (const CHAR *name)
 
INT32 LOS_GetDirtyRatioByName (const CHAR *name)
 
VOID LOS_SetDirtyRatioThreshold (UINT32 dirtyRatio)
 
VOID LOS_SetSyncThreadInterval (UINT32 interval)
 设置同步间隔,5秒 更多...
 
INT32 LOS_SetSyncThreadPrio (UINT32 prio, const CHAR *name)
 设置同步任务优先级,10 更多...
 

函数说明

◆ chattr()

int chattr ( const char *  pathname,
struct IATTR attr 
)

在文件 vfs_chattr.c58 行定义.

59{
60 struct Vnode *vnode = NULL;
61 int ret;
62
63 if (pathname == NULL || attr == NULL) {
64 set_errno(EINVAL);
65 return VFS_ERROR;
66 }
67
68 VnodeHold();
69 ret = VnodeLookup(pathname, &vnode, 0);
70 if (ret != LOS_OK) {
71 goto errout_with_lock;
72 }
73
74 if ((vnode->originMount) && (vnode->originMount->mountFlags & MS_RDONLY)) {
75 ret = -EROFS;
76 goto errout_with_lock;
77 }
78
79 /* The way we handle the stat depends on the type of vnode that we
80 * are dealing with.
81 */
82
83 if (vnode->vop != NULL && vnode->vop->Chattr != NULL) {
84 ret = vnode->vop->Chattr(vnode, attr);
85 } else {
86 ret = -ENOSYS;
87 }
88 VnodeDrop();
89
90 if (ret < 0) {
91 goto errout;
92 }
93
94 return OK;
95
96 /* Failure conditions always set the errno appropriately */
97
98errout_with_lock:
99 VnodeDrop();
100errout:
101 set_errno(-ret);
102 return VFS_ERROR;
103}
unsigned long mountFlags
Definition: mount.h:80
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
Definition: vnode.h:164
struct VnodeOps * vop
Definition: vnode.h:174
struct Mount * originMount
Definition: vnode.h:180
int(* Chattr)(struct Vnode *vnode, struct IATTR *attr)
改变节点属性(change attr)
Definition: vnode.h:214
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
函数调用图:
这是这个函数的调用关系图:

◆ CheckCloexecFlag()

bool CheckCloexecFlag ( int  procFd)

在文件 vfs_cloexec.c75 行定义.

76{
77 bool isCloexec = 0;
78 struct fd_table_s *fdt = GetFdTable();
79 if (fdt == NULL) {
80 return false;
81 }
82
83 FileTableLock(fdt);
84 isCloexec = FD_ISSET(procFd, fdt->cloexec_fds);
85 FileTableUnLock(fdt);
86 return isCloexec;
87}
struct fd_table_s * GetFdTable(void)
获取进程文件描述符表
Definition: vfs_procfd.c:77
void FileTableLock(struct fd_table_s *fdt)
对进程文件表操作上锁
Definition: vfs_procfd.c:40
void FileTableUnLock(struct fd_table_s *fdt)
对进程文件表操作解锁
Definition: vfs_procfd.c:52
进程fd表结构体
Definition: fd_table.h:84
fd_set * cloexec_fds
Definition: fd_table.h:88
函数调用图:
这是这个函数的调用关系图:

◆ clear_fd()

void clear_fd ( int  fd)

◆ ClearCloexecFlag()

void ClearCloexecFlag ( int  procFd)

在文件 vfs_cloexec.c89 行定义.

90{
91 struct fd_table_s *fdt = GetFdTable();
92 if (fdt == NULL) {
93 return;
94 }
95
96 FileTableLock(fdt);
97 FD_CLR(procFd, fdt->cloexec_fds);
98 FileTableUnLock(fdt);
99 return;
100}
函数调用图:
这是这个函数的调用关系图:

◆ CloseOnExec()

void CloseOnExec ( struct files_struct files)

在文件 vfs_cloexec.c42 行定义.

43{
44 int sysFd;
45 if ((files == NULL) || (files->fdt == NULL)) {
46 return;
47 }
48
49 for (int i = 0; i < files->fdt->max_fds; i++) {
50 if (FD_ISSET(i, files->fdt->proc_fds) &&
51 FD_ISSET(i, files->fdt->cloexec_fds)) {
52 sysFd = DisassociateProcessFd(i);
53 if (sysFd >= 0) {
54 close(sysFd);
55 }
56
58 }
59 }
60}
unsigned int max_fds
进程的文件描述符最多有256个
Definition: fd_table.h:85
fd_set * proc_fds
进程fd管理位,用bitmap管理FD使用情况,默认打开了 0,1,2 (stdin,stdout,stderr)
Definition: fd_table.h:87
struct fd_table_s * fdt
持有的文件表
Definition: fd_table.h:95
void FreeProcessFd(int procFd)
释放进程文件描述符
Definition: vfs_procfd.c:191
int DisassociateProcessFd(int procFd)
解绑系统文件描述符,返回系统文件描述符
Definition: vfs_procfd.c:206
函数调用图:
这是这个函数的调用关系图:

◆ format()

int format ( const char *  dev,
int  sectors,
int  option 
)

formatting sd card

Description:
formatting sd card.
注意
  • The prefix of the parameter dev must be "/dev", and the length must be less than the value defined by PATH_MAX. There are four kind of format option: FMT_FAT16, FMT_FAT32, FMT_ANY, FMT_ERASE. If users input anything else, the default format option is FMT_ANY. Format option is decided by the number of clusters. Choosing the wrong option will cause error of format. The detailed information of (FAT16,FAT32) is ff.h.
参数
dev[IN] Type #const char* path of the block device to format, which must be a really existing block device node.
sectors[IN] Type int number of sectors per cluster.
option[IN] Type int option of format.
返回值
#0Format success.
#-1Format failed.
Dependency:
  • unistd.h: the header file that contains the API declaration.
参见

在文件 format.c44 行定义.

45{
46 struct Vnode *device = NULL;
47 INT err;
48 if (dev == NULL) {
49 set_errno(EINVAL);
50 return -1;
51 }
52
53 if (strncmp(dev, "/dev", DEV_NAME_SIZE) != 0) {
54 PRINTK("Usage :\n");
55 PRINTK(" format <dev_vnodename> <sectors> <option> <label>\n");
56 PRINTK(" dev_vnodename : the name of dev\n");
57 PRINTK(" sectors : Size of allocation unit in unit of byte or sector, ");
58 PRINTK("0 instead of default size\n");
59 PRINTK(" options : Index of filesystem. 1 for FAT filesystem, 2 for FAT32 filesystem, ");
60 PRINTK("7 for any, 8 for erase\n");
61 PRINTK(" label : The volume of device. It will be emptyed when this parameter is null\n");
62 PRINTK("Example:\n");
63 PRINTK(" format /dev/mmcblk0 128 2\n");
64
65 set_errno(EINVAL);
66 return -1;
67 }
68 VnodeHold();
69 err = VnodeLookup(dev, &device, 0);
70 if (err == -ENOENT || err == -ENOSYS) {
71 VnodeDrop();
72 set_errno(ENODEV);
73 return -1;
74 } else if (err < 0) {
75 VnodeDrop();
76 set_errno(-err);
77 return -1;
78 }
79 err = fatfs_mkfs(device, sectors, option);
80 if (err < 0) {
81 VnodeDrop();
82 set_errno(-err);
83 return -1;
84 }
85#ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION
86 else if (err >= VIRERR_BASE) {
87 set_errno(err);
88 }
89#endif
90 VnodeDrop();
91 return 0;
92}
int fatfs_mkfs(struct Vnode *device, int sectors, int option)
Definition: fatfs.c:1885
函数调用图:
这是这个函数的调用关系图:

◆ LOS_BcacheSyncByName()

INT32 LOS_BcacheSyncByName ( const CHAR name)
Description:
The LOS_BcacheSyncByName() function shall sync all the data in the cache corresponding to the disk name to the disk.
参数
name[IN] name of the disk
注意
  • Now only fat filesystem support this function.
返回值
#0On success.
INT32On failure.
Dependency:
  • fs.h
参见
None

在文件 bcache.c983 行定义.

984{
985 INT32 diskID = los_get_diskid_byname(name);
986 return OsSdSync(diskID);
987}
INT32 OsSdSync(INT32 id)
Definition: bcache.c:956
INT32 los_get_diskid_byname(const CHAR *diskName)
get the INUSED disk id.
Definition: disk.c:191
signed int INT32
Definition: los_typedef.h:60
函数调用图:

◆ LOS_GetDirtyRatioByName()

INT32 LOS_GetDirtyRatioByName ( const CHAR name)
Description:
The LOS_GetDirtyRatioByName() function shall return the percentage of dirty blocks in the cache corresponding to the disk name.
参数
name[IN] name of the disk
注意
  • Now only fat filesystem support this function.
返回值
INT32the percentage of dirty blocks.
#-1On failure.
Dependency:
  • fs.h
参见
None

在文件 bcache.c1017 行定义.

1018{
1019 INT32 diskID = los_get_diskid_byname(name);
1020 return BcacheGetDirtyRatio(diskID);
1021}
INT32 BcacheGetDirtyRatio(INT32 id)
Definition: bcache.c:989
函数调用图:

◆ los_set_systime_status()

int los_set_systime_status ( BOOL  b_status)

set current system time is valid or invalid for FAT file system.

Description:
The function is used for setting current system time is valid or invalid for FAT file system. The value can be set as FAT_SYSTEM_TIME_ENABLE/FAT_SYSTEM_TIME_DISABLE.
注意
  • When the system time is valid, it should set FAT_SYSTEM_TIME_ENABLE.
  • When the system time is invalid, it should set FAT_SYSTEM_TIME_DISABLE.
参数
b_status[IN] Type BOOL system time status.
返回值
#0set status success
#-22Invalid argument
Dependency:
  • fs.h: the header file that contains the API declaration.
参见

◆ LOS_SetDirtyRatioThreshold()

VOID LOS_SetDirtyRatioThreshold ( UINT32  dirtyRatio)
Description:
The LOS_SetDirtyRatioThreshold() function shall set the dirty ratio threshold of bcache. When the percentage of dirty blocks in the cache is greater than the threshold, write back data to disk.
参数
dirtyRatio[IN] Threshold of the percentage of dirty blocks, expressed in %.
注意
  • The dirtyRatio must be less than or equal to 100, or the setting is invalid.
返回值
#VOIDNone.
Dependency:
  • fs.h
参见
LOS_SetSyncThreadInterval | LOS_SetSyncThreadPrio

在文件 bcache.c85 行定义.

86{
87 if ((dirtyRatio != g_dirtyRatio) && (dirtyRatio <= 100)) { /* The ratio cannot exceed 100% */
88 g_dirtyRatio = dirtyRatio;
89 }
90}
UINT32 g_dirtyRatio
Definition: bcache.c:82

◆ LOS_SetSyncThreadInterval()

VOID LOS_SetSyncThreadInterval ( UINT32  interval)

设置同步间隔,5秒

Description:
The LOS_SetSyncThreadInterval() function shall set the interval for the sync thread to wake up.
参数
interval[IN] the interval time for the sync thread to wake up, in milliseconds, accuracy is 10ms.
注意
  • None
返回值
#VOIDNone.
Dependency:
  • fs.h
参见
LOS_SetDirtyRatioThreshold | LOS_SetSyncThreadPrio

在文件 bcache.c92 行定义.

93{
94 g_syncInterval = interval;
95}
UINT32 g_syncInterval
Definition: bcache.c:83

◆ LOS_SetSyncThreadPrio()

INT32 LOS_SetSyncThreadPrio ( UINT32  prio,
const CHAR name 
)

设置同步任务优先级,10

Description:
The LOS_SetSyncThreadPrio() function shall set the priority of the sync thread.
参数
prio[IN] priority of sync thread to be set
name[IN] name of the disk
注意
  • The prio must be less than 31 and be greater than 0, or the setting is invalid.
  • If the parameter name is NULL, it only set the value of a global variable, and take effect the next time the thread is created. If name is not NULL and can't find the disk corresponding to name, it shall return an error.
返回值
INT32On failure.
0On success.
Dependency:
  • fs.h
参见
LOS_SetDirtyRatioThreshold | LOS_SetSyncThreadInterval | LOS_TaskPriSet

在文件 bcache.c97 行定义.

98{
99 INT32 ret = VFS_ERROR;
100 INT32 diskID;
101 los_disk *disk = NULL;
102 if ((prio == 0) || (prio >= OS_TASK_PRIORITY_LOWEST)) { /* The priority can not be zero */
103 return ret;
104 }
105
106 g_syncThreadPrio = prio;
107
108 /*
109 * If the name is NULL, it only sets the value of a global variable,
110 * and takes effect the next time the thread is created.
111 */
112 if (name == NULL) {
113 return ENOERR;
114 }
115
116 /* If the name is not NULL, it shall return an error if can't find the disk corresponding to name. */
117 diskID = los_get_diskid_byname(name);//获取磁盘ID
118 disk = get_disk(diskID);//获取磁盘信息
119 if (disk == NULL) {
120 return ret;
121 }
122
123 if (pthread_mutex_lock(&disk->disk_mutex) != ENOERR) {
124 PRINT_ERR("%s %d, mutex lock fail!\n", __FUNCTION__, __LINE__);
125 return ret;
126 }
127 if ((disk->disk_status == STAT_INUSED) && (disk->bcache != NULL)) {
128 ret = LOS_TaskPriSet(disk->bcache->syncTaskId, prio);//设置任务优先级
129 }
130 if (pthread_mutex_unlock(&disk->disk_mutex) != ENOERR) {
131 PRINT_ERR("%s %d, mutex unlock fail!\n", __FUNCTION__, __LINE__);
132 return VFS_ERROR;
133 }
134 return ret;
135}
UINT32 g_syncThreadPrio
Definition: bcache.c:81
@ STAT_INUSED
Definition: disk.h:172
los_disk * get_disk(INT32 id)
Find disk driver.
Definition: disk.c:263
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_TaskPriSet(UINT32 taskID, UINT16 taskPrio)
设置指定任务的优先级
Definition: los_task.c:1071
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
struct pthread_mutex disk_mutex
Definition: disk.h:191
OsBcache * bcache
Definition: disk.h:183
UINT32 disk_status
Definition: disk.h:178
UINT32 syncTaskId
Definition: bcache.h:115
函数调用图:

◆ los_vfs_init()

void los_vfs_init ( void  )

Initializes the vfs filesystem

Description:
This API is used to initializes the vfs filesystem
注意
  • Called only once, multiple calls will cause file system error.
参数
none
返回值
none
Dependency:
  • fs.h: the header file that contains the API declaration.
参见
NULL

在文件 vfs_init.c70 行定义.

71{
72 uint retval;
73 static bool g_vfs_init = false;
74 if (g_vfs_init) {
75 return;
76 }
77
78#ifdef LOSCFG_FS_FAT_DISK //两个自旋锁
79 spin_lock_init(&g_diskSpinlock);
80 spin_lock_init(&g_diskFatBlockSpinlock);
81#endif
82 files_initialize();
83 files_initlist(&tg_filelist);
84
85 retval = VnodesInit();//索引节点初始化
86 if (retval != LOS_OK) {
87 PRINT_ERR("los_vfs_init VnodeInit failed error %d\n", retval);
88 return;
89 }
90
91 retval = PathCacheInit();//路径缓存初始化
92 if (retval != LOS_OK) {
93 PRINT_ERR("los_vfs_init PathCacheInit failed error %d\n", retval);
94 return;
95 }
96 retval = VnodeHashInit();//哈希列表初始化
97 if (retval != LOS_OK) {
98 PRINT_ERR("los_vfs_init VnodeHashInit failed error %d\n", retval);
99 return;
100 }
101
102 retval = VnodeDevInit();
103 if (retval != LOS_OK) {
104 PRINT_ERR("los_vfs_init VnodeDevInit failed error %d\n", retval);
105 return;
106 }
107
108 g_vfs_init = true;
109}
spinlock_t g_diskFatBlockSpinlock
磁盘Fat块自旋锁
Definition: disk.c:83
spinlock_t g_diskSpinlock
磁盘自锁锁
Definition: disk.c:82
int PathCacheInit(void)
Definition: path_cache.c:58
int VnodesInit(void)
Definition: vnode.c:91
int VnodeDevInit(void)
设备初始化,设备结点:/dev目录下,对应一个设备,如/dev/mmcblk0
Definition: vnode.c:627
int VnodeHashInit(void)
Definition: vnode_hash.c:44
函数调用图:
这是这个函数的调用关系图:

◆ ls()

void ls ( const char *  pathname)

list directory contents.

Description:
List information about the FILEs (the current directory by default).
注意
  • The total length of parameter pathname must be less than the value defined by PATH_MAX.
参数
pathname[IN] Type #const char* The file pathname.
返回值
<ul>None.</ul>
Dependency:
  • fs.h: the header file that contains the API declaration.
参见
ls

在文件 vfs_other.c600 行定义.

601{
602 struct stat statInfo = { 0 };
603 char *path = NULL;
604 int ret;
605
606 if (pathname == NULL) {
607#ifdef VFS_USING_WORKDIR
608 UINTPTR lock_flags;
610
611 /* open current working directory */
612
613 spin_lock_irqsave(&curr->files->workdir_lock, lock_flags);
614 path = strdup(curr->files->workdir);
615 spin_unlock_irqrestore(&curr->files->workdir_lock, lock_flags);
616#else
617 path = strdup("/");
618#endif
619 if (path == NULL) {
620 return;
621 }
622 } else {
623 ret = vfs_normalize_path(NULL, pathname, &path);
624 if (ret < 0) {
625 set_errno(-ret);
626 return;
627 }
628 }
629
630 ret = stat(path, &statInfo);
631 if (ret < 0) {
632 perror("ls error");
633 free(path);
634 return;
635 }
636
637 if (statInfo.st_mode & S_IFDIR) { /* list all directory and file */
638 ret = LsDir((pathname == NULL) ? path : pathname);
639 } else { /* show the file infomation */
640 ret = LsFile(path);
641 }
642 if (ret < 0) {
643 perror("ls error");
644 }
645
646 free(path);
647 return;
648}
int vfs_normalize_path(const char *directory, const char *filename, char **pathname)
Definition: fullpath.c:245
STATIC INLINE LosProcessCB * OsCurrProcessGet(VOID)
unsigned long UINTPTR
Definition: los_typedef.h:68
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
struct files_struct * files
spinlock_t workdir_lock
工作区目录自旋锁
Definition: fd_table.h:99
char workdir[PATH_MAX]
工作区路径,最大 256个字符
Definition: fd_table.h:100
int LsFile(const char *path)
Definition: vfs_other.c:537
int LsDir(const char *path)
Definition: vfs_other.c:553
函数调用图:

◆ rindex()

char * rindex ( const char *  s,
int  c 
)

locate character in string.

Description:
The API function returns a pointer to the last occurrence of the character c in the string s.
注意
  • The parameter s must point a valid string, which end with the terminating null byte.
参数
s[IN] Type #const char* A pointer to string.
c[IN] Type int The character.
返回值
#char*a pointer to the matched character or NULL if the character is not found.
Dependency:
  • fs.h: the header file that contains the API declaration.
参见
rindex

在文件 vfs_other.c432 行定义.

433{
434 if (s == NULL) {
435 return NULL;
436 }
437
438 /* Don't bother tracing - strrchr can do that */
439 return (char *)strrchr(s, c);
440}

◆ set_label()

void set_label ( const char *  name)
Description:
The set_label() function shall set the value of a global varible, the value will be used to set the label of SD card in format().
参数
name[IN] label to set, the length must be less than 12
注意
  • The function must be called before format().
返回值
voidNone.
Dependency:
  • fs.h
参见
format

在文件 format.c94 行定义.

95{
96 INT len;
97 INT err;
98
99 (void)memset_s(FatLabel, LABEL_LEN, 0, LABEL_LEN);
100
101 if (name == NULL || *name == '\0') {
102 return;
103 }
104
105 len = strlen(name);
106 if (len >= LABEL_LEN) {
107 len = LABEL_LEN - 1;
108 }
109
110 err = strncpy_s(FatLabel, LABEL_LEN, name, len);
111 if (err != EOK) {
112 PRINT_ERR("Fat set_label error");
113 }
114}
char FatLabel[LABEL_LEN]
Definition: format.c:41
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
这是这个函数的调用关系图:

◆ SetCloexecFlag()

void SetCloexecFlag ( int  procFd)

在文件 vfs_cloexec.c62 行定义.

63{
64 struct fd_table_s *fdt = GetFdTable();
65 if (fdt == NULL) {
66 return;
67 }
68
69 FileTableLock(fdt);
70 FD_SET(procFd, fdt->cloexec_fds);
71 FileTableUnLock(fdt);
72 return;
73}
函数调用图:
这是这个函数的调用关系图:

◆ VfsFcntl()

int VfsFcntl ( int  fd,
int  cmd,
  ... 
)
Description:
The VfsFcntl function shall manipulate file descriptor.
返回值
#0On success.
#-1On failure with errno set.
CONTINE_NUTTX_FCNTLdoesn't support some cmds in VfsFcntl, needs to continue going through Nuttx vfs operation.
Dependency:
  • fs.h
参见
None

在文件 vfs_fcntl.c68 行定义.

69{
70 va_list ap;
71 int ret = 0;
72
73 va_start(ap, cmd);
74 switch (cmd) {
75 case F_DUPFD:
76 {
77 int arg = va_arg(ap, int);
78 ret = FcntlDupFd(procfd, arg);
79 }
80 break;
81 case F_GETFD:
82 {
83 bool isCloexec = CheckCloexecFlag(procfd);
84 ret = isCloexec ? FD_CLOEXEC : 0;
85 }
86 break;
87 case F_SETFD:
88 {
89 int oflags = va_arg(ap, int);
90 if (oflags & FD_CLOEXEC) {
91 SetCloexecFlag(procfd);
92 } else {
93 ClearCloexecFlag(procfd);
94 }
95 }
96 break;
97 default:
98 ret = CONTINE_NUTTX_FCNTL;
99 break;
100 }
101
102 va_end(ap);
103 return ret;
104}
void SetCloexecFlag(int procFd)
Definition: vfs_cloexec.c:62
bool CheckCloexecFlag(int procFd)
Definition: vfs_cloexec.c:75
void ClearCloexecFlag(int procFd)
Definition: vfs_cloexec.c:89
static int FcntlDupFd(int procfd, int leastFd)
Definition: vfs_fcntl.c:43
函数调用图:
这是这个函数的调用关系图: