---
title: "ext2文件系统结构体组成"
author: "M_ittrue"
date: "2012-11-13T11:19:14Z"
tags: []
categories: ["操作系统"]
slug: "ext2"
draft: false
lastmod: "2012-11-12T19:33:18Z"
id: 1723217
url: "https://m-ittrue.iteye.com/blog/1723217"
---

The Second Extended File System(ext2)文件系统是Linux系统中的标准文件系统，是通过对Minix的文件系统进行扩展而得到的，其存取文件的性能极好。  
在ext2文件系统中，文件由inode（包含有文件的所有信息）进行唯一标识。一个文件可能对应多个文件名，只有在所有文件名都被删除后，该文件才会被删除。此外，同一文件在磁盘中存放和被打开时所对应的inode是不同的，并由内核负责同步。  
ext2文件系统采用三级间接块来存储数据块指针，并以块（block，默认为1KB）为单位分配空间。其磁盘分配策略是尽可能将逻辑相邻的文件分配到磁盘上物理相邻的块中，并尽可能将碎片分配给尽量少的文件，以从全局上提高性能。ext2文件系统将同一目录下的文件（包括目录）尽可能的放在同一个块组中，但目录则分布在各个块组中以实现负载均衡。在扩展文件时，会尽量一次性扩展8个连续块给文件（以预留空间的形式实现）。  
**一、磁盘组织**  
在ext2系统中，所有元数据结构的大小均基于“块”，而不是“扇区”。块的大小随文件系统的大小而有所不同。而一定数量的块又组成一个块组，每个块组的起始部分有多种多样的描述该块组各种属性的元数据结构。ext2系统中对各个结构的定义都包含在源代码的include/linux/ext2\_fs.h文件中。  
**1****、超级块**  
每个ext2文件系统都必须包含一个超级块，其中存储了该文件系统的大量基本信息，包括块的大小、每块组中包含的块数等。同时，系统会对超级块进行备份，备份被存放在块组的第一个块中。超级块的起始位置为其所在分区的第1024个字节，占用1KB的空间，其结构如下：  
struct ext2\_super\_block {  
\_\_le32 s\_inodes\_count; // 文件系统中inode的总数  
\_\_le32 s\_blocks\_count; // 文件系统中块的总数  
\_\_le32 s\_r\_blocks\_count; // 保留块的总数  
\_\_le32 s\_free\_blocks\_count; // 未使用的块的总数（包括保留块）  
\_\_le32 s\_free\_inodes\_count; // 未使用的inode的总数  
\_\_le32 s\_first\_data\_block; // 块ID，在小于1KB的文件系统中为0，大于1KB的文件系统中为1  
\_\_le32 s\_log\_block\_size; // 用以计算块的大小（1024算术左移该值即为块大小）  
\_\_le32 s\_log\_frag\_size; // 用以计算段大小（为正则1024算术左移该值，否则右移）  
\_\_le32 s\_blocks\_per\_group; // 每个块组中块的总数  
\_\_le32 s\_frags\_per\_group; // 每个块组中段的总数  
\_\_le32 s\_inodes\_per\_group; // 每个块组中inode的总数  
\_\_le32 s\_mtime; // POSIX中定义的文件系统装载时间  
\_\_le32 s\_wtime; // POSIX中定义的文件系统最近被写入的时间  
\_\_le16 s\_mnt\_count; // 最近一次完整校验后被装载的次数  
\_\_le16 s\_max\_mnt\_count; // 在进行完整校验前还能被装载的次数  
\_\_le16 s\_magic; // 文件系统标志，ext2中为0xEF53  
\_\_le16 s\_state; // 文件系统的状态  
\_\_le16 s\_errors; // 文件系统发生错误时驱动程序应该执行的操作  
\_\_le16 s\_minor\_rev\_level; // 局部修订级别  
\_\_le32 s\_lastcheck; // POSIX中定义的文件系统最近一次检查的时间  
\_\_le32 s\_checkinterval; // POSIX中定义的文件系统最近检查的最大时间间隔  
\_\_le32 s\_creator\_os; // 生成该文件系统的操作系统  
\_\_le32 s\_rev\_level; // 修订级别  
\_\_le16 s\_def\_resuid; // 报留块的默认用户ID  
\_\_le16 s\_def\_resgid; // 保留块的默认组ID  
// 仅用于使用动态inode大小的修订版（EXT2\_DYNAMIC\_REV）  
\_\_le32 s\_first\_ino; // 标准文件的第一个可用inode的索引（非动态为11）  
\_\_le16 s\_inode\_size; // inode结构的大小（非动态为128）  
\_\_le16 s\_block\_group\_nr; // 保存此超级块的块组号  
\_\_le32 s\_feature\_compat; // 兼容特性掩码  
\_\_le32 s\_feature\_incompat; // 不兼容特性掩码  
\_\_le32 s\_feature\_ro\_compat; // 只读特性掩码  
\_\_u8 s\_uuid\[16]; // 卷ID，应尽可能使每个文件系统的格式唯一  
char s\_volume\_name\[16]; // 卷名（只能为ISO-Latin-1字符集，以'\\0'结束）  
char s\_last\_mounted\[64]; // 最近被安装的目录  
\_\_le32 s\_algorithm\_usage\_bitmap; // 文件系统采用的压缩算法  
// 仅在EXT2\_COMPAT\_PREALLOC标志被设置时有效  
\_\_u8 s\_prealloc\_blocks; // 预分配的块数  
\_\_u8 s\_prealloc\_dir\_blocks; // 给目录预分配的块数  
\_\_u16 s\_padding1;  
// 仅在EXT3\_FEATURE\_COMPAT\_HAS\_JOURNAL标志被设置时有效，用以支持日志  
\_\_u8 s\_journal\_uuid\[16]; // 日志超级块的卷ID  
\_\_u32 s\_journal\_inum; // 日志文件的inode数目  
\_\_u32 s\_journal\_dev; // 日志文件的设备数  
\_\_u32 s\_last\_orphan; // 要删除的inode列表的起始位置  
\_\_u32 s\_hash\_seed\[4]; // HTREE散列种子  
\_\_u8 s\_def\_hash\_version; // 默认使用的散列函数  
\_\_u8 s\_reserved\_char\_pad;  
\_\_u16 s\_reserved\_word\_pad;  
\_\_le32 s\_default\_mount\_opts;  
\_\_le32 s\_first\_meta\_bg; // 块组的第一个元块  
\_\_u32 s\_reserved\[190];  
};  
**2****、块组描述符**  
一个块组描述符用以描述一个块组的属性。块组描述符组由若干块组描述符组成，描述了文件系统中所有块组的属性，存放于超级块所在块的下一个块中。一个块组描述符的结构如下：  
struct ext2\_group\_desc  
{  
\_\_le32 bg\_block\_bitmap; // 块位图所在的第一个块的块ID  
\_\_le32 bg\_inode\_bitmap; // inode位图所在的第一个块的块ID  
\_\_le32 bg\_inode\_table; // inode表所在的第一个块的块ID  
\_\_le16 bg\_free\_blocks\_count; // 块组中未使用的块数  
\_\_le16 bg\_free\_inodes\_count; // 块组中未使用的inode数  
\_\_le16 bg\_used\_dirs\_count; // 块组分配的目录的inode数  
\_\_le16 bg\_pad;  
\_\_le32 bg\_reserved\[3];  
};  
**3****、块位图与inode位图**  
块位图和inode位图的每一位分别指出块组中对应的那个块或inode是否被使用。  
**4****、inode表**  
inode表用于跟踪定位每个文件，包括位置、大小等（但不包括文件名），一个块组只有一个inode表。一个inode的结构如下：  
struct ext2\_inode {  
\_\_le16 i\_mode; // 文件格式和访问权限  
\_\_le16 i\_uid; // 文件所有者ID的低16位  
\_\_le32 i\_size; // 文件字节数  
\_\_le32 i\_atime; // 文件上次被访问的时间  
\_\_le32 i\_ctime; // 文件创建时间  
\_\_le32 i\_mtime; // 文件被修改的时间  
\_\_le32 i\_dtime; // 文件被删除的时间（如果存在则为0）  
\_\_le16 i\_gid; // 文件所有组ID的低16位  
\_\_le16 i\_links\_count; // 此inode被连接的次数  
\_\_le32 i\_blocks; // 文件已使用和保留的总块数（以512B为单位）  
\_\_le32 i\_flags; // 此inode访问数据时ext2的实现方式  
union {  
struct {  
\_\_le32 l\_i\_reserved1; // 保留  
} linux1;  
struct {  
\_\_le32 h\_i\_translator; // “翻译者”标签  
} hurd1;  
struct {  
\_\_le32 m\_i\_reserved1; // 保留  
} masix1;  
} osd1; // 操作系统相关数据  
\_\_le32 i\_block\[EXT2\_N\_BLOCKS]; // 定位存储文件的块的数组，前12个为块号，第13个为一级间接块号，第14个为二级间接块号，第15个为三级间接块号  
\_\_le32 i\_generation; // 用于NFS的文件版本  
\_\_le32 i\_file\_acl; // 包含扩展属性的块号，老版本中为0  
\_\_le32 i\_dir\_acl; // 表示文件的“High Size”，老版本中为0  
\_\_le32 i\_faddr; // 文件最后一个段的地址  
union {  
struct {  
\_\_u8 l\_i\_frag; // 段号  
\_\_u8 l\_i\_fsize; // 段大小  
\_\_u16 i\_pad1;  
\_\_le16 l\_i\_uid\_high; // 文件所有者ID的高16位  
\_\_le16 l\_i\_gid\_high; // 文件所有组ID的高16位  
\_\_u32 l\_i\_reserved2;  
} linux2;  
struct {  
\_\_u8 h\_i\_frag; // 段号  
\_\_u8 h\_i\_fsize; // 段大小  
\_\_le16 h\_i\_mode\_high;  
\_\_le16 h\_i\_uid\_high; // 文件所有者ID的高16位  
\_\_le16 h\_i\_gid\_high; // 文件所有组ID的高16位  
\_\_le32 h\_i\_author;  
} hurd2;  
struct {  
\_\_u8 m\_i\_frag; // 段号  
\_\_u8 m\_i\_fsize; // 段大小  
\_\_u16 m\_pad1;  
\_\_u32 m\_i\_reserved2\[2];  
} masix2;  
} osd2; // 操作系统相关数据  
};

