虚拟文件系统[2]

本文介绍了VFS(虚拟文件系统)中的超级块对象及其数据结构。超级块对象代表已安装的文件系统,详细阐述了其结构体super_block的各个字段含义及初始化过程,并解释了超级块操作函数表super_operations中各项函数的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
  •  VFS对象及其数据结构

    VFS其实采用的是面向对象的思想,使用一族数据结构来代表通用文件对象。这些数据结构表现得就像是对象。因为内核纯粹使用C代码实现,没有直接利用面向对象的语义,所以内核中的数据结构都使用C结构体实现,但这些结构体包含数据的同时也包含操作这些数据的函数指针,其中的操作函数由具体文件系统实现。

   VFS中有四个主要的对象类型,它们分别是:

1. 超级块对象,它代表一个已安装文件系统

2. 索引节点对象,它代表一个文件

3. 目录项对象,它代表一个目录项,是路径的一个组成部分

4. 文件对象,它代表由进程打开的文件

   注意,因为VFS将目录作为一个文件来处理,所以不存在目录对象。目录项代表的是路径中的一个组成部分,它可能包括一个普通文件。换句话说,目录项不同于目录,但目录却和文件相同。

    每个主要对象中都包含一个操作对象,这些操作对象描述了内核针对主要对象可以使用的方法。最主要的几种操作对象如下:

1. super_operations对象,其中包括内核针对特定文件系统所能调用的方法,比如read_inode()和sync_fs()等方法。

2. inode_operations对象,其中包括内核针对特定的文件所能调用的方法,比如create()和link()等方法。

3. dentry_operations对象,其中包括内核针对特定目录所能调用的方法,比如d_compare()和d_delete()等方法。

