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