5、数据块  
数据块中存放文件的内容，包括目录表、扩展属性、符号链接等。  
二、目录结构  
在ext2文件系统中，目录是作为文件存储的。根目录总是在inode表的第二项，而其子目录则在根目录文件的内容中定义。目录项在include/linux/ext2\_fs.h文件中定义，其结构如下：  
struct ext2\_dir\_entry\_2 {  
\_\_le32 inode; // 文件入口的inode号，0表示该项未使用  
\_\_le16 rec\_len; // 目录项长度  
\_\_u8 name\_len; // 文件名包含的字符数  
\_\_u8 file\_type; // 文件类型  
char name\[255]; // 文件名  
};  
三、文件扩展属性  
文件的属性大多数是位于该文件的inode结构中的标准属性，也还包含其他一些扩展属性（于系统中所有的inode相关，通常用于增加额外的功能），在fs/ext2/xattr.h文件中定义。  
inode的i\_file\_acl字段中保存扩展属性的块的块号。属性头部项位于属性块的起始位置，其后为属性入口项，而属性值可以根据属性入口项找到所在位置。  
1、属性头部项  
struct ext2\_xattr\_header {  
\_\_le32 h\_magic; // 标识码，为0xEA020000  
\_\_le32 h\_refcount; // 属性块被链接的数目  
\_\_le32 h\_blocks; // 用于扩展属性的块数  
\_\_le32 h\_hash; // 所有属性的哈希值  
\_\_u32 h\_reserved\[4];  
};  
2、属性入口项  
struct ext2\_xattr\_entry {  
\_\_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]; // 属性名  
};

