点击蓝字 ╳ 关注我们
开源项目 openharmony是每个人的 openharmony
蒋卫峰
深圳开鸿数字产业发展有限公司
os内核开发工程师
一、 procfs介绍
procfs是类unix操作系统中进程文件系统(process file system)的缩写,主要用于通过内核访问进程信息和系统信息,以及可以修改内核参数改变系统行为。需要注意的是,procfs文件系统是一个虚拟文件系统,不存在硬盘当中,而是系统启动时动态生成的文件系统,储存在内存中。procfs文件系统通常挂载在/proc目录下。liteos-a是openatom openharmony(以下简称“openharmony”)系统中使用的轻量系统内核,实现了procfs文件系统。本文主要对liteos-a内核中的procfs文件系统的设计、实现和使用进行介绍和分析。procfs文件系统是liteos-a内核文件系统的一个案例,通过了解procfs文件系统,能够熟悉liteos-a的文件系统框架,并很好地将内核信息通过文件系统反馈给使用者。
1. linux系统中的procfs文件系统包含的内容ubuntu 20.04中的/proc文件信息如下:
图1:ubuntu proc目录信息
2. os-a系统的命令以及procfs文件系统的内容liteos-a的命令集:
liteos-a的proc目录信息如下:
图2:liteos-a proc目录信息
二、 procfs文件系统的设计
liteos-a中使用vfs作为各个文件系统的粘合层,而vfs在openharmony内核中采用树结构实现,树中的每一个节点都是vnode结构体。vfs提供统一的抽象接口用于屏蔽文件系统之间的差异,其提供三大操作接口用于统一不同文件系统调用不同接口的现状。vfs提供的三大操作接口:• vnodeops• mountops• file_operations_vfsvnodeops用于控制vnode节点,mountops控制挂载点,file_operations_vfs提供常用的文件接口。文件系统各自需要实现vfs提供的这三大接口,即实现系统本身需要的接口方法,让vfs能够调用这些接口即可。procfs文件系统虽作为一个伪文件系统pseudo-file system,但其仍旧需要实现上述接口。1. vfs提供的重要接口(1) vnode 结构体:struct vnode {
enum vnodetype type; /* vnode节点类型 */
int usecount; /* 节点链接数 */
uint32_t hash; /* 哈希值 */
uint uid; /* 文件拥有者的user id */
uint gid; /* 文件群组id */
mode_t mode; /* 文件读写执行权限 */
list_head parentpathcaches; /* 指向父节点的路径缓存 */
list_head childpathcaches; /* 指向儿子节点的路径缓存 */
struct vnode *parent; /* vnode父节点 */
struct vnodeops *vop; /* vnode操作接口 */
struct file_operations_vfs *fop; /* 文件操作接口,即指定文件系统 */
void *data; /* 数据,指向内部数据的指针 */
uint32_t flag; /* 节点标签 */
list_entry hashentry; /* 挂入v_vnodehashentry[i]中 */
list_entry actfreeentry; /* 通过本节点挂载到空闲和使用链表中 */
struct mount *originmount; /* 所在文件系统挂载信息 */
struct mount *newmount; /* 其他挂载在这个节点中的文件系统信息 */
char *filepath; /* vnode的路径信息 */
struct page_mapping mapping; /* page mapping of the vnode */
};图3:vnode structure
vnode功能接口定义:struct vnodeops {
int (*create)(struct vnode *parent, const char *name, int mode, struct vnode **vnode);// 创建节点
int (*lookup)(struct vnode *parent, const char *name, int len, struct vnode **vnode);// 查询节点
int (*open)(struct vnode *vnode, int fd, int mode, int flags);// 打开节点
ssize_t (*readpage)(struct vnode *vnode, char *buffer, off_t pos);
ssize_t (*writepage)(struct vnode *vnode, char *buffer, off_t pos, size_t buflen);
int (*close)(struct vnode *vnode);// 关闭节点
int (*reclaim)(struct vnode *vnode);// 回收节点
int (*unlink)(struct vnode *parent, struct vnode *vnode, const char *filename);// 取消硬链接
int (*rmdir)(struct vnode *parent, struct vnode *vnode, const char *dirname);// 删除目录节点
int (*mkdir)(struct vnode *parent, const char *dirname, mode_t mode, struct vnode **vnode);// 创建目录节点
int (*readdir)(struct vnode *vnode, struct fs_dirent_s *dir);// 读目录节点信息
int (*opendir)(struct vnode *vnode, struct fs_dirent_s *dir);// 打开目录节点
int (*rewinddir)(struct vnode *vnode, struct fs_dirent_s *dir);// 定位目录节点
int (*closedir)(struct vnode *vnode, struct fs_dirent_s *dir);// 关闭目录节点
int (*getattr)(struct vnode *vnode, struct stat *st);// 获取节点属性
int (*setattr)(struct vnode *vnode, struct stat *st);// 设置节点属性
int (*chattr)(struct vnode *vnode, struct iattr *attr);// 改变节点属性
int (*rename)(struct vnode *src, struct vnode *dstparent, const char *srcname, const char *dstname);
int (*truncate)(struct vnode *vnode, off_t len);// 缩小或扩大
int (*truncate64)(struct vnode *vnode, off64_t len);
int (*fscheck)(struct vnode *vnode, struct fs_dirent_s *dir);
int (*link)(struct vnode *src, struct vnode *dstparent, struct vnode **dst, const char *dstname);
int (*symlink)(struct vnode *parentvnode, struct vnode **newvnode, const char *path, const char *target);
ssize_t (*readlink)(struct vnode *vnode, char *buffer, size_t buflen);
};
vnode根节点的初始化操作:将全局vnode表进行初始化,开始节点指向根目录/,全局节点g_rootvnode。int vnodesinit(void)
{
int retval = los_muxinit(&g_vnodemux, null);
if (retval != los_ok) {
print_err(create mutex for vnode fail, status: %d, retval);
return retval;
}
los_listinit(&g_vnodefreelist);
los_listinit(&g_vnodevirtuallist);
los_listinit(&g_vnodeactivelist);
retval = vnodealloc(null, &g_rootvnode);
if (retval != los_ok) {
print_err(vnodeinit failed error %d, retval);
return retval;
}
g_rootvnode->mode = s_irwxu | s_irwxg | s_irwxo | s_ifdir;
g_rootvnode->type = vnode_type_dir;
g_rootvnode->filepath = /;
return los_ok;
}
(2) mount结构体:struct mount {
list_entry mountlist; /* 全局mount链表 */
const struct mountops *ops; /* mount的功能函数 */
struct vnode *vnodebecovered; /* 要被挂载的节点 */
struct vnode *vnodecovered; /* 要挂载的节点 */
struct vnode *vnodedev; /* 设备vnode */
list_head vnodelist; /* vnode表的表头 */
int vnodesize; /* vnode表的节点数量 */
list_head activevnodelist; /* 激活的节点链表 */
int activevnodesize; /* 激活的节点数量 */
void *data; /* 数据,指向内部数据的指针 */
uint32_t hashseed; /* random seed for vfshash */
unsigned long mountflags; /* 挂载标签 */
char pathname[path_max]; /* 挂载点路径 */
char devname[path_max]; /* 设备名称 /dev/sdb-1 */
};图4:mount structure
挂载点的接口定义:struct mountops {
int (*mount)(struct mount *mount, struct vnode *vnode, const void *data);
int (*unmount)(struct mount *mount, struct vnode **blkdriver);
int (*statfs)(struct mount *mount, struct statfs *sbp);//统计文件系统的信息,类型、大小等
int (*sync)(struct mount *mount);
};
(3)文件结构定义:struct file
{
unsigned int f_magicnum; /* file magic number. -- to be deleted */
int f_oflags; /* open mode flags */
struct vnode *f_vnode; /* driver interface */
loff_t f_pos; /* file position */
unsigned long f_refcount; /* reference count */
char *f_path; /* file fullpath */
void *f_priv; /* per file driver private data */
const char *f_relpath; /* realpath. -- to be deleted */
struct page_mapping *f_mapping; /* mapping file to memory */
void *f_dir; /* dir struct for iterate the directory if open a directory */
const struct file_operations_vfs *ops;
int fd;
};
文件接口功能定义:struct file_operations_vfs
{
/* the device driver open method differs from the mountpoint open method */
int (*open)(struct file *filep);
int (*close)(struct file *filep);
ssize_t (*read)(struct file *filep, char *buffer, size_t buflen);
ssize_t (*write)(struct file *filep, const char *buffer, size_t buflen);
off_t (*seek)(struct file *filep, off_t offset, int whence);
int (*ioctl)(struct file *filep, int cmd, unsigned long arg);
int (*mmap)(struct file* filep, struct vmmapregion *region);
/* the two structures need not be common after this point */
int (*poll)(struct file *filep, poll_table *fds);
int (*stat)(struct file *filep, struct stat* st);
int (*fallocate)(struct file* filep, int mode, off_t offset, off_t len);
int (*fallocate64)(struct file *filep, int mode, off64_t offset, off64_t len);
int (*fsync)(struct file *filep);
ssize_t (*readpage)(struct file *filep, char *buffer, size_t buflen);
int (*unlink)(struct vnode *vnode);
};
2.文件系统的重要接口设计procfs文件系统中每个目录或文件都是一个vnode,也可以理解为一个entry。procdirentry中的subdir指向的目录中的一个子项,其本质是一个单向链表的形式,并且采用头插法的形式进行节点的插入。图5:direntry
图6:procfile
图7:procdata
图8: procfileoperations
三、 procfs文件系统的实现
1.procfs的注册过程(1)向系统注册文件系统入口函数:los_module_init(procfsinit, los_init_level_kmod_extended);
(2)向vfs文件系统表注册系统名以及实现的接口等:const struct mountops procfs_operations = {
.mount = vfsprocfsmount,
.unmount = null,
.statfs = vfsprocfsstatfs,
};
static struct vnodeops g_procfsvops = {
.lookup = vfsprocfslookup,
.getattr = vfsprocfsstat,
.readdir = vfsprocfsreaddir,
.opendir = vfsprocfsopendir,
.closedir = vfsprocfsclosedir,
.truncate = vfsprocfstruncate
};
static struct file_operations_vfs g_procfsfops = {
.read = vfsprocfsread,
.write = vfsprocfswrite,
.open = vfsprocfsopen,
.close = vfsprocfsclose
};
// 注册文件系统名字以及实现的接口方法等
fsmap_entry(procfs_fsmap, procfs, procfs_operations, false, false);
2.procfs的初始化初始化需要做的工作主要包括向os注册procfs文件系统,生成procfs文件目录中的文件初始项,在liteos-a具体包含目录power、mounts等。procfs文件系统的初始化流程大致如下:// 系统的入口函数
main(void)
|-> osmain() // ./liteos/kernel/liteos_a/kernel/common/main.c
| // 进行系统的相关初始化工作
| -> earliestinit()
| -> ...
|
| -> kmodinit()
|-> ...
|
|-> osinitcall(los_init_level_kmod_extended) // 生成procfs文件系统并挂载到/proc目录
|-> initlevelcall(level)//根据不同的级别进行相关初始化工作,procfs的级别是8,其级别是文件系统向os注册的
| // ./liteos/kernel/liteos_a/fs/proc/os_adapt/proc_init.c
|
|-> procfsinit() // 进行procfs文件系统的具体初始化工作
| |-> mkdir(procfs_mount_point, procfs_default_mode) // 先生成/proc目录,之后需要将procfs文件系统挂载到该目录下
| |-> mount(null, procfs_mount_point, procfs, 0, null)
| | // 生成mount文件,包括分配vnode和挂载vnode
| |
| |-> procmountsinit()
| | | // procfs具体项的初始化都写在一个独立的文件中,例如mounts在./liteos/kernel/liteos_a/fs/proc/os_adapt/mounts_proc.c
| | |
| | |-> procmountsinit(void)
| | | // 创建mounts节点并挂载到该目录下,null位parent为父节点,若parent为null,则默认父节点为/proc
| | |
| | |-> createprocentry(mounts, 0, null)
| | | // 先判断节点是文件属性还是目录属性,后选择具体的节点创建函数,在这选择file节点
| | |
| | |-> proccreatefile(parent, name, null, mode)
| | |-> struct procdirentry *pn = null
| | |-> procallocnode(&parent, name, s_ifreg | mode) // 具体的分配节点
| | |-> struct procdirentry *pn = null;
| | | // 首先对节点的合法性进行相关检查,例如parent是否null,name是否null等
| | |
| | |-> pn = (struct procdirentry *)malloc(sizeof(struct procdirentry));//分配一个struct procdirentry内存地址
| | | // 对生成的节点赋予一些属性,例如节点名字长度,权限,名字等,每个procdirentry都需要指定一个procfile成员,里面含有具体信息
| | |
| | |-> pn->namelen = strlen(lastname);
| | |-> pn->mode = mode;
| | |-> ret = memcpy_s(pn->name, sizeof(pn->name), lastname, strlen(lastname) + 1);
| | |-> pn->pf = (struct procfile *)malloc(sizeof(struct procfile));
| | |-> pn->pf->ppde = pn;// procfile的parent是生成的pn节点
| | | // 生成对应的节点,对节点指定相应的函数接口后,需要挂载的父节点中
| | |
| | |-> procaddnode(parent, pn)
| | | // 先判断parent是否为null以及pn是否已经有parent,即判断是否已挂载
| | |
| | | // 在这里可知一个目录下的子目录以及文件都是以一个单链表的形式存储的,且采用的是头插法,即最先生成的在最后面
| | |-> pn->parent = parent;
| | |-> pn->next = parent->subdir;
| | |-> parent->subdir = pn;
| |->...
| |
| |->procpminit() // 目录初始化工作
| | | // power目录下含有子目录,但是目录生成的过程都一样,在这以power文件夹为例
| | |-> struct procdirentry *power = createprocentry(power, s_ifdir | s_irwxu | s_irwxg | s_iroth, null);
| | | |-> createprocentry(power, s_ifdir | s_irwxu | s_irwxg | s_iroth, null)
| | | | |-> // 先判断节点是文件属性还是目录属性,后选择具体的节点创建函数,在这选择目录节点
| | | | |
| | | | |-> proccreatedir(parent, name, null, mode)
| | | | | | // 这里节点创建和挂载和上述文件节点创建一样,不再赘述
| | | | | |
| | | | | |-> procallocnode(&parent, name, s_ifreg | mode) // 具体的分配节点
| | | | | |-> procaddnode(parent, pn)
| | | | |
| | | |
| | |-> ...
| |
|...
四、procfs业务分析
1.procfs挂载过程分析在procfs文件系统的挂载过程中,若使用qemu进行调试,则挂载的命令大致如下: mount -r -t procfs [dir_path] mount的系统调用间接调用procfs的mount接口。用户输入挂载命令后,引发系统调用sysmount开始逐层调用:-> ...
-> sysmount(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags,const void *data)
|--|-> 将路径,文件系统等转化之后调用mount
| |-> mount(sourceret, targetret, (filesystemtype ? fstyperet : null), mountflags, dataret)
| | |-> //找到指定的文件系统
| | |-> fsmap = mount_findfs(filesystemtype)
| | |-> mops = fsmap->fs_mops // 为mount节点指定mount的接口函数
| | |-> //找到挂载目录对应的vnode并且设置文件系统相关信息
| | |-> vnodelookup(target, &mountpt_vnode, 0)
| | | |->vnodelookupat(path, vnode, flags, null)
| | | | |-> //对目录变成绝对路径并且从全局vnode链表中开始找
| | | | |-> preprocess(path, &startvnode, &normalizedpath)
| | | | | |-> vfs_normalize_path(null, originpath, &absolutepath)
| | |-> mnt = mountalloc(mountpt_vnode, (struct mountops*)mops)
| | |-> mops->mount(mnt, device, data)//进入具体的procfs文件系统的mount函数
| | | |-> vfsprocfsmount(struct mount *mnt, struct vnode *device, const void *data)
| | | | |-> vnodealloc(&g_procfsvops, &vp);//生成一个vnode用于挂载mount节点和procfs文件系统的root节点
| | | | |-> root = getprocrootentry(); //获取procfs文件系统的根节点
| | | | |-> vp->data = root; //
| | | | |-> vp->originmount = mnt;// 将vp挂载在挂载目录所对应的mount节点上
| | | | |-> mnt->data = null;
| | | | |-> mnt->vnodecovered = vp;// mount节点挂载的vnode是该文件系统,方便后续在mount链表中找挂载点
| | | | |-> vp->type = root->type;
| | |...
2.节点的增加过程分析关键代码如下: temp = procfindnode(parent, pn->name);
if (temp != null) {
print_err(error!procdirentry '%s/%s' already registered, parent->name, pn->name);
spin_unlock(&procfslock);
return -eexist;
}
pn->parent = parent;
pn->next = parent->subdir;
parent->subdir = pn;为了更好地说明,假设目前已经在系统中生成了proc/和mounts节点,proc/节点就是该文件系统的根节点,此时两者的关系可以用下图表示:
图9:层级目录的关系
此时若需要在两者在插入一个power节点,则首先需要先生成一个power节点如下,再改变相应的指向即可,具体可以参考图10,给出三者之间的关系,最终的节点效果如图11。图10:生成一个新节点
图11:重新组合
3、writeproc shell命令的创建liteos-a中含有一个叫writeproc的shell命令,使用格式如下:writeproc value >> path
shell命令的创建方式主要有两种,分静态注册和动态注册,writeproc命令使用静态注册方式进行注册,在本文中也主要介绍静态注册。shell开发的流程如下:① 定义一个新增命令所要调用的执行函数xxx;② 使用shellcmd_entry函数添加新增命令项;③ 在链接选项liteos_tables_ldflags.mk中添加链接该新增命令项参数;④ 重新编译代码后运行。writeproc的注册如下:// 定义一个具体的执行函数
int osshellcmdwriteproc(int argc, char **argv);
// 新增命令项
shellcmd_entry(writeproc_shellcmd, cmd_type_ex, writeproc, xargs, (cmdcallbackfunc)osshellcmdwriteproc);writeproc的具体流程分析:①首先由用户按照命令格式进行输入;②osshellcmdwriteproc函数对输入的命令进行分析,并采取相关的动作。-> ...
-> // 使用shell命令唤起writeproc注册函数
-> writeproc value >> path
|-> // 进行初始化工作,主要用于判断输入路径是否合法,节点是否存在
|-> struct procdirentry *handle = null;
|-> const char *rootprocdir = /proc/;
|-> handle = openprocfile(realpath, o_trunc) // 若路径合法则找到对应的vnode
| |-> pn = procfindentry(filename)
| | |-> int leveltotal = 0;// leveltotal用于判定文件所对应的层级,一个/表示一层
| | | // 遍历vnode找到对应的vnode并返回
| |-> pn->flags = (unsigned int)(pn->flags) | (unsigned int)flags// 设置节点相应的权限
| |-> ...
| writeprocfile(handle, value, len) // 找到文件句柄之后开始写入数据
| | // 使用vnode的文件接口对procfile数据成员进行写入
| |-> result = pde->procfileops->write(pde->pf, (const char *)buf, len, &(pde->pf->fpos))
|...根据文件名查找vnode的关键代码:pn = &g_procrootdirentry;
while ((pn != null) && (levelcount name) == 0) {
spin_unlock(&procfslock);
return pn;
}
pn = pn->next;
}
pn = null;
spin_unlock(&procfslock);
return pn;
}
len = next - path;
if (pn == &g_procrootdirentry) {
if (levelcount == leveltotal) {
spin_unlock(&procfslock);
return pn;
}
len = g_procrootdirentry.namelen;
}
if (procmatch(len, path, pn)) {
isfoundsub = 1;
path += len + 1;
break;
}
pn = pn->next;
}
}
五、总结
本文介绍了liteos-a内核下proc相关目录信息,并且对liteos-a内核中procfs文件系统的原理和实现,结合源码进行了分析。同时,通过writeproc shell命令介绍了procfs的使用。希望读者可以掌握liteos-a文件系统的基本知识,更好地运用于基于liteos-a内核的系统移植工作。关于openharmony内核的内容,之前我还介绍了liteos-a内核之基础硬件——中断控制器、gic400内核对象队列的算法、openharmony liteos-m内核事件的运作机制,以及内核ipc机制数据结构、openharmony liteos-a内核之iperf3移植方法,感兴趣的读者可以点击阅读:《浅谈openharmony liteos-a内核之基础硬件——中断控制器gic400》、《openharmony——内核对象队列之算法详解(上)》、《openharmony——内核对象队列之算法详解(下)》、《openharmony——内核对象事件之源码详解》、《openharmony——内核ipc机制数据结构解析》、《openharmony liteos_a内核之iperf3移植心得》。
原文标题:liteos-a内核中的procfs文件系统分析
文章出处:【微信公众号:openatom openharmony】欢迎添加关注!文章转载请注明出处。
苹果全新Mac Pro高端顶配版详解
5G标准的第二版本已经启动,重点是5G在物联网应用场景的增强
兆易创新并购思立微一案正式获得证监会有条件批准!
六家CDMA2000运营商印度逐鹿 业务发展快速
深入了解三极管的放大倍数β与α
LiteOS-A内核中的procfs文件系统分析
BQ3568_烧录说明
手机OLED面板渗透率提升 加速2017年NOR Flash涨价
笔记本BIOS设置(高手必读)
现阶段中国产品哪些全球占有率最高?本文为你揭晓
vivo新设计专利采用双屏和LED灯环设计
业界以Micro LED的技术初期先发展可以量产的Mini LED
Intel为解决14nm产能短缺与三星达成协议 后者将为其代工新CPU
卢森堡邮政与爱立信签定为期多年的5G商业合同
颠覆创新,改变未来,梦之墨获得全国颠覆性技术创新大赛优胜项目
贸泽成为Raspberry Pi的原厂授权代理商
Altera Stratix V GX FPGA实现了与PCIe Gen3的兼容
物联网设备安全需要端到端的方法
普通硬盘和固态硬盘的区别是什么
华为3nm芯片最新信息 华为12nm芯片的手机有哪些