深入linux内核架构--虚拟文件系统VFS(mount及lookup)

Linux文件存储抽象VFS 概念VFS(Virtual Filesystem Switch)称为虚拟文件系统或虚拟文件系统转换,是一个内核软件层,在具体的文件系统之上抽象的一层,用来处理与Posix文件系统相关的所有调用,表现为能够给各种文件系统提供一个通用的接口,使上层的应用程序能够使用通用的接口访问不同文件系统,同时也为不同文件系统的通信提供了媒介。架构VFS在整个Linux系统中的架构视图如下:Linux系统的User... 阅读详情
AI摘要

本文深入解析了Linux VFS(虚拟文件系统)的概念,包括super_block、dentry、inode、address_space和file等核心数据结构。通过EXT2文件系统实例,详细阐述了文件系统挂载(mount)、查找(lookup)和卸载(umount)的过程,以及inode在block_device中的存储细节。

上一篇文章对VFS做了一下简单介绍,基本理解到VFS中的基本数据结构为super_block,dentry,inode,address_space及file;
其中super_block是文件系统初始化时建立起来了,其中存储着inode信息,文件系统类型,等等信息;
dentry:管理着文件名filename 到 inode的映射;
inode:管理着文件的元数据信息,主要包括对应的存储设备,在数据存储设备的位置,文件的大小,访问信息等等元数据
address_space: 是内存cache的核心数据结构,主要负责内存与磁盘数据的同步,包括内存页的写入写出等。
file:是与进程关联的数据结构,其中关联着address_space,而address_space是真正与磁盘等设备进行交互的真正的地方,所以多个进程可能会对同一个文件的操作,当然如果多个进程同时打开一个文件进行写的话会把文件流corrupted。

基本介绍

在之前的简介实战中我们有讲到一个磁盘设备格式化为对应的文件系统,初始化完super_block后需要进行挂载,以及在操作文件之前都需要通过vfs_path_lookup来找到对应的inode,以便真正的进行读写数据,那么文件挂载做了一些什么事情,而文件系统又是以什么样的方式来找到对应的inode的呢?结合着EXT2文件系统的具体实现我们来彻底探寻一下vfsmount和path_look_up。

mount

注册文件系统

fs/super.c中的register_filesystem用来向内核注册文件系统,内核中所有的文件系统都保存在一个单链表中,各个文件系统的名称存储为字符串,所以该函数会扫描文件系统链表,直至找到对应的文件系统或到达链表尾部,如果是第一种情况会报一个不能注册两次的错误,如果是后者就会将对应的文件系统放置链表末尾完成注册。相关代码如下:

 

int register_filesystem(struct file_system_type * fs)
{
    int res = 0;
    struct file_system_type ** p;
    BUG_ON(strchr(fs->name, '.'));
    if (fs->next)
        return -EBUSY;
    write_lock(&file_systems_lock);
    p = find_filesystem(fs->name, strlen(fs->name)); // 遍历文件系统链表
    if (*p)
        res = -EBUSY;
    else
        *p = fs;
    write_unlock(&file_systems_lock);
    return res;
}
struct file_system_type {
    const char *name;// 文件系统名称
    int fs_flags;
#define FS_REQUIRES_DEV     1 
#define FS_BINARY_MOUNTDATA 2
#define FS_HAS_SUBTYPE      4
#define FS_USERNS_MOUNT     8   /* Can be mounted by userns root */
#define FS_RENAME_DOES_D_MOVE   32768   /* FS will handle d_move() during rename() internally. */
    struct dentry *(*mount) (struct file_system_type *, int,
               const char *, void *);// 将该文件系统mount到对应的目录
    void (*kill_sb) (struct super_block *);
    struct module *owner;
    struct file_system_type * next;
    ...
};

mount结构

目录树的装载和卸载要比文件系统注册复杂的多,其需要对内核的数据结构执行很多操作,比较复杂,后面结合结构图详细说明一下。
在Unix中文件系统层次结构如下图:其中每个方框中的是一个dentry目录项,其可以挂载为不同的文件系统,但是其path_lookup都需要从根目录开始索引,并当其是一个挂载点(图中的/, /mnt, /mnt/cdrom)时,会更新其dentry中的super_block数据结构。因此文件系统的挂载是可以嵌套的,其主要体现在父文件系统中的dentry中存储的super_block信息来区分其对应的具体是哪一个文件系统。

 

mount结构

 

vfsmount数据结构如下:

 