**本文来自ChinaUnix博客，如果查看原文请点：**[http://blog.chinaunix.net/u/27624/showart\_234187.html](http://blog.chinaunix.net/u/27624/showart_234187.html)

/*  
*  linux/include/linux/ext2\_fs.h  
\*  
\* Copyright (C) 1992, 1993, 1994, 1995  
\* Remy Card ([card@masi.ibp.fr](mailto:card@masi.ibp.fr))  
\* Laboratoire MASI - Institut Blaise Pascal  
\* Universite Pierre et Marie Curie (Paris VI)  
\*  
*  from  
\*  
*  linux/include/linux/minix\_fs.h  
\*  
*  Copyright (C) 1991, 1992  Linus Torvalds  
\*/

#ifndef \_LINUX\_EXT2\_FS\_H  
#define \_LINUX\_EXT2\_FS\_H

#include &lt;linux/types.h&gt;  
#include &lt;linux/magic.h&gt;  
#include &lt;linux/fs.h&gt;

/*  
\* The second extended filesystem constants/structures  
\*/

/*  
\* Define EXT2FS\_DEBUG to produce debug messages  
\*/  
#undef EXT2FS\_DEBUG

/*  
\* Define EXT2\_RESERVATION to reserve data blocks for expanding files  
\*/  
#define EXT2\_DEFAULT\_RESERVE\_BLOCKS     8  
/\*max window size: 1024(direct blocks) + 3(\[t,d]indirect blocks) \*/  
#define EXT2\_MAX\_RESERVE\_BLOCKS         1027  
#define EXT2\_RESERVE\_WINDOW\_NOT\_ALLOCATED 0  
/*  
\* The second extended file system version  
\*/  
#define EXT2FS\_DATE                "95/08/09"  
#define EXT2FS\_VERSION                "0.5b"

/*  
\* Debug code  
\*/  
#ifdef EXT2FS\_DEBUG  
#        define ext2\_debug(f, a...)        { \\  
                                        printk ("EXT2-fs DEBUG (%s, %d): %s:", \\  
                                                \_\_FILE\_\_, \_\_LINE\_\_, \_\_func\_\_); \\  
                                          printk (f, ## a); \\  
                                        }  
#else  
#        define ext2\_debug(f, a...)        /\*\*/  
#endif

/*  
\* Special inode numbers  
\*/  
#define        EXT2\_BAD\_INO                 1        /* Bad blocks inode \*/  
#define EXT2\_ROOT\_INO                 2        /* Root inode \*/  
#define EXT2\_BOOT\_LOADER\_INO         5        /* Boot loader inode \*/  
#define EXT2\_UNDEL\_DIR\_INO         6        /* Undelete directory inode \*/

/* First non-reserved inode for old ext2 filesystems \*/  
#define EXT2\_GOOD\_OLD\_FIRST\_INO        11

/* Assume that user mode programs are passing in an ext2fs superblock, not  
\* a kernel struct super\_block.  This will allow us to call the feature-test  
\* macros from user land. \*/  
#define EXT2\_SB(sb)        (sb)

/*  
\* Maximal count of links to a file  
\*/  
#define EXT2\_LINK\_MAX                32000

/*  
\* Macro-instructions used to manage several block sizes  
\*/  
#define EXT2\_MIN\_BLOCK\_SIZE                1024  
#define        EXT2\_MAX\_BLOCK\_SIZE                4096  
#define EXT2\_MIN\_BLOCK\_LOG\_SIZE                  10  
\# define EXT2\_BLOCK\_SIZE(s)                (EXT2\_MIN\_BLOCK\_SIZE &lt;&lt; (s)-&gt;s\_log\_block\_size)  
#define        EXT2\_ADDR\_PER\_BLOCK(s)                (EXT2\_BLOCK\_SIZE(s) / sizeof (\_\_u32))  
\# define EXT2\_BLOCK\_SIZE\_BITS(s)        ((s)-&gt;s\_log\_block\_size + 10)  
#define EXT2\_INODE\_SIZE(s)        (((s)-&gt;s\_rev\_level == EXT2\_GOOD\_OLD\_REV) ? \\  
                                 EXT2\_GOOD\_OLD\_INODE\_SIZE : \\  
                                 (s)-&gt;s\_inode\_size)  
#define EXT2\_FIRST\_INO(s)        (((s)-&gt;s\_rev\_level == EXT2\_GOOD\_OLD\_REV) ? \\  
                                 EXT2\_GOOD\_OLD\_FIRST\_INO : \\  
                                 (s)-&gt;s\_first\_ino)

/*  
\* Macro-instructions used to manage fragments  
\*/  
#define EXT2\_MIN\_FRAG\_SIZE                1024  
#define        EXT2\_MAX\_FRAG\_SIZE                4096  
#define EXT2\_MIN\_FRAG\_LOG\_SIZE                  10  
\# define EXT2\_FRAG\_SIZE(s)                (EXT2\_MIN\_FRAG\_SIZE &lt;&lt; (s)-&gt;s\_log\_frag\_size)  
\# define EXT2\_FRAGS\_PER\_BLOCK(s)        (EXT2\_BLOCK\_SIZE(s) / EXT2\_FRAG\_SIZE(s))