4. file对象,其中,包括进程针对已打开文件所能调用的方法,比如read()和write()等方法。

  操作对象作为一个指针结构体被实现,此结构体中包含指向操作其父对象的函数指针。对于其中许多方法来说,可以继承使用VFS提供的通用函数,如果通用函数提供的基本功能无法满足需要,就不许使用实际文件系统的独有方法填充这些函数指针,使其指向文件系统实例。

    注意,这里所说的对象指的是结构体,不是像C++和Java那样的真正的数据类型。

    VFS使用了大量结构体对象,它所包括的对象远远多于上述几种对象,比如每个注册的文件系统都由file_system_type结构体表还是,它描述了文件系统及其能;另外,每一个安装点也都用vfsmount结构体表示,它保护的是安装点的相关信息,如位置和安装标志等。三个与进程相关的结构体分别是:file_struct,fs_struct和namespace。它们描述了文件系统以及和进程相关的文件。

  •    超级块对象

   各种文件系统都必须实现超级块,该对象用于存储特定文件系统的信息,通常对应于存放在磁盘特定扇区中(所以叫超级块对象)的文件系统超级块或文件替他控制块。对于并非基于磁盘的文件系统(如基于内存的文件系统,比如sysfs),它们会在使用现场创建超级块并将其保存到内存中。

  超级块对象由super_block结构体表示:

 

  1. 在<Fs.h(include/linux)>中
  2. struct super_block {
  3.     struct list_head    s_list;     /* Keep this first */
  4.     dev_t           s_dev;      /* search index; _not_ kdev_t */
  5.     unsigned long       s_blocksize;    /* 以字节为单位的块大小 */
  6.     unsigned char       s_blocksize_bits;    /* 以位为单位的块大小 */
  7.     unsigned char       s_dirt;    /* 修改(脏)标志*/
  8.     unsigned long long  s_maxbytes; /* Max file size */
  9.     struct file_system_type *s_type;    /*文件系统类型 */
  10.     const struct super_operations   *s_op;    /* 超级块方法 */
  11.     struct dquot_operations *dq_op;    /* 磁盘限额方法 */
  12.     struct quotactl_ops *s_qcop;   /*限额控制方法  */
  13.     struct export_operations *s_export_op;  /* 导出方法 */
  14.     unsigned long       s_flags;    /* 登录标志 */
  15.     unsigned long       s_magic;    /* 文件系统的魔数 */
  16.     struct dentry       *s_root;   /* 目录登录点 */
  17.     struct rw_semaphore s_umount;   /* 卸载信号量 */
  18.     struct mutex        s_lock;    /* 超级块互斥锁 */
  19.     int         s_count;       /* 超级块引用计数 */
  20.     int         s_syncing;     /* 文件系统同步标志 */
  21.     int         s_need_sync_fs;  /* 尚未同步标志 */
  22.     atomic_t        s_active;    /* 活动引用计数 */
  23. #ifdef CONFIG_SECURITY
  24.     void                    *s_security;   /* 安全模块 */
  25. #endif
  26.     struct xattr_handler    **s_xattr;   /*  */
  27.     struct list_head    s_inodes;   /* all inodes */
  28.     struct list_head    s_dirty;    /* dirty inodes */
  29.     struct list_head    s_io;       /* parked for writeback */
  30.     struct hlist_head   s_anon;     /* anonymous dentries for (nfs) exporting */
  31.     struct list_head    s_files;  /* 被分配文件链表 */
  32.     struct block_device *s_bdev;   /* 相关块设备 */
  33.     struct list_head    s_instances;  /* 该类型的文件系统 */
  34.     struct quota_info   s_dquot;    /* Diskquota specific options */
  35.     int         s_frozen;   /*  */
  36.     wait_queue_head_t   s_wait_unfrozen;
  37.     char s_id[32];              /* Informational name */
  38.     void            *s_fs_info; /* Filesystem private info */
  39.     /*
  40.      * The next field is for VFS *only*. No filesystems have any business
  41.      * even looking at it. You had been warned.
  42.      */
  43.     struct mutex s_vfs_rename_mutex;    /* Kludge */
  44.     /* Granularity of c/m/atime in ns.
  45.        Cannot be worse than a second */
  46.     u32        s_time_gran;
  47. };

  创建、管理和销毁超级块对象的代码位于文件fs/super.c中。超级块对象通过alloc_super()函数创建并初始化。在文件系统安装时,内核会调用该函数以便从磁盘读取文件系统超级块,并且将其信息填充到内存中的超级块对象中。

  1. /**
  2.  *  alloc_super -   create new superblock
  3.  *  @type:  filesystem type superblock should belong to
  4.  *
  5.  *  Allocates and initializes a new &struct super_block.  alloc_super()
  6.  *  returns a pointer new superblock or %NULL if allocation had failed.
  7.  */
  8. static struct super_block *alloc_super(struct file_system_type *type)
  9. {
  10.     struct super_block *s = kzalloc(sizeof(struct super_block),  GFP_USER);
  11.     static struct super_operations default_op;
  12.     if (s) {
  13.         if (security_sb_alloc(s)) {
  14.             kfree(s);
  15.             s = NULL;
  16.             goto out;
  17.         }
  18.         INIT_LIST_HEAD(&s->s_dirty);
  19.         INIT_LIST_HEAD(&s->s_io);
  20.         INIT_LIST_HEAD(&s->s_files);
  21.         INIT_LIST_HEAD(&s->s_instances);
  22.         INIT_HLIST_HEAD(&s->s_anon);
  23.         INIT_LIST_HEAD(&s->s_inodes);
  24.         init_rwsem(&s->s_umount);
  25.         mutex_init(&s->s_lock);
  26.         lockdep_set_class(&s->s_umount, &type->s_umount_key);
  27.         /*
  28.          * The locking rules for s_lock are up to the
  29.          * filesystem. For example ext3fs has different
  30.          * lock ordering than usbfs:
  31.          */
  32.         lockdep_set_class(&s->s_lock, &type->s_lock_key);
  33.         down_write(&s->s_umount);
  34.         s->s_count = S_BIAS;
  35.         atomic_set(&s->s_active, 1);
  36.         mutex_init(&s->s_vfs_rename_mutex);
  37.         mutex_init(&s->s_dquot.dqio_mutex);
  38.         mutex_init(&s->s_dquot.dqonoff_mutex);
  39.         init_rwsem(&s->s_dquot.dqptr_sem);
  40.         init_waitqueue_head(&s->s_wait_unfrozen);
  41.         s->s_maxbytes = MAX_NON_LFS;
  42.         s->dq_op = sb_dquot_ops;
  43.         s->s_qcop = sb_quotactl_ops;
  44.         s->s_op = &default_op;
  45.         s->s_time_gran = 1000000000;
  46.     }
  47. out:
  48.     return s;
  49. }

 