struct vfsmount {
    struct dentry *mnt_root;    /* root of the mounted tree */
    struct super_block *mnt_sb; /* pointer to superblock */
    int mnt_flags;
} __randomize_layout;
struct mount {
    struct hlist_node mnt_hash;
    struct mount *mnt_parent;
    struct dentry *mnt_mountpoint;
    struct vfsmount mnt;
    union {
        struct rcu_head mnt_rcu;
        struct llist_node mnt_llist;
    };
#ifdef CONFIG_SMP
    struct mnt_pcp __percpu *mnt_pcp;
#else
    int mnt_count;
    int mnt_writers;
#endif
    struct list_head mnt_mounts;    /* list of children, anchored here */
    struct list_head mnt_child; /* and going through their mnt_child */
    struct list_head mnt_instance;  /* mount instance on sb->s_mounts */
    const char *mnt_devname;    /* Name of device e.g. /dev/dsk/hda1 */
    struct list_head mnt_list;
    struct list_head mnt_expire;    /* link in fs-specific expiry list */
    struct list_head mnt_share; /* circular list of shared mounts */
    struct list_head mnt_slave_list;/* list of slave mounts */
    struct list_head mnt_slave; /* slave list entry */
    struct mount *mnt_master;   /* slave is on master->mnt_slave_list */
    struct mnt_namespace *mnt_ns;   /* containing namespace */
    struct mountpoint *mnt_mp;  /* where is it mounted */
    struct hlist_node mnt_mp_list;  /* list mounts with the same mountpoint */
    struct list_head mnt_umounting; /* list entry for umount propagation */
#ifdef CONFIG_FSNOTIFY
    struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
    __u32 mnt_fsnotify_mask;
#endif
    int mnt_id;         /* mount identifier */
    int mnt_group_id;       /* peer group identifier */
    int mnt_expiry_mark;        /* true if marked for expiry */
    struct hlist_head mnt_pins;
    struct fs_pin mnt_umount;
    struct dentry *mnt_ex_mountpoint;
} __randomize_layout;
mount mnt = real_mount(vfsmount) // real_mount map of  vfsmount to mount 
static inline struct mount *real_mount(struct vfsmount *mnt)
{
    return container_of(mnt, struct mount, mnt);
}
#define container_of(ptr, type, member) ({              \
    void *__mptr = (void *)(ptr);                   \
    BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&   \
             !__same_type(*(ptr), void),            \
             "pointer type mismatch in container_of()");    \
    ((type *)(__mptr - offsetof(type, member))); })
/*
 * 可以看出其实 mount 数据结构是伴随着 vfsmount的,并存储在vfsmount的上一个连续内存地址中
 * 其中mount数据结构就维护着整个mount tree结构,而vfsmount只管理着当前挂载点相关的文件系统管理。
 */

mount系统调用

mount系统调用的入口点时sys_mount函数,其定义在fs/namespace.c中,其主要是委托给do_mount函数进行挂载操作
调用流程图如下:

mount调用流程

数据结构关系图如下:

 

mount struct

mount的最主要的目标是初始化vfsmount.mnt_root dentry到内存,其包括以下几个步骤:

  1. 根据被挂载的设备名称查找对应设备的inode,从而获得其block_device,也就是简介中提到的/dev/sda硬盘设备,这一块具体到设备管理相关再介绍,这也体现了“万物皆文件”的思想。
  2. 从对应的block_device中读取对应数据到内存中生成super_block对象。一般各个具体的文件系统会将自己相关的信息写入到super_block->s_fs_info中,有兴趣的可以看一下ext2_fill_super相关的具体实现。
  3. 将vfsmount的mnt_root=super_block->s_root, mnt_sb= super_block
  4. 将当前mountpoint添加到父文件系统命名空间,并将mountpoint加入到父挂载点的子列表中,以便当前挂载点卸载时能回到之前的挂载点。

代码如下:

 

/**
* domount充当多路分解器的角色,其判断传入的flag,然后交给实际的处理函数,包括:
* do_reconfigure_mnt修改装载文件系统的选项
* do_loopback通过换回接口装载文件系统
* do_change_type 更改文件系统类型
* do_move_mount 移动已经装载的文件系统
* do_new_mount 挂载一个新的文件系统(主要讲解一下这个)
**/
long do_mount(const char *dev_name, const char __user *dir_name,
        const char *type_page, unsigned long flags, void *data_page)
{
    ... // flags判断处理
    if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
        retval = do_reconfigure_mnt(&path, mnt_flags);
    else if (flags & MS_REMOUNT)
        retval = do_remount(&path, flags, sb_flags, mnt_flags,
                    data_page);
    else if (flags & MS_BIND)
        retval = do_loopback(&path, dev_name, flags & MS_REC);
    else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
        retval = do_change_type(&path, flags);
    else if (flags & MS_MOVE)
        retval = do_move_mount(&path, dev_name);
    else
        retval = do_new_mount(&path, type_page, sb_flags, mnt_flags,
                      dev_name, data_page);
dput_out:
    path_put(&path);
    return retval;
}
static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
            int mnt_flags, const char *name, void *data)
{
    struct file_system_type *type;
    struct vfsmount *mnt;
    int err;
    if (!fstype)
        return -EINVAL;
    type = get_fs_type(fstype); 
    if (!type)
        return -ENODEV;
    mnt = vfs_kern_mount(type, sb_flags, name, data); // 
    if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
        !mnt->mnt_sb->s_subtype)
        mnt = fs_set_subtype(mnt, fstype);
    put_filesystem(type);
    if (IS_ERR(mnt))
        return PTR_ERR(mnt);
    if (mount_too_revealing(mnt, &mnt_flags)) {
        mntput(mnt);
        return -EPERM;
    }
    err = do_add_mount(real_mount(mnt), path, mnt_flags);
    if (err)
        mntput(mnt);
    return err;
}

