可可熊的窝

Tag Archives: Ext2

Ext2文件系统源代码分析(3)

IN:C, 编程相关   Tags:    Comments:0

symlink.c:

/*
 *  linux/fs/ext2/symlink.c
 *
 * Only fast symlinks left here - the rest is done by generic code. AV, 1999
 *
 * 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/symlink.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  ext2 symlink handling code
 */

#include "ext2.h"
#include "xattr.h"
#include <linux /namei.h>

static void *ext2_follow_link(struct dentry *dentry, struct nameidata *nd)
{
        struct ext2_inode_info *ei = EXT2_I(dentry->d_inode);
        nd_set_link(nd, (char *)ei->i_data);
        return NULL;
}
//ext2_follow_link()函数用于搜索符号连接所在的目标文件

const struct inode_operations ext2_symlink_inode_operations = {
        .readlink       = generic_readlink,
        .follow_link    = page_follow_link_light,
        .put_link       = page_put_link,
#ifdef CONFIG_EXT2_FS_XATTR
        .setxattr       = generic_setxattr,
        .getxattr       = generic_getxattr,
        .listxattr      = ext2_listxattr,
        .removexattr    = generic_removexattr,
#endif
};
//ext2_symlink_inode_operations定义了上层(VFS)符号连接操作在ext2文件系统这一层中的具体实现函数

const struct inode_operations ext2_fast_symlink_inode_operations = {
        .readlink       = generic_readlink,
        .follow_link    = ext2_follow_link,
#ifdef CONFIG_EXT2_FS_XATTR
        .setxattr       = generic_setxattr,
        .getxattr       = generic_getxattr,
        .listxattr      = ext2_listxattr,
        .removexattr    = generic_removexattr,
#endif
};
//ext2_fast_symlink_inode_operations应该是为了向下兼容2.4内核使用的符号连接操作函数

fsync.c:

/*
 *  linux/fs/ext2/fsync.c
 *
 *  Copyright (C) 1993  Stephen Tweedie (sct@dcs.ed.ac.uk)
 *  from
 *  Copyright (C) 1992  Remy Card (card@masi.ibp.fr)
 *                      Laboratoire MASI - Institut Blaise Pascal
 *                      Universite Pierre et Marie Curie (Paris VI)
 *  from
 *  linux/fs/minix/truncate.c   Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  ext2fs fsync primitive
 *
 *  Big-endian to little-endian byte-swapping/bitmaps by
 *        David S. Miller (davem@caip.rutgers.edu), 1995
 *
 *  Removed unnecessary code duplication for little endian machines
 *  and excessive __inline__s.
 *        Andi Kleen, 1997
 *
 * Major simplications and cleanup - we only need to do the metadata, because
 * we can depend on generic_block_fdatasync() to sync the data blocks.
 */

#include "ext2.h"
#include </linux><linux /buffer_head.h>          /* for sync_mapping_buffers() */

/*
 *      File may be NULL when we are called. Perhaps we shouldn't
 *      even pass file to fsync ?
 */

int ext2_sync_file(struct file *file, struct dentry *dentry, int datasync)
{
        struct inode *inode = dentry->d_inode;
        int err;
        int ret;

        ret = sync_mapping_buffers(inode->i_mapping);
        if (!(inode->i_state & I_DIRTY))
                return ret;
        if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
                return ret;

        err = ext2_sync_inode(inode);
        if (ret == 0)
                ret = err;
        return ret;
}
//ext2_sync_file函数对文件进行同步,它只检查inode中的脏数据,通过对ext2_sync_inode调用把数据写入硬盘
//sync_mapping_buffers作用是写出地址空间所有间接的blocks
05-06
2008

Ext2文件系统源代码分析(2)

IN:C, 编程相关   Tags:    Comments:2

xattr.h:

/*
  File: linux/ext2_xattr.h

  On-disk format of extended attributes for the ext2 filesystem.

  (C) 2001 Andreas Gruenbacher, <a .gruenbacher@computer.org>
*/

#include <linux /init.h>
#include </linux><linux /xattr.h>

