Linux内核中的虚拟文件系统详解

Linux内核中的虚拟文件系统详解

引言

虚拟文件系统(VFS)是Linux内核中一个至关重要的抽象层,它为用户空间程序提供了统一的文件系统接口,隐藏了不同文件系统的实现细节。通过VFS,Linux能够同时支持ext4、XFS、Btrfs等多种文件系统。本文将深入探讨VFS的架构、核心数据结构和操作机制。

VFS概述

1. VFS的作用

VFS是用户进程与具体文件系统之间的抽象层,它的主要作用包括:

  • 提供统一的文件操作接口
  • 管理所有已挂载的文件系统
  • 维护文件系统的层次结构
  • 处理进程与文件系统之间的数据交换

2. VFS支持的文件系统类型

  • 磁盘文件系统:ext2/3/4、XFS、Btrfs、ReiserFS等
  • 网络文件系统:NFS、CIFS/SMB等
  • 特殊文件系统:proc、sysfs、tmpfs、devpts等
  • 虚拟文件系统:debugfs、configfs等

核心数据结构

1. super_block(超级块)

超级块描述一个已挂载的文件系统的整体信息。

#include <linux/fs.h>

struct super_block {
    struct list_head s_list;         // 超级块链表
    dev_t s_dev;                      // 设备号
    unsigned long s_blocksize;         // 块大小
    unsigned char s_blocksize_bits;    // 块大小位数
    unsigned long long s_maxbytes;     // 最大文件大小
    struct file_system_type *s_type;   // 文件系统类型
    const struct super_operations *s_op;  // 超级块操作
    const struct dquot_operations *dq_op;
    const struct quotactl_ops *s_qcop;
    const struct export_operations *s_export_op;
    unsigned long s_flags;
    unsigned long s_magic;            // 文件系统魔数
    struct dentry *s_root;            // 根目录项
    struct rw_semaphore s_umount;
    int s_count;
    atomic_t s_active;
    void *s_security;
    // 其他字段...
};

2. inode(索引节点)

inode描述一个文件的元数据信息。

#include <linux/fs.h>

struct inode {
    umode_t i_mode;                   // 访问权限
    unsigned short i_opflags;
    kuid_t i_uid;                     // 用户ID
    kgid_t i_gid;                     // 组ID
    unsigned long i_flags;
    const struct inode_operations *i_op;  // inode操作
    struct super_block *i_sb;         // 超级块
    struct address_space *i_mapping;   // 地址空间
    unsigned long i_ino;              // inode号
    union {
        const unsigned int i_nlink;
        unsigned int __i_nlink;
    };
    dev_t i_rdev;                     // 设备号(对于设备文件)
    loff_t i_size;                    // 文件大小
    struct timespec i_atime;          // 访问时间
    struct timespec i_mtime;          // 修改时间
    struct timespec i_ctime;          // 状态改变时间
    unsigned long i_state;
    unsigned int i_dirt;
    atomic_t i_count;
    unsigned long i_generation;
    // 其他字段...
};

3. dentry(目录项)

dentry描述目录中的条目,用于路径名查找。

#include <linux/dcache.h>

struct dentry {
    unsigned int d_flags;
    seqcount_t d_seq;
    struct hlist_node d_hash;
    struct dentry *d_parent;         // 父目录
    struct qstr d_name;              // 文件名
    struct inode *d_inode;            // 对应的inode
    unsigned char d_iname[DNAME_INLINE_LEN];  // 短文件名
    struct lockref d_lockref;
    const struct dentry_operations *d_op;
    struct super_block *d_sb;         // 超级块
    unsigned long d_time;
    void *d_fsdata;
    // 其他字段...
};

4. file(文件)

file描述一个已打开的文件实例。

#include <linux/fs.h>

struct file {
    union {
        struct llist_node fu_llist;
        struct rcu_head fu_rcuhead;
    } f_u;
    struct path f_path;
    struct inode *f_inode;
    const struct file_operations *f_op;  // 文件操作
    spinlock_t f_lock;
    atomic_long_t f_count;
    unsigned int f_flags;
    fmode_t f_mode;
    struct mutex f_pos_lock;
    loff_t f_pos;                     // 文件位置
    struct fown_struct f_owner;
    const struct cred *f_cred;
    struct file_ra_state f_ra;
    // 其他字段...
};

