EXT4檔案系統學習(13)VFS之VFS超級塊super_block
VFS超級塊
VFS超級塊是根據具體檔案系統的超級塊建立起來的記憶體結構:
struct super_block {
struct list_head s_list; /* Keep this first */
dev_t s_dev; /* search index; _not_ kdev_t */
unsigned char s_blocksize_bits;
unsigned long s_blocksize;
loff_t s_maxbytes; /* Max file size */
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;
#ifdef CONFIG_SECURITY
void *s_security;
#endif
const struct xattr_handler **s_xattr;
struct list_head s_inodes; /* all inodes */
struct hlist_bl_head s_anon; /* anonymous dentries for (nfs) exporting */
struct list_head s_mounts; /* list of mounts; _not_ for fs use */
struct block_device *s_bdev;
struct backing_dev_info *s_bdi;
struct mtd_info *s_mtd;
struct hlist_node s_instances;
unsigned int s_quota_types; /* Bitmask of supported quota types */
struct quota_info s_dquot; /* Diskquota specific options */
struct sb_writers s_writers;
char s_id[32]; /* Informational name */
u8 s_uuid[16]; /* UUID */
void *s_fs_info; /* Filesystem private info */指向具體檔案系統的超級塊記憶體物件,就是ext4_sb_info
unsigned int s_max_links;
fmode_t s_mode;
/* Granularity of c/m/atime in ns.
Cannot be worse than a second */
u32 s_time_gran;
/*
* The next field is for VFS *only*. No filesystems have any business
* even looking at it. You had been warned.
*/
struct mutex s_vfs_rename_mutex; /* Kludge */
/*
* Filesystem subtype. If non-empty the filesystem type field
* in /proc/mounts will be "type.subtype"
*/
char *s_subtype;
/*
* Saved mount options for lazy filesystems using
* generic_show_options()
*/
char __rcu *s_options;
const struct dentry_operations *s_d_op; /* default d_op for dentries */
/*
* Saved pool identifier for cleancache (-1 means none)
*/
int cleancache_poolid;
struct shrinker s_shrink; /* per-sb shrinker handle */
/* Number of inodes with nlink == 0 but still referenced */
atomic_long_t s_remove_count;
/* Being remounted read-only */
int s_readonly_remount;
/* AIO completions deferred from interrupt context */
struct workqueue_struct *s_dio_done_wq;
struct hlist_head s_pins;
/*
* Keep the lru lists last in the structure so they always sit on their
* own individual cachelines.
*/
struct list_lru s_dentry_lru ____cacheline_aligned_in_smp;
struct list_lru s_inode_lru ____cacheline_aligned_in_smp;
struct rcu_head rcu;
/*
* Indicates how deep in a filesystem stack this SB is
*/
int s_stack_depth;
};
當核心需要掛載(mount)一個塊裝置時,可以從分割槽表中資訊得知這個塊裝置的檔案系統型別,從文章EXT4檔案系統學習(八)磁碟結構可以看出分割槽資訊中的檔案系統型別,也可以從分割槽的superblock資訊中看出檔案系統型別。
static struct file_system_type ext4_fs_type = {
.owner = THIS_MODULE,
.name = "ext4",
.mount = ext4_mount,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};
MODULE_ALIAS_FS("ext4");
然後從file_system_type檔案系統物件連結串列中找到對應的檔案系統驅動程式的檔案系統物件,呼叫裡面的mount()函式獲取具體的檔案系統超級塊資訊。然後根據這些資訊初始化VFS超級塊,結構中的s_fs_info就指向具體檔案系統的超級塊記憶體物件,也就是ext4_sb_info。
由於各個檔案系統的超級塊不同,所以對操作超級塊的方法也不同。為此核心定義了一個super_operations結構,定義如下:
struct super_operations {
struct inode *(*alloc_inode)(struct super_block *sb);分配一個inode結構
void (*destroy_inode)(struct inode *);釋放一個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 *);
int (*freeze_fs) (struct super_block *);
int (*thaw_super) (struct super_block *);
int (*unfreeze_fs) (struct super_block *);
int (*statfs) (struct dentry *, struct kstatfs *);
int (*remount_fs) (struct super_block *, int *, char *);
void (*umount_begin) (struct super_block *);
int (*show_options)(struct seq_file *, struct dentry *);
int (*show_devname)(struct seq_file *, struct dentry *);
int (*show_path)(struct seq_file *, struct dentry *);
int (*show_stats)(struct seq_file *, struct dentry *);
#ifdef CONFIG_QUOTA
ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
struct dquot **(*get_dquots)(struct inode *);
#endif
int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
long (*nr_cached_objects)(struct super_block *,
struct shrink_control *);
long (*free_cached_objects)(struct super_block *,
struct shrink_control *);
};
可以看出super_operations結構中的函式指標都是在操作下層檔案系統,不同的檔案系統super_operations也是不同的。
當核心掛載塊裝置時,會根據分割槽表讀出檔案系統型別資訊,然後找到驅動中對應的已經註冊過的檔案系統物件,並呼叫它的mount函式設定s_op指標。
ext4檔案系統的mount函式是ext4_mount,裡面呼叫了ext4_fill_super函式會把磁碟上資料讀出,裝載磁碟和記憶體超級塊以及VFS超級塊。(裝載磁碟和記憶體超級塊可參考11節裡面介紹ext4_fill_super函式)
static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
const char *dev_name, void *data)
{
return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
}
這裡比較重要的是設定s_op指標:
static int ext4_fill_super(struct super_block *sb, void *data, int silent)
{
sb->s_op = &ext4_sops;
這樣就建立起了抽象的VFS超級塊物件與具體ext4超級塊物件的聯絡。
操作具體檔案系統的操作函式ext4_sops如下:
static const struct super_operations ext4_sops = {
.alloc_inode = ext4_alloc_inode,
.destroy_inode = ext4_destroy_inode,
.write_inode = ext4_write_inode,
.dirty_inode = ext4_dirty_inode,
.drop_inode = ext4_drop_inode,
.evict_inode = ext4_evict_inode,
.put_super = ext4_put_super,
.sync_fs = ext4_sync_fs,
.freeze_fs = ext4_freeze,
.unfreeze_fs = ext4_unfreeze,
.statfs = ext4_statfs,
.remount_fs = ext4_remount,
.show_options = ext4_show_options,
#ifdef CONFIG_QUOTA
.quota_read = ext4_quota_read,
.quota_write = ext4_quota_write,
.get_dquots = ext4_get_dquots,
#endif
.bdev_try_to_free_page = bdev_try_to_free_page,
};
ext4_fill_super函式最後會請求讀取根目錄的inode,呼叫
#define EXT4_ROOT_INO 2 /* Root inode */
root = ext4_iget(sb, EXT4_ROOT_INO);
繼續分析iget函式,先去inode雜湊連結串列快取裡面查詢,沒有的話就分配一個,分配不帶指定inode號,所以這裡必須在在表裡面查詢成功,但是根目錄的inode號什麼時候載入到記憶體inode表裡面的?
struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
{
struct inode *inode;
inode = iget_locked(sb, ino);
掛載檔案系統根目錄時,根目錄的inode號肯定不在雜湊連結串列中,所以需要新分配一個, 分配後再去連結串列中查詢inode為2的號, 沒有找到的話就把根目錄號賦值給新分配的inode,且標誌設定為I_NEW,
struct inode *iget_locked(struct super_block *sb, unsigned long ino)
{
struct inode *inode;
spin_lock(&inode_hash_lock);
inode = find_inode_fast(sb, head, ino);
spin_unlock(&inode_hash_lock);
if (inode) {
wait_on_inode(inode);
return inode;
}
inode = alloc_inode(sb);
if (inode) {
struct inode *old;
spin_lock(&inode_hash_lock);
/* We released the lock, so.. */
old = find_inode_fast(sb, head, ino);
if (!old) {
inode->i_ino = ino;
spin_lock(&inode->i_lock);
inode->i_state = I_NEW;
hlist_add_head(&inode->i_hash, head);
spin_unlock(&inode->i_lock);
inode_sb_list_add(inode);
spin_unlock(&inode_hash_lock);
/* Return the locked inode with I_NEW set, the
* caller is responsible for filling in the contents
*/
return inode;
}
iget_locked把根目錄的inode返回後,VFS inode就已經分配好了;這時候通過巨集EXT4_I轉換得到EXT4 記憶體inode結構。
struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
{
struct ext4_inode_info *ei;
struct inode *inode;
inode = iget_locked(sb, ino);
ei = EXT4_I(inode);
獲取到inode號資訊後就可以讀取磁碟上面邏輯的inode資料,讀取方法:
根據inode號獲取出屬於哪個塊組,然後根據inode在塊組內的偏移計算出塊inode在哪個塊內,最後把塊資料讀出到buffer_head中,然後再根據塊內偏移獲取得到磁碟inode資料:
struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
{
struct ext4_iloc iloc;
struct ext4_inode *raw_inode;磁碟inode
struct ext4_inode_info *ei;記憶體inode
struct inode *inode;VFS inode
inode = iget_locked(sb, ino);
ei = EXT4_I(inode);
__ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc, int in_mem)
iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
inode_offset = ((inode->i_ino - 1) %
EXT4_INODES_PER_GROUP(sb));
block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
bh = sb_getblk(sb, block);
iloc->bh = bh;
raw_inode = ext4_raw_inode(&iloc);
後面根據邏輯raw_inode設定記憶體inode和VFS inode:
ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
inode->i_mode = le16_to_cpu(raw_inode->i_mode);
ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
ei->i_flags = le32_to_cpu(raw_inode->i_flags);
inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
設定i_op和i_fop指標,這些個函式指標都是操作下層具體檔案系統。
if (S_ISREG(inode->i_mode)) {
inode->i_op = &ext4_file_inode_operations;
inode->i_fop = &ext4_file_operations;
ext4_set_aops(inode);
} else if (S_ISDIR(inode->i_mode)) {
inode->i_op = &ext4_dir_inode_operations;
inode->i_fop = &ext4_dir_operations;
} else if (S_ISLNK(inode->i_mode)) {
if (ext4_inode_is_fast_symlink(inode) &&
!ext4_encrypted_inode(inode)) {
inode->i_op = &ext4_fast_symlink_inode_operations;
nd_terminate_link(ei->i_data, inode->i_size,
sizeof(ei->i_data) - 1);
} else {
inode->i_op = &ext4_symlink_inode_operations;
ext4_set_aops(inode);
}
} else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
inode->i_op = &ext4_special_inode_operations;
if (raw_inode->i_block[0])
init_special_inode(inode, inode->i_mode,
old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
else
init_special_inode(inode, inode->i_mode,
new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
} else if (ino == EXT4_BOOT_LOADER_INO) {
make_bad_inode(inode);
} else {
ret = -EIO;
EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
goto bad_inode;
}
VFS超級塊介紹完畢,下一篇介紹VFS inode。
相關文章
- EXT4檔案系統學習(14)VFS之VFS inode
- EXT4檔案系統學習(15)VFS之VFS 檔案/目錄物件物件
- EXT4檔案系統學習(12)VFS之檔案系統物件file_system_type物件
- EXT4檔案系統學習(10)VFS之磁碟結構Group和superblockBloC
- EXT4檔案系統學習(9)VFS之磁碟結構inode和direntry
- EXT4檔案系統學習(11)VFS之記憶體結構sb和inode記憶體
- Linux VFSLinux
- VFS簡介和核心操作函式函式
- Linux 虛擬檔案系統四大物件:超級塊、inode、dentry、file之間關係Linux物件
- 簡單瞭解EXT4檔案系統
- 恢復ext4檔案系統被誤刪的檔案
- 檔案系統(六):一文看懂linux ext4檔案系統工作原理Linux
- 深入理解 ext4 等 Linux 檔案系統Linux
- 如何系統學習C 語言(下)之 檔案篇
- ros學習檔案系統介紹ROS
- NTFS、exFAT、FAT32、Ext4檔案系統的區別
- ext4和xfs檔案系統的擴容和收縮
- 在FreeBSD上mount Ext4檔案系統的行動硬碟硬碟
- python菜鳥教程學習13:檔案操作Python
- 超級詳細的mac系統檔案許可權修改指南Mac
- 記憶體檔案系統的再學習記憶體
- Hadoop學習(一)——HDFS分散式檔案系統Hadoop分散式
- Linux學習之檔案操作Linux
- Linux系統檔案學習內容多嗎?linux系統命令Linux
- Linux系統中檔案被刪除後的恢復方法(ext4)Linux
- Google分散式檔案系統GFS論文學習Go分散式
- C#學習 [型別系統] 類(13)C#型別
- python學習之讀寫檔案Python
- (十七)Python學習之檔案操作Python
- Linux EXT4檔案系統TF卡空間容量顯示和計算Linux
- 理解Linux檔案系統之 inodeLinux
- 分散式檔案系統之 FastDFS分散式AST
- 【python系統學習17】python中的檔案讀寫Python
- Linux 檔案系統之入門必看!Linux
- 不點兒之Linux檔案系統Linux
- 『學了就忘』Linux檔案系統管理 — 57、Linux檔案系統介紹Linux
- Linux系統篇-檔案系統&虛擬檔案系統Linux
- 檔案和檔案系統