/* Magic value in attribute blocks */
#define EXT2_XATTR_MAGIC                0xEA020000
//定义属性block中使用的魔数(一个标记,唯一的值)

/* Maximum number of references to one attribute block */
#define EXT2_XATTR_REFCOUNT_MAX         1024
//定义一个属性block

/* Name indexes */
#define EXT2_XATTR_INDEX_USER                   1
#define EXT2_XATTR_INDEX_POSIX_ACL_ACCESS       2
#define EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT      3
#define EXT2_XATTR_INDEX_TRUSTED                4
#define EXT2_XATTR_INDEX_LUSTRE                 5
#define EXT2_XATTR_INDEX_SECURITY               6

struct ext2_xattr_header {
        __le32  h_magic;        /* magic number for identification */
        __le32  h_refcount;     /* reference count */
        __le32  h_blocks;       /* number of disk blocks used */
        __le32  h_hash;         /* hash value of all attributes */
        __u32   h_reserved[4];  /* zero right now */
};
//__le32 h_magic; 标识码,为0xEA020000 

//__le32 h_refcount; 属性块被链接的数目 

//__le32 h_blocks; 用于扩展属性的块数 

//__le32 h_hash;  所有属性的哈希值 

//__u32 h_reserved[4]; 保留变量,目前为0

struct ext2_xattr_entry {
        __u8    e_name_len;     /* length of name */
        __u8    e_name_index;   /* attribute name index */
        __le16  e_value_offs;   /* offset in disk block of value */
        __le32  e_value_block;  /* disk block attribute is stored on (n/i) */
        __le32  e_value_size;   /* size of attribute value */
        __le32  e_hash;         /* hash value of name and value */
        char    e_name[0];      /* attribute name */
};

//__u8 e_name_len; 属性名长度 

//__u8 e_name_index; 属性名索引 

//__le16 e_value_offs; 属性值在值块中的偏移量 

//__le32 e_value_block;  保存属性值的块的块号 

//__le32 e_value_size;  属性值长度 

//__le32 e_hash;  属性名和值的哈希值 

//char e_name[0];  属性名

#define EXT2_XATTR_PAD_BITS             2
#define EXT2_XATTR_PAD          (1< <EXT2_XATTR_PAD_BITS)
#define EXT2_XATTR_ROUND                (EXT2_XATTR_PAD-1)
#define EXT2_XATTR_LEN(name_len) \
        (((name_len) + EXT2_XATTR_ROUND + \
        sizeof(struct ext2_xattr_entry)) & ~EXT2_XATTR_ROUND)
#define EXT2_XATTR_NEXT(entry) \
        ( (struct ext2_xattr_entry *)( \
          (char *)(entry) + EXT2_XATTR_LEN((entry)->e_name_len)) )
#define EXT2_XATTR_SIZE(size) \
        (((size) + EXT2_XATTR_ROUND) & ~EXT2_XATTR_ROUND)

# ifdef CONFIG_EXT2_FS_XATTR
//当内核编译时指定使用xattr扩展属性时,xattr相关函数的申明

extern struct xattr_handler ext2_xattr_user_handler;
extern struct xattr_handler ext2_xattr_trusted_handler;
extern struct xattr_handler ext2_xattr_acl_access_handler;
extern struct xattr_handler ext2_xattr_acl_default_handler;
extern struct xattr_handler ext2_xattr_security_handler;

extern ssize_t ext2_listxattr(struct dentry *, char *, size_t);

extern int ext2_xattr_get(struct inode *, int, const char *, void *, size_t);
extern int ext2_xattr_set(struct inode *, int, const char *, const void *, size_t, int);

extern void ext2_xattr_delete_inode(struct inode *);
extern void ext2_xattr_put_super(struct super_block *);

extern int init_ext2_xattr(void);
extern void exit_ext2_xattr(void);

extern struct xattr_handler *ext2_xattr_handlers[];

# else  /* CONFIG_EXT2_FS_XATTR */

//当内核编译时未使用xattr扩展属性,xattr相关函数被置为空

static inline int
ext2_xattr_get(struct inode *inode, int name_index,
               const char *name, void *buffer, size_t size)
{
        return -EOPNOTSUPP;
}

