Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs.h revision 1.8.2.1
      1  1.8.2.1      he /*	$NetBSD: ext2fs.h,v 1.8.2.1 2000/02/01 23:38:39 he Exp $	*/
      2      1.1  bouyer 
      3      1.1  bouyer /*
      4      1.1  bouyer  * Copyright (c) 1997 Manuel Bouyer.
      5      1.1  bouyer  * Copyright (c) 1982, 1986, 1993
      6      1.1  bouyer  *	The Regents of the University of California.  All rights reserved.
      7      1.1  bouyer  *
      8      1.1  bouyer  * Redistribution and use in source and binary forms, with or without
      9      1.1  bouyer  * modification, are permitted provided that the following conditions
     10      1.1  bouyer  * are met:
     11      1.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     12      1.1  bouyer  *    notice, this list of conditions and the following disclaimer.
     13      1.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     14      1.1  bouyer  *    notice, this list of conditions and the following disclaimer in the
     15      1.1  bouyer  *    documentation and/or other materials provided with the distribution.
     16      1.1  bouyer  * 3. All advertising materials mentioning features or use of this software
     17      1.1  bouyer  *    must display the following acknowledgement:
     18      1.1  bouyer  *	This product includes software developed by the University of
     19      1.1  bouyer  *	California, Berkeley and its contributors.
     20      1.1  bouyer  * 4. Neither the name of the University nor the names of its contributors
     21      1.1  bouyer  *    may be used to endorse or promote products derived from this software
     22      1.1  bouyer  *    without specific prior written permission.
     23      1.1  bouyer  *
     24      1.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25      1.1  bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26      1.1  bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27      1.1  bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28      1.1  bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29      1.1  bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30      1.1  bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31      1.1  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32      1.1  bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33      1.1  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34      1.1  bouyer  * SUCH DAMAGE.
     35      1.1  bouyer  *
     36      1.1  bouyer  *	@(#)fs.h	8.10 (Berkeley) 10/27/94
     37      1.1  bouyer  *  Modified for ext2fs by Manuel Bouyer.
     38      1.1  bouyer  */
     39      1.5  bouyer 
     40      1.7  bouyer #include <machine/bswap.h>
     41      1.1  bouyer 
     42      1.1  bouyer /*
     43      1.1  bouyer  * Each disk drive contains some number of file systems.
     44      1.1  bouyer  * A file system consists of a number of cylinder groups.
     45      1.1  bouyer  * Each cylinder group has inodes and data.
     46      1.1  bouyer  *
     47      1.1  bouyer  * A file system is described by its super-block, which in turn
     48      1.1  bouyer  * describes the cylinder groups.  The super-block is critical
     49      1.1  bouyer  * data and is replicated in each cylinder group to protect against
     50      1.1  bouyer  * catastrophic loss.  This is done at `newfs' time and the critical
     51      1.1  bouyer  * super-block data does not change, so the copies need not be
     52      1.1  bouyer  * referenced further unless disaster strikes.
     53      1.1  bouyer  *
     54      1.1  bouyer  * The first boot and super blocks are given in absolute disk addresses.
     55      1.1  bouyer  * The byte-offset forms are preferred, as they don't imply a sector size.
     56      1.1  bouyer  */
     57      1.1  bouyer #define BBSIZE		1024
     58      1.1  bouyer #define SBSIZE		1024
     59      1.1  bouyer #define	BBOFF		((off_t)(0))
     60      1.1  bouyer #define	SBOFF		((off_t)(BBOFF + BBSIZE))
     61      1.3    fvdl #define	BBLOCK		((ufs_daddr_t)(0))
     62      1.3    fvdl #define	SBLOCK		((ufs_daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
     63      1.1  bouyer 
     64      1.1  bouyer /*
     65      1.1  bouyer  * Addresses stored in inodes are capable of addressing blocks
     66      1.1  bouyer  * XXX
     67      1.1  bouyer  */
     68      1.1  bouyer 
     69      1.1  bouyer /*
     70      1.1  bouyer  * MINBSIZE is the smallest allowable block size.
     71      1.1  bouyer  * MINBSIZE must be big enough to hold a cylinder group block,
     72      1.1  bouyer  * thus changes to (struct cg) must keep its size within MINBSIZE.
     73      1.1  bouyer  * Note that super blocks are always of size SBSIZE,
     74      1.1  bouyer  * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
     75      1.1  bouyer  */
     76      1.1  bouyer #define LOG_MINBSIZE 10
     77      1.1  bouyer #define MINBSIZE	(1 << LOG_MINBSIZE)
     78      1.1  bouyer 
     79      1.1  bouyer /*
     80      1.1  bouyer  * The path name on which the file system is mounted is maintained
     81      1.1  bouyer  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
     82      1.1  bouyer  * the super block for this name.
     83      1.1  bouyer  */
     84      1.1  bouyer #define MAXMNTLEN	512
     85      1.1  bouyer 
     86      1.1  bouyer /*
     87      1.1  bouyer  * MINFREE gives the minimum acceptable percentage of file system
     88      1.1  bouyer  * blocks which may be free. If the freelist drops below this level
     89      1.1  bouyer  * only the superuser may continue to allocate blocks. This may
     90      1.1  bouyer  * be set to 0 if no reserve of free blocks is deemed necessary,
     91      1.1  bouyer  * however throughput drops by fifty percent if the file system
     92      1.1  bouyer  * is run at between 95% and 100% full; thus the minimum default
     93      1.1  bouyer  * value of fs_minfree is 5%. However, to get good clustering
     94      1.1  bouyer  * performance, 10% is a better choice. hence we use 10% as our
     95      1.1  bouyer  * default value. With 10% free space, fragmentation is not a
     96      1.1  bouyer  * problem, so we choose to optimize for time.
     97      1.1  bouyer  */
     98      1.1  bouyer #define MINFREE		5
     99      1.1  bouyer 
    100      1.1  bouyer /*
    101      1.1  bouyer  * Super block for an ext2fs file system.
    102      1.1  bouyer  */
    103      1.1  bouyer struct ext2fs {
    104      1.8  bouyer 	u_int32_t  e2fs_icount;		/* Inode count */
    105      1.8  bouyer 	u_int32_t  e2fs_bcount;		/* blocks count */
    106      1.8  bouyer 	u_int32_t  e2fs_rbcount;	/* reserved blocks count */
    107      1.8  bouyer 	u_int32_t  e2fs_fbcount;	/* free blocks count */
    108      1.8  bouyer 	u_int32_t  e2fs_ficount;	/* free inodes count */
    109      1.1  bouyer 	u_int32_t  e2fs_first_dblock;	/* first data block */
    110      1.8  bouyer 	u_int32_t  e2fs_log_bsize;	/* block size = 1024*(2^e2fs_log_bsize) */
    111      1.8  bouyer 	u_int32_t  e2fs_fsize;		/* fragment size */
    112      1.8  bouyer 	u_int32_t  e2fs_bpg;		/* blocks per group */
    113      1.8  bouyer 	u_int32_t  e2fs_fpg;		/* frags per group */
    114      1.8  bouyer 	u_int32_t  e2fs_ipg;		/* inodes per group */
    115      1.8  bouyer 	u_int32_t  e2fs_mtime;		/* mount time */
    116      1.8  bouyer 	u_int32_t  e2fs_wtime;		/* write time */
    117      1.8  bouyer 	u_int16_t  e2fs_mnt_count;	/* mount count */
    118      1.1  bouyer 	u_int16_t  e2fs_max_mnt_count;	/* max mount count */
    119      1.8  bouyer 	u_int16_t  e2fs_magic;		/* magic number */
    120      1.8  bouyer 	u_int16_t  e2fs_state;		/* file system state */
    121      1.8  bouyer 	u_int16_t  e2fs_beh;		/* behavior on errors */
    122      1.1  bouyer 	u_int16_t  reserved;
    123      1.8  bouyer 	u_int32_t  e2fs_lastfsck;	/* time of last fsck */
    124      1.8  bouyer 	u_int32_t  e2fs_fsckintv;	/* max time between fscks */
    125      1.8  bouyer 	u_int32_t  e2fs_creator;	/* creator OS */
    126      1.8  bouyer 	u_int32_t  e2fs_rev;		/* revision level */
    127      1.8  bouyer 	u_int16_t  e2fs_ruid;		/* default uid for reserved blocks */
    128      1.8  bouyer 	u_int16_t  e2fs_rgid;		/* default gid for reserved blocks */
    129      1.8  bouyer 	/* EXT2_DYNAMIC_REV superblocks */
    130      1.8  bouyer 	u_int32_t  e2fs_first_ino;	/* first non-reserved inode */
    131      1.8  bouyer 	u_int16_t  e2fs_inode_size;	/* size of inode structure */
    132      1.8  bouyer 	u_int16_t  e2fs_block_group_nr;	/* block grp number of this sblk*/
    133      1.8  bouyer 	u_int32_t  e2fs_features_compat; /*  OK to mount if unknown */
    134      1.8  bouyer 	u_int32_t  e2fs_features_incompat; /* not OK to mount if unknown */
    135  1.8.2.1      he 	u_int32_t  e2fs_features_rocompat; /* OK to mount ro if unknown */
    136      1.8  bouyer 	u_int32_t  reserved2[230];
    137      1.1  bouyer };
    138      1.1  bouyer 
    139      1.1  bouyer 
    140      1.1  bouyer /* in-memory data for ext2fs */
    141      1.1  bouyer struct m_ext2fs {
    142      1.1  bouyer 	struct ext2fs e2fs;
    143      1.1  bouyer 	u_char	e2fs_fsmnt[MAXMNTLEN];	/* name mounted on */
    144      1.8  bouyer 	int8_t	e2fs_ronly;	/* mounted read-only flag */
    145      1.8  bouyer 	int8_t	e2fs_fmod;	/* super block modified flag */
    146      1.8  bouyer 	int32_t	e2fs_bsize;	/* block size */
    147      1.8  bouyer 	int32_t e2fs_bshift;	/* ``lblkno'' calc of logical blkno */
    148      1.8  bouyer 	int32_t e2fs_bmask;	/* ``blkoff'' calc of blk offsets */
    149      1.8  bouyer 	int64_t e2fs_qbmask;	/* ~fs_bmask - for use with quad size */
    150      1.8  bouyer 	int32_t	e2fs_fsbtodb;	/* fsbtodb and dbtofsb shift constant */
    151      1.8  bouyer 	int32_t	e2fs_ncg;	/* number of cylinder groups */
    152      1.8  bouyer 	int32_t	e2fs_ngdb;	/* number of group descriptor block */
    153      1.8  bouyer 	int32_t	e2fs_ipb;	/* number of inodes per block */
    154      1.8  bouyer 	int32_t	e2fs_itpg;	/* number of inode table per group */
    155      1.8  bouyer 	struct	ext2_gd *e2fs_gd; /* group descripors */
    156      1.1  bouyer };
    157      1.1  bouyer 
    158      1.1  bouyer 
    159      1.1  bouyer 
    160      1.1  bouyer /*
    161      1.1  bouyer  * Filesystem identification
    162      1.1  bouyer  */
    163      1.1  bouyer #define	E2FS_MAGIC	0xef53	/* the ext2fs magic number */
    164  1.8.2.1      he #define E2FS_REV0	0	/* revision level */
    165  1.8.2.1      he #define E2FS_REV1	1	/* revision level */
    166  1.8.2.1      he 
    167  1.8.2.1      he /* compatible/imcompatible features */
    168  1.8.2.1      he #define EXT2F_COMPAT_PREALLOC		0x0001
    169  1.8.2.1      he 
    170  1.8.2.1      he #define EXT2F_ROCOMPAT_SPARSESUPER	0x0001
    171  1.8.2.1      he #define EXT2F_ROCOMPAT_LARGEFILE	0x0002
    172  1.8.2.1      he #define EXT2F_ROCOMPAT_BTREE_DIR	0x0004
    173  1.8.2.1      he 
    174  1.8.2.1      he #define EXT2F_INCOMPAT_COMP		0x0001
    175  1.8.2.1      he #define EXT2F_INCOMPAT_FTYPE		0x0002
    176  1.8.2.1      he 
    177  1.8.2.1      he /* features supported in this implementation */
    178  1.8.2.1      he #define EXT2F_COMPAT_SUPP		0x0000
    179  1.8.2.1      he #define EXT2F_ROCOMPAT_SUPP		0x0000
    180  1.8.2.1      he #define EXT2F_INCOMPAT_SUPP		EXT2F_INCOMPAT_FTYPE
    181      1.1  bouyer 
    182      1.1  bouyer /*
    183      1.1  bouyer  * OS identification
    184      1.1  bouyer  */
    185      1.1  bouyer #define E2FS_OS_LINUX 0
    186      1.1  bouyer #define E2FS_OS_HURD  1
    187      1.1  bouyer #define E2FS_OS_MASIX 2
    188      1.1  bouyer 
    189      1.1  bouyer /*
    190      1.1  bouyer  * Filesystem clean flags
    191      1.1  bouyer  */
    192      1.1  bouyer #define	E2FS_ISCLEAN	0x01
    193      1.1  bouyer #define	E2FS_ERRORS		0x02
    194      1.1  bouyer 
    195      1.1  bouyer /* ext2 file system block group descriptor */
    196      1.1  bouyer 
    197      1.1  bouyer struct ext2_gd {
    198      1.1  bouyer 	u_int32_t ext2bgd_b_bitmap;	/* blocks bitmap block */
    199      1.1  bouyer 	u_int32_t ext2bgd_i_bitmap;	/* inodes bitmap block */
    200      1.1  bouyer 	u_int32_t ext2bgd_i_tables;	/* inodes table block  */
    201      1.1  bouyer 	u_int16_t ext2bgd_nbfree;	/* number of free blocks */
    202      1.1  bouyer 	u_int16_t ext2bgd_nifree;	/* number of free inodes */
    203      1.1  bouyer 	u_int16_t ext2bgd_ndirs;	/* number of directories */
    204      1.1  bouyer 	u_int16_t reserved;
    205      1.1  bouyer 	u_int32_t reserved2[3];
    206      1.1  bouyer 
    207      1.1  bouyer };
    208      1.2  bouyer 
    209      1.2  bouyer /* EXT2FS metadatas are stored in little-endian byte order. These macros
    210      1.2  bouyer  * helps reading theses metadatas
    211      1.2  bouyer  */
    212      1.2  bouyer 
    213      1.2  bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    214      1.2  bouyer #	define h2fs16(x) (x)
    215      1.2  bouyer #	define h2fs32(x) (x)
    216      1.2  bouyer #	define fs2h16(x) (x)
    217      1.2  bouyer #	define fs2h32(x) (x)
    218      1.4   perry #	define e2fs_sbload(old, new) memcpy((new), (old), SBSIZE);
    219      1.4   perry #	define e2fs_cgload(old, new, size) memcpy((new), (old), (size));
    220      1.4   perry #	define e2fs_sbsave(old, new) memcpy((new), (old), SBSIZE);
    221      1.4   perry #	define e2fs_cgsave(old, new, size) memcpy((new), (old), (size));
    222      1.2  bouyer #else
    223      1.2  bouyer void e2fs_sb_bswap __P((struct ext2fs *, struct ext2fs *));
    224      1.2  bouyer void e2fs_cg_bswap __P((struct ext2_gd *, struct ext2_gd *, int));
    225      1.2  bouyer #	define h2fs16(x) bswap16(x)
    226      1.2  bouyer #	define h2fs32(x) bswap32(x)
    227      1.2  bouyer #	define fs2h16(x) bswap16(x)
    228      1.2  bouyer #	define fs2h32(x) bswap32(x)
    229      1.2  bouyer #	define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
    230      1.2  bouyer #	define e2fs_cgload(old, new, size) e2fs_cg_bswap((old), (new), (size));
    231      1.2  bouyer #	define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
    232      1.2  bouyer #	define e2fs_cgsave(old, new, size) e2fs_cg_bswap((old), (new), (size));
    233      1.2  bouyer #endif
    234      1.1  bouyer 
    235      1.1  bouyer /*
    236      1.1  bouyer  * Turn file system block numbers into disk block addresses.
    237      1.1  bouyer  * This maps file system blocks to device size blocks.
    238      1.1  bouyer  */
    239      1.1  bouyer #define fsbtodb(fs, b)	((b) << (fs)->e2fs_fsbtodb)
    240      1.1  bouyer #define dbtofsb(fs, b)	((b) >> (fs)->e2fs_fsbtodb)
    241      1.1  bouyer 
    242      1.1  bouyer /*
    243      1.1  bouyer  * Macros for handling inode numbers:
    244      1.1  bouyer  *	 inode number to file system block offset.
    245      1.1  bouyer  *	 inode number to cylinder group number.
    246      1.1  bouyer  *	 inode number to file system block address.
    247      1.1  bouyer  */
    248      1.1  bouyer #define	ino_to_cg(fs, x)	(((x) - 1) / (fs)->e2fs.e2fs_ipg)
    249      1.1  bouyer #define	ino_to_fsba(fs, x)						\
    250      1.1  bouyer 	((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables + \
    251      1.1  bouyer 	(((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
    252      1.1  bouyer #define	ino_to_fsbo(fs, x)	(((x)-1) % (fs)->e2fs_ipb)
    253      1.1  bouyer 
    254      1.1  bouyer /*
    255      1.1  bouyer  * Give cylinder group number for a file system block.
    256      1.1  bouyer  * Give cylinder group block number for a file system block.
    257      1.1  bouyer  */
    258      1.1  bouyer #define	dtog(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
    259      1.1  bouyer #define	dtogd(fs, d) \
    260      1.1  bouyer 	(((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
    261      1.1  bouyer 
    262      1.1  bouyer /*
    263      1.1  bouyer  * The following macros optimize certain frequently calculated
    264      1.1  bouyer  * quantities by using shifts and masks in place of divisions
    265      1.1  bouyer  * modulos and multiplications.
    266      1.1  bouyer  */
    267      1.1  bouyer #define blkoff(fs, loc)		/* calculates (loc % fs->e2fs_bsize) */ \
    268      1.1  bouyer 	((loc) & (fs)->e2fs_qbmask)
    269      1.1  bouyer #define lblktosize(fs, blk)	/* calculates (blk * fs->e2fs_bsize) */ \
    270      1.1  bouyer 	((blk) << (fs)->e2fs_bshift)
    271      1.1  bouyer #define lblkno(fs, loc)		/* calculates (loc / fs->e2fs_bsize) */ \
    272      1.1  bouyer 	((loc) >> (fs)->e2fs_bshift)
    273      1.1  bouyer #define blkroundup(fs, size)	/* calculates roundup(size, fs->e2fs_bsize) */ \
    274      1.1  bouyer 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
    275      1.1  bouyer #define fragroundup(fs, size)	/* calculates roundup(size, fs->e2fs_bsize) */ \
    276      1.1  bouyer 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
    277      1.1  bouyer /*
    278      1.1  bouyer  * Determine the number of available frags given a
    279      1.1  bouyer  * percentage to hold in reserve.
    280      1.1  bouyer  */
    281      1.1  bouyer #define freespace(fs) \
    282      1.1  bouyer    ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
    283      1.1  bouyer 
    284      1.1  bouyer /*
    285      1.1  bouyer  * Number of indirects in a file system block.
    286      1.1  bouyer  */
    287      1.1  bouyer #define	NINDIR(fs)	((fs)->e2fs_bsize / sizeof(u_int32_t))
    288