挂载的基本流程如上;其基本思路就是通过设备初始化mountpoint的dentry中的superblock。

umount

既然介绍了mount,那也简单介绍一下umount吧,umount的系统调用入口为ksys_umount,最终会调用到do_umount函数,其中ksys_umount会传入文件名,然后在ksys_umount中根据文件名找到对应的path,也就是对应的vfsmount数据结构,然后根据vfsmount字节偏移得到mount,后续do_umount就可以直接操作mount数据结构进行相关的umount操作了。do_umount会先通过特定于超级块的的umount_begin函数来释放与特定文件系统相关的内存或者nfs中的网络连接,mount相关的树数据结构交由umount_tree处理。
具体代码如下:

 

int ksys_umount(char __user *name, int flags)
{
    ...
    retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); // 根据用户文件名获取path
       ...
    mnt = real_mount(path.mnt); // 根据字节偏移形成映射 vfsmount -> mount
    ...
    retval = do_umount(mnt, flags);
dput_and_out:
    /* we mustn't call path_put() as that would clear mnt_expiry_mark */
    dput(path.dentry); // 更新dcache
    mntput_no_expire(mnt);
out:
    return retval;
}
static int do_umount(struct mount *mnt, int flags)
{
    struct super_block *sb = mnt->mnt.mnt_sb;
    int retval;
    retval = security_sb_umount(&mnt->mnt, flags);
    if (retval)
        return retval;
    /*
     * Allow userspace to request a mountpoint be expired rather than
     * unmounting unconditionally. Unmount only happens if:
     *  (1) the mark is already set (the mark is cleared by mntput())
     *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
     */
    if (flags & MNT_EXPIRE) {
        if (&mnt->mnt == current->fs->root.mnt ||
            flags & (MNT_FORCE | MNT_DETACH))
            return -EINVAL;
        /*
         * probably don't strictly need the lock here if we examined
         * all race cases, but it's a slowpath.
         */
        lock_mount_hash();
        if (mnt_get_count(mnt) != 2) {
            unlock_mount_hash();
            return -EBUSY;
        }
        unlock_mount_hash();
        if (!xchg(&mnt->mnt_expiry_mark, 1))
            return -EAGAIN;
    }
    /*
     * If we may have to abort operations to get out of this
     * mount, and they will themselves hold resources we must
     * allow the fs to do things. In the Unix tradition of
     * 'Gee thats tricky lets do it in userspace' the umount_begin
     * might fail to complete on the first run through as other tasks
     * must return, and the like. Thats for the mount program to worry
     * about for the moment.
     */
    if (flags & MNT_FORCE && sb->s_op->umount_begin) {
        sb->s_op->umount_begin(sb);
    }
    /*
     * No sense to grab the lock for this test, but test itself looks
     * somewhat bogus. Suggestions for better replacement?
     * Ho-hum... In principle, we might treat that as umount + switch
     * to rootfs. GC would eventually take care of the old vfsmount.
     * Actually it makes sense, especially if rootfs would contain a
     * /reboot - static binary that would close all descriptors and
     * call reboot(9). Then init(8) could umount root and exec /reboot.
     */
    if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
        /*
         * Special case for "unmounting" root ...
         * we just try to remount it readonly.
         */
        if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
            return -EPERM;
        down_write(&sb->s_umount);
        if (!sb_rdonly(sb))
            retval = do_remount_sb(sb, SB_RDONLY, NULL, 0);
        up_write(&sb->s_umount);
        return retval;
    }
    namespace_lock();
    lock_mount_hash();
    /* Recheck MNT_LOCKED with the locks held */
    retval = -EINVAL;
    if (mnt->mnt.mnt_flags & MNT_LOCKED)
        goto out;
    event++;
    if (flags & MNT_DETACH) {
        if (!list_empty(&mnt->mnt_list))
            umount_tree(mnt, UMOUNT_PROPAGATE);
        retval = 0;
    } else {
        shrink_submounts(mnt);
        retval = -EBUSY;
        if (!propagate_mount_busy(mnt, 2)) {
            if (!list_empty(&mnt->mnt_list))
                umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
            retval = 0;
        }
    }
out:
    unlock_mount_hash();
    namespace_unlock();
    return retval;
}

lookup