static inline int
ext2_xattr_set(struct inode *inode, int name_index, const char *name,
               const void *value, size_t size, int flags)
{
        return -EOPNOTSUPP;
}

static inline void
ext2_xattr_delete_inode(struct inode *inode)
{
}

static inline void
ext2_xattr_put_super(struct super_block *sb)
{
}

static inline int
init_ext2_xattr(void)
{
        return 0;
}

static inline void
exit_ext2_xattr(void)
{
}

#define ext2_xattr_handlers NULL

# endif  /* CONFIG_EXT2_FS_XATTR */

#ifdef CONFIG_EXT2_FS_SECURITY
extern int ext2_init_security(struct inode *inode, struct inode *dir);
#else
static inline int ext2_init_security(struct inode *inode, struct inode *dir)
{
        return 0;
}
#endif

xip.h:

/*
 *  linux/fs/ext2/xip.h
 *
 * Copyright (C) 2005 IBM Corporation
 * Author: Carsten Otte (cotte@de.ibm.com)
 */

#ifdef CONFIG_EXT2_FS_XIP
//当内核编译时指定使用xip扩展属性时,xip相关操作的函数申明
extern void ext2_xip_verify_sb (struct super_block *);
extern int ext2_clear_xip_target (struct inode *, int);

static inline int ext2_use_xip (struct super_block *sb)
{
        struct ext2_sb_info *sbi = EXT2_SB(sb);
        return (sbi->s_mount_opt & EXT2_MOUNT_XIP);
}
struct page* ext2_get_xip_page (struct address_space *, sector_t, int);
#define mapping_is_xip(map) unlikely(map->a_ops->get_xip_page)
#else
//当内核编译时未指定使用xattr扩展属性时,置这些函数操作为空

#define mapping_is_xip(map)                     0
#define ext2_xip_verify_sb(sb)                  do { } while (0)
#define ext2_use_xip(sb)                        0
#define ext2_clear_xip_target(inode, chain)     0
#define ext2_get_xip_page                       NULL
#endif

05-05
2008

Ext2文件系统源代码分析(1)

IN:C, 编程相关   Tags:    Comments:0

acl.h:

/*
  File: fs/ext2/acl.h

  (C) 2001 Andreas Gruenbacher, <a .gruenbacher@computer.org>
*/

#include <linux /posix_acl_xattr.h>

//简单说一下acl(访问控制表),比传统的Linux具有更灵活的文件权限设定,ext2文件系统的扩展属性支持acl,这一点在编译的时候可以指定。
#define EXT2_ACL_VERSION        0x0001

//定义acl的版本;
typedef struct {
        __le16          e_tag;
        __le16          e_perm;
        __le32          e_id;
} ext2_acl_entry;
//__le16类型的定义:
//typedef __u16 __bitwise __le16;
//看起来比较复杂,其实(在x86平台下)只是一个unsigned short,__bitwise这个只是通过gcc的扩展使用sparse这个工具来进行代码检查。

//ext2_acl_entry定义访问控制项的结构体
//__le16          e_tag;   自身的使用者或組
//__le16          e_perm;  允许访问的
//__le32          e_id; 

typedef struct {
        __le16          e_tag;
        __le16          e_perm;
} ext2_acl_entry_short;
//定义访问控制项的简单结构;

typedef struct {
        __le32          a_version;
} ext2_acl_header;

//接下来两个函数分别用到了size_t与ssize_t对应在x86平台下分别是unsigned int与int
static inline size_t ext2_acl_size(int count)
{
        if (count < = 4) {
                return sizeof(ext2_acl_header) +
                       count * sizeof(ext2_acl_entry_short);
        } else {
                return sizeof(ext2_acl_header) +
                       4 * sizeof(ext2_acl_entry_short) +
                       (count - 4) * sizeof(ext2_acl_entry);
        }
}