文件系统操作

1. super_operations

#include <linux/fs.h>

struct super_operations {
    struct inode *(*alloc_inode)(struct super_block *sb);
    void (*destroy_inode)(struct inode *);
    void (*dirty_inode)(struct inode *, int flags);
    int (*write_inode)(struct inode *, struct writeback_control *wbc);
    int (*drop_inode)(struct inode *);
    void (*evict_inode)(struct inode *);
    void (*put_super)(struct super_block *);
    int (*sync_fs)(struct super_block *sb, int wait);
    int (*freeze_super)(struct super_block *sb);
    int (*freeze_fs)(struct super_block *sb);
    int (*unfreeze_fs)(struct super_block *sb);
    int (*statfs)(struct dentry *, struct kstatfs *);
    int (*remount_fs)(struct super_block *, int *, char *);
    void (*umount_begin)(struct super_block *);
    // 其他字段...
};

2. inode_operations

struct inode_operations {
    int (*create)(struct inode *, struct dentry *, umode_t, bool);
    struct dentry * (*lookup)(struct inode *, struct dentry *, unsigned int);
    int (*link)(struct dentry *, struct inode *, struct dentry *);
    int (*unlink)(struct inode *, struct dentry *);
    int (*symlink)(struct inode *, struct dentry *, const char *);
    int (*mkdir)(struct inode *, struct dentry *, umode_t);
    int (*rmdir)(struct inode *, struct dentry *);
    int (*mknod)(struct inode *, struct dentry *, umode_t, dev_t);
    int (*rename)(struct inode *, struct dentry *, struct inode *, struct dentry *, unsigned int);
    int (*readlink)(struct dentry *, char __user *, int);
    const char *(*get_link)(struct dentry *, struct inode *, struct delayed_call *);
    int (*permission)(struct inode *, int);
    int (*setattr)(struct dentry *, struct iattr *);
    int (*getattr)(const struct path *, struct kstat *, unsigned int, __u32);
    int (*setxattr)(struct dentry *, const char *, const void *, size_t, int);
    ssize_t (*getxattr)(struct dentry *, const char *, void *, size_t);
    ssize_t (*listxattr)(struct dentry *, char *, size_t);
    int (*removexattr)(struct dentry *, const char *);
    // 其他字段...
};

3. file_operations

struct file_operations {
    struct module *owner;
    loff_t (*llseek)(struct file *, loff_t, int);
    ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
    ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
    ssize_t (*read_iter)(struct kiocb *, struct iov_iter *);
    ssize_t (*write_iter)(struct kiocb *, struct iov_iter *);
    int (*iterate)(struct file *, struct dir_context *);
    unsigned int (*poll)(struct file *, struct poll_table_struct *);
    long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
    long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
    int (*mmap)(struct file *, struct vm_area_struct *);
    int (*open)(struct inode *, struct file *);
    int (*flush)(struct file *, fl_owner_t id);
    int (*release)(struct inode *, struct file *);
    int (*fsync)(struct file *, loff_t, loff_t, int datasync);
    int (*fasync)(int, struct file *, int);
    int (*lock)(struct file *, int, struct file_lock *);
    // 其他字段...
};

文件系统注册与挂载

1. 注册文件系统

#include <linux/filesystem.h>

struct file_system_type {
    const char *name;                 // 文件系统名称
    int fs_flags;
    int (*mount)(struct file_system_type *, int, const char *, void *);
    void (*kill_sb)(struct super_block *);
    struct module *owner;
    struct file_system_type *next;
    struct list_head fs_supers;
    // 其他字段...
};

// 注册文件系统
register_filesystem(struct file_system_type *fs);

// 注销文件系统
unregister_filesystem(struct file_system_type *fs);

2. 挂载文件系统

# 挂载文件系统
mount -t ext4 /dev/sda1 /mnt

# 重新挂载
mount -o remount /

# 绑定挂载
mount --bind /old /new

# 查看已挂载的文件系统
mount
cat /proc/mounts

VFS缓存机制

1. dentry缓存

dentry缓存用于加速路径名查找。

#include <linux/dcache.h>