前面对于mount挂载相关做了一些简单介绍,接下来结合Ext2文件系统简单分析一下linux是如何通过filename查找对应的inode。
vfs_path_lookup是lookup的前端函数,最终会调用到filename_lookup,那么所以主要来分析一下filename_lookup中的调用流程和逻辑。调用图如下:

 

lookup调用逻辑

 

其中path_init负责初始化nameidata中的root及path数据结构至superblock中的s_root,然后后续就是基于当前path中的dentry依次往后查找。
link_path_walk依次遍历一个一个目录项,以'/'作为分隔,并去掉重复的'/',并将真正的工作委托给walk_component进行。
walk_component首先从缓存中查找对应的inode,找不到则委托给文件系统去查找
可以看到最终会交给inode->iop->lookup,这个operation我们在之前的章节中可以找到,这个是一个与具体的文件系统相关的操作。
相关代码如下:

 

static int filename_lookup(int dfd, struct filename *name, unsigned flags,
               struct path *path, struct path *root)
{
    int retval;
    struct nameidata nd;
    if (IS_ERR(name))
        return PTR_ERR(name);
    if (unlikely(root)) {
        nd.root = *root;
        flags |= LOOKUP_ROOT;
    }
    set_nameidata(&nd, dfd, name);
    retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
    if (unlikely(retval == -ECHILD))
        retval = path_lookupat(&nd, flags, path);
    if (unlikely(retval == -ESTALE))
        retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);

    if (likely(!retval))
        audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
    restore_nameidata();
    putname(name);
    return retval;
}
/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
{
    const char *s = path_init(nd, flags); // 初始化nd中的root及path
    // ...
    while (!(err = link_path_walk(s, nd)) // 依次遍历每个目录项
        && ((err = lookup_last(nd)) > 0)) {
        s = trailing_symlink(nd);
    }
    // ...
    terminate_walk(nd);
    return err;
}
static int link_path_walk(const char *name, struct nameidata *nd)
{
    int err;
    if (IS_ERR(name))
        return PTR_ERR(name);
    while (*name=='/') // 去掉多余重复的slash
        name++;
    if (!*name)
        return 0;
    /* At this point we know we have a real path component. */
    for(;;) {
        // ... // 参数检测 及flag等nd相关参数设置
        name += hashlen_len(hash_len);
        if (!*name)
            goto OK;
        /*
         * If it wasn't NUL, we know it was '/'. Skip that
         * slash, and continue until no more slashes.
         */
        do {
            name++;
        } while (unlikely(*name == '/'));
        if (unlikely(!*name)) {
OK:
            /* pathname body, done */
            if (!nd->depth)
                return 0;
            name = nd->stack[nd->depth - 1].name;
            /* trailing symlink, done */
            if (!name)
                return 0;
            /* last component of nested symlink */
            err = walk_component(nd, WALK_FOLLOW);
        } else {
            /* not the last component */
            err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
        }
        // ...
    }
}
static int walk_component(struct nameidata *nd, int flags)
{
    struct path path;
    struct inode *inode;
    unsigned seq;
    int err;
    /*
     * "." and ".." are special - ".." especially so because it has
     * to be able to know about the current root directory and
     * parent relationships.
     */
    if (unlikely(nd->last_type != LAST_NORM)) {
        err = handle_dots(nd, nd->last_type);
        if (!(flags & WALK_MORE) && nd->depth)
            put_link(nd);
        return err;
    }
    err = lookup_fast(nd, &path, &inode, &seq); // 缓存查询
    if (unlikely(err <= 0)) {
        if (err < 0)
            return err;
        path.dentry = lookup_slow(&nd->last, nd->path.dentry,
                      nd->flags); // 文件系统中读取
        if (IS_ERR(path.dentry))
            return PTR_ERR(path.dentry);

        path.mnt = nd->path.mnt;
        err = follow_managed(&path, nd);
        if (unlikely(err < 0))
            return err;
        if (unlikely(d_is_negative(path.dentry))) {
            path_to_nameidata(&path, nd);
            return -ENOENT;
        }
        seq = 0;    /* we are already out of RCU mode */
        inode = d_backing_inode(path.dentry);
    }
    return step_into(nd, &path, flags, inode, seq);
}
static struct dentry *__lookup_slow(const struct qstr *name,
                    struct dentry *dir,
                    unsigned int flags)
{
    struct dentry *dentry, *old;
    struct inode *inode = dir->d_inode;
    // ...
    if (unlikely(!d_in_lookup(dentry))) {
        if (!(flags & LOOKUP_NO_REVAL)) {
            int error = d_revalidate(dentry, flags);
            if (unlikely(error <= 0)) {
                if (!error) {
                    d_invalidate(dentry);
                    dput(dentry);
                    goto again;
                }
                dput(dentry);
                dentry = ERR_PTR(error);
            }
        }
    } else {
        old = inode->i_op->lookup(inode, dentry, flags);
        d_lookup_done(dentry);
        if (unlikely(old)) {
            dput(dentry);
            dentry = old;
        }
    }
    return dentry;
}