//该函数用来计算acl的占用空间大小,当使用的acl多于4后剩余的acl使用完整的ext2_acl_entry来记录。
static inline int ext2_acl_count(size_t size)
{
        ssize_t s;
        size -= sizeof(ext2_acl_header);
        s = size - 4 * sizeof(ext2_acl_entry_short);
        if (s < 0) {
                if (size % sizeof(ext2_acl_entry_short))
                        return -1;
                return size / sizeof(ext2_acl_entry_short);
        } else {
                if (s % sizeof(ext2_acl_entry))
                        return -1;
                return s / sizeof(ext2_acl_entry) + 4;
        }
}
//该函数与上面的函数刚好相反
#ifdef CONFIG_EXT2_FS_POSIX_ACL

//当内核编译时指定使用ext2的acl属性时这里ext2_permission等函数被申明,否则这些函数被定义为空。
/* Value for inode->u.ext2_i.i_acl and inode->u.ext2_i.i_default_acl
   if the ACL has not been cached */
#define EXT2_ACL_NOT_CACHED ((void *)-1)

/* acl.c */
extern int ext2_permission (struct inode *, int, struct nameidata *);
extern int ext2_acl_chmod (struct inode *);
extern int ext2_init_acl (struct inode *, struct inode *);

#else
#include </linux><linux /sched.h>
#define ext2_permission NULL
#define ext2_get_acl    NULL
#define ext2_set_acl    NULL

static inline int
ext2_acl_chmod (struct inode *inode)
{
        return 0;
}

static inline int ext2_init_acl (struct inode *inode, struct inode *dir)
{
        return 0;
}
#endif

file.c:

/*
 *  linux/fs/ext2/file.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/file.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  ext2 fs regular file handling primitives
 *
 *  64-bit file support on 64-bit platforms by Jakub Jelinek
 *      (jj@sunsite.ms.mff.cuni.cz)
 */

#include </linux><linux /time.h>
#include "ext2.h"
#include "xattr.h"
#include "acl.h"

/*
 * Called when filp is released. This happens when all file descriptors
 * for a single struct file are closed. Note that different open() calls
 * for the same file yield different struct file structures.
 */
static int ext2_release_file (struct inode * inode, struct file * filp)
{
        if (filp->f_mode & FMODE_WRITE) {
                mutex_lock(&EXT2_I(inode)->truncate_mutex);
                ext2_discard_reservation(inode);
                mutex_unlock(&EXT2_I(inode)->truncate_mutex);
        }
        return 0;
}

//该函数释放文件所使用的block,前提是该文件以"写"的模式被打开。
/*
 * We have mostly NULL's here: the current defaults are ok for
 * the ext2 filesystem.
 */
const struct file_operations ext2_file_operations = {
        .llseek         = generic_file_llseek,
        .read           = do_sync_read,
        .write          = do_sync_write,
        .aio_read       = generic_file_aio_read,
        .aio_write      = generic_file_aio_write,
        .ioctl          = ext2_ioctl,
#ifdef CONFIG_COMPAT
        .compat_ioctl   = ext2_compat_ioctl,
#endif
        .mmap           = generic_file_mmap,
        .open           = generic_file_open,
        .release        = ext2_release_file,
        .fsync          = ext2_sync_file,
        .splice_read    = generic_file_splice_read,
        .splice_write   = generic_file_splice_write,
};

//ext2_file_operations这个结构体定义了上层(VFS)文件操作函数在ext2文件系统这一层中的具体实现函数。
//这里又用到了gcc的扩展使用.[index]这样的方式来初始化结构体,这样结构体中的成员就不会受到顺序的限制。
#ifdef CONFIG_EXT2_FS_XIP
const struct file_operations ext2_xip_file_operations = {
        .llseek         = generic_file_llseek,
        .read           = xip_file_read,
        .write          = xip_file_write,
        .ioctl          = ext2_ioctl,
#ifdef CONFIG_COMPAT
        .compat_ioctl   = ext2_compat_ioctl,
#endif
        .mmap           = xip_file_mmap,
        .open           = generic_file_open,
        .release        = ext2_release_file,
        .fsync          = ext2_sync_file,
};
#endif

