Linux 内核中文件系统相关 API分为两类VFS 通用接口所有文件系统通用最常用文件系统注册 / 挂载 / 操作底层接口开发自定义文件系统用内核文件操作核心 API内核态操作文件替代用户态open/read/write/close无需进入用户态直接在内核中操作文件。1. 打开文件struct file *filp_open(const char *filename, int flags, umode_t mode);用途内核中打开文件返回struct file文件描述符结构体常用flagsO_RDONLY(只读)、O_WRONLY(只写)、O_RDWR(读写)、O_CREAT(创建)注意打开失败返回ERR_PTR(-ERROR)必须用IS_ERR()判断2. 关闭文件void filp_close(struct file *filp, fl_owner_t id);用途关闭filp_open打开的文件参数id一般填NULL3. 读文件ssize_t kernel_read(struct file *filp, void *buf, size_t count, loff_t *pos);用途内核安全读取文件替代老接口 vfs_read参数pos是文件偏移量传NULL表示从当前偏移读4. 写文件ssize_t kernel_write(struct file *filp, const void *buf, size_t count, loff_t *pos);用途内核安全写入文件替代老接口 vfs_write5. 定位文件偏移loff_t vfs_llseek(struct file *filp, loff_t offset, int whence);用途修改文件读写位置同用户态lseek目录 / 路径 / 索引节点inode操作 API文件系统的核心是inode索引节点所有文件 / 目录的元数据都存在 inode 中。1. 查找路径对应的 inodestruct inode *path_lookup(const char *name, unsigned int flags, struct path *path);用途根据文件路径获取内核path和inode结构体常用flagsLOOKUP_FOLLOW(追踪符号链接)kern_pathkern_path是现代 Linux 内核5.0 全版本通用官方推荐的路径查找函数完全替代了旧的path_lookup。标准函数原型#include linux/namei.h // 必须包含头文件 int kern_path(const char *name, unsigned int flags, struct path *path);返回值0成功负数失败错误码如 -ENOENT 找不到、-EACCES 权限不足3 个参数详解name文件路径内核态虚拟地址字符串必须是绝对路径如/tmp/test.txt不能直接传用户态指针必须用copy_from_user()复制flags查找标志最常用 3 个LOOKUP_FOLLOW // 追踪软链接遇到符号链接找到真实文件 LOOKUP_DIRECTORY // 要求路径必须是目录 LOOKUP_EMPTY // 允许路径不存在极少用✅最常用LOOKUP_FOLLOWpath输出结果最重要查找成功后这里会存储内核路径对象struct path { struct vfsmount *mnt; // 挂载点 struct dentry *dentry; // 目录项包含 inode };关键注意事项成功必须 path_putkern_path会增加引用计数不释放会内存泄漏。不能传用户态字符串如果路径来自用户态必须先copy_from_user复制到内核缓冲区。返回 0 才是成功负数 失败不要用 IS_ERR 判断不能长期持有 struct path用完立即释放。2. 获取文件 inode 信息struct inode *file_inode(struct file *file);用途从struct file获取对应的 inode文件大小、权限、设备号都在这里3. 创建目录int vfs_mkdir(struct user_namespace *ns, struct inode *dir, struct dentry *dentry, umode_t mode);用途内核中创建文件夹4. 删除文件 / 目录int vfs_unlink(struct user_namespace *ns, struct inode *dir, struct dentry *dentry); // 删除文件 int vfs_rmdir(struct user_namespace *ns, struct inode *dir, struct dentry *dentry); // 删除空目录VFS 虚拟文件系统 注册 / 挂载 API用于注册自定义文件系统、挂载 / 卸载文件系统是内核文件系统的底层核心。1. 注册文件系统int register_filesystem(struct file_system_type *fs_type);用途向内核注册一个新文件系统如 ext4、tmpfs、自定义文件系统配套必须定义struct file_system_type包含挂载函数、名称等2. 卸载文件系统int unregister_filesystem(struct file_system_type *fs_type);3. 挂载文件系统int vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data);用途内核态挂载文件系统4. 超级块操作文件系统根管理struct super_block *sget(struct file_system_type *type, int (*test)(struct super_block *,void *), int (*set)(struct super_block *,void *), int flags, void *data);用途分配 / 获取文件系统超级块super_block文件系统的核心管理结构文件属性 / 元数据操作 API获取 / 修改文件大小、权限、时间等元数据。1. 获取文件属性int vfs_getattr(const struct path *path, struct kstat *stat);用途获取文件大小、类型、权限、修改时间同用户态stat2. 修改文件大小int vfs_truncate(struct path *path, loff_t length);用途截断 / 扩展文件大小3. 修改权限 / 所有者int vfs_chmod(struct user_namespace *ns, struct path *path, umode_t mode); int vfs_chown(struct user_namespace *ns, struct path *path, kuid_t uid, kgid_t gid);内核文件系统关键结构体API 操作的核心结构体是理解文件系统的基础struct file打开的文件描述符文件偏移、权限、对应 inodestruct inode索引节点文件元数据大小、权限、设备、存储位置struct dentry目录项文件名、路径、关联 inodestruct super_block文件系统超级块整个文件系统的管理信息struct file_operations文件操作函数集open/read/write/ioctl 实现struct file_system_type文件系统类型注册文件系统用总结日常内核文件操作用filp_open/filp_close/kernel_read/kernel_write路径 /inode 操作用path_lookup/vfs_getattr/vfs_unlink开发自定义文件系统用register_filesystem/sget/super_block所有 API 均为内核态专用不能在用户态使用
linux内核 文件系统 相关api
Linux 内核中文件系统相关 API分为两类VFS 通用接口所有文件系统通用最常用文件系统注册 / 挂载 / 操作底层接口开发自定义文件系统用内核文件操作核心 API内核态操作文件替代用户态open/read/write/close无需进入用户态直接在内核中操作文件。1. 打开文件struct file *filp_open(const char *filename, int flags, umode_t mode);用途内核中打开文件返回struct file文件描述符结构体常用flagsO_RDONLY(只读)、O_WRONLY(只写)、O_RDWR(读写)、O_CREAT(创建)注意打开失败返回ERR_PTR(-ERROR)必须用IS_ERR()判断2. 关闭文件void filp_close(struct file *filp, fl_owner_t id);用途关闭filp_open打开的文件参数id一般填NULL3. 读文件ssize_t kernel_read(struct file *filp, void *buf, size_t count, loff_t *pos);用途内核安全读取文件替代老接口 vfs_read参数pos是文件偏移量传NULL表示从当前偏移读4. 写文件ssize_t kernel_write(struct file *filp, const void *buf, size_t count, loff_t *pos);用途内核安全写入文件替代老接口 vfs_write5. 定位文件偏移loff_t vfs_llseek(struct file *filp, loff_t offset, int whence);用途修改文件读写位置同用户态lseek目录 / 路径 / 索引节点inode操作 API文件系统的核心是inode索引节点所有文件 / 目录的元数据都存在 inode 中。1. 查找路径对应的 inodestruct inode *path_lookup(const char *name, unsigned int flags, struct path *path);用途根据文件路径获取内核path和inode结构体常用flagsLOOKUP_FOLLOW(追踪符号链接)kern_pathkern_path是现代 Linux 内核5.0 全版本通用官方推荐的路径查找函数完全替代了旧的path_lookup。标准函数原型#include linux/namei.h // 必须包含头文件 int kern_path(const char *name, unsigned int flags, struct path *path);返回值0成功负数失败错误码如 -ENOENT 找不到、-EACCES 权限不足3 个参数详解name文件路径内核态虚拟地址字符串必须是绝对路径如/tmp/test.txt不能直接传用户态指针必须用copy_from_user()复制flags查找标志最常用 3 个LOOKUP_FOLLOW // 追踪软链接遇到符号链接找到真实文件 LOOKUP_DIRECTORY // 要求路径必须是目录 LOOKUP_EMPTY // 允许路径不存在极少用✅最常用LOOKUP_FOLLOWpath输出结果最重要查找成功后这里会存储内核路径对象struct path { struct vfsmount *mnt; // 挂载点 struct dentry *dentry; // 目录项包含 inode };关键注意事项成功必须 path_putkern_path会增加引用计数不释放会内存泄漏。不能传用户态字符串如果路径来自用户态必须先copy_from_user复制到内核缓冲区。返回 0 才是成功负数 失败不要用 IS_ERR 判断不能长期持有 struct path用完立即释放。2. 获取文件 inode 信息struct inode *file_inode(struct file *file);用途从struct file获取对应的 inode文件大小、权限、设备号都在这里3. 创建目录int vfs_mkdir(struct user_namespace *ns, struct inode *dir, struct dentry *dentry, umode_t mode);用途内核中创建文件夹4. 删除文件 / 目录int vfs_unlink(struct user_namespace *ns, struct inode *dir, struct dentry *dentry); // 删除文件 int vfs_rmdir(struct user_namespace *ns, struct inode *dir, struct dentry *dentry); // 删除空目录VFS 虚拟文件系统 注册 / 挂载 API用于注册自定义文件系统、挂载 / 卸载文件系统是内核文件系统的底层核心。1. 注册文件系统int register_filesystem(struct file_system_type *fs_type);用途向内核注册一个新文件系统如 ext4、tmpfs、自定义文件系统配套必须定义struct file_system_type包含挂载函数、名称等2. 卸载文件系统int unregister_filesystem(struct file_system_type *fs_type);3. 挂载文件系统int vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data);用途内核态挂载文件系统4. 超级块操作文件系统根管理struct super_block *sget(struct file_system_type *type, int (*test)(struct super_block *,void *), int (*set)(struct super_block *,void *), int flags, void *data);用途分配 / 获取文件系统超级块super_block文件系统的核心管理结构文件属性 / 元数据操作 API获取 / 修改文件大小、权限、时间等元数据。1. 获取文件属性int vfs_getattr(const struct path *path, struct kstat *stat);用途获取文件大小、类型、权限、修改时间同用户态stat2. 修改文件大小int vfs_truncate(struct path *path, loff_t length);用途截断 / 扩展文件大小3. 修改权限 / 所有者int vfs_chmod(struct user_namespace *ns, struct path *path, umode_t mode); int vfs_chown(struct user_namespace *ns, struct path *path, kuid_t uid, kgid_t gid);内核文件系统关键结构体API 操作的核心结构体是理解文件系统的基础struct file打开的文件描述符文件偏移、权限、对应 inodestruct inode索引节点文件元数据大小、权限、设备、存储位置struct dentry目录项文件名、路径、关联 inodestruct super_block文件系统超级块整个文件系统的管理信息struct file_operations文件操作函数集open/read/write/ioctl 实现struct file_system_type文件系统类型注册文件系统用总结日常内核文件操作用filp_open/filp_close/kernel_read/kernel_write路径 /inode 操作用path_lookup/vfs_getattr/vfs_unlink开发自定义文件系统用register_filesystem/sget/super_block所有 API 均为内核态专用不能在用户态使用