/*  
\* Structure of a blocks group descriptor  
\*/  
struct ext2\_group\_desc  
{  
        \_\_le32        bg\_block\_bitmap;                /* Blocks bitmap block \*/  
        \_\_le32        bg\_inode\_bitmap;                /* Inodes bitmap block \*/  
        \_\_le32        bg\_inode\_table;                /* Inodes table block \*/  
        \_\_le16        bg\_free\_blocks\_count;        /* Free blocks count \*/  
        \_\_le16        bg\_free\_inodes\_count;        /* Free inodes count \*/  
        \_\_le16        bg\_used\_dirs\_count;        /* Directories count \*/  
        \_\_le16        bg\_pad;  
        \_\_le32        bg\_reserved\[3];  
};

/*  
\* Macro-instructions used to manage group descriptors  
\*/  
\# define EXT2\_BLOCKS\_PER\_GROUP(s)        ((s)-&gt;s\_blocks\_per\_group)  
\# define EXT2\_DESC\_PER\_BLOCK(s)                (EXT2\_BLOCK\_SIZE(s) / sizeof (struct ext2\_group\_desc))  
\# define EXT2\_INODES\_PER\_GROUP(s)        ((s)-&gt;s\_inodes\_per\_group)

/*  
\* Constants relative to the data blocks  
\*/  
#define        EXT2\_NDIR\_BLOCKS                12  
#define        EXT2\_IND\_BLOCK                        EXT2\_NDIR\_BLOCKS  
#define        EXT2\_DIND\_BLOCK                        (EXT2\_IND\_BLOCK + 1)  
#define        EXT2\_TIND\_BLOCK                        (EXT2\_DIND\_BLOCK + 1)  
#define        EXT2\_N\_BLOCKS                        (EXT2\_TIND\_BLOCK + 1)

/*  
\* Inode flags (GETFLAGS/SETFLAGS)  
\*/  
#define        EXT2\_SECRM\_FL                        FS\_SECRM\_FL        /* Secure deletion \*/  
#define        EXT2\_UNRM\_FL                        FS\_UNRM\_FL        /* Undelete \*/  
#define        EXT2\_COMPR\_FL                        FS\_COMPR\_FL        /* Compress file \*/  
#define EXT2\_SYNC\_FL                        FS\_SYNC\_FL        /* Synchronous updates \*/  
#define EXT2\_IMMUTABLE\_FL                FS\_IMMUTABLE\_FL        /* Immutable file \*/  
#define EXT2\_APPEND\_FL                        FS\_APPEND\_FL        /* writes to file may only append \*/  
#define EXT2\_NODUMP\_FL                        FS\_NODUMP\_FL        /* do not dump file \*/  
#define EXT2\_NOATIME\_FL                        FS\_NOATIME\_FL        /* do not update atime \*/  
/* Reserved for compression usage... \*/  
#define EXT2\_DIRTY\_FL                        FS\_DIRTY\_FL  
#define EXT2\_COMPRBLK\_FL                FS\_COMPRBLK\_FL        /* One or more compressed clusters \*/  
#define EXT2\_NOCOMP\_FL                        FS\_NOCOMP\_FL        /* Don't compress \*/  
#define EXT2\_ECOMPR\_FL                        FS\_ECOMPR\_FL        /* Compression error \*/  
/* End compression flags --- maybe not all used \*/          
#define EXT2\_BTREE\_FL                        FS\_BTREE\_FL        /* btree format dir \*/  
#define EXT2\_INDEX\_FL                        FS\_INDEX\_FL        /* hash-indexed directory \*/  
#define EXT2\_IMAGIC\_FL                        FS\_IMAGIC\_FL        /* AFS directory \*/  
#define EXT2\_JOURNAL\_DATA\_FL                FS\_JOURNAL\_DATA\_FL /* Reserved for ext3 \*/  
#define EXT2\_NOTAIL\_FL                        FS\_NOTAIL\_FL        /* file tail should not be merged \*/  
#define EXT2\_DIRSYNC\_FL                        FS\_DIRSYNC\_FL        /* dirsync behaviour (directories only) \*/  
#define EXT2\_TOPDIR\_FL                        FS\_TOPDIR\_FL        /* Top of directory hierarchies\*/  
#define EXT2\_RESERVED\_FL                FS\_RESERVED\_FL        /* reserved for ext2 lib \*/

#define EXT2\_FL\_USER\_VISIBLE                FS\_FL\_USER\_VISIBLE        /* User visible flags \*/  
#define EXT2\_FL\_USER\_MODIFIABLE                FS\_FL\_USER\_MODIFIABLE        /* User modifiable flags \*/

/* Flags that should be inherited by new inodes from their parent. \*/  
#define EXT2\_FL\_INHERITED (EXT2\_SECRM\_FL | EXT2\_UNRM\_FL | EXT2\_COMPR\_FL |\\  
                           EXT2\_SYNC\_FL | EXT2\_IMMUTABLE\_FL | EXT2\_APPEND\_FL |\\  
                           EXT2\_NODUMP\_FL | EXT2\_NOATIME\_FL | EXT2\_COMPRBLK\_FL|\\  
                           EXT2\_NOCOMP\_FL | EXT2\_JOURNAL\_DATA\_FL |\\  
                           EXT2\_NOTAIL\_FL | EXT2\_DIRSYNC\_FL)

/* Flags that are appropriate for regular files (all but dir-specific ones). \*/  
#define EXT2\_REG\_FLMASK (~(EXT2\_DIRSYNC\_FL | EXT2\_TOPDIR\_FL))

/* Flags that are appropriate for non-directories/regular files. \*/  
#define EXT2\_OTHER\_FLMASK (EXT2\_NODUMP\_FL | EXT2\_NOATIME\_FL)