//ext2_xip_file_operations定义了使用xip(eXecute In Place)的文件操作函数
const struct inode_operations ext2_file_inode_operations = {
        .truncate       = ext2_truncate,
#ifdef CONFIG_EXT2_FS_XATTR
        .setxattr       = generic_setxattr,
        .getxattr       = generic_getxattr,
        .listxattr      = ext2_listxattr,
        .removexattr    = generic_removexattr,
#endif
        .setattr        = ext2_setattr,
        .permission     = ext2_permission,
};
//ext2_file_inode_operations定义了ext2中inode的文件操作函数。

05-05
2008

Ext2文件系统源代码分析(0)

IN:C, 编程相关   Tags:    Comments:0

Makefile:

#
# Makefile for the linux ext2-filesystem routines.
#

obj-$(CONFIG_EXT2_FS) += ext2.o

ext2-y := balloc.o dir.o file.o fsync.o ialloc.o inode.o \
          ioctl.o namei.o super.o symlink.o

ext2-$(CONFIG_EXT2_FS_XATTR)     += xattr.o xattr_user.o xattr_trusted.o
ext2-$(CONFIG_EXT2_FS_POSIX_ACL) += acl.o
ext2-$(CONFIG_EXT2_FS_SECURITY)  += xattr_security.o
ext2-$(CONFIG_EXT2_FS_XIP)       += xip.o

obj-$(CONFIG_EXT2_FS)指定编译生成的目标文件,CONFIG_EXT2_FS变量在配置内核时指定,有三个选项:y,m,x分别代表编译,以模块方式编译,不编译。

ext2-y指定了编译ext2.o时所需要同时编译的其它文件。

下面四个是ext2文件系统的一些扩展属性,也是在编译内核时配置是否使用。

ext2.h:

#include <linux /fs.h>
#include </linux><linux /ext2_fs.h>

/*
 * ext2 mount options
 */
struct ext2_mount_options {
        unsigned long s_mount_opt;
        uid_t s_resuid;
        gid_t s_resgid;
};
//这里定义了一个ext2文件系统挂载时所使用的一些选项,uid_t,gid_t在i386上是unsigned int。
//s_mount_opt:安装选项
//s_resuid:可以使用保留块的用户id
//s_resgid:可以使用保留块的用户组id

/*
 * second extended file system inode data in memory
 */
struct ext2_inode_info {
        __le32  i_data[15];
        __u32   i_flags;
        __u32   i_faddr;
        __u8    i_frag_no;
        __u8    i_frag_size;
        __u16   i_state;
        __u32   i_file_acl;
        __u32   i_dir_acl;
        __u32   i_dtime;

        /*
         * i_block_group is the number of the block group which contains
         * this file's inode.  Constant across the lifetime of the inode,
         * it is ued for making block allocation decisions - we try to
         * place a file's data blocks near its inode block, and new inodes
         * near to their parent directory's inode.
         */
        __u32   i_block_group;

        /* block reservation info */
        struct ext2_block_alloc_info *i_block_alloc_info;

        __u32   i_dir_start_lookup;
#ifdef CONFIG_EXT2_FS_XATTR
        /*
         * Extended attributes can be read independently of the main file
         * data. Taking i_mutex even when reading would cause contention
         * between readers of EAs and writers of regular file data, so
         * instead we synchronize on xattr_sem when reading or changing
         * EAs.
         */
        struct rw_semaphore xattr_sem;
#endif
#ifdef CONFIG_EXT2_FS_POSIX_ACL
        struct posix_acl        *i_acl;
        struct posix_acl        *i_default_acl;
#endif
        rwlock_t i_meta_lock;

        /*
         * truncate_mutex is for serialising ext2_truncate() against
         * ext2_getblock().  It also protects the internals of the inode's
         * reservation data structures: ext2_reserve_window and
         * ext2_reserve_window_node.
         */
        struct mutex truncate_mutex;
        struct inode    vfs_inode;
        struct list_head i_orphan;      /* unlinked but open inodes */
};