所以我们结合Ext2中的相关实现来探究一下inode的查找,以及inode在block_device中的存储详情。
i_op->lookup在Ext2中的实现为ext2_lookup,我们来看下相关的调用流程:

 

ext2_lookup

 

ext2_inode_by_name(dir, &dentry->d_name);在dir inode中查找对应filename所在的ino
ext2_find_entry (dir, child, &page);根据dirinode中的信息,比如目录项中数据的大小;通过address_space从磁盘依次读取相关该目录下的所有目录项到内存page中,Ext2中的目录项数据结构为ext2_dirent,而磁盘中存储的是该数据结构的数组链表,所以会变量内存page中所有的ext2_dirent,直至找到对应名字存在的目录项并返回对应的ino编号。
ext2_iget(dir->i_sb, ino);会委托ext2_get_inode获取对应的raw_inode信息,并最终会进行一些小端转存到真正的inode返回
ext2_get_inode(inode->i_sb, ino, &bh);从super_block中根据ino获取对应的inode信息
相关代码如下:

 

/*
 *  ext2_find_entry()
 *
 * finds an entry in the specified directory with the wanted name. It
 * returns the page in which the entry was found (as a parameter - res_page),
 * and the entry itself. Page is returned mapped and unlocked.
 * Entry is guaranteed to be valid.
 */
struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
            const struct qstr *child, struct page **res_page)
{
    const char *name = child->name;
    int namelen = child->len;
    unsigned reclen = EXT2_DIR_REC_LEN(namelen);
    unsigned long start, n;
    unsigned long npages = dir_pages(dir); // 根据dir->inode->size计算page数
    struct page *page = NULL;
    struct ext2_inode_info *ei = EXT2_I(dir); // 读取ext2_inode信息
    ext2_dirent * de;
    int dir_has_error = 0;
    if (npages == 0) // 空目录
        goto out;
    /* OFFSET_CACHE */
    *res_page = NULL;
    start = ei->i_dir_start_lookup; // 起始页偏移
    if (start >= npages)
        start = 0;
    n = start;
    do {
        char *kaddr;
        page = ext2_get_page(dir, n, dir_has_error); // 通过address_space读取一页到内存page
        if (!IS_ERR(page)) {
            kaddr = page_address(page);// 获取地址
            de = (ext2_dirent *) kaddr; // 地址内存读取
            kaddr += ext2_last_byte(dir, n) - reclen; // 下一个ext2_dirent地址
            while ((char *) de <= kaddr) {
                if (de->rec_len == 0) {
                    ext2_error(dir->i_sb, __func__,
                        "zero-length directory entry");
                    ext2_put_page(page);
                    goto out;
                }
                if (ext2_match (namelen, name, de)) // 找到对应的目录项
                    goto found;
                de = ext2_next_entry(de);
            }
            ext2_put_page(page);
        } else
            dir_has_error = 1;
        if (++n >= npages)
            n = 0;
        /* next page is past the blocks we've got */
        if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
            ext2_error(dir->i_sb, __func__,
                "dir %lu size %lld exceeds block count %llu",
                dir->i_ino, dir->i_size,
                (unsigned long long)dir->i_blocks);
            goto out;
        }
    } while (n != start);
out:
    return NULL;

found:
    *res_page = page;
    ei->i_dir_start_lookup = n;
    return de;
}
static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino, struct buffer_head **p)
{
    struct buffer_head * bh;
    unsigned long block_group;
    unsigned long block;
    unsigned long offset;
    struct ext2_group_desc * gdp;
    *p = NULL;
    if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
        ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
        goto Einval;
    block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);// 在Ext2中,inode是按指定个数分组存储的根据ino编号获取对应的block group
    gdp = ext2_get_group_desc(sb, block_group, NULL); // 根据block group位置从super_block获取group描述信息
    if (!gdp)
        goto Egdp;
    /*
     * Figure out the offset within the block group inode table
     */
    offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb); // 获取指定ino在sb的block_group中的内存地址偏移。
    block = le32_to_cpu(gdp->bg_inode_table) +
        (offset >> EXT2_BLOCK_SIZE_BITS(sb));
    if (!(bh = sb_bread(sb, block))) //读取一个block到buffer_head块缓存中
        goto Eio;
    *p = bh;
    offset &= (EXT2_BLOCK_SIZE(sb) - 1);
    return (struct ext2_inode *) (bh->b_data + offset); // 根据地址偏移返回对应的inode。
Einval:
    ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
           (unsigned long) ino);
    return ERR_PTR(-EINVAL);
Eio:
    ext2_error(sb, "ext2_get_inode",
           "unable to read inode block - inode=%lu, block=%lu",
           (unsigned long) ino, block);