/* Mask out flags that are inappropriate for the given type of inode. \*/  
static \_\_inline\__ \_\_u32 ext2\_mask\_flags(umode\_t mode, \_\_u32 flags)  
{  
        if (S\_ISDIR(mode))  
                return flags;  
        else if (S\_ISREG(mode))  
                return flags & EXT2\_REG\_FLMASK;  
        else  
                return flags & EXT2\_OTHER\_FLMASK;  
}

/*  
\* ioctl commands  
\*/  
#define        EXT2\_IOC\_GETFLAGS                FS\_IOC\_GETFLAGS  
#define        EXT2\_IOC\_SETFLAGS                FS\_IOC\_SETFLAGS  
#define        EXT2\_IOC\_GETVERSION                FS\_IOC\_GETVERSION  
#define        EXT2\_IOC\_SETVERSION                FS\_IOC\_SETVERSION  
#define        EXT2\_IOC\_GETRSVSZ                \_IOR('f', 5, long)  
#define        EXT2\_IOC\_SETRSVSZ                \_IOW('f', 6, long)

/*  
\* ioctl commands in 32 bit emulation  
\*/  
#define EXT2\_IOC32\_GETFLAGS                FS\_IOC32\_GETFLAGS  
#define EXT2\_IOC32\_SETFLAGS                FS\_IOC32\_SETFLAGS  
#define EXT2\_IOC32\_GETVERSION                FS\_IOC32\_GETVERSION  
#define EXT2\_IOC32\_SETVERSION                FS\_IOC32\_SETVERSION

/*  
\* Structure of an inode on the disk  
\*/  
struct ext2\_inode {  
        \_\_le16        i\_mode;                /* File mode \*/  
        \_\_le16        i\_uid;                /* Low 16 bits of Owner Uid \*/  
        \_\_le32        i\_size;                /* Size in bytes \*/  
        \_\_le32        i\_atime;        /* Access time \*/  
        \_\_le32        i\_ctime;        /* Creation time \*/  
        \_\_le32        i\_mtime;        /* Modification time \*/  
        \_\_le32        i\_dtime;        /* Deletion Time \*/  
        \_\_le16        i\_gid;                /* Low 16 bits of Group Id \*/  
        \_\_le16        i\_links\_count;        /* Links count \*/  
        \_\_le32        i\_blocks;        /* Blocks count \*/  
        \_\_le32        i\_flags;        /* File flags \*/  
        union {  
                struct {  
                        \_\_le32  l\_i\_reserved1;  
                } linux1;  
                struct {  
                        \_\_le32  h\_i\_translator;  
                } hurd1;  
                struct {  
                        \_\_le32  m\_i\_reserved1;  
                } masix1;  
        } osd1;                                /* OS dependent 1 \*/  
        \_\_le32        i\_block\[EXT2\_N\_BLOCKS];/* Pointers to blocks \*/  
        \_\_le32        i\_generation;        /* File version (for NFS) \*/  
        \_\_le32        i\_file\_acl;        /* File ACL \*/  
        \_\_le32        i\_dir\_acl;        /* Directory ACL \*/  
        \_\_le32        i\_faddr;        /* Fragment address \*/  
        union {  
                struct {  
                        \_\_u8        l\_i\_frag;        /* Fragment number \*/  
                        \_\_u8        l\_i\_fsize;        /* Fragment size \*/  
                        \_\_u16        i\_pad1;  
                        \_\_le16        l\_i\_uid\_high;        /* these 2 fields    \*/  
                        \_\_le16        l\_i\_gid\_high;        /* were reserved2\[0] \*/  
                        \_\_u32        l\_i\_reserved2;  
                } linux2;  
                struct {  
                        \_\_u8        h\_i\_frag;        /* Fragment number \*/  
                        \_\_u8        h\_i\_fsize;        /* Fragment size \*/  
                        \_\_le16        h\_i\_mode\_high;  
                        \_\_le16        h\_i\_uid\_high;  
                        \_\_le16        h\_i\_gid\_high;  
                        \_\_le32        h\_i\_author;  
                } hurd2;  
                struct {  
                        \_\_u8        m\_i\_frag;        /* Fragment number \*/  
                        \_\_u8        m\_i\_fsize;        /* Fragment size \*/  
                        \_\_u16        m\_pad1;  
                        \_\_u32        m\_i\_reserved2\[2];  
                } masix2;  
        } osd2;                                /* OS dependent 2 \*/  
};

#define i\_size\_high        i\_dir\_acl

#if defined(\_\_KERNEL\_\_) || defined(\_\_linux\_\_)  
#define i\_reserved1        osd1.linux1.l\_i\_reserved1  
#define i\_frag                osd2.linux2.l\_i\_frag  
#define i\_fsize                osd2.linux2.l\_i\_fsize  
#define i\_uid\_low        i\_uid  
#define i\_gid\_low        i\_gid  
#define i\_uid\_high        osd2.linux2.l\_i\_uid\_high  
#define i\_gid\_high        osd2.linux2.l\_i\_gid\_high  
#define i\_reserved2        osd2.linux2.l\_i\_reserved2  
#endif

#ifdef        \_\_hurd\__  
#define i\_translator        osd1.hurd1.h\_i\_translator  
#define i\_frag                osd2.hurd2.h\_i\_frag  
#define i\_fsize                osd2.hurd2.h\_i\_fsize  
#define i\_uid\_high        osd2.hurd2.h\_i\_uid\_high  
#define i\_gid\_high        osd2.hurd2.h\_i\_gid\_high  
#define i\_author        osd2.hurd2.h\_i\_author  
#endif