//ext2_inode_info这个结构体定义了inode在内存中存放的信息。
//__le32  i_data[15];  __le32是带bitwise属性的unsigned int;数据块指针数组
//__u32   i_flags;  __u32是unsigned int;文件标志
//__u32   i_faddr;  fragment地址
//__u8    i_frag_no;  fragment号
//__u8    i_frag_size;  fragment大小
//__u16   i_state;
//__u32   i_file_acl;  文件访问控制链表
//__u32   i_dir_acl;  目录访问控制链表
//__u32   i_dtime;  文件删除时间
//__u32   i_block_group;  inode所在组号
//struct ext2_block_alloc_info *i_block_alloc_info;  block保留信息

/*
 * Inode dynamic state flags
 */
#define EXT2_STATE_NEW                  0x00000001 /* inode is newly created */

/*
 * Function prototypes
 */

/*
 * Ok, these declarations are also in </linux><linux /kernel.h> but none of the
 * ext2 source programs needs to include it so they are duplicated here.
 */

static inline struct ext2_inode_info *EXT2_I(struct inode *inode)
{
        return container_of(inode, struct ext2_inode_info, vfs_inode);
}
//EXT2_I函数利用到了内核中经常使用的一个宏:container_of,它的参数为inode指针,返回一个ext2_inode_info的指针,也就是inode所在结构体的地址。
//container_of在linux-2.6.24/include/linux/kernel.h里定义为:
//#define container_of(ptr, type, member) ({                      \
       const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
       (type *)( (char *)__mptr - offsetof(type,member) );})

//这个宏的原理其实很简单就是利用结构体中某个元素的地址减去该元素相对于结构体的偏移量,得到结构体的地址。

//offsetof在linux-2.6.24/include/linux/stddef.h中定义为:
//#undef offsetof
//#ifdef __compiler_offsetof
//#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
//#else
//#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
//#endif

//接下来这这些是定义在ext2其它文件中的函数的申明。
/* balloc.c */
extern int ext2_bg_has_super(struct super_block *sb, int group);
extern unsigned long ext2_bg_num_gdb(struct super_block *sb, int group);
extern ext2_fsblk_t ext2_new_block(struct inode *, unsigned long, int *);
extern ext2_fsblk_t ext2_new_blocks(struct inode *, unsigned long,
                                unsigned long *, int *);
extern void ext2_free_blocks (struct inode *, unsigned long,
                              unsigned long);
extern unsigned long ext2_count_free_blocks (struct super_block *);
extern unsigned long ext2_count_dirs (struct super_block *);
extern void ext2_check_blocks_bitmap (struct super_block *);
extern struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
                                                    unsigned int block_group,
                                                    struct buffer_head ** bh);
extern void ext2_discard_reservation (struct inode *);
extern int ext2_should_retry_alloc(struct super_block *sb, int *retries);
extern void ext2_init_block_alloc_info(struct inode *);
extern void ext2_rsv_window_add(struct super_block *sb, struct ext2_reserve_window_node *rsv);

/* dir.c */
extern int ext2_add_link (struct dentry *, struct inode *);
extern ino_t ext2_inode_by_name(struct inode *, struct dentry *);
extern int ext2_make_empty(struct inode *, struct inode *);
extern struct ext2_dir_entry_2 * ext2_find_entry (struct inode *,struct dentry *, struct page **);
extern int ext2_delete_entry (struct ext2_dir_entry_2 *, struct page *);
extern int ext2_empty_dir (struct inode *);
extern struct ext2_dir_entry_2 * ext2_dotdot (struct inode *, struct page **);
extern void ext2_set_link(struct inode *, struct ext2_dir_entry_2 *, struct page *, struct inode *);

/* fsync.c */
extern int ext2_sync_file (struct file *, struct dentry *, int);

/* ialloc.c */
extern struct inode * ext2_new_inode (struct inode *, int);
extern void ext2_free_inode (struct inode *);
extern unsigned long ext2_count_free_inodes (struct super_block *);
extern void ext2_check_inodes_bitmap (struct super_block *);
extern unsigned long ext2_count_free (struct buffer_head *, unsigned);

