ext2檔案系統super.c原始碼分析(Linux 2.6.24)
關於linux2.6.24版本的ext2檔案系統的原始碼,super.c也是第三大的檔案,歡迎指正!
/*
* linux/fs/ext2/super.c
*
* Copyright (C) 1992, 1993, 1994, 1995
* Remy Card (card@masi.ibp.fr)
* Laboratoire MASI - Institut Blaise Pascal
* Universite Pierre et Marie Curie (Paris VI)
*
* from
*
* linux/fs/minix/inode.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*
* Big-endian to little-endian byte-swapping/bitmaps by
* David S. Miller (davem@caip.rutgers.edu), 1995
*/
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/parser.h>
#include <linux/random.h>
#include <linux/buffer_head.h>
#include <linux/exportfs.h>
#include <linux/smp_lock.h>
#include <linux/vfs.h>
#include <linux/seq_file.h>
#include <linux/mount.h>
#include <linux/log2.h>
#include <asm/uaccess.h>
#include "ext2.h"
#include "xattr.h"
#include "acl.h"
#include "xip.h"
/*函式宣告,超級塊的同步函式,重新掛載檔案系統的函式以及得到檔案系統狀態的函式*/
static void ext2_sync_super(struct super_block *sb,
struct ext2_super_block *es);
static int ext2_remount (struct super_block * sb, int * flags, char * data);
static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
/*ext2的列印錯誤資訊的函式,引數有可變引數*/
void ext2_error (struct super_block * sb, const char * function,
const char * fmt, ...)
{
va_list args;
/*記憶體裡存放的超級塊資訊結構體*/
struct ext2_sb_info *sbi = EXT2_SB(sb);
/*ext2硬碟上存放的超級塊,此時在塊快取中*/
struct ext2_super_block *es = sbi->s_es;
/*如果檔案系統不是隻讀的*/
if (!(sb->s_flags & MS_RDONLY)) {
/*出錯了,就在ext2_sb_info結構體的對應位上或上對應位,標記檔案系統有錯誤發生*/
sbi->s_mount_state |= EXT2_ERROR_FS;
/*ext2_super_block結構體上對應位置位,標識檔案系統有錯誤發生*/
es->s_state =
cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
/*超級塊立即同步*/
ext2_sync_super(sb, es);
}
/*列印出錯的資訊*/
va_start(args, fmt);
printk(KERN_CRIT "EXT2-fs error (device %s): %s: ",sb->s_id, function);
vprintk(fmt, args);
printk("\n");
va_end(args);
/*如果是嚴重錯誤,列印相應的資訊*/
if (test_opt(sb, ERRORS_PANIC))
panic("EXT2-fs panic from previous error\n");
/*如果是隻讀錯誤,就列印應當把檔案系統重新只讀掛載*/
if (test_opt(sb, ERRORS_RO)) {
printk("Remounting filesystem read-only\n");
/*超級塊對應位設定為只讀*/
sb->s_flags |= MS_RDONLY;
}
}
/*列印警告資訊,錯誤不是很嚴重*/
void ext2_warning (struct super_block * sb, const char * function,
const char * fmt, ...)
{
va_list args;
/*僅僅是簡單的列印資訊*/
va_start(args, fmt);
printk(KERN_WARNING "EXT2-fs warning (device %s): %s: ",
sb->s_id, function);
vprintk(fmt, args);
printk("\n");
va_end(args);
}
/*更新檔案系統為動態的inode的修改級別*/
void ext2_update_dynamic_rev(struct super_block *sb)
{
struct ext2_super_block *es = EXT2_SB(sb)->s_es;
/*如果檔案系統的修改級別已經是新的了,直接返回*/
if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
return;
/*如果檔案系統不是新的修改級別,先警告,我們要修改了*/
ext2_warning(sb, __FUNCTION__,
"updating to rev %d because of new feature flag, "
"running e2fsck is recommended",
EXT2_DYNAMIC_REV);
/*設定第一個inode號,inode大小*/
es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
/*新的修改級別*/
es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
/* 此處不處理 es->s_feature_*compat*/
/* es->s_uuid會被e2fsck處理 */
/* 超級塊的其他欄位應該是0,如果不是,說明他們正在被使用,我們可以接下來讓e2fsck來解決*/
/* leave es->s_feature_*compat flags alone */
/* es->s_uuid will be set by e2fsck if empty */
/*
* The rest of the superblock fields should be zero, and if not it
* means they are likely already in use, so leave them alone. We
* can leave it up to e2fsck to clean up any inconsistencies there.
*/
}
/*釋放正在使用的超級塊*/
static void ext2_put_super (struct super_block * sb)
{
int db_count;
int i;
struct ext2_sb_info *sbi = EXT2_SB(sb);
/*釋放這個超級塊的塊裝置的所有在記憶體裡的緩衝區*/
ext2_xattr_put_super(sb);
/*如果檔案系統不是隻讀掛載的*/
if (!(sb->s_flags & MS_RDONLY)) {
struct ext2_super_block *es = sbi->s_es;
/*同步檔案系統的掛載狀態*/
es->s_state = cpu_to_le16(sbi->s_mount_state);
ext2_sync_super(sb, es);
}
/*檔案系統的儲存組描述符塊的數目,不是組描述符的個數*/
db_count = sbi->s_gdb_count;
/*遍歷所有的塊,釋放所有的組描述符的緩衝區*/
for (i = 0; i < db_count; i++)
if (sbi->s_group_desc[i])
brelse (sbi->s_group_desc[i]);
/*釋放組描述符陣列和檔案系統負載陣列*/
kfree(sbi->s_group_desc);
kfree(sbi->s_debts);
/*釋放者三個結構體*/
percpu_counter_destroy(&sbi->s_freeblocks_counter);
percpu_counter_destroy(&sbi->s_freeinodes_counter);
percpu_counter_destroy(&sbi->s_dirs_counter);
brelse (sbi->s_sbh);
sb->s_fs_info = NULL;
/*釋放ext2_sb_info結構體*/
kfree(sbi);
return;
}
/*用來存放ext2的inode的slab緩衝區*/
static struct kmem_cache * ext2_inode_cachep;
/*ext2分配一個空的inode,先分配ext2_inode_info,再返回其中的inode物件*/
static struct inode *ext2_alloc_inode(struct super_block *sb)
{
struct ext2_inode_info *ei;
/*先從ext2_inode_cachep上分配記憶體對應的ext2_inode_info結構體*/
ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
if (!ei)
return NULL;
/*配置了acl,先初始化acl為沒有對映狀態*/
#ifdef CONFIG_EXT2_FS_POSIX_ACL
ei->i_acl = EXT2_ACL_NOT_CACHED;
ei->i_default_acl = EXT2_ACL_NOT_CACHED;
#endif/*版本號*/
ei->i_block_alloc_info = NULL;
ei->vfs_inode.i_version = 1;
return &ei->vfs_inode;
}
/*釋放一個inode磁碟記憶體結構*/
static void ext2_destroy_inode(struct inode *inode)
{ /*呼叫記憶體的緩衝釋放函式,從ext2_inode_cachep上釋放*/
kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
}
/*初始化ext2_inode_info的指標,foo為ext2_inode_info*/
static void init_once(struct kmem_cache * cachep, void *foo)
{ /*先轉化獲得ext2_inode_info指標*/
struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
/*逐個初始化讀寫鎖,屬性鎖*/
rwlock_init(&ei->i_meta_lock);
#ifdef CONFIG_EXT2_FS_XATTR
init_rwsem(&ei->xattr_sem);
#endif
/*初始化vfs層的inode*/
mutex_init(&ei->truncate_mutex);
inode_init_once(&ei->vfs_inode);
}
/*初始化ext2_inode_cachep的緩衝區*/
static int init_inodecache(void)
{
/*建立一塊可延展的記憶體緩衝區,並使用init_once函式初始化,
然後賦值給ext2_inode_cachep,kmem_cache_create是記憶體子系統的函式,
第一個引數是這個緩衝區的名字,第二個引數是緩衝區的大小,先設定為一個ext2_inode_info的大小,
第三個引數是對齊,第四個引數flag,標誌這緩衝區可延展,就是可以向後繼續分配,並且可以重新設定大小,
第五個引數是初始化函式*/
ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
sizeof(struct ext2_inode_info),
0, (SLAB_RECLAIM_ACCOUNT|
SLAB_MEM_SPREAD),
init_once);
/*失敗了,就返回記憶體不夠的錯誤*/
if (ext2_inode_cachep == NULL)
return -ENOMEM;
return 0;
}
/*銷燬上個函式建立的ext2_inode_cachep*/
static void destroy_inodecache(void)
{
/*記憶體子系統的記憶體銷燬函式*/
kmem_cache_destroy(ext2_inode_cachep);
}
/*清理inode的額外的記憶體資源,一般是指acl結構體*/
static void ext2_clear_inode(struct inode *inode)
{
struct ext2_block_alloc_info *rsv = EXT2_I(inode)->i_block_alloc_info;
/*如果沒有配置acl,就不用釋放了*/
#ifdef CONFIG_EXT2_FS_POSIX_ACL
struct ext2_inode_info *ei = EXT2_I(inode);
/*如果i_acl不是未對映狀態就說明需要釋放*/
if (ei->i_acl && ei->i_acl != EXT2_ACL_NOT_CACHED) {
/*之前講過的函式,釋放acl結構體*/
posix_acl_release(ei->i_acl);
/*標記為未對映*/
ei->i_acl = EXT2_ACL_NOT_CACHED;
}
/*如果i_default_acl不是未對映狀態就說明需要釋放*/
if (ei->i_default_acl && ei->i_default_acl != EXT2_ACL_NOT_CACHED) {
/*之前講過的函式,釋放acl結構體*/
posix_acl_release(ei->i_default_acl);
/*標記為未對映*/
ei->i_default_acl = EXT2_ACL_NOT_CACHED;
}
#endif
ext2_discard_reservation(inode);
EXT2_I(inode)->i_block_alloc_info = NULL;
if (unlikely(rsv))
kfree(rsv);
}
/*得到檔案系統的掛載選項,存放在seq裡*/
static int ext2_show_options(struct seq_file *seq, struct vfsmount *vfs)
{
/*先得到超級塊的記憶體的資訊結構體,大部分資訊都在這個結構體裡放著*/
struct super_block *sb = vfs->mnt_sb;
struct ext2_sb_info *sbi = EXT2_SB(sb);
struct ext2_super_block *es = sbi->s_es;
unsigned long def_mount_opts;
def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
if (sbi->s_sb_block != 1)
seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
if (test_opt(sb, MINIX_DF))
seq_puts(seq, ",minixdf");
if (test_opt(sb, GRPID))
seq_puts(seq, ",grpid");
if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
seq_puts(seq, ",nogrpid");
if (sbi->s_resuid != EXT2_DEF_RESUID ||
le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
seq_printf(seq, ",resuid=%u", sbi->s_resuid);
}
if (sbi->s_resgid != EXT2_DEF_RESGID ||
le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
seq_printf(seq, ",resgid=%u", sbi->s_resgid);
}
if (test_opt(sb, ERRORS_CONT)) {
int def_errors = le16_to_cpu(es->s_errors);
if (def_errors == EXT2_ERRORS_PANIC ||
def_errors == EXT2_ERRORS_RO) {
seq_puts(seq, ",errors=continue");
}
}
if (test_opt(sb, ERRORS_RO))
seq_puts(seq, ",errors=remount-ro");
if (test_opt(sb, ERRORS_PANIC))
seq_puts(seq, ",errors=panic");
if (test_opt(sb, NO_UID32))
seq_puts(seq, ",nouid32");
if (test_opt(sb, DEBUG))
seq_puts(seq, ",debug");
if (test_opt(sb, OLDALLOC))
seq_puts(seq, ",oldalloc");
#ifdef CONFIG_EXT2_FS_XATTR
if (test_opt(sb, XATTR_USER))
seq_puts(seq, ",user_xattr");
if (!test_opt(sb, XATTR_USER) &&
(def_mount_opts & EXT2_DEFM_XATTR_USER)) {
seq_puts(seq, ",nouser_xattr");
}
#endif
#ifdef CONFIG_EXT2_FS_POSIX_ACL
if (test_opt(sb, POSIX_ACL))
seq_puts(seq, ",acl");
if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT2_DEFM_ACL))
seq_puts(seq, ",noacl");
#endif
if (test_opt(sb, NOBH))
seq_puts(seq, ",nobh");
#if defined(CONFIG_QUOTA)
if (sbi->s_mount_opt & EXT2_MOUNT_USRQUOTA)
seq_puts(seq, ",usrquota");
if (sbi->s_mount_opt & EXT2_MOUNT_GRPQUOTA)
seq_puts(seq, ",grpquota");
#endif
#if defined(CONFIG_EXT2_FS_XIP)
if (sbi->s_mount_opt & EXT2_MOUNT_XIP)
seq_puts(seq, ",xip");
#endif
return 0;
}
/*配額的操作函式*/
#ifdef CONFIG_QUOTA
static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
#endif
/*超級塊的操作函式集合*/
static const struct super_operations ext2_sops = {
.alloc_inode = ext2_alloc_inode,
.destroy_inode = ext2_destroy_inode,
.read_inode = ext2_read_inode,
.write_inode = ext2_write_inode,
.delete_inode = ext2_delete_inode,
.put_super = ext2_put_super,
.write_super = ext2_write_super,
.statfs = ext2_statfs,
.remount_fs = ext2_remount,
.clear_inode = ext2_clear_inode,
.show_options = ext2_show_options,
#ifdef CONFIG_QUOTA
.quota_read = ext2_quota_read,
.quota_write = ext2_quota_write,
#endif
};
/*用在nfs和ext2交流上,sb是ext2的超級塊,vobjp是一個記憶體塊,第一個是inode號碼,第二個是ext2版本號,返回檔案的dentry結構體*/
static struct inode *ext2_nfs_get_inode(struct super_block *sb,
u64 ino, u32 generation)
{
struct inode *inode;
if (ino < EXT2_FIRST_INO(sb) && ino != EXT2_ROOT_INO)
return ERR_PTR(-ESTALE);
if (ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
return ERR_PTR(-ESTALE);
/* iget isn't really right if the inode is currently unallocated!!
* ext2_read_inode currently does appropriate checks, but
* it might be "neater" to call ext2_get_inode first and check
* if the inode is valid.....
*/
inode = iget(sb, ino);
if (inode == NULL)
return ERR_PTR(-ENOMEM);
if (is_bad_inode(inode) ||
(generation && inode->i_generation != generation)) {
/* we didn't find the right inode.. */
iput(inode);
return ERR_PTR(-ESTALE);
}
return inode;
}
static struct dentry *ext2_fh_to_dentry(struct super_block *sb, struct fid *fid,
int fh_len, int fh_type)
{
return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
ext2_nfs_get_inode);
}
static struct dentry *ext2_fh_to_parent(struct super_block *sb, struct fid *fid,
int fh_len, int fh_type)
{
return generic_fh_to_parent(sb, fid, fh_len, fh_type,
ext2_nfs_get_inode);
}
/* Yes, most of these are left as NULL!!
* A NULL value implies the default, which works with ext2-like file
* systems, but can be improved upon.
* Currently only get_parent is required.
*//* nfs檔案系統與作業系統進行交流的操作函式集合 */
static const struct export_operations ext2_export_ops = {
.fh_to_dentry = ext2_fh_to_dentry,
.fh_to_parent = ext2_fh_to_parent,
.get_parent = ext2_get_parent,
};
/*獲得超級塊塊號,引數data是掛載檔案系統的選項*/
static unsigned long get_sb_block(void **data)
{
unsigned long sb_block;
/*先獲得選項字串*/
char *options = (char *) *data;
/*檢驗字串是不是sb=開頭的,不是就說明錯了*/
if (!options || strncmp(options, "sb=", 3) != 0)
return 1; /* Default location */
options += 3;/*繼續看後邊的*/
/*sb_block是從引數得到超級塊塊號*/
sb_block = simple_strtoul(options, &options, 0);
/*後邊沒了,也不對*/
if (*options && *options != ',') {
printk("EXT2-fs: Invalid sb specification: %s\n",
(char *) *data);
return 1;
}
if (*options == ',')
options++;
*data = (void *) options;
return sb_block;
}
/*檔案系統的各種選項*/
enum {
Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
Opt_acl, Opt_noacl, Opt_xip, Opt_ignore, Opt_err, Opt_quota,
Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
};
/*檔案系統的選項和對應的引數字串*/
static match_table_t tokens = {
{Opt_bsd_df, "bsddf"},
{Opt_minix_df, "minixdf"},
{Opt_grpid, "grpid"},
{Opt_grpid, "bsdgroups"},
{Opt_nogrpid, "nogrpid"},
{Opt_nogrpid, "sysvgroups"},
{Opt_resgid, "resgid=%u"},
{Opt_resuid, "resuid=%u"},
{Opt_sb, "sb=%u"},
{Opt_err_cont, "errors=continue"},
{Opt_err_panic, "errors=panic"},
{Opt_err_ro, "errors=remount-ro"},
{Opt_nouid32, "nouid32"},
{Opt_nocheck, "check=none"},
{Opt_nocheck, "nocheck"},
{Opt_debug, "debug"},
{Opt_oldalloc, "oldalloc"},
{Opt_orlov, "orlov"},
{Opt_nobh, "nobh"},
{Opt_user_xattr, "user_xattr"},
{Opt_nouser_xattr, "nouser_xattr"},
{Opt_acl, "acl"},
{Opt_noacl, "noacl"},
{Opt_xip, "xip"},
{Opt_grpquota, "grpquota"},
{Opt_ignore, "noquota"},
{Opt_quota, "quota"},
{Opt_usrquota, "usrquota"},
{Opt_reservation, "reservation"},
{Opt_noreservation, "noreservation"},
{Opt_err, NULL}
};
/*轉化引數字串,變成ext2_sb_info結構體上相應的位*/
static int parse_options (char * options,
struct ext2_sb_info *sbi)
{
char * p;
substring_t args[MAX_OPT_ARGS];
int option;
/*字串不可以是NULL*/
if (!options)
return 1;
/*大迴圈,一個一個轉換字串為ext2_sb_info的flag,每次讀取逗號之間的*/
while ((p = strsep (&options, ",")) != NULL) {
int token;
/*空的,讀下一個*/
if (!*p)
continue;
/*在tokens陣列裡尋找和p指標指向的字串選項匹配的選項*/
token = match_token(p, tokens, args);
/*在ext2_sb_info的相應位上設定*/
switch (token) {
case Opt_bsd_df:
clear_opt (sbi->s_mount_opt, MINIX_DF);
break;
case Opt_minix_df:
set_opt (sbi->s_mount_opt, MINIX_DF);
break;
case Opt_grpid:
set_opt (sbi->s_mount_opt, GRPID);
break;
case Opt_nogrpid:
clear_opt (sbi->s_mount_opt, GRPID);
break;
case Opt_resuid:
if (match_int(&args[0], &option))
return 0;
sbi->s_resuid = option;
break;
case Opt_resgid:
if (match_int(&args[0], &option))
return 0;
sbi->s_resgid = option;
break;
case Opt_sb:
/* handled by get_sb_block() instead of here */
/* *sb_block = match_int(&args[0]); */
break;
case Opt_err_panic:
clear_opt (sbi->s_mount_opt, ERRORS_CONT);
clear_opt (sbi->s_mount_opt, ERRORS_RO);
set_opt (sbi->s_mount_opt, ERRORS_PANIC);
break;
case Opt_err_ro:
clear_opt (sbi->s_mount_opt, ERRORS_CONT);
clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
set_opt (sbi->s_mount_opt, ERRORS_RO);
break;
case Opt_err_cont:
clear_opt (sbi->s_mount_opt, ERRORS_RO);
clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
set_opt (sbi->s_mount_opt, ERRORS_CONT);
break;
case Opt_nouid32:
set_opt (sbi->s_mount_opt, NO_UID32);
break;
case Opt_nocheck:
clear_opt (sbi->s_mount_opt, CHECK);
break;
case Opt_debug:
set_opt (sbi->s_mount_opt, DEBUG);
break;
case Opt_oldalloc:
set_opt (sbi->s_mount_opt, OLDALLOC);
break;
case Opt_orlov:
clear_opt (sbi->s_mount_opt, OLDALLOC);
break;
case Opt_nobh:
set_opt (sbi->s_mount_opt, NOBH);
break;
#ifdef CONFIG_EXT2_FS_XATTR
case Opt_user_xattr:
set_opt (sbi->s_mount_opt, XATTR_USER);
break;
case Opt_nouser_xattr:
clear_opt (sbi->s_mount_opt, XATTR_USER);
break;
#else
case Opt_user_xattr:
case Opt_nouser_xattr:
printk("EXT2 (no)user_xattr options not supported\n");
break;
#endif
#ifdef CONFIG_EXT2_FS_POSIX_ACL
case Opt_acl:
set_opt(sbi->s_mount_opt, POSIX_ACL);
break;
case Opt_noacl:
clear_opt(sbi->s_mount_opt, POSIX_ACL);
break;
#else
case Opt_acl:
case Opt_noacl:
printk("EXT2 (no)acl options not supported\n");
break;
#endif
case Opt_xip:
#ifdef CONFIG_EXT2_FS_XIP
set_opt (sbi->s_mount_opt, XIP);
#else
printk("EXT2 xip option not supported\n");
#endif
break;
#if defined(CONFIG_QUOTA)
case Opt_quota:
case Opt_usrquota:
set_opt(sbi->s_mount_opt, USRQUOTA);
break;
case Opt_grpquota:
set_opt(sbi->s_mount_opt, GRPQUOTA);
break;
#else
case Opt_quota:
case Opt_usrquota:
case Opt_grpquota:
printk(KERN_ERR
"EXT2-fs: quota operations not supported.\n");
break;
#endif
case Opt_reservation:
set_opt(sbi->s_mount_opt, RESERVATION);
printk("reservations ON\n");
break;
case Opt_noreservation:
clear_opt(sbi->s_mount_opt, RESERVATION);
printk("reservations OFF\n");
break;
case Opt_ignore:
break;
default:
return 0;
}
}
return 1;
}
/*ext2的初始化超級塊,read_only標誌檔案系統是不是隻讀掛載*/
static int ext2_setup_super (struct super_block * sb,
struct ext2_super_block * es,
int read_only)
{
int res = 0;
struct ext2_sb_info *sbi = EXT2_SB(sb);
/*檢查修改級別的引數是不是合法*/
if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
printk ("EXT2-fs warning: revision level too high, "
"forcing read-only mode\n");
res = MS_RDONLY;
}
/*只讀掛載就不用繼續了*/
if (read_only)
return res;
/*合法位沒有置位,說明檔案系統沒有經過檢查,應當檢查一下*/
if (!(sbi->s_mount_state & EXT2_VALID_FS))
printk ("EXT2-fs warning: mounting unchecked fs, "
"running e2fsck is recommended\n");
/*如果掛在的時候有錯誤,列印資訊*/
else if ((sbi->s_mount_state & EXT2_ERROR_FS))
printk ("EXT2-fs warning: mounting fs with errors, "
"running e2fsck is recommended\n");
/*檔案系統最大掛載次數大於等於0,並且當前已經掛載超過最大次數了,列印警告資訊*/
else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
le16_to_cpu(es->s_mnt_count) >=
(unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
printk ("EXT2-fs warning: maximal mount count reached, "
"running e2fsck is recommended\n");
/*檢查是不是很長時間沒有進行檢查了,如果是,列印資訊*/
else if (le32_to_cpu(es->s_checkinterval) &&
(le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
printk ("EXT2-fs warning: checktime reached, "
"running e2fsck is recommended\n");
/*如果檔案系統的最大掛載次數字段為0,設定這個變數為預設的EXT2_DFL_MAX_MNT_COUNT*/
if (!le16_to_cpu(es->s_max_mnt_count))
es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
/*當前掛載的次數加一*/
es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
ext2_write_super(sb);/*把這個超級塊寫入到裝置上*/
if (test_opt (sb, DEBUG))/*除錯資訊的列印*/
printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
"bpg=%lu, ipg=%lu, mo=%04lx]\n",
EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
sbi->s_frag_size,
sbi->s_groups_count,
EXT2_BLOCKS_PER_GROUP(sb),
EXT2_INODES_PER_GROUP(sb),
sbi->s_mount_opt);
return res;
}
/*ext2檔案系統檢查組描述符*/
static int ext2_check_descriptors (struct super_block * sb)
{
int i;
int desc_block = 0;//目前是第幾塊組描述符
struct ext2_sb_info *sbi = EXT2_SB(sb);
/*第一個資料塊塊號*/
unsigned long first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
unsigned long last_block;
struct ext2_group_desc * gdp = NULL;//組描述符的緩衝塊
ext2_debug ("Checking group descriptors");
/*遍歷每一個組*/
for (i = 0; i < sbi->s_groups_count; i++)
{ /*判斷當前遍歷是不是到了最後一個組*/
if (i == sbi->s_groups_count - 1)
/*如果到了最後一個組,last_block指向最後一個塊,因為最後一組可能由於邊界的原因不是和之前的組有一樣的塊數目*/
last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
else
/*last_block是本組最後一個塊的塊號*/
last_block = first_block +
(EXT2_BLOCKS_PER_GROUP(sb) - 1);
/*組描述符是放在連續的塊上的,一個塊有若干個描述符,如果當前組過了一個塊,就使得gdp新的描述符塊緩衝區,因為每一個塊是對應一個緩衝區*/
if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
/*判斷組描述符的塊點陣圖編號是不是合法(大於本組內第一個資料塊,小於本組內最後一個塊)*/
if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
le32_to_cpu(gdp->bg_block_bitmap) > last_block)
{
ext2_error (sb, "ext2_check_descriptors",
"Block bitmap for group %d"
" not in group (block %lu)!",
i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
return 0;
}
/*inode點陣圖的塊編號是不是合法*/
if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
{
ext2_error (sb, "ext2_check_descriptors",
"Inode bitmap for group %d"
" not in group (block %lu)!",
i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
return 0;
}
/*判斷inodetable的塊編號是不是合法*/
if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >//s_itb_per_group為inode table佔的塊數
last_block)
{
ext2_error (sb, "ext2_check_descriptors",
"Inode table for group %d"
" not in group (block %lu)!",
i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
return 0;
}
/*如果都合法,說明這個組描述符是正確的,我們去找下一個組,first_block指向下一個組的第一個塊的塊號*/
first_block += EXT2_BLOCKS_PER_GROUP(sb);
/*組描述符指標指向下一個*/
gdp++;
}
return 1;
}
/*
* Maximal file size. There is a direct, and {,double-,triple-}indirect
* block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
* We need to be 1 filesystem block less than the 2^32 sector limit.
*//* 返回ext2檔案系統的最大檔案大小,因為ext2支援三重間接連線塊 */
static loff_t ext2_max_size(int bits)
{ /*res先賦值為直接塊的數目*/
loff_t res = EXT2_NDIR_BLOCKS;
/* This constant is calculated to be the largest file size for a
* dense, 4k-blocksize file such that the total number of
* sectors in the file, including data and all indirect blocks,
* does not exceed 2^32. */
const loff_t upper_limit = 0x1ff7fffd000LL;/*檔案大小上限,不超過32位大小限制*/
/*一級間接塊大小*/
res += 1LL << (bits-2);
res += 1LL << (2*(bits-2)); /*二級間接塊大小*/
res += 1LL << (3*(bits-2));/*三級間接塊大小*/
res <<= bits;
if (res > upper_limit)/*超過最大限制,修改下*/
res = upper_limit;
return res;
}
//返回第nr個組描述符的快號
static unsigned long descriptor_loc(struct super_block *sb,
unsigned long logic_sb_block,
int nr)
{
struct ext2_sb_info *sbi = EXT2_SB(sb);
unsigned long bg, first_data_block, first_meta_bg;
int has_super = 0;
/*檔案系統的第一個資料塊的塊號*/
first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);/*第一個處理塊組*/
if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
nr < first_meta_bg)
return (logic_sb_block + nr + 1);
bg = sbi->s_desc_per_block * nr;
if (ext2_bg_has_super(sb, bg))/*如果這個塊組有超級塊*/
has_super = 1;
return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));//first_meta_bg塊全是儲存的描述符
}
/*ext2的填充超級塊的函式*/
static int ext2_fill_super(struct super_block *sb, void *data, int silent)
{
struct buffer_head * bh;
struct ext2_sb_info * sbi;
struct ext2_super_block * es;
struct inode *root;
unsigned long block;
/*之前講過的函式,從data的字串引數中獲得超級塊的塊號*/
unsigned long sb_block = get_sb_block(&data);
unsigned long logic_sb_block;
unsigned long offset = 0;
unsigned long def_mount_opts;
int blocksize = BLOCK_SIZE;
int db_count;
int i, j;
__le32 features;
int err;
/*分配超級塊在記憶體的資訊結構體ext2_sb_info*/
sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
if (!sbi)
return -ENOMEM;
sb->s_fs_info = sbi;
sbi->s_sb_block = sb_block;
/*
* See what the current blocksize for the device is, and
* use that as the blocksize. Otherwise (or if the blocksize
* is smaller than the default) use the default.
* This is important for devices that have a hardware
* sectorsize that is larger than the default.
*//* 看一下當前的裝置的塊大小,使用作為檔案系統的塊大小 */
blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
if (!blocksize) {
printk ("EXT2-fs: unable to set blocksize\n");
goto failed_sbi;
}
/*
* If the superblock doesn't start on a hardware sector boundary,
* calculate the offset.
*//* 如果塊大小不一致,那麼剛才獲得的超級塊也不對了 */
if (blocksize != BLOCK_SIZE) {
logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
offset = (sb_block*BLOCK_SIZE) % blocksize;
} else {
logic_sb_block = sb_block;
}
/*得到了超級塊的塊號,就可以讀取超級塊了*/
if (!(bh = sb_bread(sb, logic_sb_block))) {
printk ("EXT2-fs: unable to read superblock\n");
goto failed_sbi;
}
/*
* Note: s_es must be initialized as soon as possible because
* some ext2 macro-instructions depend on its value
*//* 使用ext2_super_block指標es指向緩衝區中ext2的超級塊 */
es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
sbi->s_es = es;//指向緩衝區中超級快的指標
/*根據讀取到的超級塊資訊填充vfs層的檔案系統超級塊結構體*/
sb->s_magic = le16_to_cpu(es->s_magic);
/*魔數如果不是ext2的,說明不是ext2檔案系統,直接退出吧*/
if (sb->s_magic != EXT2_SUPER_MAGIC)
goto cantfind_ext2;
/* 設定sbi結構體,在我們轉化掛載選項的之前,先設定為預設值*/
/* Set defaults before we parse the mount options */
def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
/*除錯選項,使用者id和組id,*/
if (def_mount_opts & EXT2_DEFM_DEBUG)
set_opt(sbi->s_mount_opt, DEBUG);
if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
set_opt(sbi->s_mount_opt, GRPID);
if (def_mount_opts & EXT2_DEFM_UID16)
set_opt(sbi->s_mount_opt, NO_UID32);
/*屬性選項*/
#ifdef CONFIG_EXT2_FS_XATTR
if (def_mount_opts & EXT2_DEFM_XATTR_USER)
set_opt(sbi->s_mount_opt, XATTR_USER);
#endif
/*是不是支援acl*/
#ifdef CONFIG_EXT2_FS_POSIX_ACL
if (def_mount_opts & EXT2_DEFM_ACL)
set_opt(sbi->s_mount_opt, POSIX_ACL);
#endif
/*記錄發生的錯誤*/
if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
set_opt(sbi->s_mount_opt, ERRORS_PANIC);
else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
set_opt(sbi->s_mount_opt, ERRORS_RO);
else
set_opt(sbi->s_mount_opt, ERRORS_CONT);
/*保留uid和gid*/
sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
set_opt(sbi->s_mount_opt, RESERVATION);
/*轉化選項並設定*/
if (!parse_options ((char *) data, sbi))
goto failed_mount;
/*acl轉化*/
sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
MS_POSIXACL : 0);
/*看塊裝置是不是支援片內操作*/
ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
EXT2_MOUNT_XIP if not */
/*修改級別和feature是不是適合*/
if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
(EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
printk("EXT2-fs warning: feature flags set on rev 0 fs, "
"running e2fsck is recommended\n");
/*
* Check feature flags regardless of the revision level, since we
* previously didn't change the revision level when setting the flags,
* so there is a chance incompat flags are set on a rev 0 filesystem.
*//*選項檢查*/
features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
if (features) {
printk("EXT2-fs: %s: couldn't mount because of "
"unsupported optional features (%x).\n",
sb->s_id, le32_to_cpu(features));
goto failed_mount;
}
/*如果檔案系統不是隻讀掛載,且feature欄位不支援,返回掛載失敗*/
if (!(sb->s_flags & MS_RDONLY) &&
(features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
printk("EXT2-fs: %s: couldn't mount RDWR because of "
"unsupported optional features (%x).\n",
sb->s_id, le32_to_cpu(features));
goto failed_mount;
}
/*取到ext2_super_block中儲存的blocksize */
blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
/*片內執行的裝置的話,實際的塊大小和我們猜想的不一致,就不支援*/
if ((ext2_use_xip(sb)) && ((blocksize != PAGE_SIZE) ||
(sb->s_blocksize != blocksize))) {
if (!silent)
printk("XIP: Unsupported blocksize\n");
goto failed_mount;
}
/* 如果讀取到的真實的塊大小不匹配的話 */
/* If the blocksize doesn't match, re-read the thing.. */
if (sb->s_blocksize != blocksize) {
brelse(bh);
/*先設定塊大小,如果失敗了,說明設定的快大小太小*/
if (!sb_set_blocksize(sb, blocksize)) {
printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
goto failed_sbi;
}
/*根據新的塊大小重新計算超級塊號和偏移*/
logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
offset = (sb_block*BLOCK_SIZE) % blocksize;
bh = sb_bread(sb, logic_sb_block);
if(!bh) {
printk("EXT2-fs: Couldn't read superblock on "
"2nd try.\n");
goto failed_sbi;
}
es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
sbi->s_es = es;
if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
printk ("EXT2-fs: Magic mismatch, very weird !\n");
goto failed_mount;
}
}
/*繼續用ext2_super_block中的內容來填充ext2_sb_info*/
/*檔案的最大位元組數*/
sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
/*根據修改級別的不同,inode的大小和開始編號不一樣*/
if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
} else {
sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
!is_power_of_2(sbi->s_inode_size) ||
(sbi->s_inode_size > blocksize)) {
printk ("EXT2-fs: unsupported inode size: %d\n",
sbi->s_inode_size);
goto failed_mount;
}
}
/*碎片的資訊填充*/
sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
le32_to_cpu(es->s_log_frag_size);
if (sbi->s_frag_size == 0)
goto cantfind_ext2;
sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
/*組的資料塊數目,碎片數目,inode數目*/
sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
if (EXT2_INODE_SIZE(sb) == 0)
goto cantfind_ext2;
/*每塊的inode數*/
sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
goto cantfind_ext2;
/*每組的inodetable塊數目*/
sbi->s_itb_per_group = sbi->s_inodes_per_group /
sbi->s_inodes_per_block;
/*每塊的描述符數目*/
sbi->s_desc_per_block = sb->s_blocksize /
sizeof (struct ext2_group_desc);
sbi->s_sbh = bh;
sbi->s_mount_state = le16_to_cpu(es->s_state);
sbi->s_addr_per_block_bits =
ilog2 (EXT2_ADDR_PER_BLOCK(sb));
sbi->s_desc_per_block_bits =
ilog2 (EXT2_DESC_PER_BLOCK(sb));
if (sb->s_magic != EXT2_SUPER_MAGIC)
goto cantfind_ext2;
/*裝置的塊大小是不是支援*/
if (sb->s_blocksize != bh->b_size) {
if (!silent)
printk ("VFS: Unsupported blocksize on dev "
"%s.\n", sb->s_id);
goto failed_mount;
}
/*碎片大小是不是不等於塊大小*/
if (sb->s_blocksize != sbi->s_frag_size) {
printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
sbi->s_frag_size, sb->s_blocksize);
goto failed_mount;
}
/*每組的塊數目必須大於8*/
if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
printk ("EXT2-fs: #blocks per group too big: %lu\n",
sbi->s_blocks_per_group);
goto failed_mount;
}/*每組的碎片必須多於8塊*/
if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
printk ("EXT2-fs: #fragments per group too big: %lu\n",
sbi->s_frags_per_group);
goto failed_mount;
}/*inode必須多於8塊*/
if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
printk ("EXT2-fs: #inodes per group too big: %lu\n",
sbi->s_inodes_per_group);
goto failed_mount;
}
if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
goto cantfind_ext2;
/*記錄組的數目*/
sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
le32_to_cpu(es->s_first_data_block) - 1)
/ EXT2_BLOCKS_PER_GROUP(sb)) + 1;
db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
EXT2_DESC_PER_BLOCK(sb);
/*為組描述符分配空間*/
sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
if (sbi->s_group_desc == NULL) {
printk ("EXT2-fs: not enough memory\n");
goto failed_mount;
}
bgl_lock_init(&sbi->s_blockgroup_lock);
/*檔案系統的負載分配空間*/
sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
if (!sbi->s_debts) {
printk ("EXT2-fs: not enough memory\n");
goto failed_mount_group_desc;
}
/*初始化每一組的負載的值,並讀入組描述符*/
for (i = 0; i < db_count; i++) {
block = descriptor_loc(sb, logic_sb_block, i);//定位第i個組描述符的塊號
sbi->s_group_desc[i] = sb_bread(sb, block);//讀入快
if (!sbi->s_group_desc[i]) {//如果讀入失敗
for (j = 0; j < i; j++)
brelse (sbi->s_group_desc[j]);
printk ("EXT2-fs: unable to read group descriptors\n");
goto failed_mount_group_desc;
}
}
if (!ext2_check_descriptors (sb)) {/*ext2檔案系統檢查組描述符一致性*/
printk ("EXT2-fs: group descriptors corrupted!\n");
goto failed_mount2;
}
sbi->s_gdb_count = db_count;
get_random_bytes(&sbi->s_next_generation, sizeof(u32));
spin_lock_init(&sbi->s_next_gen_lock);
/* per fileystem reservation list head & lock */
spin_lock_init(&sbi->s_rsv_window_lock);
sbi->s_rsv_window_root = RB_ROOT;
/*
* Add a single, static dummy reservation to the start of the
* reservation window list --- it gives us a placeholder for
* append-at-start-of-list which makes the allocation logic
* _much_ simpler.
*/
sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
sbi->s_rsv_window_head.rsv_alloc_hit = 0;
sbi->s_rsv_window_head.rsv_goal_size = 0;
ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
/*初始化percpu相關的幾個變數*/
err = percpu_counter_init(&sbi->s_freeblocks_counter,
ext2_count_free_blocks(sb));
if (!err) {
err = percpu_counter_init(&sbi->s_freeinodes_counter,
ext2_count_free_inodes(sb));
}
if (!err) {
err = percpu_counter_init(&sbi->s_dirs_counter,
ext2_count_dirs(sb));
}
if (err) {
printk(KERN_ERR "EXT2-fs: insufficient memory\n");
goto failed_mount3;
}
/*/*初始化ok
* set up enough so that it can read an inode
*/
sb->s_op = &ext2_sops;
sb->s_export_op = &ext2_export_ops;
sb->s_xattr = ext2_xattr_handlers;/*開始讀入根節點的inode*/
root = iget(sb, EXT2_ROOT_INO);
sb->s_root = d_alloc_root(root);
/*檢查讀入的inode是不是有問題*/
if (!sb->s_root) {
iput(root);
printk(KERN_ERR "EXT2-fs: get root inode failed\n");
goto failed_mount3;
}
if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
dput(sb->s_root);
sb->s_root = NULL;
printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
goto failed_mount3;
}
if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
ext2_warning(sb, __FUNCTION__,
"mounting ext3 filesystem as ext2");
/*檢查超級塊,檢查完寫入裝置(裝載操作會修改超級快的某些值,裝載計數,上一次裝載日期)*/
ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
return 0;
cantfind_ext2:
if (!silent)
printk("VFS: Can't find an ext2 filesystem on dev %s.\n",
sb->s_id);
goto failed_mount;
failed_mount3:
percpu_counter_destroy(&sbi->s_freeblocks_counter);
percpu_counter_destroy(&sbi->s_freeinodes_counter);
percpu_counter_destroy(&sbi->s_dirs_counter);
failed_mount2:
for (i = 0; i < db_count; i++)
brelse(sbi->s_group_desc[i]);
failed_mount_group_desc:
kfree(sbi->s_group_desc);
kfree(sbi->s_debts);
failed_mount:
brelse(bh);
failed_sbi:
sb->s_fs_info = NULL;
kfree(sbi);
return -EINVAL;
}
/*提交超級塊的修改*/
static void ext2_commit_super (struct super_block * sb,
struct ext2_super_block * es)
{/*記錄修改時間,標記超級塊髒*/
es->s_wtime = cpu_to_le32(get_seconds());
mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
sb->s_dirt = 0;
}
/*同步超級塊和硬碟上的*/
static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
{ /*更新空閒塊和inode的資訊,以及修改時間*/
es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
es->s_wtime = cpu_to_le32(get_seconds());
/*標記髒,然後同步*/
mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
/*同步ok,標記不髒*/
sb->s_dirt = 0;
}
/*
* In the second extended file system, it is not necessary to
* write the super block since we use a mapping of the
* disk super block in a buffer.
*
* However, this function is still used to set the fs valid
* flags to 0. We need to set this flag to 0 since the fs
* may have been checked while mounted and e2fsck may have
* set s_state to EXT2_VALID_FS after some corrections.
*/
/*ext2寫入超級塊結構體到硬碟上 */
void ext2_write_super (struct super_block * sb)
{
struct ext2_super_block * es;
lock_kernel();
/*只讀掛載的是不可以寫的*/
if (!(sb->s_flags & MS_RDONLY)) {
es = EXT2_SB(sb)->s_es;/*得到ext2_super_block結構體*/
/*如果狀態是合法的檔案系統的話*/
if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
ext2_debug ("setting valid to 0\n");
/*記錄一些資訊*/
es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
~EXT2_VALID_FS);
es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
es->s_mtime = cpu_to_le32(get_seconds());
/*然後同步*/
ext2_sync_super(sb, es);
} else
/*不可寫,就不進行實際上的寫操作*/
ext2_commit_super (sb, es);
}
sb->s_dirt = 0;
unlock_kernel();
}
/*ext2的重新掛載操作*/
static int ext2_remount (struct super_block * sb, int * flags, char * data)
{
struct ext2_sb_info * sbi = EXT2_SB(sb);
struct ext2_super_block * es;
unsigned long old_mount_opt = sbi->s_mount_opt;
struct ext2_mount_options old_opts;
unsigned long old_sb_flags;
int err;
/* 儲存原先掛載的選項 */
/* Store the old options */
old_sb_flags = sb->s_flags;
old_opts.s_mount_opt = sbi->s_mount_opt;
old_opts.s_resuid = sbi->s_resuid;
old_opts.s_resgid = sbi->s_resgid;
/*
* Allow the "check" option to be passed as a remount option.
*//* 轉化選項 ,將data中相應選項儲存入sbi*/
if (!parse_options (data, sbi)) {
err = -EINVAL;
goto restore_opts;
}
/*原來的acl的flag轉化為sb指標結構體上的flag*/
sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
/*看看塊裝置是不是支援xip,如果不支援,EXT2_MOUNT_XIP位就取消掉*/
ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
EXT2_MOUNT_XIP if not */
/*如果使用了xip塊大小就必須等於頁大小*/
if ((ext2_use_xip(sb)) && (sb->s_blocksize != PAGE_SIZE)) {
printk("XIP: Unsupported blocksize\n");
err = -EINVAL;
goto restore_opts;
}
es = sbi->s_es;
if (((sbi->s_mount_opt & EXT2_MOUNT_XIP) !=
(old_mount_opt & EXT2_MOUNT_XIP)) &&
invalidate_inodes(sb))
ext2_warning(sb, __FUNCTION__, "busy inodes while remounting "\
"xip remain in cache (no functional problem)");
/*都是隻讀掛載的時候,就可以結束了*/
if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
return 0;
/*新的flag是隻讀的*/
if (*flags & MS_RDONLY) {
if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
!(sbi->s_mount_state & EXT2_VALID_FS))
return 0;
/*
* OK, we are remounting a valid rw partition rdonly, so set
* the rdonly flag and then mark the partition as valid again.
*//* 把原來的讀寫掛載變成只讀掛載 */
es->s_state = cpu_to_le16(sbi->s_mount_state);
es->s_mtime = cpu_to_le32(get_seconds());
} else {
__le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
~EXT2_FEATURE_RO_COMPAT_SUPP);
if (ret) {
printk("EXT2-fs: %s: couldn't remount RDWR because of "
"unsupported optional features (%x).\n",
sb->s_id, le32_to_cpu(ret));
err = -EROFS;
goto restore_opts;
}
/*
* Mounting a RDONLY partition read-write, so reread and
* store the current valid flag. (It may have been changed
* by e2fsck since we originally mounted the partition.)
*//* 把一個只讀掛載變成讀寫掛載 */
sbi->s_mount_state = le16_to_cpu(es->s_state);
if (!ext2_setup_super (sb, es, 0))
sb->s_flags &= ~MS_RDONLY;
}
ext2_sync_super(sb, es);
return 0;
restore_opts:
sbi->s_mount_opt = old_opts.s_mount_opt;
sbi->s_resuid = old_opts.s_resuid;
sbi->s_resgid = old_opts.s_resgid;
sb->s_flags = old_sb_flags;
return err;
}
/*把ext2的狀態存到kstatfs指標裡*/
static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
{
struct super_block *sb = dentry->d_sb;//目錄物件所在的超級塊
struct ext2_sb_info *sbi = EXT2_SB(sb);//超級塊的記憶體資訊
struct ext2_super_block *es = sbi->s_es;//超級塊的磁碟資訊
u64 fsid;
/*先計算第一個資料塊前邊的塊數目,如果有巨集,就是0*/
if (test_opt (sb, MINIX_DF))
sbi->s_overhead_last = 0;//上次計算管理資料的開銷
else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {//上次計算的快數!=磁碟上塊數
unsigned long i, overhead = 0;
smp_rmb();
/*
* Compute the overhead (FS structures). This is constant
* for a given filesystem unless the number of block groups
* changes so we cache the previous value until it does.計算開銷(FS結構)。
除非塊組的數量發生更改,否則這對於給定的檔案系統而言是恆定的,因此我們將快取先前的值,
直到它改變為止。
*/
/*
* All of the blocks before first_data_block are
* overhead
*/
overhead = le32_to_cpu(es->s_first_data_block);
/*
* Add the overhead attributed to the superblock and
* block group descriptors. If the sparse superblocks
* feature is turned on, then not all groups have this.
*//* 增加前邊的每組的超級塊和描述符 為overhead*/
for (i = 0; i < sbi->s_groups_count; i++)
overhead += ext2_bg_has_super(sb, i) +
ext2_bg_num_gdb(sb, i);
/*
* Every block group has an inode bitmap, a block
* bitmap, and an inode table.
*//* inode點陣圖,塊點陣圖,inodetable */
overhead += (sbi->s_groups_count *
(2 + sbi->s_itb_per_group));
sbi->s_overhead_last = overhead;
smp_wmb();
sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
}
/*填充buf,檔案系統魔數,塊大小,塊數,空閒資料塊數目*/
buf->f_type = EXT2_SUPER_MAGIC;
buf->f_bsize = sb->s_blocksize;
buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
buf->f_bfree = ext2_count_free_blocks(sb);
es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);/*除去保留塊的空閒塊數*/
if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
buf->f_bavail = 0;
buf->f_files = le32_to_cpu(es->s_inodes_count);/*inode數目*/
buf->f_ffree = ext2_count_free_inodes(sb);/*空閒inode*/
es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
buf->f_namelen = EXT2_NAME_LEN;/*最大名稱長度*/
fsid = le64_to_cpup((void *)es->s_uuid) ^/*fsuid*/
le64_to_cpup((void *)es->s_uuid + sizeof(u64));
buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
return 0;
}
/*得到裝置的超級塊*/
static int ext2_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super, mnt);
}
/*配額的函式*/
#ifdef CONFIG_QUOTA
/* Read data from quotafile - avoid pagecache and such because we cannot afford
* acquiring the locks... As quota files are never truncated and quota code
* itself serializes the operations (and noone else should touch the files)
* we don't have to be afraid of races *//* 從配額檔案裡讀取資料 */
static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
size_t len, loff_t off)
{ /*得到硬碟上的配額檔案的inode*/
struct inode *inode = sb_dqopt(sb)->files[type];
sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
int err = 0;
int offset = off & (sb->s_blocksize - 1);
int tocopy;
size_t toread;
struct buffer_head tmp_bh;
struct buffer_head *bh;
/*得到檔案的大小*/
loff_t i_size = i_size_read(inode);
/*偏移超過檔案大小,出錯了*/
if (off > i_size)
return 0;
/*讀的長度超出檔案尾部,也不可能*/
if (off+len > i_size)
len = i_size-off;
toread = len;
while (toread > 0) {
/*將要讀的長度*/
tocopy = sb->s_blocksize - offset < toread ?
sb->s_blocksize - offset : toread;
tmp_bh.b_state = 0;
/*讀取檔案內容*/
err = ext2_get_block(inode, blk, &tmp_bh, 0);
if (err < 0)
return err;
/*如果沒有對映,賦值為0*/
if (!buffer_mapped(&tmp_bh)) /* A hole? */
memset(data, 0, tocopy);
else {
bh = sb_bread(sb, tmp_bh.b_blocknr);
if (!bh)
return -EIO;
memcpy(data, bh->b_data+offset, tocopy);
brelse(bh);
}
offset = 0;
toread -= tocopy;
data += tocopy;
blk++;
}
return len;
}
/* Write to quotafile *//* 寫資料到配額檔案 */
static ssize_t ext2_quota_write(struct super_block *sb, int type,
const char *data, size_t len, loff_t off)
{
struct inode *inode = sb_dqopt(sb)->files[type];
sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
int err = 0;
int offset = off & (sb->s_blocksize - 1);
int tocopy;
size_t towrite = len;
struct buffer_head tmp_bh;
struct buffer_head *bh;
mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
while (towrite > 0) {/*迴圈寫入*/
tocopy = sb->s_blocksize - offset < towrite ?/*先寫的資料長度*/
sb->s_blocksize - offset : towrite;
tmp_bh.b_state = 0;/*得到要寫入的檔案*/
err = ext2_get_block(inode, blk, &tmp_bh, 1);
if (err < 0)
goto out;
if (offset || tocopy != EXT2_BLOCK_SIZE(sb))/*得到緩衝區*/
bh = sb_bread(sb, tmp_bh.b_blocknr);
else
bh = sb_getblk(sb, tmp_bh.b_blocknr);
if (!bh) {
err = -EIO;
goto out;
}
lock_buffer(bh);
memcpy(bh->b_data+offset, data, tocopy);/*複製資料到檔案緩衝區*/
flush_dcache_page(bh->b_page);/*清空緩衝區,寫入到裝置上*/
set_buffer_uptodate(bh);
mark_buffer_dirty(bh);
unlock_buffer(bh);
brelse(bh);
offset = 0;/*更新這些變數*/
towrite -= tocopy;
data += tocopy;
blk++;
}
out:
if (len == towrite)
return err;
if (inode->i_size < off+len-towrite)
i_size_write(inode, off+len-towrite);
inode->i_version++;
inode->i_mtime = inode->i_ctime = CURRENT_TIME;
mark_inode_dirty(inode);
mutex_unlock(&inode->i_mutex);
return len - towrite;
}
#endif
/*ext2檔案系統型別結構體*/
static struct file_system_type ext2_fs_type = {
.owner = THIS_MODULE,
.name = "ext2",
.get_sb = ext2_get_sb,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};
/*ext2的模組初始化*/
static int __init init_ext2_fs(void)
{/*初始化屬性*/
int err = init_ext2_xattr();
if (err)
return err;
err = init_inodecache();/*初始化inode快取*/
if (err)
goto out1;
err = register_filesystem(&ext2_fs_type);/*登記ext2*/
if (err)
goto out;
return 0;
out:
destroy_inodecache();
out1:
exit_ext2_xattr();
return err;
}
/*ext2的模組清理*/
static void __exit exit_ext2_fs(void)
{ /*登出ext2*/
unregister_filesystem(&ext2_fs_type);
destroy_inodecache();
exit_ext2_xattr();
}
module_init(init_ext2_fs)
module_exit(exit_ext2_fs)
相關文章
- Linux EXT2 檔案系統Linux
- ext2 檔案系統解析
- 鴻蒙輕核心原始碼分析:檔案系統LittleFS鴻蒙原始碼
- Linux中檔案系統ext2和ext3有什麼區別?Linux
- Linux檔案系統與日誌分析Linux
- linux檔案系統和日誌分析Linux
- PHP檔案分享系統原始碼PHP原始碼
- Linux--檔案系統與日誌分析Linux
- Linux檔案系統Linux
- [Linux]檔案系統Linux
- Hadoop3.2.1 【 HDFS 】原始碼分析 : 檔案系統資料集 [一]Hadoop原始碼
- Abp原始碼分析之虛擬檔案系統Volo.Abp.VirtualFileSystem原始碼
- Linux系統篇-檔案系統&虛擬檔案系統Linux
- 線上直播系統原始碼,前後端大檔案上傳程式碼分析原始碼後端
- Linux系統檔案系統及檔案基礎篇Linux
- 論Linux檔案系統Linux
- Linux AUFS 檔案系統Linux
- linux的檔案系統Linux
- 【linux】系統呼叫版串列埠分析&原始碼實戰Linux串列埠原始碼
- 分散式檔案系統(HDFS)與 linux系統檔案系統 對比分散式Linux
- vue原始碼分析系列之入口檔案分析Vue原始碼
- 『學了就忘』Linux檔案系統管理 — 57、Linux檔案系統介紹Linux
- 檔案系統(十一):Linux Squashfs只讀檔案系統介紹Linux
- linux之路(五)檔案系統Linux
- Linux檔案系統、目錄Linux
- Linux 檔案系統詳解Linux
- linux 檔案系統擴容Linux
- Linux檔案系統詳解Linux
- 【核心檔案系統】原始碼閱讀stat.h原始碼
- Linux無系統核心原始碼Linux原始碼
- Springboot 載入配置檔案原始碼分析Spring Boot原始碼
- Android 系統原始碼-1:Android 系統啟動流程原始碼分析Android原始碼
- linux原始碼分析Linux原始碼
- 導覽Linux系統檔案系統型別Linux型別
- Linux系統程式設計【4】——檔案系統Linux程式設計
- Linux 檔案系統與日誌分析的相關知識Linux
- Linux系統網路檔案配置Linux
- 12_Linux檔案系統概念Linux