#ifdef        \_\_masix\__  
#define i\_reserved1        osd1.masix1.m\_i\_reserved1  
#define i\_frag                osd2.masix2.m\_i\_frag  
#define i\_fsize                osd2.masix2.m\_i\_fsize  
#define i\_reserved2        osd2.masix2.m\_i\_reserved2  
#endif

/*  
\* File system states  
\*/  
#define        EXT2\_VALID\_FS                        0x0001        /* Unmounted cleanly \*/  
#define        EXT2\_ERROR\_FS                        0x0002        /* Errors detected \*/

/*  
\* Mount flags  
\*/  
#define EXT2\_MOUNT\_CHECK                0x000001  /* Do mount-time checks \*/  
#define EXT2\_MOUNT\_OLDALLOC                0x000002  /* Don't use the new Orlov allocator \*/  
#define EXT2\_MOUNT\_GRPID                0x000004  /* Create files with directory's group \*/  
#define EXT2\_MOUNT\_DEBUG                0x000008  /* Some debugging messages \*/  
#define EXT2\_MOUNT\_ERRORS\_CONT                0x000010  /* Continue on errors \*/  
#define EXT2\_MOUNT\_ERRORS\_RO                0x000020  /* Remount fs ro on errors \*/  
#define EXT2\_MOUNT\_ERRORS\_PANIC                0x000040  /* Panic on errors \*/  
#define EXT2\_MOUNT\_MINIX\_DF                0x000080  /* Mimics the Minix statfs \*/  
#define EXT2\_MOUNT\_NOBH                        0x000100  /* No buffer\_heads \*/  
#define EXT2\_MOUNT\_NO\_UID32                0x000200  /* Disable 32-bit UIDs \*/  
#define EXT2\_MOUNT\_XATTR\_USER                0x004000  /* Extended user attributes \*/  
#define EXT2\_MOUNT\_POSIX\_ACL                0x008000  /* POSIX Access Control Lists \*/  
#define EXT2\_MOUNT\_XIP                        0x010000  /* Execute in place \*/  
#define EXT2\_MOUNT\_USRQUOTA                0x020000  /* user quota \*/  
#define EXT2\_MOUNT\_GRPQUOTA                0x040000  /* group quota \*/  
#define EXT2\_MOUNT\_RESERVATION                0x080000  /* Preallocation \*/

#define clear\_opt(o, opt)                o &= ~EXT2\_MOUNT\_##opt  
#define set\_opt(o, opt)                        o |= EXT2\_MOUNT\_##opt  
#define test\_opt(sb, opt)                (EXT2\_SB(sb)-&gt;s\_mount\_opt & \\  
                                         EXT2\_MOUNT\_##opt)  
/*  
\* Maximal mount counts between two filesystem checks  
\*/  
#define EXT2\_DFL\_MAX\_MNT\_COUNT                20        /* Allow 20 mounts \*/  
#define EXT2\_DFL\_CHECKINTERVAL                0        /* Don't use interval check \*/

/*  
\* Behaviour when detecting errors  
\*/  
#define EXT2\_ERRORS\_CONTINUE                1        /* Continue execution \*/  
#define EXT2\_ERRORS\_RO                        2        /* Remount fs read-only \*/  
#define EXT2\_ERRORS\_PANIC                3        /* Panic \*/  
#define EXT2\_ERRORS\_DEFAULT                EXT2\_ERRORS\_CONTINUE

/*  
\* Structure of the super block  
\*/  
struct ext2\_super\_block {  
        \_\_le32        s\_inodes\_count;                /* Inodes count \*/  
        \_\_le32        s\_blocks\_count;                /* Blocks count \*/  
        \_\_le32        s\_r\_blocks\_count;        /* Reserved blocks count \*/  
        \_\_le32        s\_free\_blocks\_count;        /* Free blocks count \*/  
        \_\_le32        s\_free\_inodes\_count;        /* Free inodes count \*/  
        \_\_le32        s\_first\_data\_block;        /* First Data Block \*/  
        \_\_le32        s\_log\_block\_size;        /* Block size \*/  
        \_\_le32        s\_log\_frag\_size;        /* Fragment size \*/  
        \_\_le32        s\_blocks\_per\_group;        /* # Blocks per group \*/  
        \_\_le32        s\_frags\_per\_group;        /* # Fragments per group \*/  
        \_\_le32        s\_inodes\_per\_group;        /* # Inodes per group \*/  
        \_\_le32        s\_mtime;                /* Mount time \*/  
        \_\_le32        s\_wtime;                /* Write time \*/  
        \_\_le16        s\_mnt\_count;                /* Mount count \*/  
        \_\_le16        s\_max\_mnt\_count;        /* Maximal mount count \*/  
        \_\_le16        s\_magic;                /* Magic signature \*/  
        \_\_le16        s\_state;                /* File system state \*/  
        \_\_le16        s\_errors;                /* Behaviour when detecting errors \*/  
        \_\_le16        s\_minor\_rev\_level;         /* minor revision level \*/  
        \_\_le32        s\_lastcheck;                /* time of last check \*/  
        \_\_le32        s\_checkinterval;        /* max. time between checks \*/  
        \_\_le32        s\_creator\_os;                /* OS \*/  
        \_\_le32        s\_rev\_level;                /* Revision level \*/  
        \_\_le16        s\_def\_resuid;                /* Default uid for reserved blocks \*/  
        \_\_le16        s\_def\_resgid;                /* Default gid for reserved blocks \*/  
        /*  
         * These fields are for EXT2\_DYNAMIC\_REV superblocks only.  
         *  
         * Note: the difference between the compatible feature set and  
         * the incompatible feature set is that if there is a bit set  
         * in the incompatible feature set that the kernel doesn't  
         * know about, it should refuse to mount the filesystem.  
         *   
         * e2fsck's requirements are more strict; if it doesn't know  
         * about a feature in either the compatible or incompatible  
         * feature set, it must abort and not try to meddle with  
         * things it doesn't understand...  
         \*/  
        \_\_le32        s\_first\_ino;                 /* First non-reserved inode \*/  
        \_\_le16   s\_inode\_size;                 /* size of inode structure \*/  
        \_\_le16        s\_block\_group\_nr;         /* block group # of this superblock \*/  
        \_\_le32        s\_feature\_compat;         /* compatible feature set \*/  
        \_\_le32        s\_feature\_incompat;         /* incompatible feature set \*/  
        \_\_le32        s\_feature\_ro\_compat;         /* readonly-compatible feature set \*/  
        \_\_u8        s\_uuid\[16];                /* 128-bit uuid for volume \*/  
        char        s\_volume\_name\[16];         /* volume name \*/  
        char        s\_last\_mounted\[64];         /* directory where last mounted \*/  
        \_\_le32        s\_algorithm\_usage\_bitmap; /* For compression \*/  
        /*  
         * Performance hints.  Directory preallocation should only  
         * happen if the EXT2\_COMPAT\_PREALLOC flag is on.  
         \*/  
        \_\_u8        s\_prealloc\_blocks;        /* Nr of blocks to try to preallocate\*/  
        \_\_u8        s\_prealloc\_dir\_blocks;        /* Nr to preallocate for dirs \*/  
        \_\_u16        s\_padding1;  
        /*  
         * Journaling support valid if EXT3\_FEATURE\_COMPAT\_HAS\_JOURNAL set.  
         \*/  
        \_\_u8        s\_journal\_uuid\[16];        /* uuid of journal superblock \*/  
        \_\_u32        s\_journal\_inum;                /* inode number of journal file \*/  
        \_\_u32        s\_journal\_dev;                /* device number of journal file \*/  
        \_\_u32        s\_last\_orphan;                /* start of list of inodes to delete \*/  
        \_\_u32        s\_hash\_seed\[4];                /* HTREE hash seed \*/  
        \_\_u8        s\_def\_hash\_version;        /* Default hash version to use \*/  
        \_\_u8        s\_reserved\_char\_pad;  
        \_\_u16        s\_reserved\_word\_pad;  
        \_\_le32        s\_default\_mount\_opts;  
        \_\_le32        s\_first\_meta\_bg;         /* First metablock block group \*/  
        \_\_u32        s\_reserved\[190];        /* Padding to the end of the block \*/  
};

