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