xref: /openbsd/sys/ufs/ext2fs/ext2fs.h (revision d570f06c)
1 /*	$OpenBSD: ext2fs.h,v 1.27 2024/07/15 13:27:36 martijn Exp $	*/
2 /*	$NetBSD: ext2fs.h,v 1.10 2000/01/28 16:00:23 bouyer Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1982, 1986, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)fs.h	8.10 (Berkeley) 10/27/94
34  *  Modified for ext2fs by Manuel Bouyer.
35  */
36 
37 #include <sys/endian.h>
38 
39 /*
40  * Each disk drive contains some number of file systems.
41  * A file system consists of a number of cylinder groups.
42  * Each cylinder group has inodes and data.
43  *
44  * A file system is described by its super-block, which in turn
45  * describes the cylinder groups.  The super-block is critical
46  * data and is replicated in each cylinder group to protect against
47  * catastrophic loss.  This is done at `newfs' time and the critical
48  * super-block data does not change, so the copies need not be
49  * referenced further unless disaster strikes.
50  *
51  * The first boot and super blocks are given in absolute disk addresses.
52  * The byte-offset forms are preferred, as they don't imply a sector size.
53  */
54 #define BBSIZE		1024
55 #define SBSIZE		1024
56 #define	BBOFF		((off_t)(0))
57 #define	SBOFF		((off_t)(BBOFF + BBSIZE))
58 #define	BBLOCK		((daddr_t)(0))
59 #define	SBLOCK		((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
60 
61 /*
62  * Inodes are, like in UFS, 32-bit unsigned integers and therefore ufsino_t.
63  * Disk blocks are 32-bit, if the filesystem isn't operating in 64-bit mode
64  * (the incompatible ext4 64BIT flag).  More work is needed to properly use
65  * daddr_t as the disk block data type on both BE and LE architectures.
66  * XXX disk blocks are simply u_int32_t for now.
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  * FSIZE means fragment size.
76  */
77 #define LOG_MINBSIZE	10
78 #define MINBSIZE	(1 << LOG_MINBSIZE)
79 #define LOG_MINFSIZE	10
80 #define MINFSIZE	(1 << LOG_MINFSIZE)
81 
82 /*
83  * The path name on which the file system is mounted is maintained
84  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
85  * the super block for this name.
86  */
87 #define MAXMNTLEN	512
88 
89 /*
90  * MINFREE gives the minimum acceptable percentage of file system
91  * blocks which may be free. If the freelist drops below this level
92  * only the superuser may continue to allocate blocks. This may
93  * be set to 0 if no reserve of free blocks is deemed necessary,
94  * however throughput drops by fifty percent if the file system
95  * is run at between 95% and 100% full; thus the minimum default
96  * value of fs_minfree is 5%. However, to get good clustering
97  * performance, 10% is a better choice. hence we use 10% as our
98  * default value. With 10% free space, fragmentation is not a
99  * problem, so we choose to optimize for time.
100  */
101 #define MINFREE		5
102 
103 /*
104  * Super block for an ext2fs file system.
105  */
106 struct ext2fs {
107 	u_int32_t  e2fs_icount;		/* Inode count */
108 	u_int32_t  e2fs_bcount;		/* blocks count */
109 	u_int32_t  e2fs_rbcount;	/* reserved blocks count */
110 	u_int32_t  e2fs_fbcount;	/* free blocks count */
111 	u_int32_t  e2fs_ficount;	/* free inodes count */
112 	u_int32_t  e2fs_first_dblock;	/* first data block */
113 	u_int32_t  e2fs_log_bsize;	/* block size = 1024*(2^e2fs_log_bsize) */
114 	u_int32_t  e2fs_log_fsize;	/* fragment size log2 */
115 	u_int32_t  e2fs_bpg;		/* blocks per group */
116 	u_int32_t  e2fs_fpg;		/* frags per group */
117 	u_int32_t  e2fs_ipg;		/* inodes per group */
118 	u_int32_t  e2fs_mtime;		/* mount time */
119 	u_int32_t  e2fs_wtime;		/* write time */
120 	u_int16_t  e2fs_mnt_count;	/* mount count */
121 	u_int16_t  e2fs_max_mnt_count;	/* max mount count */
122 	u_int16_t  e2fs_magic;		/* magic number */
123 	u_int16_t  e2fs_state;		/* file system state */
124 	u_int16_t  e2fs_beh;		/* behavior on errors */
125 	u_int16_t  e2fs_minrev;		/* minor revision level */
126 	u_int32_t  e2fs_lastfsck;	/* time of last fsck */
127 	u_int32_t  e2fs_fsckintv;	/* max time between fscks */
128 	u_int32_t  e2fs_creator;	/* creator OS */
129 	u_int32_t  e2fs_rev;		/* revision level */
130 	u_int16_t  e2fs_ruid;		/* default uid for reserved blocks */
131 	u_int16_t  e2fs_rgid;		/* default gid for reserved blocks */
132 	/* EXT2_DYNAMIC_REV superblocks */
133 	u_int32_t  e2fs_first_ino;	/* first non-reserved inode */
134 	u_int16_t  e2fs_inode_size;	/* size of inode structure */
135 	u_int16_t  e2fs_block_group_nr;	/* block grp number of this sblk*/
136 	u_int32_t  e2fs_features_compat; /*  compatible feature set */
137 	u_int32_t  e2fs_features_incompat; /* incompatible feature set */
138 	u_int32_t  e2fs_features_rocompat; /* RO-compatible feature set */
139 	u_int8_t   e2fs_uuid[16];	/* 128-bit uuid for volume */
140 	char       e2fs_vname[16];	/* volume name */
141 	char       e2fs_fsmnt[64];	/* name mounted on */
142 	u_int32_t  e2fs_algo;		/* For compression */
143 	u_int8_t   e2fs_prealloc;	/* # of blocks to preallocate */
144 	u_int8_t   e2fs_dir_prealloc;	/* # of blocks to preallocate for dir */
145 	u_int16_t  e2fs_reserved_ngdb;	/* # of reserved gd blocks for resize */
146 	/* Ext3 JBD2 journaling. */
147 	u_int8_t   e2fs_journal_uuid[16];
148 	u_int32_t  e2fs_journal_ino;
149 	u_int32_t  e2fs_journal_dev;
150 	u_int32_t  e2fs_last_orphan;	/* start of list of inodes to delete */
151 	u_int32_t  e2fs_hash_seed[4];	/* htree hash seed */
152 	u_int8_t   e2fs_def_hash_version;
153 	u_int8_t   e2fs_journal_backup_type;
154 	u_int16_t  e2fs_gdesc_size;
155 	u_int32_t  e2fs_default_mount_opts;
156 	u_int32_t  e2fs_first_meta_bg;
157 	u_int32_t  e2fs_mkfs_time;
158 	u_int32_t  e2fs_journal_backup[17];
159 	u_int32_t  e2fs_bcount_hi;	/* high bits of blocks count */
160 	u_int32_t  e2fs_rbcount_hi;	/* high bits of reserved blocks count */
161 	u_int32_t  e2fs_fbcount_hi;	/* high bits of free blocks count */
162 	u_int16_t  e2fs_min_extra_isize; /* all inodes have some bytes */
163 	u_int16_t  e2fs_want_extra_isize;/* inodes must reserve some bytes */
164 	u_int32_t  e2fs_flags;		/* miscellaneous flags */
165 	u_int16_t  e2fs_raid_stride;	/* RAID stride */
166 	u_int16_t  e2fs_mmpintv;		/* seconds to wait in MMP checking */
167 	u_int64_t  e2fs_mmpblk;		/* block for multi-mount protection */
168 	u_int32_t  e2fs_raid_stripe_wid; /* blocks on data disks (N * stride) */
169 	u_int8_t   e2fs_log_gpf;		/* FLEX_BG group size */
170 	u_int8_t   e2fs_chksum_type;	/* metadata checksum algorithm used */
171 	u_int8_t   e2fs_encrypt;		/* versioning level for encryption */
172 	u_int8_t   e2fs_reserved_pad;
173 	u_int64_t  e2fs_kbytes_written;	/* number of lifetime kilobytes */
174 	u_int32_t  e2fs_snapinum;	/* inode number of active snapshot */
175 	u_int32_t  e2fs_snapid;		/* sequential ID of active snapshot */
176 	u_int64_t  e2fs_snaprbcount;	/* rsvd blocks for active snapshot */
177 	u_int32_t  e2fs_snaplist;	/* inode number for on-disk snapshot */
178 	u_int32_t  e2fs_errcount;	/* number of file system errors */
179 	u_int32_t  e2fs_first_errtime;	/* first time an error happened */
180 	u_int32_t  e2fs_first_errino;	/* inode involved in first error */
181 	u_int64_t  e2fs_first_errblk;	/* block involved of first error */
182 	u_int8_t   e2fs_first_errfunc[32];/* function where error happened */
183 	u_int32_t  e2fs_first_errline;	/* line number where error happened */
184 	u_int32_t  e2fs_last_errtime;	/* most recent time of an error */
185 	u_int32_t  e2fs_last_errino;	/* inode involved in last error */
186 	u_int32_t  e2fs_last_errline;	/* line number where error happened */
187 	u_int64_t  e2fs_last_errblk;	/* block involved of last error */
188 	u_int8_t   e2fs_last_errfunc[32];/* function where error happened */
189 	u_int8_t   e2fs_mount_opts[64];
190 	u_int32_t  e2fs_usrquota_inum;	/* inode for tracking user quota */
191 	u_int32_t  e2fs_grpquota_inum;	/* inode for tracking group quota */
192 	u_int32_t  e2fs_overhead_clusters;/* overhead blocks/clusters */
193 	u_int32_t  e2fs_backup_bgs[2];	/* groups with sparse_super2 SBs */
194 	u_int8_t   e2fs_encrypt_algos[4];/* encryption algorithms in use */
195 	u_int8_t   e2fs_encrypt_pw_salt[16];/* salt used for string2key */
196 	u_int32_t  e2fs_lpf_ino;		/* location of the lost+found inode */
197 	u_int32_t  e2fs_proj_quota_inum;	/* inode for tracking project quota */
198 	u_int32_t  e2fs_chksum_seed;	/* checksum seed */
199 	u_int32_t  e2fs_reserved[98];	/* padding to the end of the block */
200 	u_int32_t  e2fs_sbchksum;	/* superblock checksum */
201 };
202 
203 
204 /* in-memory data for ext2fs */
205 struct m_ext2fs {
206 	struct ext2fs e2fs;
207 	u_char	e2fs_fsmnt[MAXMNTLEN];	/* name mounted on */
208 	int8_t	e2fs_ronly;	/* mounted read-only flag */
209 	int8_t	e2fs_fmod;	/* super block modified flag */
210 	int32_t e2fs_fsize;	/* fragment size */
211 	int32_t	e2fs_bsize;	/* block size */
212 	int32_t e2fs_bshift;	/* ``lblkno'' calc of logical blkno */
213 	int32_t e2fs_bmask;	/* ``blkoff'' calc of blk offsets */
214 	int64_t e2fs_qbmask;	/* ~fs_bmask - for use with quad size */
215 	int32_t	e2fs_fsbtodb;	/* fsbtodb and dbtofsb shift constant */
216 	int32_t	e2fs_ncg;	/* number of cylinder groups */
217 	int32_t	e2fs_ngdb;	/* number of group descriptor block */
218 	int32_t	e2fs_ipb;	/* number of inodes per block */
219 	int32_t	e2fs_itpg;	/* number of inode table per group */
220 	off_t	e2fs_maxfilesize;	/* depends on LARGE/HUGE flags */
221 	struct	ext2_gd *e2fs_gd; /* group descriptors */
222 };
223 
224 static inline int
e2fs_overflow(struct m_ext2fs * fs,off_t lower,off_t value)225 e2fs_overflow(struct m_ext2fs *fs, off_t lower, off_t value)
226 {
227 	return (value < lower || value > fs->e2fs_maxfilesize);
228 }
229 
230 /*
231  * Filesystem identification
232  */
233 #define	E2FS_MAGIC	0xef53	/* the ext2fs magic number */
234 #define E2FS_REV0	0	/* revision levels */
235 #define E2FS_REV1	1	/* revision levels */
236 
237 /* compatible/incompatible features */
238 #define EXT2F_COMPAT_PREALLOC		0x0001
239 #define EXT2F_COMPAT_IMAGIC_INODES	0x0002
240 #define EXT2F_COMPAT_HAS_JOURNAL	0x0004
241 #define EXT2F_COMPAT_EXT_ATTR		0x0008
242 #define EXT2F_COMPAT_RESIZE		0x0010
243 #define EXT2F_COMPAT_DIR_INDEX		0x0020
244 #define EXT2F_COMPAT_SPARSE_SUPER2	0x0200
245 
246 #define EXT2F_ROCOMPAT_SPARSE_SUPER	0x0001
247 #define EXT2F_ROCOMPAT_LARGE_FILE	0x0002
248 #define EXT2F_ROCOMPAT_BTREE_DIR	0x0004
249 #define EXT2F_ROCOMPAT_HUGE_FILE	0x0008
250 #define EXT2F_ROCOMPAT_GDT_CSUM		0x0010
251 #define EXT2F_ROCOMPAT_DIR_NLINK	0x0020
252 #define EXT2F_ROCOMPAT_EXTRA_ISIZE	0x0040
253 #define EXT2F_ROCOMPAT_QUOTA		0x0100
254 #define EXT2F_ROCOMPAT_BIGALLOC		0x0200
255 #define EXT2F_ROCOMPAT_METADATA_CKSUM	0x0400
256 #define EXT2F_ROCOMPAT_READONLY		0x1000
257 #define EXT2F_ROCOMPAT_PROJECT		0x2000
258 
259 #define EXT2F_INCOMPAT_COMP		0x0001
260 #define EXT2F_INCOMPAT_FTYPE		0x0002
261 #define EXT2F_INCOMPAT_RECOVER		0x0004
262 #define EXT2F_INCOMPAT_JOURNAL_DEV	0x0008
263 #define EXT2F_INCOMPAT_META_BG		0x0010
264 #define EXT2F_INCOMPAT_EXTENTS		0x0040
265 #define EXT2F_INCOMPAT_64BIT		0x0080
266 #define EXT2F_INCOMPAT_MMP		0x0100
267 #define EXT2F_INCOMPAT_FLEX_BG		0x0200
268 #define EXT2F_INCOMPAT_EA_INODE		0x0400
269 #define EXT2F_INCOMPAT_DIRDATA		0x1000
270 #define EXT2F_INCOMPAT_CSUM_SEED	0x2000
271 #define EXT2F_INCOMPAT_LARGEDIR		0x4000
272 #define EXT2F_INCOMPAT_INLINE_DATA	0x8000
273 #define EXT2F_INCOMPAT_ENCRYPT		0x10000
274 
275 struct ext2_feature {
276 	uint32_t mask;
277 	const char *name;
278 };
279 
280 static const struct ext2_feature ro_compat[] = {
281 	{ EXT2F_ROCOMPAT_SPARSE_SUPER,		"sparse_super" },
282 	{ EXT2F_ROCOMPAT_LARGE_FILE,		"large_file" },
283 	{ EXT2F_ROCOMPAT_BTREE_DIR,		"btree_dir" },
284 	{ EXT2F_ROCOMPAT_HUGE_FILE,		"huge_file" },
285 	{ EXT2F_ROCOMPAT_GDT_CSUM,		"uninit_bg" },
286 	{ EXT2F_ROCOMPAT_DIR_NLINK,		"dir_nlink" },
287 	{ EXT2F_ROCOMPAT_EXTRA_ISIZE,		"extra_isize" },
288 	{ EXT2F_ROCOMPAT_QUOTA,			"quota" },
289 	{ EXT2F_ROCOMPAT_BIGALLOC,		"bigalloc" },
290 	{ EXT2F_ROCOMPAT_METADATA_CKSUM,	"metadata_csum" },
291 	{ EXT2F_ROCOMPAT_READONLY,		"read-only" },
292 	{ EXT2F_ROCOMPAT_PROJECT,		"project" }
293 };
294 
295 static const struct ext2_feature incompat[] = {
296 	{ EXT2F_INCOMPAT_COMP,		"compression" },
297 	{ EXT2F_INCOMPAT_FTYPE,		"filetype" },
298 	{ EXT2F_INCOMPAT_RECOVER,	"needs_recovery" },
299 	{ EXT2F_INCOMPAT_JOURNAL_DEV,	"journal_dev" },
300 	{ EXT2F_INCOMPAT_META_BG,	"meta_bg" },
301 	{ EXT2F_INCOMPAT_EXTENTS,	"extents" },
302 	{ EXT2F_INCOMPAT_64BIT,		"64bit" },
303 	{ EXT2F_INCOMPAT_MMP,		"mmp" },
304 	{ EXT2F_INCOMPAT_FLEX_BG,	"flex_bg" },
305 	{ EXT2F_INCOMPAT_EA_INODE,	"ea_inode" },
306 	{ EXT2F_INCOMPAT_DIRDATA,	"dirdata" },
307 	{ EXT2F_INCOMPAT_CSUM_SEED,	"metadata_csum_seed" },
308 	{ EXT2F_INCOMPAT_LARGEDIR,	"large_dir" },
309 	{ EXT2F_INCOMPAT_INLINE_DATA,	"inline_data" },
310 	{ EXT2F_INCOMPAT_ENCRYPT,	"encrypt" }
311 };
312 
313 /* features supported in this implementation */
314 #define EXT2F_COMPAT_SUPP		0x0000
315 #define EXT2F_ROCOMPAT_SUPP		(EXT2F_ROCOMPAT_SPARSE_SUPER | \
316 					 EXT2F_ROCOMPAT_LARGE_FILE)
317 #define EXT2F_INCOMPAT_SUPP		(EXT2F_INCOMPAT_FTYPE)
318 #define EXT4F_RO_INCOMPAT_SUPP		(EXT2F_INCOMPAT_EXTENTS | \
319 					 EXT2F_INCOMPAT_FLEX_BG | \
320 					 EXT2F_INCOMPAT_META_BG | \
321 					 EXT2F_INCOMPAT_RECOVER)
322 
323 /*
324  * Definitions of behavior on errors
325  */
326 #define E2FS_BEH_CONTINUE	1	/* continue operation */
327 #define E2FS_BEH_READONLY	2	/* remount fs read only */
328 #define E2FS_BEH_PANIC		3	/* cause panic */
329 #define E2FS_BEH_DEFAULT	E2FS_BEH_CONTINUE
330 
331 /*
332  * OS identification
333  */
334 #define E2FS_OS_LINUX 0
335 #define E2FS_OS_HURD  1
336 #define E2FS_OS_MASIX 2
337 
338 /*
339  * Filesystem clean flags
340  */
341 #define	E2FS_ISCLEAN	0x01
342 #define	E2FS_ERRORS	0x02
343 
344 /* ext2 file system block group descriptor */
345 
346 struct ext2_gd {
347 	u_int32_t ext2bgd_b_bitmap;	/* blocks bitmap block */
348 	u_int32_t ext2bgd_i_bitmap;	/* inodes bitmap block */
349 	u_int32_t ext2bgd_i_tables;	/* inodes table block  */
350 	u_int16_t ext2bgd_nbfree;	/* number of free blocks */
351 	u_int16_t ext2bgd_nifree;	/* number of free inodes */
352 	u_int16_t ext2bgd_ndirs;	/* number of directories */
353 	u_int16_t reserved;
354 	u_int32_t reserved2[3];
355 };
356 
357 /*
358  * If the EXT2F_ROCOMPAT_SPARSE_SUPER flag is set, the cylinder group has a
359  * copy of the super and cylinder group descriptors blocks only if it's
360  * a power of 3, 5 or 7
361  */
362 
363 static __inline__ int cg_has_sb(int) __attribute__((__unused__));
364 static __inline int
cg_has_sb(int i)365 cg_has_sb(int i)
366 {
367 	int a3 ,a5 , a7;
368 
369 	if (i == 0 || i == 1)
370 		return 1;
371 	for (a3 = 3, a5 = 5, a7 = 7;
372 	    a3 <= i || a5 <= i || a7 <= i;
373 	    a3 *= 3, a5 *= 5, a7 *= 7)
374 		if (i == a3 || i == a5 || i == a7)
375 			return 1;
376 	return 0;
377 }
378 
379 /*
380  * Ext2 metadata is stored in little-endian byte order.
381  * JBD2 journal used in ext3 and ext4 is big-endian!
382  */
383 #if BYTE_ORDER == LITTLE_ENDIAN
384 #define e2fs_sbload(old, new) memcpy((new), (old), SBSIZE);
385 #define e2fs_cgload(old, new, size) memcpy((new), (old), (size));
386 #define e2fs_sbsave(old, new) memcpy((new), (old), SBSIZE);
387 #define e2fs_cgsave(old, new, size) memcpy((new), (old), (size));
388 #else
389 void e2fs_sb_bswap(struct ext2fs *, struct ext2fs *);
390 void e2fs_cg_bswap(struct ext2_gd *, struct ext2_gd *, int);
391 #define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
392 #define e2fs_cgload(old, new, size) e2fs_cg_bswap((old), (new), (size));
393 #define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
394 #define e2fs_cgsave(old, new, size) e2fs_cg_bswap((old), (new), (size));
395 #endif
396 
397 /*
398  * Turn file system block numbers into disk block addresses.
399  * This maps file system blocks to device size blocks.
400  */
401 #define fsbtodb(fs, b)	((b) << (fs)->e2fs_fsbtodb)
402 #define dbtofsb(fs, b)	((b) >> (fs)->e2fs_fsbtodb)
403 
404 /*
405  * Macros for handling inode numbers:
406  *	 inode number to file system block offset.
407  *	 inode number to cylinder group number.
408  *	 inode number to file system block address.
409  */
410 #define	ino_to_cg(fs, x)	(((x) - 1) / (fs)->e2fs.e2fs_ipg)
411 #define	ino_to_fsba(fs, x)						\
412 	((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables + \
413 	(((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
414 #define	ino_to_fsbo(fs, x)	(((x)-1) % (fs)->e2fs_ipb)
415 
416 /*
417  * Give cylinder group number for a file system block.
418  * Give cylinder group block number for a file system block.
419  */
420 #define	dtog(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
421 #define	dtogd(fs, d) \
422 	(((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
423 
424 /*
425  * The following macros optimize certain frequently calculated
426  * quantities by using shifts and masks in place of divisions
427  * modulos and multiplications.
428  */
429 #define blkoff(fs, loc)		/* calculates (loc % fs->e2fs_bsize) */ \
430 	((loc) & (fs)->e2fs_qbmask)
431 #define lblktosize(fs, blk)	/* calculates (blk * fs->e2fs_bsize) */ \
432 	((blk) << (fs)->e2fs_bshift)
433 #define lblkno(fs, loc)		/* calculates (loc / fs->e2fs_bsize) */ \
434 	((loc) >> (fs)->e2fs_bshift)
435 #define blkroundup(fs, size)	/* calculates roundup(size, fs->e2fs_bsize) */ \
436 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
437 #define fragroundup(fs, size)	/* calculates roundup(size, fs->e2fs_bsize) */ \
438 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
439 /*
440  * Determine the number of available frags given a
441  * percentage to hold in reserve.
442  */
443 #define freespace(fs) \
444    ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
445 
446 /*
447  * Number of indirects in a file system block.
448  */
449 #define	NINDIR(fs)	((fs)->e2fs_bsize / sizeof(u_int32_t))
450