ext2fs.h revision 1.9 1 /* $NetBSD: ext2fs.h,v 1.9 2000/01/26 16:21:33 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1997 Manuel Bouyer.
5 * Copyright (c) 1982, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)fs.h 8.10 (Berkeley) 10/27/94
37 * Modified for ext2fs by Manuel Bouyer.
38 */
39
40 #include <machine/bswap.h>
41
42 /*
43 * Each disk drive contains some number of file systems.
44 * A file system consists of a number of cylinder groups.
45 * Each cylinder group has inodes and data.
46 *
47 * A file system is described by its super-block, which in turn
48 * describes the cylinder groups. The super-block is critical
49 * data and is replicated in each cylinder group to protect against
50 * catastrophic loss. This is done at `newfs' time and the critical
51 * super-block data does not change, so the copies need not be
52 * referenced further unless disaster strikes.
53 *
54 * The first boot and super blocks are given in absolute disk addresses.
55 * The byte-offset forms are preferred, as they don't imply a sector size.
56 */
57 #define BBSIZE 1024
58 #define SBSIZE 1024
59 #define BBOFF ((off_t)(0))
60 #define SBOFF ((off_t)(BBOFF + BBSIZE))
61 #define BBLOCK ((ufs_daddr_t)(0))
62 #define SBLOCK ((ufs_daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
63
64 /*
65 * Addresses stored in inodes are capable of addressing blocks
66 * XXX
67 */
68
69 /*
70 * MINBSIZE is the smallest allowable block size.
71 * MINBSIZE must be big enough to hold a cylinder group block,
72 * thus changes to (struct cg) must keep its size within MINBSIZE.
73 * Note that super blocks are always of size SBSIZE,
74 * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
75 */
76 #define LOG_MINBSIZE 10
77 #define MINBSIZE (1 << LOG_MINBSIZE)
78
79 /*
80 * The path name on which the file system is mounted is maintained
81 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
82 * the super block for this name.
83 */
84 #define MAXMNTLEN 512
85
86 /*
87 * MINFREE gives the minimum acceptable percentage of file system
88 * blocks which may be free. If the freelist drops below this level
89 * only the superuser may continue to allocate blocks. This may
90 * be set to 0 if no reserve of free blocks is deemed necessary,
91 * however throughput drops by fifty percent if the file system
92 * is run at between 95% and 100% full; thus the minimum default
93 * value of fs_minfree is 5%. However, to get good clustering
94 * performance, 10% is a better choice. hence we use 10% as our
95 * default value. With 10% free space, fragmentation is not a
96 * problem, so we choose to optimize for time.
97 */
98 #define MINFREE 5
99
100 /*
101 * Super block for an ext2fs file system.
102 */
103 struct ext2fs {
104 u_int32_t e2fs_icount; /* Inode count */
105 u_int32_t e2fs_bcount; /* blocks count */
106 u_int32_t e2fs_rbcount; /* reserved blocks count */
107 u_int32_t e2fs_fbcount; /* free blocks count */
108 u_int32_t e2fs_ficount; /* free inodes count */
109 u_int32_t e2fs_first_dblock; /* first data block */
110 u_int32_t e2fs_log_bsize; /* block size = 1024*(2^e2fs_log_bsize) */
111 u_int32_t e2fs_fsize; /* fragment size */
112 u_int32_t e2fs_bpg; /* blocks per group */
113 u_int32_t e2fs_fpg; /* frags per group */
114 u_int32_t e2fs_ipg; /* inodes per group */
115 u_int32_t e2fs_mtime; /* mount time */
116 u_int32_t e2fs_wtime; /* write time */
117 u_int16_t e2fs_mnt_count; /* mount count */
118 u_int16_t e2fs_max_mnt_count; /* max mount count */
119 u_int16_t e2fs_magic; /* magic number */
120 u_int16_t e2fs_state; /* file system state */
121 u_int16_t e2fs_beh; /* behavior on errors */
122 u_int16_t e2fs_minrev; /* minor revision level */
123 u_int32_t e2fs_lastfsck; /* time of last fsck */
124 u_int32_t e2fs_fsckintv; /* max time between fscks */
125 u_int32_t e2fs_creator; /* creator OS */
126 u_int32_t e2fs_rev; /* revision level */
127 u_int16_t e2fs_ruid; /* default uid for reserved blocks */
128 u_int16_t e2fs_rgid; /* default gid for reserved blocks */
129 /* EXT2_DYNAMIC_REV superblocks */
130 u_int32_t e2fs_first_ino; /* first non-reserved inode */
131 u_int16_t e2fs_inode_size; /* size of inode structure */
132 u_int16_t e2fs_block_group_nr; /* block grp number of this sblk*/
133 u_int32_t e2fs_features_compat; /* compatible feature set */
134 u_int32_t e2fs_features_incompat; /* incompatible feature set */
135 u_int32_t e2fs_features_rocompat; /* RO-compatible feature set */
136 u_int8_t e2fs_uuid[16]; /* 128-bit uuid for volume */
137 char e2fs_vname[16]; /* volume name */
138 char e2fs_fsmnt[64]; /* name mounted on */
139 u_int32_t e2fs_algo; /* For compression */
140 u_int8_t e2fs_prealloc; /* # of blocks to preallocate */
141 u_int8_t e2fs_dir_prealloc; /* # of blocks to preallocate for dir */
142 u_int16_t pad1;
143 u_int32_t reserved2[204];
144 };
145
146
147 /* in-memory data for ext2fs */
148 struct m_ext2fs {
149 struct ext2fs e2fs;
150 u_char e2fs_fsmnt[MAXMNTLEN]; /* name mounted on */
151 int8_t e2fs_ronly; /* mounted read-only flag */
152 int8_t e2fs_fmod; /* super block modified flag */
153 int32_t e2fs_bsize; /* block size */
154 int32_t e2fs_bshift; /* ``lblkno'' calc of logical blkno */
155 int32_t e2fs_bmask; /* ``blkoff'' calc of blk offsets */
156 int64_t e2fs_qbmask; /* ~fs_bmask - for use with quad size */
157 int32_t e2fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
158 int32_t e2fs_ncg; /* number of cylinder groups */
159 int32_t e2fs_ngdb; /* number of group descriptor block */
160 int32_t e2fs_ipb; /* number of inodes per block */
161 int32_t e2fs_itpg; /* number of inode table per group */
162 struct ext2_gd *e2fs_gd; /* group descripors */
163 };
164
165
166
167 /*
168 * Filesystem identification
169 */
170 #define E2FS_MAGIC 0xef53 /* the ext2fs magic number */
171 #define E2FS_REV0 0 /* revision levels */
172 #define E2FS_REV1 1 /* revision levels */
173
174 /* compatible/imcompatible features */
175 #define EXT2F_COMPAT_PREALLOC 0x0001
176
177 #define EXT2F_ROCOMPAT_SPARSESUPER 0x0001
178 #define EXT2F_ROCOMPAT_LARGEFILE 0x0002
179 #define EXT2F_ROCOMPAT_BTREE_DIR 0x0004
180
181 #define EXT2F_INCOMPAT_COMP 0x0001
182 #define EXT2F_INCOMPAT_FTYPE 0x0002
183
184 /* features supported in this implementation */
185 #define EXT2F_COMPAT_SUPP 0x0000
186 #define EXT2F_ROCOMPAT_SUPP 0x0000
187 #define EXT2F_INCOMPAT_SUPP EXT2F_INCOMPAT_FTYPE
188
189 /*
190 * OS identification
191 */
192 #define E2FS_OS_LINUX 0
193 #define E2FS_OS_HURD 1
194 #define E2FS_OS_MASIX 2
195
196 /*
197 * Filesystem clean flags
198 */
199 #define E2FS_ISCLEAN 0x01
200 #define E2FS_ERRORS 0x02
201
202 /* ext2 file system block group descriptor */
203
204 struct ext2_gd {
205 u_int32_t ext2bgd_b_bitmap; /* blocks bitmap block */
206 u_int32_t ext2bgd_i_bitmap; /* inodes bitmap block */
207 u_int32_t ext2bgd_i_tables; /* inodes table block */
208 u_int16_t ext2bgd_nbfree; /* number of free blocks */
209 u_int16_t ext2bgd_nifree; /* number of free inodes */
210 u_int16_t ext2bgd_ndirs; /* number of directories */
211 u_int16_t reserved;
212 u_int32_t reserved2[3];
213
214 };
215
216 /* EXT2FS metadatas are stored in little-endian byte order. These macros
217 * helps reading theses metadatas
218 */
219
220 #if BYTE_ORDER == LITTLE_ENDIAN
221 # define h2fs16(x) (x)
222 # define h2fs32(x) (x)
223 # define fs2h16(x) (x)
224 # define fs2h32(x) (x)
225 # define e2fs_sbload(old, new) memcpy((new), (old), SBSIZE);
226 # define e2fs_cgload(old, new, size) memcpy((new), (old), (size));
227 # define e2fs_sbsave(old, new) memcpy((new), (old), SBSIZE);
228 # define e2fs_cgsave(old, new, size) memcpy((new), (old), (size));
229 #else
230 void e2fs_sb_bswap __P((struct ext2fs *, struct ext2fs *));
231 void e2fs_cg_bswap __P((struct ext2_gd *, struct ext2_gd *, int));
232 # define h2fs16(x) bswap16(x)
233 # define h2fs32(x) bswap32(x)
234 # define fs2h16(x) bswap16(x)
235 # define fs2h32(x) bswap32(x)
236 # define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
237 # define e2fs_cgload(old, new, size) e2fs_cg_bswap((old), (new), (size));
238 # define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
239 # define e2fs_cgsave(old, new, size) e2fs_cg_bswap((old), (new), (size));
240 #endif
241
242 /*
243 * Turn file system block numbers into disk block addresses.
244 * This maps file system blocks to device size blocks.
245 */
246 #define fsbtodb(fs, b) ((b) << (fs)->e2fs_fsbtodb)
247 #define dbtofsb(fs, b) ((b) >> (fs)->e2fs_fsbtodb)
248
249 /*
250 * Macros for handling inode numbers:
251 * inode number to file system block offset.
252 * inode number to cylinder group number.
253 * inode number to file system block address.
254 */
255 #define ino_to_cg(fs, x) (((x) - 1) / (fs)->e2fs.e2fs_ipg)
256 #define ino_to_fsba(fs, x) \
257 ((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables + \
258 (((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
259 #define ino_to_fsbo(fs, x) (((x)-1) % (fs)->e2fs_ipb)
260
261 /*
262 * Give cylinder group number for a file system block.
263 * Give cylinder group block number for a file system block.
264 */
265 #define dtog(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
266 #define dtogd(fs, d) \
267 (((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
268
269 /*
270 * The following macros optimize certain frequently calculated
271 * quantities by using shifts and masks in place of divisions
272 * modulos and multiplications.
273 */
274 #define blkoff(fs, loc) /* calculates (loc % fs->e2fs_bsize) */ \
275 ((loc) & (fs)->e2fs_qbmask)
276 #define lblktosize(fs, blk) /* calculates (blk * fs->e2fs_bsize) */ \
277 ((blk) << (fs)->e2fs_bshift)
278 #define lblkno(fs, loc) /* calculates (loc / fs->e2fs_bsize) */ \
279 ((loc) >> (fs)->e2fs_bshift)
280 #define blkroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
281 (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
282 #define fragroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
283 (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
284 /*
285 * Determine the number of available frags given a
286 * percentage to hold in reserve.
287 */
288 #define freespace(fs) \
289 ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
290
291 /*
292 * Number of indirects in a file system block.
293 */
294 #define NINDIR(fs) ((fs)->e2fs_bsize / sizeof(u_int32_t))
295