xref: /freebsd/sys/fs/ext2fs/ext2fs.h (revision a0ee8cc6)
1 /*-
2  *  modified for EXT2FS support in Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  *
7  * $FreeBSD$
8  */
9 /*-
10  * Copyright (c) 2009 Aditya Sarawgi
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *
35  */
36 
37 #ifndef _FS_EXT2FS_EXT2FS_H_
38 #define	_FS_EXT2FS_EXT2FS_H_
39 
40 #include <sys/types.h>
41 
42 /*
43  * Super block for an ext2fs file system.
44  */
45 struct ext2fs {
46 	uint32_t  e2fs_icount;		/* Inode count */
47 	uint32_t  e2fs_bcount;		/* blocks count */
48 	uint32_t  e2fs_rbcount;		/* reserved blocks count */
49 	uint32_t  e2fs_fbcount;		/* free blocks count */
50 	uint32_t  e2fs_ficount;		/* free inodes count */
51 	uint32_t  e2fs_first_dblock;	/* first data block */
52 	uint32_t  e2fs_log_bsize;	/* block size = 1024*(2^e2fs_log_bsize) */
53 	uint32_t  e2fs_log_fsize;	/* fragment size */
54 	uint32_t  e2fs_bpg;		/* blocks per group */
55 	uint32_t  e2fs_fpg;		/* frags per group */
56 	uint32_t  e2fs_ipg;		/* inodes per group */
57 	uint32_t  e2fs_mtime;		/* mount time */
58 	uint32_t  e2fs_wtime;		/* write time */
59 	uint16_t  e2fs_mnt_count;	/* mount count */
60 	uint16_t  e2fs_max_mnt_count;	/* max mount count */
61 	uint16_t  e2fs_magic;		/* magic number */
62 	uint16_t  e2fs_state;		/* file system state */
63 	uint16_t  e2fs_beh;		/* behavior on errors */
64 	uint16_t  e2fs_minrev;		/* minor revision level */
65 	uint32_t  e2fs_lastfsck;	/* time of last fsck */
66 	uint32_t  e2fs_fsckintv;	/* max time between fscks */
67 	uint32_t  e2fs_creator;		/* creator OS */
68 	uint32_t  e2fs_rev;		/* revision level */
69 	uint16_t  e2fs_ruid;		/* default uid for reserved blocks */
70 	uint16_t  e2fs_rgid;		/* default gid for reserved blocks */
71 	/* EXT2_DYNAMIC_REV superblocks */
72 	uint32_t  e2fs_first_ino;	/* first non-reserved inode */
73 	uint16_t  e2fs_inode_size;	/* size of inode structure */
74 	uint16_t  e2fs_block_group_nr;	/* block grp number of this sblk*/
75 	uint32_t  e2fs_features_compat; /*  compatible feature set */
76 	uint32_t  e2fs_features_incompat; /* incompatible feature set */
77 	uint32_t  e2fs_features_rocompat; /* RO-compatible feature set */
78 	uint8_t	  e2fs_uuid[16];	/* 128-bit uuid for volume */
79 	char      e2fs_vname[16];	/* volume name */
80 	char      e2fs_fsmnt[64];	/* name mounted on */
81 	uint32_t  e2fs_algo;		/* For compression */
82 	uint8_t   e2fs_prealloc;	/* # of blocks for old prealloc */
83 	uint8_t   e2fs_dir_prealloc;	/* # of blocks for old prealloc dirs */
84 	uint16_t  e2fs_reserved_ngdb;	/* # of reserved gd blocks for resize */
85 	char      e3fs_journal_uuid[16]; /* uuid of journal superblock */
86 	uint32_t  e3fs_journal_inum;	/* inode number of journal file */
87 	uint32_t  e3fs_journal_dev;	/* device number of journal file */
88 	uint32_t  e3fs_last_orphan;	/* start of list of inodes to delete */
89 	uint32_t  e3fs_hash_seed[4];	/* HTREE hash seed */
90 	char      e3fs_def_hash_version; /* Default hash version to use */
91 	char      e3fs_reserved_char_pad;
92 	uint32_t  e3fs_default_mount_opts;
93 	uint32_t  e3fs_first_meta_bg;	/* First metablock block group */
94 	uint32_t  e3fs_mkfs_time;      /* when the fs was created */
95 	uint32_t  e3fs_jnl_blks[17];   /* backup of the journal inode */
96 	uint32_t  e4fs_bcount_hi;      /* block count */
97 	uint32_t  e4fs_rbcount_hi;     /* reserved blocks count */
98 	uint32_t  e4fs_fbcount_hi;     /* free blocks count */
99 	uint16_t  e4fs_min_extra_isize;/* all inodes have at least some bytes */
100 	uint16_t  e4fs_want_extra_isize; /* inodes must reserve some bytes */
101 	uint32_t  e4fs_flags;	  /* miscellaneous flags */
102 	uint16_t  e4fs_raid_stride;    /* RAID stride */
103 	uint16_t  e4fs_mmpintv;	/* number of seconds to wait in MMP checking */
104 	uint64_t  e4fs_mmpblk;	 /* block for multi-mount protection */
105 	uint32_t  e4fs_raid_stripe_wid;/* blocks on all data disks (N * stride) */
106 	uint8_t   e4fs_log_gpf;	/* FLEX_BG group size */
107 	uint8_t   e4fs_char_pad2;
108 	uint16_t  e4fs_pad;
109 	uint32_t  reserved2[162];	/* Padding to the end of the block */
110 };
111 
112 /*
113  * The path name on which the file system is mounted is maintained
114  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
115  * the super block for this name.
116  */
117 #define	MAXMNTLEN 512
118 
119 /*
120  * In-Memory Superblock
121  */
122 
123 struct m_ext2fs {
124 	struct ext2fs * e2fs;
125 	char     e2fs_fsmnt[MAXMNTLEN];/* name mounted on */
126 	char     e2fs_ronly;	  /* mounted read-only flag */
127 	char     e2fs_fmod;	  /* super block modified flag */
128 	uint32_t e2fs_bsize;	  /* Block size */
129 	uint32_t e2fs_bshift;	  /* calc of logical block no */
130 	uint32_t e2fs_bpg;	  /* Number of blocks per group */
131 	int64_t  e2fs_qbmask;	  /* = s_blocksize -1 */
132 	uint32_t e2fs_fsbtodb;	  /* Shift to get disk block */
133 	uint32_t e2fs_ipg;	  /* Number of inodes per group */
134 	uint32_t e2fs_ipb;	  /* Number of inodes per block */
135 	uint32_t e2fs_itpg;	  /* Number of inode table per group */
136 	uint32_t e2fs_fsize;	  /* Size of fragments per block */
137 	uint32_t e2fs_fpb;	  /* Number of fragments per block */
138 	uint32_t e2fs_fpg;	  /* Number of fragments per group */
139 	uint32_t e2fs_gdbcount;	  /* Number of group descriptors */
140 	uint32_t e2fs_gcount;	  /* Number of groups */
141 	uint32_t e2fs_isize;	  /* Size of inode */
142 	uint32_t e2fs_total_dir;  /* Total number of directories */
143 	uint8_t	*e2fs_contigdirs; /* (u) # of contig. allocated dirs */
144 	char     e2fs_wasvalid;	  /* valid at mount time */
145 	off_t    e2fs_maxfilesize;
146 	struct   ext2_gd *e2fs_gd; /* Group Descriptors */
147 	int32_t  e2fs_contigsumsize;    /* size of cluster summary array */
148 	int32_t *e2fs_maxcluster;       /* max cluster in each cyl group */
149 	struct   csum *e2fs_clustersum; /* cluster summary in each cyl group */
150 };
151 
152 /* cluster summary information */
153 
154 struct csum {
155 	int8_t   cs_init; /* cluster summary has been initialized */
156 	int32_t *cs_sum;  /* cluster summary array */
157 };
158 
159 /*
160  * The second extended file system magic number
161  */
162 #define	E2FS_MAGIC		0xEF53
163 
164 /*
165  * Revision levels
166  */
167 #define	E2FS_REV0		0	/* The good old (original) format */
168 #define	E2FS_REV1		1	/* V2 format w/ dynamic inode sizes */
169 
170 #define	E2FS_REV0_INODE_SIZE 128
171 
172 /*
173  * compatible/incompatible features
174  */
175 #define	EXT2F_COMPAT_PREALLOC		0x0001
176 #define	EXT2F_COMPAT_HASJOURNAL		0x0004
177 #define	EXT2F_COMPAT_RESIZE		0x0010
178 #define	EXT2F_COMPAT_DIRHASHINDEX	0x0020
179 
180 #define	EXT2F_ROCOMPAT_SPARSESUPER	0x0001
181 #define	EXT2F_ROCOMPAT_LARGEFILE	0x0002
182 #define	EXT2F_ROCOMPAT_BTREE_DIR	0x0004
183 #define	EXT2F_ROCOMPAT_HUGE_FILE	0x0008
184 #define	EXT2F_ROCOMPAT_GDT_CSUM		0x0010
185 #define	EXT2F_ROCOMPAT_DIR_NLINK	0x0020
186 #define	EXT2F_ROCOMPAT_EXTRA_ISIZE	0x0040
187 
188 #define	EXT2F_INCOMPAT_COMP		0x0001
189 #define	EXT2F_INCOMPAT_FTYPE		0x0002
190 #define	EXT2F_INCOMPAT_RECOVER		0x0004
191 #define	EXT2F_INCOMPAT_META_BG		0x0010
192 #define	EXT2F_INCOMPAT_EXTENTS		0x0040
193 #define	EXT2F_INCOMPAT_64BIT		0x0080
194 #define	EXT2F_INCOMPAT_MMP		0x0100
195 #define	EXT2F_INCOMPAT_FLEX_BG		0x0200
196 
197 /*
198  * Features supported in this implementation
199  *
200  * We support the following REV1 features:
201  * - EXT2F_ROCOMPAT_SPARSESUPER
202  * - EXT2F_ROCOMPAT_LARGEFILE
203  * - EXT2F_ROCOMPAT_EXTRA_ISIZE
204  * - EXT2F_INCOMPAT_FTYPE
205  *
206  * We partially (read-only) support the following EXT4 features:
207  * - EXT2F_ROCOMPAT_HUGE_FILE
208  * - EXT2F_INCOMPAT_EXTENTS
209  *
210  * We do not support these EXT4 features but they are irrelevant
211  * for read-only support:
212  * - EXT2F_INCOMPAT_RECOVER
213  * - EXT2F_INCOMPAT_FLEX_BG
214  * - EXT2F_INCOMPAT_META_BG
215  */
216 #define	EXT2F_ROCOMPAT_SUPP		(EXT2F_ROCOMPAT_SPARSESUPER | \
217 					 EXT2F_ROCOMPAT_LARGEFILE | \
218 					 EXT2F_ROCOMPAT_EXTRA_ISIZE)
219 #define	EXT2F_INCOMPAT_SUPP		EXT2F_INCOMPAT_FTYPE
220 #define	EXT4F_RO_INCOMPAT_SUPP		(EXT2F_INCOMPAT_EXTENTS | \
221 					 EXT2F_INCOMPAT_RECOVER | \
222 					 EXT2F_INCOMPAT_FLEX_BG | \
223 					 EXT2F_INCOMPAT_META_BG )
224 
225 /* Assume that user mode programs are passing in an ext2fs superblock, not
226  * a kernel struct super_block.  This will allow us to call the feature-test
227  * macros from user land. */
228 #define	EXT2_SB(sb)	(sb)
229 
230 /*
231  * Feature set definitions
232  */
233 #define	EXT2_HAS_COMPAT_FEATURE(sb,mask)			\
234 	( EXT2_SB(sb)->e2fs->e2fs_features_compat & htole32(mask) )
235 #define	EXT2_HAS_RO_COMPAT_FEATURE(sb,mask)			\
236 	( EXT2_SB(sb)->e2fs->e2fs_features_rocompat & htole32(mask) )
237 #define	EXT2_HAS_INCOMPAT_FEATURE(sb,mask)			\
238 	( EXT2_SB(sb)->e2fs->e2fs_features_incompat & htole32(mask) )
239 
240 /*
241  * File clean flags
242  */
243 #define	E2FS_ISCLEAN			0x0001	/* Unmounted cleanly */
244 #define	E2FS_ERRORS			0x0002	/* Errors detected */
245 
246 /* ext2 file system block group descriptor */
247 
248 struct ext2_gd {
249 	uint32_t ext2bgd_b_bitmap;	/* blocks bitmap block */
250 	uint32_t ext2bgd_i_bitmap;	/* inodes bitmap block */
251 	uint32_t ext2bgd_i_tables;	/* inodes table block  */
252 	uint16_t ext2bgd_nbfree;	/* number of free blocks */
253 	uint16_t ext2bgd_nifree;	/* number of free inodes */
254 	uint16_t ext2bgd_ndirs;		/* number of directories */
255 	uint16_t ext4bgd_flags;		/* block group flags */
256 	uint32_t ext4bgd_x_bitmap;	/* snapshot exclusion bitmap loc. */
257 	uint16_t ext4bgd_b_bmap_csum;	/* block bitmap checksum */
258 	uint16_t ext4bgd_i_bmap_csum;	/* inode bitmap checksum */
259 	uint16_t ext4bgd_i_unused;	/* unused inode count */
260 	uint16_t ext4bgd_csum;		/* group descriptor checksum */
261 };
262 
263 
264 /* EXT2FS metadatas are stored in little-endian byte order. These macros
265  * helps reading these metadatas
266  */
267 
268 #define	e2fs_cgload(old, new, size) memcpy((new), (old), (size));
269 #define	e2fs_cgsave(old, new, size) memcpy((new), (old), (size));
270 
271 /*
272  * Macro-instructions used to manage several block sizes
273  */
274 #define	EXT2_MAX_BLOCK_SIZE		4096
275 #define	EXT2_MIN_BLOCK_LOG_SIZE		  10
276 #define	EXT2_BLOCK_SIZE(s)		((s)->e2fs_bsize)
277 #define	EXT2_ADDR_PER_BLOCK(s)		(EXT2_BLOCK_SIZE(s) / sizeof(uint32_t))
278 #define	EXT2_INODE_SIZE(s)		(EXT2_SB(s)->e2fs_isize)
279 
280 /*
281  * Macro-instructions used to manage fragments
282  */
283 #define	EXT2_MIN_FRAG_SIZE		1024
284 #define	EXT2_MAX_FRAG_SIZE		4096
285 #define	EXT2_MIN_FRAG_LOG_SIZE		  10
286 #define	EXT2_FRAG_SIZE(s)		(EXT2_SB(s)->e2fs_fsize)
287 #define	EXT2_FRAGS_PER_BLOCK(s)		(EXT2_SB(s)->e2fs_fpb)
288 
289 /*
290  * Macro-instructions used to manage group descriptors
291  */
292 #define	EXT2_BLOCKS_PER_GROUP(s)	(EXT2_SB(s)->e2fs_bpg)
293 
294 #endif	/* !_FS_EXT2FS_EXT2FS_H_ */
295