ext2fs.h revision 1.3 1 /* $NetBSD: ext2fs.h,v 1.3 1998/03/01 02:23:45 fvdl 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 /*
41 * Each disk drive contains some number of file systems.
42 * A file system consists of a number of cylinder groups.
43 * Each cylinder group has inodes and data.
44 *
45 * A file system is described by its super-block, which in turn
46 * describes the cylinder groups. The super-block is critical
47 * data and is replicated in each cylinder group to protect against
48 * catastrophic loss. This is done at `newfs' time and the critical
49 * super-block data does not change, so the copies need not be
50 * referenced further unless disaster strikes.
51 *
52 * The first boot and super blocks are given in absolute disk addresses.
53 * The byte-offset forms are preferred, as they don't imply a sector size.
54 */
55 #define BBSIZE 1024
56 #define SBSIZE 1024
57 #define BBOFF ((off_t)(0))
58 #define SBOFF ((off_t)(BBOFF + BBSIZE))
59 #define BBLOCK ((ufs_daddr_t)(0))
60 #define SBLOCK ((ufs_daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
61
62 /*
63 * Addresses stored in inodes are capable of addressing blocks
64 * XXX
65 */
66
67 /*
68 * MINBSIZE is the smallest allowable block size.
69 * MINBSIZE must be big enough to hold a cylinder group block,
70 * thus changes to (struct cg) must keep its size within MINBSIZE.
71 * Note that super blocks are always of size SBSIZE,
72 * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
73 */
74 #define LOG_MINBSIZE 10
75 #define MINBSIZE (1 << LOG_MINBSIZE)
76
77 /*
78 * The path name on which the file system is mounted is maintained
79 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
80 * the super block for this name.
81 */
82 #define MAXMNTLEN 512
83
84 /*
85 * MINFREE gives the minimum acceptable percentage of file system
86 * blocks which may be free. If the freelist drops below this level
87 * only the superuser may continue to allocate blocks. This may
88 * be set to 0 if no reserve of free blocks is deemed necessary,
89 * however throughput drops by fifty percent if the file system
90 * is run at between 95% and 100% full; thus the minimum default
91 * value of fs_minfree is 5%. However, to get good clustering
92 * performance, 10% is a better choice. hence we use 10% as our
93 * default value. With 10% free space, fragmentation is not a
94 * problem, so we choose to optimize for time.
95 */
96 #define MINFREE 5
97
98 /*
99 * Super block for an ext2fs file system.
100 */
101 struct ext2fs {
102 u_int32_t e2fs_icount; /* Inode count */
103 u_int32_t e2fs_bcount; /* blocks count */
104 u_int32_t e2fs_rbcount; /* reserved blocks count */
105 u_int32_t e2fs_fbcount; /* free blocks count */
106 u_int32_t e2fs_ficount; /* free inodes count */
107 u_int32_t e2fs_first_dblock; /* first data block */
108 u_int32_t e2fs_log_bsize; /* block size = 1024*(2^e2fs_log_bsize) */
109 u_int32_t e2fs_fsize; /* fragment size */
110 u_int32_t e2fs_bpg; /* blocks per group */
111 u_int32_t e2fs_fpg; /* frags per group */
112 u_int32_t e2fs_ipg; /* inodes per group */
113 u_int32_t e2fs_mtime; /* mount time */
114 u_int32_t e2fs_wtime; /* write time */
115 u_int16_t e2fs_mnt_count; /* mount count */
116 u_int16_t e2fs_max_mnt_count; /* max mount count */
117 u_int16_t e2fs_magic; /* magic number */
118 u_int16_t e2fs_state; /* file system state */
119 u_int16_t e2fs_beh; /* behavior on errors */
120 u_int16_t reserved;
121 u_int32_t e2fs_lastfsck; /* time of last fsck */
122 u_int32_t e2fs_fsckintv; /* max time between fscks */
123 u_int32_t e2fs_creator; /* creator OS */
124 u_int32_t e2fs_rev; /* revision level */
125 u_int16_t e2fs_ruid; /* default uid for reserved blocks */
126 u_int16_t e2fs_rgid; /* default gid for reserved blocks */
127 u_int32_t reserved2[235];
128 };
129
130
131 /* in-memory data for ext2fs */
132 struct m_ext2fs {
133 struct ext2fs e2fs;
134 u_char e2fs_fsmnt[MAXMNTLEN]; /* name mounted on */
135 int8_t e2fs_ronly; /* mounted read-only flag */
136 int8_t e2fs_fmod; /* super block modified flag */
137 int32_t e2fs_bsize; /* block size */
138 int32_t e2fs_bshift; /* ``lblkno'' calc of logical blkno */
139 int32_t e2fs_bmask; /* ``blkoff'' calc of blk offsets */
140 int64_t e2fs_qbmask; /* ~fs_bmask - for use with quad size */
141 int32_t e2fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
142 int32_t e2fs_ncg; /* number of cylinder groups */
143 int32_t e2fs_ngdb; /* number of group descriptor block */
144 int32_t e2fs_ipb; /* number of inodes per block */
145 int32_t e2fs_itpg; /* number of inode table per group */
146 struct ext2_gd *e2fs_gd; /* group descripors */
147 };
148
149
150
151 /*
152 * Filesystem identification
153 */
154 #define E2FS_MAGIC 0xef53 /* the ext2fs magic number */
155 #define E2FS_REV 0 /* revision level */
156
157 /*
158 * OS identification
159 */
160 #define E2FS_OS_LINUX 0
161 #define E2FS_OS_HURD 1
162 #define E2FS_OS_MASIX 2
163
164 /*
165 * Filesystem clean flags
166 */
167 #define E2FS_ISCLEAN 0x01
168 #define E2FS_ERRORS 0x02
169
170 /* ext2 file system block group descriptor */
171
172 struct ext2_gd {
173 u_int32_t ext2bgd_b_bitmap; /* blocks bitmap block */
174 u_int32_t ext2bgd_i_bitmap; /* inodes bitmap block */
175 u_int32_t ext2bgd_i_tables; /* inodes table block */
176 u_int16_t ext2bgd_nbfree; /* number of free blocks */
177 u_int16_t ext2bgd_nifree; /* number of free inodes */
178 u_int16_t ext2bgd_ndirs; /* number of directories */
179 u_int16_t reserved;
180 u_int32_t reserved2[3];
181
182 };
183
184 /* EXT2FS metadatas are stored in little-endian byte order. These macros
185 * helps reading theses metadatas
186 */
187
188 #if BYTE_ORDER == LITTLE_ENDIAN
189 # define h2fs16(x) (x)
190 # define h2fs32(x) (x)
191 # define fs2h16(x) (x)
192 # define fs2h32(x) (x)
193 # define e2fs_sbload(old, new) bcopy((old), (new), SBSIZE);
194 # define e2fs_cgload(old, new, size) bcopy((old), (new), (size));
195 # define e2fs_sbsave(old, new) bcopy((old), (new), SBSIZE);
196 # define e2fs_cgsave(old, new, size) bcopy((old), (new), (size));
197 #else
198 void e2fs_sb_bswap __P((struct ext2fs *, struct ext2fs *));
199 void e2fs_cg_bswap __P((struct ext2_gd *, struct ext2_gd *, int));
200 # define h2fs16(x) bswap16(x)
201 # define h2fs32(x) bswap32(x)
202 # define fs2h16(x) bswap16(x)
203 # define fs2h32(x) bswap32(x)
204 # define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
205 # define e2fs_cgload(old, new, size) e2fs_cg_bswap((old), (new), (size));
206 # define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
207 # define e2fs_cgsave(old, new, size) e2fs_cg_bswap((old), (new), (size));
208 #endif
209
210 /*
211 * Turn file system block numbers into disk block addresses.
212 * This maps file system blocks to device size blocks.
213 */
214 #define fsbtodb(fs, b) ((b) << (fs)->e2fs_fsbtodb)
215 #define dbtofsb(fs, b) ((b) >> (fs)->e2fs_fsbtodb)
216
217 /*
218 * Macros for handling inode numbers:
219 * inode number to file system block offset.
220 * inode number to cylinder group number.
221 * inode number to file system block address.
222 */
223 #define ino_to_cg(fs, x) (((x) - 1) / (fs)->e2fs.e2fs_ipg)
224 #define ino_to_fsba(fs, x) \
225 ((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables + \
226 (((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
227 #define ino_to_fsbo(fs, x) (((x)-1) % (fs)->e2fs_ipb)
228
229 /*
230 * Give cylinder group number for a file system block.
231 * Give cylinder group block number for a file system block.
232 */
233 #define dtog(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
234 #define dtogd(fs, d) \
235 (((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
236
237 /*
238 * The following macros optimize certain frequently calculated
239 * quantities by using shifts and masks in place of divisions
240 * modulos and multiplications.
241 */
242 #define blkoff(fs, loc) /* calculates (loc % fs->e2fs_bsize) */ \
243 ((loc) & (fs)->e2fs_qbmask)
244 #define lblktosize(fs, blk) /* calculates (blk * fs->e2fs_bsize) */ \
245 ((blk) << (fs)->e2fs_bshift)
246 #define lblkno(fs, loc) /* calculates (loc / fs->e2fs_bsize) */ \
247 ((loc) >> (fs)->e2fs_bshift)
248 #define blkroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
249 (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
250 #define fragroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
251 (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
252 /*
253 * Determine the number of available frags given a
254 * percentage to hold in reserve.
255 */
256 #define freespace(fs) \
257 ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
258
259 /*
260 * Number of indirects in a file system block.
261 */
262 #define NINDIR(fs) ((fs)->e2fs_bsize / sizeof(u_int32_t))
263