超级块操作

  超级块对象中最重要的一个域是s_op,它指向超级块的操作函数表。超级块操作函数表由super_operations结构标示:

  1. /*
  2.  * NOTE: write_inode, delete_inode, clear_inode, put_inode can be called
  3.  * without the big kernel lock held in all filesystems.
  4.  */
  5. struct super_operations {
  6. /* 该方法在给定的超级块下创建一个新的索引节点对象 */
  7.     struct inode *(*alloc_inode)(struct super_block *sb);
  8. /* 该函数用于释放给定的索引节点 */
  9.     void (*destroy_inode)(struct inode *);
  10. /* 该函数以inode->i_ino为索引。从磁盘上读取索引节点,并填充内存中对应的索引节点结构的剩余部分 */
  11.     void (*read_inode) (struct inode *);
  12.  /* VFS在索引节点脏(被修改)时会调用此函数 。日志文件系统(如ext3)执行该函数进行日志更新*/ 
  13.     void (*dirty_inode) (struct inode *);
  14.      /* 该函数用于将给定的索引节点写入磁盘 */  
  15.     int (*write_inode) (struct inode *, int);
  16. /* 该函数用于释放给定的索引节点 */
  17.     void (*put_inode) (struct inode *);
  18. /* 在最后一个指向索引节点的引用被释放后,VFS会调用该函数。VFS只需要简单地删除这个索引节点后,普通Unix文件系统就不定义这个函数。注意调用者必须持有inode_lock锁 */
  19.     void (*drop_inode) (struct inode *);
  20. /* 该函数用于从磁盘上释放给定的索引节点 */
  21.     void (*delete_inode) (struct inode *);
  22. /* 该函数在卸载文件系统时由VFS调用,用来释放超级块 */
  23.     void (*put_super) (struct super_block *);
  24. /* 该函数用给定的超级块跟新磁盘上的超级块。VFS通过该函数对内存中的内存中的超级块和磁盘折哦你哦个的超级块进行同步 */
  25.     void (*write_super) (struct super_block *);
  26. /* 该函数使文件系统的数据元与磁盘上的文件系统同步。wait参数指定操作是否同步*/
  27.     int (*sync_fs)(struct super_block *sb, int wait);
  28. /* 该函数首先禁止对文件系统做改变,再使用给定的超级块更新磁盘上的超级块。目前LVM(逻辑卷标管理)会调用该函数 */
  29.     void (*write_super_lockfs) (struct super_block *);
  30. /* 该函数对文件系统解除锁定,它是write_super_lockfs()的逆操作*/
  31.     void (*unlockfs) (struct super_block *);
  32. /* VFS通过调用该函数获取文件系统状态。指定文件系统相关的统计信息将放置在kstatfs结构中 */
  33.     int (*statfs) (struct dentry *, struct kstatfs *);
  34. /*当指定新的安装选项重新安装文件系统时,VFS会调用该函数*/
  35.     int (*remount_fs) (struct super_block *, int *, char *);
  36. /* VFS调用该函数释放索引节点,并清空包含相关数据的所有页面*/
  37.     void (*clear_inode) (struct inode *);
  38. /* VFS调用该函数中断安装操作。该函数被网络文件系统使用,如NFS */
  39.     void (*umount_begin) (struct vfsmount *, int);
  40.     int (*show_options)(struct seq_file *, struct vfsmount *);
  41.     int (*show_stats)(struct seq_file *, struct vfsmount *);
  42. #ifdef CONFIG_QUOTA
  43.     ssize_t (*quota_read)(struct super_block *, intchar *, size_t, loff_t);
  44.     ssize_t (*quota_write)(struct super_block *, intconst char *, size_t, loff_t);
  45. #endif
  46. };

   该结构的每一项都是一个指向超级块操作函数的指针,超级块操作函数执行文件系统和索引节点的低层操作。当文件系统需要对其超级块执行操作时,首先要在超级块对象中寻找需要的操作方法。所有以上函数都是由VFS在进程上下文中调用。必要时,它们都可以阻塞。其中的一些函数是可选的:在超级块操作表中,文件系统可以将不需要的函数指针设置成NULL。

 

 

 

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值