/*  
\* Codes for operating systems  
\*/  
#define EXT2\_OS\_LINUX                0  
#define EXT2\_OS\_HURD                1  
#define EXT2\_OS\_MASIX                2  
#define EXT2\_OS\_FREEBSD                3  
#define EXT2\_OS\_LITES                4

/*  
\* Revision levels  
\*/  
#define EXT2\_GOOD\_OLD\_REV        0        /* The good old (original) format \*/  
#define EXT2\_DYNAMIC\_REV        1         /* V2 format w/ dynamic inode sizes \*/

#define EXT2\_CURRENT\_REV        EXT2\_GOOD\_OLD\_REV  
#define EXT2\_MAX\_SUPP\_REV        EXT2\_DYNAMIC\_REV

#define EXT2\_GOOD\_OLD\_INODE\_SIZE 128

/*  
\* Feature set definitions  
\*/

#define EXT2\_HAS\_COMPAT\_FEATURE(sb,mask)                        \\  
        ( EXT2\_SB(sb)-&gt;s\_es-&gt;s\_feature\_compat & cpu\_to\_le32(mask) )  
#define EXT2\_HAS\_RO\_COMPAT\_FEATURE(sb,mask)                        \\  
        ( EXT2\_SB(sb)-&gt;s\_es-&gt;s\_feature\_ro\_compat & cpu\_to\_le32(mask) )  
#define EXT2\_HAS\_INCOMPAT\_FEATURE(sb,mask)                        \\  
        ( EXT2\_SB(sb)-&gt;s\_es-&gt;s\_feature\_incompat & cpu\_to\_le32(mask) )  
#define EXT2\_SET\_COMPAT\_FEATURE(sb,mask)                        \\  
        EXT2\_SB(sb)-&gt;s\_es-&gt;s\_feature\_compat |= cpu\_to\_le32(mask)  
#define EXT2\_SET\_RO\_COMPAT\_FEATURE(sb,mask)                        \\  
        EXT2\_SB(sb)-&gt;s\_es-&gt;s\_feature\_ro\_compat |= cpu\_to\_le32(mask)  
#define EXT2\_SET\_INCOMPAT\_FEATURE(sb,mask)                        \\  
        EXT2\_SB(sb)-&gt;s\_es-&gt;s\_feature\_incompat |= cpu\_to\_le32(mask)  
#define EXT2\_CLEAR\_COMPAT\_FEATURE(sb,mask)                        \\  
        EXT2\_SB(sb)-&gt;s\_es-&gt;s\_feature\_compat &= ~cpu\_to\_le32(mask)  
#define EXT2\_CLEAR\_RO\_COMPAT\_FEATURE(sb,mask)                        \\  
        EXT2\_SB(sb)-&gt;s\_es-&gt;s\_feature\_ro\_compat &= ~cpu\_to\_le32(mask)  
#define EXT2\_CLEAR\_INCOMPAT\_FEATURE(sb,mask)                        \\  
        EXT2\_SB(sb)-&gt;s\_es-&gt;s\_feature\_incompat &= ~cpu\_to\_le32(mask)

