xref: /freebsd/sys/fs/ext2fs/ext2fs.h (revision aa0a1e58f0189b0fde359a8bda032887e72057fa)
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_EXT2_FS_H_
38 #define _FS_EXT2FS_EXT2_FS_H_
39 
40 #include <sys/types.h>
41 
42 /*
43  * Maximal count of links to a file
44  */
45 #define EXT2_LINK_MAX		32000
46 
47 /*
48  * Constants relative to the data blocks
49  */
50 #define	EXT2_NDIR_BLOCKS		12
51 #define	EXT2_IND_BLOCK			EXT2_NDIR_BLOCKS
52 #define	EXT2_DIND_BLOCK			(EXT2_IND_BLOCK + 1)
53 #define	EXT2_TIND_BLOCK			(EXT2_DIND_BLOCK + 1)
54 #define	EXT2_N_BLOCKS			(EXT2_TIND_BLOCK + 1)
55 #define EXT2_MAXSYMLINKLEN		(EXT2_N_BLOCKS * sizeof (uint32_t))
56 
57 /*
58  * The path name on which the file system is mounted is maintained
59  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
60  * the super block for this name.
61  */
62 #define MAXMNTLEN 512
63 
64 /*
65  * Super block for an ext2fs file system.
66  */
67 struct ext2fs {
68 	u_int32_t  e2fs_icount;		/* Inode count */
69 	u_int32_t  e2fs_bcount;		/* blocks count */
70 	u_int32_t  e2fs_rbcount;	/* reserved blocks count */
71 	u_int32_t  e2fs_fbcount;	/* free blocks count */
72 	u_int32_t  e2fs_ficount;	/* free inodes count */
73 	u_int32_t  e2fs_first_dblock;	/* first data block */
74 	u_int32_t  e2fs_log_bsize;	/* block size = 1024*(2^e2fs_log_bsize) */
75 	u_int32_t  e2fs_log_fsize;	/* fragment size */
76 	u_int32_t  e2fs_bpg;		/* blocks per group */
77 	u_int32_t  e2fs_fpg;		/* frags per group */
78 	u_int32_t  e2fs_ipg;		/* inodes per group */
79 	u_int32_t  e2fs_mtime;		/* mount time */
80 	u_int32_t  e2fs_wtime;		/* write time */
81 	u_int16_t  e2fs_mnt_count;	/* mount count */
82 	u_int16_t  e2fs_max_mnt_count;	/* max mount count */
83 	u_int16_t  e2fs_magic;		/* magic number */
84 	u_int16_t  e2fs_state;		/* file system state */
85 	u_int16_t  e2fs_beh;		/* behavior on errors */
86 	u_int16_t  e2fs_minrev;		/* minor revision level */
87 	u_int32_t  e2fs_lastfsck;	/* time of last fsck */
88 	u_int32_t  e2fs_fsckintv;	/* max time between fscks */
89 	u_int32_t  e2fs_creator;	/* creator OS */
90 	u_int32_t  e2fs_rev;		/* revision level */
91 	u_int16_t  e2fs_ruid;		/* default uid for reserved blocks */
92 	u_int16_t  e2fs_rgid;		/* default gid for reserved blocks */
93 	/* EXT2_DYNAMIC_REV superblocks */
94 	u_int32_t  e2fs_first_ino;	/* first non-reserved inode */
95 	u_int16_t  e2fs_inode_size;	/* size of inode structure */
96 	u_int16_t  e2fs_block_group_nr;	/* block grp number of this sblk*/
97 	u_int32_t  e2fs_features_compat; /*  compatible feature set */
98 	u_int32_t  e2fs_features_incompat; /* incompatible feature set */
99 	u_int32_t  e2fs_features_rocompat; /* RO-compatible feature set */
100 	u_int8_t   e2fs_uuid[16];	/* 128-bit uuid for volume */
101 	char       e2fs_vname[16];	/* volume name */
102 	char       e2fs_fsmnt[64]; 	/* name mounted on */
103 	u_int32_t  e2fs_algo;		/* For comcate for dir */
104 	u_int16_t  e2fs_reserved_ngdb; /* # of reserved gd blocks for resize */
105 	u_int32_t  reserved2[204];
106 };
107 
108 
109 /*
110  * In-Memory Superblock
111  */
112 
113 struct m_ext2fs {
114 	struct ext2fs * e2fs;
115 	char e2fs_fsmnt[MAXMNTLEN];/* name mounted on */
116 	char e2fs_ronly;          /* mounted read-only flag */
117 	char e2fs_fmod;           /* super block modified flag */
118 	uint32_t e2fs_bsize;      /* Block size */
119 	uint32_t e2fs_bshift;     /* calc of logical block no */
120 	int32_t e2fs_bmask;       /* calc of block offset */
121 	int32_t e2fs_bpg;         /* Number of blocks per group */
122 	int64_t e2fs_qbmask;      /* = s_blocksize -1 */
123 	uint32_t e2fs_fsbtodb;    /* Shift to get disk block */
124 	uint32_t e2fs_ipg;        /* Number of inodes per group */
125 	uint32_t e2fs_ipb;        /* Number of inodes per block */
126 	uint32_t e2fs_itpg;       /* Number of inode table per group */
127 	uint32_t e2fs_fsize;      /* Size of fragments per block */
128 	uint32_t e2fs_fpb;        /* Number of fragments per block */
129 	uint32_t e2fs_fpg;        /* Number of fragments per group */
130 	uint32_t e2fs_dbpg;       /* Number of descriptor blocks per group */
131 	uint32_t e2fs_descpb;     /* Number of group descriptors per block */
132 	uint32_t e2fs_gdbcount;   /* Number of group descriptors */
133 	uint32_t e2fs_gcount;     /* Number of groups */
134 	uint32_t e2fs_first_inode;/* First inode on fs */
135 	int32_t  e2fs_isize;      /* Size of inode */
136 	uint32_t e2fs_mount_opt;
137 	uint32_t e2fs_blocksize_bits;
138 	uint32_t e2fs_total_dir;  /* Total number of directories */
139 	uint8_t	*e2fs_contigdirs; /* (u) # of contig. allocated dirs */
140 	char e2fs_wasvalid;       /* valid at mount time */
141 	off_t e2fs_maxfilesize;
142 	struct ext2_gd *e2fs_gd; /* Group Descriptors */
143 };
144 
145 /*
146  * The second extended file system version
147  */
148 #define E2FS_DATE		"95/08/09"
149 #define E2FS_VERSION		"0.5b"
150 
151 /*
152  * The second extended file system magic number
153  */
154 #define E2FS_MAGIC		0xEF53
155 
156 /*
157  * Revision levels
158  */
159 #define E2FS_REV0		0	/* The good old (original) format */
160 #define E2FS_REV1		1 	/* V2 format w/ dynamic inode sizes */
161 
162 #define E2FS_CURRENT_REV	E2FS_REV0
163 #define E2FS_MAX_SUPP_REV	E2FS_REV1
164 
165 #define E2FS_REV0_INODE_SIZE 128
166 
167 /*
168  * compatible/incompatible features
169  */
170 #define EXT2F_COMPAT_PREALLOC		0x0001
171 #define EXT2F_COMPAT_HASJOURNAL		0x0004
172 #define EXT2F_COMPAT_RESIZE		0x0010
173 
174 #define EXT2F_ROCOMPAT_SPARSESUPER	0x0001
175 #define EXT2F_ROCOMPAT_LARGEFILE	0x0002
176 #define EXT2F_ROCOMPAT_BTREE_DIR	0x0004
177 
178 #define EXT2F_INCOMPAT_COMP		0x0001
179 #define EXT2F_INCOMPAT_FTYPE		0x0002
180 
181 /*
182  * Features supported in this implementation
183  *
184  * We support the following REV1 features:
185  * - EXT2F_ROCOMPAT_SPARSESUPER
186  * - EXT2F_ROCOMPAT_LARGEFILE
187  * - EXT2F_INCOMPAT_FTYPE
188  */
189 #define EXT2F_COMPAT_SUPP		0x0000
190 #define EXT2F_ROCOMPAT_SUPP		(EXT2F_ROCOMPAT_SPARSESUPER \
191 					 | EXT2F_ROCOMPAT_LARGEFILE)
192 #define EXT2F_INCOMPAT_SUPP		EXT2F_INCOMPAT_FTYPE
193 
194 /* Assume that user mode programs are passing in an ext2fs superblock, not
195  * a kernel struct super_block.  This will allow us to call the feature-test
196  * macros from user land. */
197 #define EXT2_SB(sb)	(sb)
198 
199 /*
200  * Feature set definitions
201  */
202 #define EXT2_HAS_COMPAT_FEATURE(sb,mask)			\
203 	( EXT2_SB(sb)->e2fs->e2fs_features_compat & htole32(mask) )
204 #define EXT2_HAS_RO_COMPAT_FEATURE(sb,mask)			\
205 	( EXT2_SB(sb)->e2fs->e2fs_features_rocompat & htole32(mask) )
206 #define EXT2_HAS_INCOMPAT_FEATURE(sb,mask)			\
207 	( EXT2_SB(sb)->e2fs->e2fs_features_incompat & htole32(mask) )
208 
209 /*
210  * Definitions of behavior on errors
211  */
212 #define E2FS_BEH_CONTINUE		1	/* continue operation */
213 #define E2FS_BEH_READONLY		2	/* remount fs read only */
214 #define E2FS_BEH_PANIC			3	/* cause panic */
215 #define E2FS_BEH_DEFAULT		E2FS_BEH_CONTINUE
216 
217 /*
218  * OS identification
219  */
220 #define E2FS_OS_LINUX		0
221 #define E2FS_OS_HURD		1
222 #define E2FS_OS_MASIX		2
223 #define E2FS_OS_FREEBSD		3
224 #define E2FS_OS_LITES		4
225 
226 /*
227  * File clean flags
228  */
229 #define	E2FS_ISCLEAN			0x0001	/* Unmounted cleanly */
230 #define	E2FS_ERRORS			0x0002	/* Errors detected */
231 
232 /* ext2 file system block group descriptor */
233 
234 struct ext2_gd {
235 	u_int32_t ext2bgd_b_bitmap;	/* blocks bitmap block */
236 	u_int32_t ext2bgd_i_bitmap;	/* inodes bitmap block */
237 	u_int32_t ext2bgd_i_tables;	/* inodes table block  */
238 	u_int16_t ext2bgd_nbfree;	/* number of free blocks */
239 	u_int16_t ext2bgd_nifree;	/* number of free inodes */
240 	u_int16_t ext2bgd_ndirs;	/* number of directories */
241 	u_int16_t reserved;
242 	u_int32_t reserved2[3];
243 };
244 
245 /* EXT2FS metadatas are stored in little-endian byte order. These macros
246  * helps reading these metadatas
247  */
248 
249 #define e2fs_cgload(old, new, size) memcpy((new), (old), (size));
250 #define e2fs_cgsave(old, new, size) memcpy((new), (old), (size));
251 
252 /*
253  * Macro-instructions used to manage several block sizes
254  */
255 #define EXT2_MIN_BLOCK_SIZE		1024
256 #define	EXT2_MAX_BLOCK_SIZE		4096
257 #define EXT2_MIN_BLOCK_LOG_SIZE		  10
258 #if defined(_KERNEL)
259 # define EXT2_BLOCK_SIZE(s)		((s)->e2fs_bsize)
260 #else
261 # define EXT2_BLOCK_SIZE(s)		(EXT2_MIN_BLOCK_SIZE << (s)->e2fs_log_bsize)
262 #endif
263 #define	EXT2_ADDR_PER_BLOCK(s)		(EXT2_BLOCK_SIZE(s) / sizeof (uint32_t))
264 #if defined(_KERNEL)
265 # define EXT2_BLOCK_SIZE_BITS(s)	((s)->e2fs_blocksize_bits)
266 #else
267 # define EXT2_BLOCK_SIZE_BITS(s)	((s)->e2fs_log_bsize + 10)
268 #endif
269 #if defined(_KERNEL)
270 #define	EXT2_ADDR_PER_BLOCK_BITS(s)	(EXT2_SB(s)->s_addr_per_block_bits)
271 #define EXT2_INODE_SIZE(s)		(EXT2_SB(s)->e2fs_isize)
272 #define EXT2_FIRST_INO(s)		(EXT2_SB(s)->e2fs_first_inode)
273 #else
274 #define EXT2_INODE_SIZE(s)	(((s)->s_rev_level == E2FS_REV0) ? \
275 				 E2FS_REV0 : (s)->s_inode_size)
276 #define EXT2_FIRST_INO(s)	(((s)->s_rev_level == E2FS_REV0) ? \
277 				 E2FS_REV0 : (s)->e2fs_first_ino)
278 #endif
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 #if defined(_KERNEL)
287 # define EXT2_FRAG_SIZE(s)		(EXT2_SB(s)->e2fs_fsize)
288 # define EXT2_FRAGS_PER_BLOCK(s)	(EXT2_SB(s)->e2fs_fpb)
289 #else
290 # define EXT2_FRAG_SIZE(s)		(EXT2_MIN_FRAG_SIZE << (s)->e2fs_log_fsize)
291 # define EXT2_FRAGS_PER_BLOCK(s)	(EXT2_BLOCK_SIZE(s) / EXT2_FRAG_SIZE(s))
292 #endif
293 
294 /*
295  * Macro-instructions used to manage group descriptors
296  */
297 #if defined(_KERNEL)
298 # define EXT2_BLOCKS_PER_GROUP(s)	(EXT2_SB(s)->e2fs_bpg)
299 # define EXT2_DESC_PER_BLOCK(s)		(EXT2_SB(s)->e2fs_descpb)
300 # define EXT2_DESC_PER_BLOCK_BITS(s)	(EXT2_SB(s)->s_desc_per_block_bits)
301 #else
302 # define EXT2_BLOCKS_PER_GROUP(s)	((s)->e2fs_bpg)
303 # define EXT2_DESC_PER_BLOCK(s)		(EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_gd))
304 
305 #endif
306 
307 #endif	/* !_FS_EXT2FS_EXT2FS_H_ */
308