Egdp:
    return ERR_PTR(-EIO);
}
struct ext2_group_desc
{
    __le32  bg_block_bitmap;        /* Blocks bitmap block */
    __le32  bg_inode_bitmap;        /* Inodes bitmap block */
    __le32  bg_inode_table;     /* Inodes table block */
    __le16  bg_free_blocks_count;   /* Free blocks count */
    __le16  bg_free_inodes_count;   /* Free inodes count */
    __le16  bg_used_dirs_count; /* Directories count */
    __le16  bg_pad;
    __le32  bg_reserved[3];
};

EXT2文件系统基本的存储结构图如下:

 

设备存储图



作者:淡泊宁静_3652
链接:https://www.jianshu.com/p/a98cb5519a50
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Linux系统的各个mount点以及文件系统挂载分析 分析代码,内核的do_sys_open加入检查点,过滤进程touch的目的是,这样可以简单的在用户态通过touch命令创建文件的方式触发进入此分支。df命令查看文件系统挂载情况创建脚本,分别在上述路径下创建一个.txt后缀名的文件执行sudo bash ./a.sh, 可以看到有些文件无法写入,所以touch写的方式调试失效了,不过没关系,先看其它几个文件系统。dmesg查看内核打印输出。 阅读详情

相关推荐

图解Linux虚拟文件系统(VFS)之挂载机制解析

本文深入解析Linux虚拟文件系统(VFS)的挂载机制,详细讲解挂载点索引、挂载实例创建和挂载对接三大核心环节。通过代码示例和实际案例,揭示VFS如何统一管理ext4、NTFS等不同文件系统,并探讨绑定挂载、命名空间隔离等高级应用场景,为系统开发和故障排查提供实用指导。

weixin_30357231的博客 492

Linux Kernel:mount 流程(从 VFS 到实际文件系统 exfat)

方法,此方法判断是否存在挂载到(path->mnt,path->dentry)上的文件系统,如果找到就返回这个 vfsmount 信息,然后继续查找是否有挂载到它身上的文件系统,如此反复直到找不到为止。可以看出是记录了路径名,和字符串的 hash 值。函数,此函数不仅仅是完成加锁功能,更重要的是。此处逻辑就是一个循环,此处会不断调用以。),来查找最后一次挂载的文件系统的根。首先在内核中维护了一个哈希链表,由。从链表中找到这个文件系统类 (作为即将挂载文件系统的挂载点。其可通过传来的挂载点的。

dnfkr的博客 1957

深入浅出文件系统(二)文件系统对象

文件系统对象 Linux文件系统对象之间的关系可以概括为文件系统类型、超级块、inode、dentry和vfsmount之间的关系。 文件系统类型规定了某种类型文件系统的行为,它存在的主要目的是为了构造这种类型文件系统的实例,或者被称为超级块实例。 超级块反映了文件系统整体的控制信息,超级块以多种方式存在。对于基于磁盘的文件系统,它以特定格式存在于磁盘的固定区域(取决于文件系统类型),为磁盘上的超级块。在文件系统被装载时,其内容被读入内存,构建内存中的超级块。其中某些信息为各种类型的文件系统所共有..

sinat_37817094的博客 1512

VFS数据结构之(其它结构)

1. 与文件系统相关的数据结构 1.1 文件系统类型 1.1.1 file_system_type 数据结构 用来描述特定文件系统的结构。 struct file_system_type { const char *name; /* 文件系统名 */ int fs_flags; /* 文件系统类型标志 */ int (*get_sb) (struct file_system_type *, int, const char *, void *, struct

指向NULL 318

AI智能体升级实战:从规则匹配到Function Call,准确率提升86%的技术选型之路

AI智能体升级实战:从规则匹配到Function Call的技术演进 文章记录了AI邮件助手从传统规则匹配升级到Function Call的技术迭代过程。通过案例分析,揭示了规则匹配的三大核心问题:覆盖不全(用户表达千变万化)、组合困难(多条件容易漏掉)、维护噩梦(每个需求都要新增规则)。测试数据显示,规则匹配在复杂查询中的准确率仅51%,严重影响用户体验。 技术团队对比了三种升级方案:1) 继续优化规则匹配(天花板明显);2) 微调专用NLP模型(成本高);3) 采用Function Call(灵活高效)

yweng18的博客 1083

经典脉冲激光测距原理&雷达方程

脉冲激光测距机的工作过程如下:首先用瞄准光学系统瞄准目标,然后接通激光电源,储能电容器充电,产生触发闪光灯的触发脉冲,闪光灯点亮,激光器受激辐射,从输出反射镜发射出一个前沿陡峭、峰值功率高的激光脉冲,通过发射光学系统压缩光束发射角后射向目标。通过计算激光发射功率经介质传输的衰减,目标表面截获和反射的光功率,到达接收视场的光功率,以及接收光学系统接收到的光功率,就可以得到到达光电探测器的光功率,即接收功率。⑤光東路程上的折射率变化;),测量光脉冲到达目标并由目标返回到接收机的时间,由此计算出目标的距离。