/* inode.c */
extern void ext2_read_inode (struct inode *);
extern int ext2_write_inode (struct inode *, int);
extern void ext2_put_inode (struct inode *);
extern void ext2_delete_inode (struct inode *);
extern int ext2_sync_inode (struct inode *);
extern int ext2_get_block(struct inode *, sector_t, struct buffer_head *, int);
extern void ext2_truncate (struct inode *);
extern int ext2_setattr (struct dentry *, struct iattr *);
extern void ext2_set_inode_flags(struct inode *inode);
extern void ext2_get_inode_flags(struct ext2_inode_info *);
int __ext2_write_begin(struct file *file, struct address_space *mapping,
                loff_t pos, unsigned len, unsigned flags,
                struct page **pagep, void **fsdata);

/* ioctl.c */
extern int ext2_ioctl (struct inode *, struct file *, unsigned int,
                       unsigned long);
extern long ext2_compat_ioctl(struct file *, unsigned int, unsigned long);

/* namei.c */
struct dentry *ext2_get_parent(struct dentry *child);

/* super.c */
extern void ext2_error (struct super_block *, const char *, const char *, ...)
        __attribute__ ((format (printf, 3, 4)));
extern void ext2_warning (struct super_block *, const char *, const char *, ...)
        __attribute__ ((format (printf, 3, 4)));
extern void ext2_update_dynamic_rev (struct super_block *sb);
extern void ext2_write_super (struct super_block *);

/*
 * Inodes and files operations
 */

/* dir.c */
extern const struct file_operations ext2_dir_operations;

/* file.c */
extern const struct inode_operations ext2_file_inode_operations;
extern const struct file_operations ext2_file_operations;
extern const struct file_operations ext2_xip_file_operations;

/* inode.c */
extern const struct address_space_operations ext2_aops;
extern const struct address_space_operations ext2_aops_xip;
extern const struct address_space_operations ext2_nobh_aops;

/* namei.c */
extern const struct inode_operations ext2_dir_inode_operations;
extern const struct inode_operations ext2_special_inode_operations;

/* symlink.c */
extern const struct inode_operations ext2_fast_symlink_inode_operations;
extern const struct inode_operations ext2_symlink_inode_operations;

static inline ext2_fsblk_t
ext2_group_first_block_no(struct super_block *sb, unsigned long group_no)
{
        return group_no * (ext2_fsblk_t)EXT2_BLOCKS_PER_GROUP(sb) +
                le32_to_cpu(EXT2_SB(sb)->s_es->s_first_data_block);
}
04-29
2008

Ext2到Ext3转换

IN:Linux   Tags: , ,    Comments:2

在U盘上进行一次完整的文件系统创建转换,很多人应该没有用过mkfs.ext2(其实是mke2fs这个命令)这个命令来创建文件系统,大多数的时候都是在安装系统时已经完成了文件系统的创建,下面先看看如何使用mkfs.ext2来创建文件系统(这里需要root权限):

[command]
[cocobear@cocobear minix]$ sudo mkfs.ext2 /dev/sdb
mke2fs 1.40.4 (31-Dec-2007)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
31360 inodes, 125440 blocks
6272 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
16 block groups
8192 blocks per group, 8192 fragments per group
1960 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[/command]

从Filesystem label= 开始解释,mkfs.ext2可以通过-L来指定文件系统的卷标,这里我们没有指定,因此为空;
OS type: Linux:mkfs.ext2在创建文件系统时检查操作系统的类型;
Block size=1024 (log=0):ext2文件系统是由一个个的block组成,这里给出来新创建的block的大小,1024bytes,当然也可以使用-b选项手动指定block的大小,一般情况下block大小为1024bytes或4096bytes;
Fragment size=1024 (log=0):fragment在这里是没有意义的,因为ext2文件系统不支持fragment,我们可以直接忽略。
31360 inodes, 125440 blocks:总共生成的inodes与blocks的数量,block数量我们可以通过U盘的大小来计算出来,首先使用fdisk来得到U盘的大小,该U盘的大小为:128450560 bytes,因为我们使用的block大小为1024 bytes,因此总block数为:128450560 bytes / 1024 =125400。

6272 blocks (5.00%) reserved for the super user:在ext2文件系统的创建时候默认会为root用户保留5%的空间,这个可以-m选项来指定。

First data block=1:表示数据从第一个block开始。

