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