weixin_46153372的博客 2547

VFS的数据结构

linux混混之牢骚: 万恶的金钱,有人为你卖儿卖女,有人为你去坐牢。在具有中国特色的社会主义制度下,剥削似乎并没有像《资本论》中所述那样消失,反倒日益的猖獗。富人,是我个人所能理解的,毕竟他们曾付出过。但富人中有一大批富不仁之人,开宝马撞人,砍手.....比比皆是。 是否真的要让富人消灭了穷人才能达到共同富裕的目标,才能使中国富强?这是个问题!linux结构复杂,但靠着大批量的程序员不断的奋战

linux-混混 2580

STM32双ADC同步采样实战:FFT测量相位差的5个关键细节

本文深入探讨了利用STM32双ADC同步采样结合FFT算法精确测量信号相位差的实战技巧。文章聚焦五个关键细节:确保ADC时钟同源、优化DMA缓冲区设计、信号调理与窗函数选择、精确的相位提取与解缠绕,以及系统误差校准与验证。这些细节是提升相位差测量精度、确保系统稳定性的核心,尤其强调了STM32配置中易被忽视的时钟同步问题。

oauth7security的博客 598

图解Linux虚拟文件系统(VFS)之挂载实战篇

本文深入剖析Linux虚拟文件系统(VFS)的挂载机制,通过“嫁接树枝”的生动比喻,详解从路径查找、创建超级块到挂载实例对接的完整流程。核心阐述了VFS如何统一管理不同文件系统,并利用`struct mount`、`struct path`等关键数据结构实现精密的挂载操作,是理解Linux文件子系统运作的关键。

perl8的博客 449

从车机到广告屏:CS5523芯片在5大显示场景的应用方案全解析

本文全面解析了CS5523芯片在车载显示、数字广告机、医疗设备和工业HMI等五大场景的应用方案。作为高性能MIPI转eDP芯片,CS5523凭借出色的兼容性和低功耗特性,成为国产替代方案的优选。文章详细介绍了芯片架构、核心技术及实际应用中的设计要点,为工程师提供全面的技术参考。

julia4scientist的博客 910

HACK RF学习之旅记录1——填坑历程