#define EXT2\_FEATURE\_COMPAT\_DIR\_PREALLOC        0x0001  
#define EXT2\_FEATURE\_COMPAT\_IMAGIC\_INODES        0x0002  
#define EXT3\_FEATURE\_COMPAT\_HAS\_JOURNAL                0x0004  
#define EXT2\_FEATURE\_COMPAT\_EXT\_ATTR                0x0008  
#define EXT2\_FEATURE\_COMPAT\_RESIZE\_INO                0x0010  
#define EXT2\_FEATURE\_COMPAT\_DIR\_INDEX                0x0020  
#define EXT2\_FEATURE\_COMPAT\_ANY                        0xffffffff

#define EXT2\_FEATURE\_RO\_COMPAT\_SPARSE\_SUPER        0x0001  
#define EXT2\_FEATURE\_RO\_COMPAT\_LARGE\_FILE        0x0002  
#define EXT2\_FEATURE\_RO\_COMPAT\_BTREE\_DIR        0x0004  
#define EXT2\_FEATURE\_RO\_COMPAT\_ANY                0xffffffff

#define EXT2\_FEATURE\_INCOMPAT\_COMPRESSION        0x0001  
#define EXT2\_FEATURE\_INCOMPAT\_FILETYPE                0x0002  
#define EXT3\_FEATURE\_INCOMPAT\_RECOVER                0x0004  
#define EXT3\_FEATURE\_INCOMPAT\_JOURNAL\_DEV        0x0008  
#define EXT2\_FEATURE\_INCOMPAT\_META\_BG                0x0010  
#define EXT2\_FEATURE\_INCOMPAT\_ANY                0xffffffff

#define EXT2\_FEATURE\_COMPAT\_SUPP        EXT2\_FEATURE\_COMPAT\_EXT\_ATTR  
#define EXT2\_FEATURE\_INCOMPAT\_SUPP        (EXT2\_FEATURE\_INCOMPAT\_FILETYPE| \\  
                                         EXT2\_FEATURE\_INCOMPAT\_META\_BG)  
#define EXT2\_FEATURE\_RO\_COMPAT\_SUPP        (EXT2\_FEATURE\_RO\_COMPAT\_SPARSE\_SUPER| \\  
                                         EXT2\_FEATURE\_RO\_COMPAT\_LARGE\_FILE| \\  
                                         EXT2\_FEATURE\_RO\_COMPAT\_BTREE\_DIR)  
#define EXT2\_FEATURE\_RO\_COMPAT\_UNSUPPORTED        ~EXT2\_FEATURE\_RO\_COMPAT\_SUPP  
#define EXT2\_FEATURE\_INCOMPAT\_UNSUPPORTED        ~EXT2\_FEATURE\_INCOMPAT\_SUPP

/*  
\* Default values for user and/or group using reserved blocks  
\*/  
#define        EXT2\_DEF\_RESUID                0  
#define        EXT2\_DEF\_RESGID                0

/*  
\* Default mount options  
\*/  
#define EXT2\_DEFM\_DEBUG                0x0001  
#define EXT2\_DEFM\_BSDGROUPS        0x0002  
#define EXT2\_DEFM\_XATTR\_USER        0x0004  
#define EXT2\_DEFM\_ACL                0x0008  
#define EXT2\_DEFM\_UID16                0x0010  
    /* Not used by ext2, but reserved for use by ext3 \*/  
#define EXT3\_DEFM\_JMODE                0x0060   
#define EXT3\_DEFM\_JMODE\_DATA        0x0020  
#define EXT3\_DEFM\_JMODE\_ORDERED        0x0040  
#define EXT3\_DEFM\_JMODE\_WBACK        0x0060

/*  
\* Structure of a directory entry  
\*/  
#define EXT2\_NAME\_LEN 255

struct ext2\_dir\_entry {  
        \_\_le32        inode;                        /* Inode number \*/  
        \_\_le16        rec\_len;                /* Directory entry length \*/  
        \_\_le16        name\_len;                /* Name length \*/  
        char        name\[EXT2\_NAME\_LEN];        /* File name \*/  
};

/*  
\* The new version of the directory entry.  Since EXT2 structures are  
\* stored in intel byte order, and the name\_len field could never be  
\* bigger than 255 chars, it's safe to reclaim the extra byte for the  
\* file\_type field.  
\*/  
struct ext2\_dir\_entry\_2 {  
        \_\_le32        inode;                        /* Inode number \*/  
        \_\_le16        rec\_len;                /* Directory entry length \*/  
        \_\_u8        name\_len;                /* Name length \*/  
        \_\_u8        file\_type;  
        char        name\[EXT2\_NAME\_LEN];        /* File name \*/  
};

/*  
\* Ext2 directory file types.  Only the low 3 bits are used.  The  
\* other bits are reserved for now.  
\*/  
enum {  
        EXT2\_FT\_UNKNOWN                = 0,  
        EXT2\_FT\_REG\_FILE        = 1,  
        EXT2\_FT\_DIR                = 2,  
        EXT2\_FT\_CHRDEV                = 3,  
        EXT2\_FT\_BLKDEV                = 4,  
        EXT2\_FT\_FIFO                = 5,  
        EXT2\_FT\_SOCK                = 6,  
        EXT2\_FT\_SYMLINK                = 7,  
        EXT2\_FT\_MAX  
};

/*  
\* EXT2\_DIR\_PAD defines the directory entries boundaries  
\*  
\* NOTE: It must be a multiple of 4  
\*/  
#define EXT2\_DIR\_PAD                         4  
#define EXT2\_DIR\_ROUND                         (EXT2\_DIR\_PAD - 1)  
#define EXT2\_DIR\_REC\_LEN(name\_len)        (((name\_len) + 8 + EXT2\_DIR\_ROUND) & \\  
                                         ~EXT2\_DIR\_ROUND)  
#define EXT2\_MAX\_REC\_LEN                ((1&lt;&lt;16)-1)

#endif        /* \_LINUX\_EXT2\_FS\_H \*/