1 /* $NetBSD: ext2fs.h,v 1.36 2013/06/23 07:28:37 dholland Exp $ */
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)fs.h 8.10 (Berkeley) 10/27/94
32 * Modified for ext2fs by Manuel Bouyer.
36 * Copyright (c) 1997 Manuel Bouyer.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 * @(#)fs.h 8.10 (Berkeley) 10/27/94
59 * Modified for ext2fs by Manuel Bouyer.
62 #ifndef _UFS_EXT2FS_EXT2FS_H_
63 #define _UFS_EXT2FS_EXT2FS_H_
65 #include <sys/bswap.h>
68 * Each disk drive contains some number of file systems.
69 * A file system consists of a number of cylinder groups.
70 * Each cylinder group has inodes and data.
72 * A file system is described by its super-block, which in turn
73 * describes the cylinder groups. The super-block is critical
74 * data and is replicated in each cylinder group to protect against
75 * catastrophic loss. This is done at `newfs' time and the critical
76 * super-block data does not change, so the copies need not be
77 * referenced further unless disaster strikes.
79 * The first boot and super blocks are given in absolute disk addresses.
80 * The byte-offset forms are preferred, as they don't imply a sector size.
84 #define BBOFF ((off_t)(0))
85 #define SBOFF ((off_t)(BBOFF + BBSIZE))
86 #define BBLOCK ((daddr_t)(0))
87 #define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
90 * Addresses stored in inodes are capable of addressing blocks
95 * MINBSIZE is the smallest allowable block size.
96 * MINBSIZE must be big enough to hold a cylinder group block,
97 * thus changes to (struct cg) must keep its size within MINBSIZE.
98 * Note that super blocks are always of size SBSIZE,
99 * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
101 #define LOG_MINBSIZE 10
102 #define MINBSIZE (1 << LOG_MINBSIZE)
105 * The path name on which the file system is mounted is maintained
106 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
107 * the super block for this name.
109 #define MAXMNTLEN 512
112 * MINFREE gives the minimum acceptable percentage of file system
113 * blocks which may be free. If the freelist drops below this level
114 * only the superuser may continue to allocate blocks. This may
115 * be set to 0 if no reserve of free blocks is deemed necessary,
116 * however throughput drops by fifty percent if the file system
117 * is run at between 95% and 100% full; thus the minimum default
118 * value of fs_minfree is 5%. However, to get good clustering
119 * performance, 10% is a better choice. hence we use 10% as our
120 * default value. With 10% free space, fragmentation is not a
121 * problem, so we choose to optimize for time.
126 * Super block for an ext2fs file system.
129 uint32_t e2fs_icount
; /* Inode count */
130 uint32_t e2fs_bcount
; /* blocks count */
131 uint32_t e2fs_rbcount
; /* reserved blocks count */
132 uint32_t e2fs_fbcount
; /* free blocks count */
133 uint32_t e2fs_ficount
; /* free inodes count */
134 uint32_t e2fs_first_dblock
; /* first data block */
135 uint32_t e2fs_log_bsize
; /* block size = 1024*(2^e2fs_log_bsize) */
136 uint32_t e2fs_fsize
; /* fragment size */
137 uint32_t e2fs_bpg
; /* blocks per group */
138 uint32_t e2fs_fpg
; /* frags per group */
139 uint32_t e2fs_ipg
; /* inodes per group */
140 uint32_t e2fs_mtime
; /* mount time */
141 uint32_t e2fs_wtime
; /* write time */
142 uint16_t e2fs_mnt_count
; /* mount count */
143 uint16_t e2fs_max_mnt_count
; /* max mount count */
144 uint16_t e2fs_magic
; /* magic number */
145 uint16_t e2fs_state
; /* file system state */
146 uint16_t e2fs_beh
; /* behavior on errors */
147 uint16_t e2fs_minrev
; /* minor revision level */
148 uint32_t e2fs_lastfsck
; /* time of last fsck */
149 uint32_t e2fs_fsckintv
; /* max time between fscks */
150 uint32_t e2fs_creator
; /* creator OS */
151 uint32_t e2fs_rev
; /* revision level */
152 uint16_t e2fs_ruid
; /* default uid for reserved blocks */
153 uint16_t e2fs_rgid
; /* default gid for reserved blocks */
154 /* EXT2_DYNAMIC_REV superblocks */
155 uint32_t e2fs_first_ino
; /* first non-reserved inode */
156 uint16_t e2fs_inode_size
; /* size of inode structure */
157 uint16_t e2fs_block_group_nr
; /* block grp number of this sblk*/
158 uint32_t e2fs_features_compat
; /* compatible feature set */
159 uint32_t e2fs_features_incompat
; /* incompatible feature set */
160 uint32_t e2fs_features_rocompat
; /* RO-compatible feature set */
161 uint8_t e2fs_uuid
[16]; /* 128-bit uuid for volume */
162 char e2fs_vname
[16]; /* volume name */
163 char e2fs_fsmnt
[64]; /* name mounted on */
164 uint32_t e2fs_algo
; /* For compression */
165 uint8_t e2fs_prealloc
; /* # of blocks to preallocate */
166 uint8_t e2fs_dir_prealloc
; /* # of blocks to preallocate for dir */
167 uint16_t e2fs_reserved_ngdb
; /* # of reserved gd blocks for resize */
168 uint32_t reserved2
[204];
172 /* in-memory data for ext2fs */
175 u_char e2fs_fsmnt
[MAXMNTLEN
]; /* name mounted on */
176 int8_t e2fs_ronly
; /* mounted read-only flag */
177 int8_t e2fs_fmod
; /* super block modified flag */
178 int32_t e2fs_bsize
; /* block size */
179 int32_t e2fs_bshift
; /* ``lblkno'' calc of logical blkno */
180 int32_t e2fs_bmask
; /* ``blkoff'' calc of blk offsets */
181 int64_t e2fs_qbmask
; /* ~fs_bmask - for use with quad size */
182 int32_t e2fs_fsbtodb
; /* fsbtodb and dbtofsb shift constant */
183 int32_t e2fs_ncg
; /* number of cylinder groups */
184 int32_t e2fs_ngdb
; /* number of group descriptor block */
185 int32_t e2fs_ipb
; /* number of inodes per block */
186 int32_t e2fs_itpg
; /* number of inode table per group */
187 struct ext2_gd
*e2fs_gd
; /* group descripors */
193 * Filesystem identification
195 #define E2FS_MAGIC 0xef53 /* the ext2fs magic number */
196 #define E2FS_REV0 0 /* GOOD_OLD revision */
197 #define E2FS_REV1 1 /* Support compat/incompat features */
199 /* compatible/incompatible features */
200 #define EXT2F_COMPAT_PREALLOC 0x0001
201 #define EXT2F_COMPAT_AFS 0x0002
202 #define EXT2F_COMPAT_HASJOURNAL 0x0004
203 #define EXT2F_COMPAT_EXTATTR 0x0008
204 #define EXT2F_COMPAT_RESIZE 0x0010
205 #define EXT2F_COMPAT_DIRHASHINDEX 0x0020
206 #define EXT2F_COMPAT_BITS \
208 "\06COMPAT_DIRHASHINDEX" \
210 "\04COMPAT_EXTATTR" \
211 "\03COMPAT_HASJOURNAL" \
215 #define EXT2F_ROCOMPAT_SPARSESUPER 0x0001
216 #define EXT2F_ROCOMPAT_LARGEFILE 0x0002
217 #define EXT2F_ROCOMPAT_BTREE_DIR 0x0004
218 #define EXT2F_ROCOMPAT_HUGE_FILE 0x0008
219 #define EXT2F_ROCOMPAT_GDT_CSUM 0x0010
220 #define EXT2F_ROCOMPAT_DIR_NLINK 0x0020
221 #define EXT2F_ROCOMPAT_EXTRA_ISIZE 0x0040
222 #define EXT2F_ROCOMPAT_BITS \
224 "\07ROCOMPAT_EXTRA_ISIZE" \
225 "\06ROCOMPAT_DIR_NLINK" \
226 "\05ROCOMPAT_GDT_CSUM" \
227 "\04ROCOMPAT_HUGE_FILE" \
228 "\03ROCOMPAT_BTREE_DIR" \
229 "\02ROCOMPAT_LARGEFILE" \
230 "\01ROCOMPAT_SPARSESUPER"
232 #define EXT2F_INCOMPAT_COMP 0x0001
233 #define EXT2F_INCOMPAT_FTYPE 0x0002
234 #define EXT2F_INCOMPAT_REPLAY_JOURNAL 0x0004
235 #define EXT2F_INCOMPAT_USES_JOURNAL 0x0008
236 #define EXT2F_INCOMPAT_META_BG 0x0010
237 #define EXT2F_INCOMPAT_EXTENTS 0x0040
238 #define EXT2F_INCOMPAT_64BIT 0x0080
239 #define EXT2F_INCOMPAT_MMP 0x0100
240 #define EXT2F_INCOMPAT_FLEX_BG 0x0200
241 #define EXT2F_INCOMPAT_BITS \
243 "\012INCOMPAT_FLEX_BG" \
245 "\010INCOMPAT_64BIT" \
246 "\07INCOMPAT_EXTENTS" \
247 "\05INCOMPAT_META_BG" \
248 "\04INCOMPAT_USES_JOURNAL" \
249 "\03INCOMPAT_REPLAY_JOURNAL" \
250 "\02INCOMPAT_FTYPE" \
254 * Features supported in this implementation
256 * We support the following REV1 features:
257 * - EXT2F_ROCOMPAT_SPARSESUPER
258 * superblock backups stored only in cg_has_sb(bno) groups
259 * - EXT2F_ROCOMPAT_LARGEFILE
260 * use e2di_dacl in struct ext2fs_dinode to store
261 * upper 32bit of size for >2GB files
262 * - EXT2F_INCOMPAT_FTYPE
263 * store file type to e2d_type in struct ext2fs_direct
264 * (on REV0 e2d_namlen is uint16_t and no e2d_type, like ffs)
266 #define EXT2F_COMPAT_SUPP 0x0000
267 #define EXT2F_ROCOMPAT_SUPP (EXT2F_ROCOMPAT_SPARSESUPER \
268 | EXT2F_ROCOMPAT_LARGEFILE \
269 | EXT2F_ROCOMPAT_HUGE_FILE)
270 #define EXT2F_INCOMPAT_SUPP EXT2F_INCOMPAT_FTYPE
273 * Definitions of behavior on errors
275 #define E2FS_BEH_CONTINUE 1 /* continue operation */
276 #define E2FS_BEH_READONLY 2 /* remount fs read only */
277 #define E2FS_BEH_PANIC 3 /* cause panic */
278 #define E2FS_BEH_DEFAULT E2FS_BEH_CONTINUE
283 #define E2FS_OS_LINUX 0
284 #define E2FS_OS_HURD 1
285 #define E2FS_OS_MASIX 2
286 #define E2FS_OS_FREEBSD 3
287 #define E2FS_OS_LITES 4
290 * Filesystem clean flags
292 #define E2FS_ISCLEAN 0x01
293 #define E2FS_ERRORS 0x02
295 /* ext2 file system block group descriptor */
298 uint32_t ext2bgd_b_bitmap
; /* blocks bitmap block */
299 uint32_t ext2bgd_i_bitmap
; /* inodes bitmap block */
300 uint32_t ext2bgd_i_tables
; /* inodes table block */
301 uint16_t ext2bgd_nbfree
; /* number of free blocks */
302 uint16_t ext2bgd_nifree
; /* number of free inodes */
303 uint16_t ext2bgd_ndirs
; /* number of directories */
305 uint32_t reserved2
[3];
310 * If the EXT2F_ROCOMPAT_SPARSESUPER flag is set, the cylinder group has a
311 * copy of the super and cylinder group descriptors blocks only if it's
312 * 1, a power of 3, 5 or 7
315 static __inline
int cg_has_sb(int) __unused
;
321 if (i
== 0 || i
== 1)
323 for (a3
= 3, a5
= 5, a7
= 7;
324 a3
<= i
|| a5
<= i
|| a7
<= i
;
325 a3
*= 3, a5
*= 5, a7
*= 7)
326 if (i
== a3
|| i
== a5
|| i
== a7
)
331 /* EXT2FS metadatas are stored in little-endian byte order. These macros
332 * helps reading theses metadatas
335 #if BYTE_ORDER == LITTLE_ENDIAN
336 # define h2fs16(x) (x)
337 # define h2fs32(x) (x)
338 # define h2fs64(x) (x)
339 # define fs2h16(x) (x)
340 # define fs2h32(x) (x)
341 # define fs2h64(x) (x)
342 # define e2fs_sbload(old, new) memcpy((new), (old), SBSIZE);
343 # define e2fs_cgload(old, new, size) memcpy((new), (old), (size));
344 # define e2fs_sbsave(old, new) memcpy((new), (old), SBSIZE);
345 # define e2fs_cgsave(old, new, size) memcpy((new), (old), (size));
347 void e2fs_sb_bswap(struct ext2fs
*, struct ext2fs
*);
348 void e2fs_cg_bswap(struct ext2_gd
*, struct ext2_gd
*, int);
349 # define h2fs16(x) bswap16(x)
350 # define h2fs32(x) bswap32(x)
351 # define h2fs64(x) bswap64(x)
352 # define fs2h16(x) bswap16(x)
353 # define fs2h32(x) bswap32(x)
354 # define fs2h64(x) bswap64(x)
355 # define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
356 # define e2fs_cgload(old, new, size) e2fs_cg_bswap((old), (new), (size));
357 # define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
358 # define e2fs_cgsave(old, new, size) e2fs_cg_bswap((old), (new), (size));
362 * Turn file system block numbers into disk block addresses.
363 * This maps file system blocks to device size blocks.
365 #define EXT2_FSBTODB(fs, b) ((b) << (fs)->e2fs_fsbtodb)
366 #define EXT2_DBTOFSB(fs, b) ((b) >> (fs)->e2fs_fsbtodb)
369 * Macros for handling inode numbers:
370 * inode number to file system block offset.
371 * inode number to cylinder group number.
372 * inode number to file system block address.
374 #define ino_to_cg(fs, x) (((x) - 1) / (fs)->e2fs.e2fs_ipg)
375 #define ino_to_fsba(fs, x) \
376 ((fs)->e2fs_gd[ino_to_cg((fs), (x))].ext2bgd_i_tables + \
377 (((x) - 1) % (fs)->e2fs.e2fs_ipg) / (fs)->e2fs_ipb)
378 #define ino_to_fsbo(fs, x) (((x) - 1) % (fs)->e2fs_ipb)
381 * Give cylinder group number for a file system block.
382 * Give cylinder group block number for a file system block.
384 #define dtog(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
385 #define dtogd(fs, d) \
386 (((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
389 * The following macros optimize certain frequently calculated
390 * quantities by using shifts and masks in place of divisions
391 * modulos and multiplications.
393 #define ext2_blkoff(fs, loc) /* calculates (loc % fs->e2fs_bsize) */ \
394 ((loc) & (fs)->e2fs_qbmask)
395 #define ext2_lblktosize(fs, blk) /* calculates (blk * fs->e2fs_bsize) */ \
396 ((blk) << (fs)->e2fs_bshift)
397 #define ext2_lblkno(fs, loc) /* calculates (loc / fs->e2fs_bsize) */ \
398 ((loc) >> (fs)->e2fs_bshift)
399 #define ext2_blkroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
400 (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
401 #define ext2_fragroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
402 (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
404 * Determine the number of available frags given a
405 * percentage to hold in reserve.
407 #define freespace(fs) \
408 ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
411 * Number of indirects in a file system block.
413 #define EXT2_NINDIR(fs) ((fs)->e2fs_bsize / sizeof(uint32_t))
415 #endif /* !_UFS_EXT2FS_EXT2FS_H_ */