在家隔离期偶尔发现RTL SDR,是realtek芯片破解了做软件无线电,价格非常实在不到百元,功能可以玩电视 广播等等。后来搜搜发现更强大的Hack RF ONE开发板,淘宝和闲鱼都看了大多都是Hack RF ONE单板999,便宜的闲鱼有600多的。还有一块扩展板是MCU+屏+时钟,看起来也很实用(非常推荐)。通过闲鱼某卖家找了渠道,从上海一家店的淘宝下单优惠价格(据说是原厂,超出想象,暗号是...

ZForce2009的博客 1万+

【Linux内核专栏 12】VFS 与文件系统

本篇讲 VFS 怎么把"ext4/proc/sysfs/设备驱动"统一成一套 open/read/write。讲清四大对象(superblock/inode/dentry/file)、挂载、路径查找、procfs/sysfs/debugfs、ext4 概念。读完能理解 `cat /proc/cpuinfo` 和 `cat /etc/hosts` 走同一套 VFS 但到不同后端。

peter_pan_study的博客 696

微信小程序云开发 - 实现商家转账到零钱(微信企业付款到零钱) V3功能流程及完整实例代码,过微信云开发的云函数实现企业付款到零钱(提供详细示例源码纯云函数实现,证书、密钥、签名等各种常见问题解决方案

企业付款到零钱(微信小程序提现功能,提现到零钱)微信小程序-云函数实现微信企业付款到零钱功能,实现微信商户号中的企业付款到零钱的功能,微信小程序+云开发+商家转账至零钱+微信小程序用户提现,使用云开发环境的云函数实现微信小程序的支付功能,微信小程序实现微信企业付款到零钱功能的流程,V3 V2基于微信云开发的商家转账至零钱,小程序云函数企业付款到零钱功能实现,云开发实现微信商户支付企业付款到零钱功能,不用服务器纯云函数实现,详细示例代码demo教程,API证书错误,转账不到账怎么办,密钥生成有误,转账失败,有

🏆 前端领域优质创作者 1万+

解析 Linux 中的 VFS 文件系统机制

developerWorks 中国Linux文档库 解析 Linux 中的 VFS 文件系统机制 Ricard Chen (ricard_chen@yahoo.com) XML error: Please enter a value for the author element's jobtitle attribute, or the compan

四哥的专栏 670

【电源专题】Type-C母座外壳直接接主板GND还是接RC呢?别再纠结了!

Type-C屏蔽壳接地这件事,没有唯一标准答案。符合规范、贴合产品、能过认证,就是最优解。希望这篇文章能帮你理清思路。

一点硬件 633

黑猫详解【UFS Unipro】第1篇:Unipro协议框架详解

UniPro规范适用于广泛的设备类型,如应用程序处理器、co-处理器、调制解调器、存储子系统,包括非易失性内存模块、显示器、相机传感器、3D图形和多媒体加速器。它还适用于不同类型的数据流量,如控制消息、批量数据传输和分组流。MIPI UniPro是一个成熟的通用接口,易于实现、可在传统模块化设备架构中灵活使用,适合满足并应对移动和其他行业不断增长的需求。UniPro充当UFS的数据链路层,负责host和device的连接。UniPro不仅定义了数据链路层,它本是也是一个比较完整的协议栈。

黑猫奥利奥的博客 3658

解析Linux中的VFS文件系统机制

转载:原文地址https://www.ibm.com/developerworks/cn/linux/l-vfs/ 1. 摘要 本文阐述 Linux 中的文件系统部分,源代码来自基于 IA32 的 2.4.20 内核。总体上说 Linux 下的文件系统主要可分为三大块:一是上层的文件系统的系统调用,二是虚拟文件系统 VFS(Virtual Filesystem Switch),三是挂...

diaozhiwa5526的博客 204

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

【转载】解析Linux中的VFS文件系统机制

本文系转载, 已经征得原作者的同意(本人修改了部分错误)。原作者为 Ricard Chen (ricard_chen@yahoo.com)。原文链接:http://www.ibm.com/developerworks/cn/linux/l-vfs/ 本文阐述 Linux 中的文件系统部分,源代码来自基于 IA32 的 2.4.20 内核。总体上说 Linux 下的文件系统...

anffh1770的博客 167

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

linux调用vfsmkdir函数,解析Linux中的VFS文件系统机制(下)【转载】

5. VFS 下目录的建立为了更好地理解 VFS,下面我们用一个实际例子来看看 Linux 是如何在 VFS 的根目录下建立一个新的目录 "/dev" 的。要在 VFS中建立一个新的目录,首先我们得对该目录进行搜索,搜索的目的是找到将要建立的目录其父目录的相关信息,因为"皮之不存,毛将焉附"。比如要建立目录/home/ricard,那么首先必须沿目录路径进行逐层搜索,本例中先从根目录找起,然后在根...

weixin_34012355的博客 509

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Linux VFS 虚拟文件系统:四大对象、调用链路与踩坑排查

在Linux中,“一切皆文件”背后依赖虚拟文件系统VFS:它把ext4、XFS、NFS、tmpfs等具体文件系统的差异,抽象成统一的open、read、write、stat、mount接口。VFS并非直接存储数据,而是通过super_block、inode、dentry、file等核心对象管理挂载、路径解析、引用计数与page cache,并用file_operations把请求分发给底层实现。理解这套机制,既能解释dcache加速、脏页回刷、inode耗尽等行为,也能支撑运维排查磁盘满但du看不到、lso

weixin_33997389的博客 417

Linux VFS原理与性能优化实战指南

虚拟文件系统(VFS)作为Linux内核的核心子系统,通过统一的抽象接口实现了对多种文件系统的支持。其工作原理基于四大对象模型:超级块管理文件系统元数据,inode描述文件实体,dentry缓存加速路径查找,file对象关联进程操作。在存储性能优化领域,VFS的dentry缓存机制和文件系统选择策略直接影响IO吞吐量,特别是在处理海量小文件或混合负载场景时尤为关键。通过调整/proc/sys/fs/dentry-state参数可以优化目录项缓存,而ext4/XFS/Btrfs等文件系统的特性差异决定了不同业

weixin_30367873的博客 321

Linux 中的 VFS 文件系统机制

1. 摘要 本文阐述 Linux 中的文件系统部分,源代码来自基于 IA32 的 2.4.20 内核。总体上说 Linux 下的文件系统主要可分为三大块:一是上层的文件系统的系统调用,二是虚拟文件系统 VFS(Virtual Filesystem Switch),三是挂载到 V

ADC0809608的专栏 1161

Linux VFS机制简析(一)

Linux VFS机制简析(一) 本文主要基于Linux内核文档,简单分析Linux VFS机制,以期对编写新的内核文件系统(通常是给分布式文件系统编写内核客户端)的场景有所帮助。 个人渊源 切入正文之前先扯点别的,舰队我在04年刚接触Linux时就深入分析了VFS,当时刚毕业入职一家做NAS存储的公司,需要对VFS、block device、MD等内核模块深入了解。时隔10几年之后的今天,因给一...

weixin_33998125的博客 570
上一篇: ROS中的消息到底长什么样子
下一篇: 深入linux内核架构--虚拟文件系统VFS
土豆西瓜大芝麻
博客等级 码龄18年 1199粉丝 238原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值