// 查找dentry
struct dentry *d_lookup(struct dentry *parent, struct qstr *name);

// 添加dentry
void d_add(struct dentry *entry, struct inode *inode);

// 删除dentry
void d_drop(struct dentry *dentry);

// 释放dentry
void dput(struct dentry *);

2. inode缓存

inode缓存用于缓存已访问文件的inode信息。

#include <linux/fs.h>

// 获取inode
struct inode *iget5_locked(struct super_block *sb, unsigned long ino,
                          int (*test)(struct inode *, void *),
                          int (*set)(struct inode *, void *), void *data);

// 释放inode
void iput(struct inode *inode);

// 删除inode
void iput_final(struct inode *inode);

3. 页面缓存

页面缓存用于缓存文件数据。

#include <linux/pagemap.h>

// 添加页面到页面缓存
int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
                              pgoff_t index, gfp_t gfp_mask);

// 从页面缓存读取页面
struct page *find_get_page(struct address_space *mapping, pgoff_t index);

// 释放页面
void put_page(struct page *page);

路径名查找

1. 路径查找流程

#include <linux/namei.h>

// 执行路径名查找
struct path {
    struct vfsmount *mnt;
    struct dentry *dentry;
};

int path_lookup(const char *name, unsigned int flags, struct path *path);
int kern_path(const char *name, unsigned int flags, struct path *path);
int user_path_at(int dfd, const char __user *name, unsigned flags, struct path *path);

2. 路径查找示例

static int my_lookup(const char *pathname) {
    struct path path;
    int err;
    
    err = kern_path(pathname, LOOKUP_FOLLOW, &path);
    if (err) {
        printk(KERN_ERR "Path lookup failed: %d\n", err);
        return err;
    }
    
    printk(KERN_INFO "Found: %s, inode: %lu\n", 
           path.dentry->d_name.name, path.dentry->d_inode->i_ino);
    
    path_put(&path);
    return 0;
}

文件锁

1. fcntl文件锁

#include <linux/fcntl.h>
#include <linux/file.h>

// 文件锁结构
struct file_lock {
    struct file_lock *fl_next;
    struct list_head fl_link;
    struct list_head fl_block;
    fl_owner_t fl_owner;
    unsigned int fl_flags;
    unsigned char fl_type;
    unsigned int fl_pid;
    struct file *fl_file;
    loff_t fl_start;
    loff_t fl_end;
    // ...
};

// 锁定文件
int fcntl(int fd, int cmd, ...);

//  flock锁定
int flock(int fd, int operation);

2. 文件锁示例

#include <fcntl.h>
#include <unistd.h>

int lock_file(int fd) {
    struct flock fl;
    
    fl.l_type = F_WRLCK;    // 写锁
    fl.l_whence = SEEK_SET;
    fl.l_start = 0;
    fl.l_len = 0;           // 锁定整个文件
    fl.l_pid = getpid();
    
    return fcntl(fd, F_SETLKW, &fl);
}

int unlock_file(int fd) {
    struct flock fl;
    
    fl.l_type = F_UNLCK;
    fl.l_whence = SEEK_SET;
    fl.l_start = 0;
    fl.l_len = 0;
    
    return fcntl(fd, F_SETLK, &fl);
}

特殊文件系统

1. proc文件系统

proc文件系统提供了访问内核信息的接口。

# 查看进程信息
ls /proc/
cat /proc/cpuinfo
cat /proc/meminfo
cat /proc/<pid>/status

2. sysfs文件系统

sysfs导出内核对象信息。

# 查看设备
ls /sys/devices/
ls /sys/class/
ls /sys/bus/

# 查看设备属性
cat /sys/class/net/eth0/operstate

3. tmpfs文件系统

tmpfs是基于内存的临时文件系统。

# 创建tmpfs
mount -t tmpfs tmpfs /mnt/tmp

# 指定大小
mount -t tmpfs -o size=1G tmpfs /mnt/tmp

结论

虚拟文件系统是Linux内核中最核心的子系统之一,它通过抽象层让用户空间程序能够透明地访问各种不同类型的文件系统。理解VFS的数据结构和操作机制对于文件系统开发、设备驱动编写和系统性能优化都有重要意义。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值