Maximum filesystem blocks=67371008:一个文件系统

16 block groups:一共有16个block分组;

8192 blocks per group, 8192 fragments per group:每个分组有8192个block;

1960 inodes per group:每个组有1960个inode;

Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
超级块在以前这几个block上进行了备份。

当然在文件系统创建好后也可以使用dumpe2fs或者tune2fs来获得上面的信息,tune2fs正是我们下面需要使用的转换工具。
[command]
[cocobear@cocobear ~]$ sudo dumpe2fs /dev/sdb
dumpe2fs 1.40.4 (31-Dec-2007)
Filesystem volume name:
Last mounted on:
Filesystem UUID: d8d7cb8e-4cc0-4d91-b7e3-dcbc3f403345
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: resize_inode dir_index filetype sparse_super
Filesystem flags: signed directory hash
Default mount options: (none)
Filesystem state: not clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 31360
Block count: 125440
Reserved block count: 6272
Free blocks: 119925
Free inodes: 31349
First block: 1
Block size: 1024
Fragment size: 1024
Reserved GDT blocks: 256
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 1960
Inode blocks per group: 245
Filesystem created: Sat Apr 26 08:13:03 2008
Last mount time: Sat Apr 26 17:13:06 2008
Last write time: Sat Apr 26 17:13:06 2008
Mount count: 1
Maximum mount count: 31
Last checked: Sat Apr 26 08:13:03 2008
Check interval: 15552000 (6 months)
Next check after: Thu Oct 23 08:13:03 2008
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128
Default directory hash: tea
Directory Hash Seed: f8b3e365-54e7-43b1-ad25-b3270db87684

Group 0: (Blocks 1-8192)
Primary superblock at 1, Group descriptors at 2-2
Reserved GDT blocks at 3-258
Block bitmap at 259 (+258), Inode bitmap at 260 (+259)
Inode table at 261-505 (+260)
7673 free blocks, 1949 free inodes, 2 directories
Free blocks: 520-8192
Free inodes: 12-1960
………………
[/command]
首先卸载掉U盘,然后使用下面的命令:

[command]
[cocobear@cocobear ~]$ sudo tune2fs -j /dev/sdb
tune2fs 1.40.4 (31-Dec-2007)
Creating journal inode: done
This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[/command]

然后我们再来看一下文件系统信息:
[command]
[cocobear@cocobear ~]$ sudo dumpe2fs /dev/sdb
dumpe2fs 1.40.4 (31-Dec-2007)
Filesystem volume name:
Last mounted on:
Filesystem UUID: d8d7cb8e-4cc0-4d91-b7e3-dcbc3f403345
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal resize_inode dir_index filetype sparse_super
Filesystem flags: signed directory hash
Default mount options: (none)
Filesystem state: not clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 31360
Block count: 125440
Reserved block count: 6272
Free blocks: 115812
Free inodes: 31348
First block: 1
Block size: 1024
Fragment size: 1024
Reserved GDT blocks: 256
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 1960
Inode blocks per group: 245
Filesystem created: Sat Apr 26 08:13:03 2008
Last mount time: Sat Apr 26 17:13:06 2008
Last write time: Sat Apr 26 17:55:15 2008
Mount count: 1
Maximum mount count: 31
Last checked: Sat Apr 26 08:13:03 2008
Check interval: 15552000 (6 months)
Next check after: Thu Oct 23 08:13:03 2008
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128
Journal inode: 12
Default directory hash: tea
Directory Hash Seed: f8b3e365-54e7-43b1-ad25-b3270db87684
Journal size: 4113k

Group 0: (Blocks 1-8192)
Primary superblock at 1, Group descriptors at 2-2
Reserved GDT blocks at 3-258
Block bitmap at 259 (+258), Inode bitmap at 260 (+259)
Inode table at 261-505 (+260)
5113 free blocks, 1948 free inodes, 2 directories
Free blocks: 520-5632
Free inodes: 13-1960
[/command]

从这里“Filesystem features: has_journal resize_inode dir_index filetype sparse_super”我们可以看到已经加入了ext3的日志功能。

04-26
2008
loading...