xref: /dragonfly/sys/vfs/ext2fs/fs.h (revision 58645856)
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 /*-
8  * SPDX-License-Identifier: BSD-3-Clause
9  *
10  * Copyright (c) 1982, 1986, 1993
11  *	The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)fs.h	8.7 (Berkeley) 4/19/94
38  * $FreeBSD$
39  */
40 
41 #ifndef _FS_EXT2FS_FS_H_
42 #define	_FS_EXT2FS_FS_H_
43 
44 /*
45  * Each disk drive contains some number of file systems.
46  * A file system consists of a number of cylinder groups.
47  * Each cylinder group has inodes and data.
48  *
49  * A file system is described by its super-block, which in turn
50  * describes the cylinder groups.  The super-block is critical
51  * data and is replicated in each cylinder group to protect against
52  * catastrophic loss.  This is done at `newfs' time and the critical
53  * super-block data does not change, so the copies need not be
54  * referenced further unless disaster strikes.
55  *
56  * The first boot and super blocks are given in absolute disk addresses.
57  * The byte-offset forms are preferred, as they don't imply a sector size.
58  */
59 #define	SBSIZE		1024
60 #define	SBLOCK		2
61 #define	SBOFF		((off_t)1024)
62 
63 /*
64  * The path name on which the file system is mounted is maintained
65  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
66  * the super block for this name.
67  */
68 #define	MAXMNTLEN	512
69 
70 /*
71  * A summary of contiguous blocks of various sizes is maintained
72  * in each cylinder group. Normally this is set by the initial
73  * value of fs_maxcontig.
74  *
75  * XXX:FS_MAXCONTIG is set to 16 to conserve space. Here we set
76  * EXT2_MAXCONTIG to 32 for better performance.
77  */
78 #define	EXT2_MAXCONTIG	32
79 
80 /*
81  * Grigoriy Orlov <gluk@ptci.ru> has done some extensive work to fine
82  * tune the layout preferences for directories within a filesystem.
83  * His algorithm can be tuned by adjusting the following parameters
84  * which tell the system the average file size and the average number
85  * of files per directory. These defaults are well selected for typical
86  * filesystems, but may need to be tuned for odd cases like filesystems
87  * being used for squid caches or news spools.
88  * AVFPDIR is the expected number of files per directory. AVGDIRSIZE is
89  * obtained by multiplying AVFPDIR and AVFILESIZ which is assumed to be
90  * 16384.
91  */
92 
93 #define	AFPDIR		64
94 #define	AVGDIRSIZE	1048576
95 
96 /*
97  * Macros for access to superblock array structures
98  */
99 
100 /*
101  * Turn file system block numbers into disk block addresses.
102  * This maps file system blocks to device size blocks.
103  */
104 #define	fsbtodb(fs, b)	((daddr_t)(b) << (fs)->e2fs_fsbtodb)
105 #define	dbtofsb(fs, b)	((b) >> (fs)->e2fs_fsbtodb)
106 
107 /* get group containing inode */
108 #define	ino_to_cg(fs, x)	(((x) - 1) / (fs->e2fs_ipg))
109 
110 /* get block containing inode from its number x */
111 #define	ino_to_fsba(fs, x)                                              \
112 	(e2fs_gd_get_i_tables(&(fs)->e2fs_gd[ino_to_cg((fs), (x))]) +   \
113 	    (((x) - 1) % (fs)->e2fs_ipg) / (fs)->e2fs_ipb)
114 
115 /* get offset for inode in block */
116 #define	ino_to_fsbo(fs, x)	((x-1) % (fs->e2fs_ipb))
117 
118 /*
119  * Give cylinder group number for a file system block.
120  * Give cylinder group block number for a file system block.
121  */
122 #define	dtog(fs, d)	(((d) - le32toh(fs->e2fs->e2fs_first_dblock)) / \
123     EXT2_BLOCKS_PER_GROUP(fs))
124 #define	dtogd(fs, d)	(((d) - le32toh(fs->e2fs->e2fs_first_dblock)) % \
125     EXT2_BLOCKS_PER_GROUP(fs))
126 
127 /*
128  * The following macros optimize certain frequently calculated
129  * quantities by using shifts and masks in place of divisions
130  * modulos and multiplications.
131  */
132 #define	blkoff(fs, loc)		/* calculates (loc % fs->fs_bsize) */ \
133 	((loc) & (fs)->e2fs_qbmask)
134 
135 #define	lblktosize(fs, blk)	/* calculates (blk * fs->fs_bsize) */ \
136 	((blk) << (fs->e2fs_bshift))
137 
138 #define	lblkno(fs, loc)		/* calculates (loc / fs->fs_bsize) */ \
139 	((loc) >> (fs->e2fs_bshift))
140 
141 /*
142  * Used when converting logical blocks to offsets for getblk, bread, etc.
143  */
144 #define lblktodoff(fs, blk)	/* calculates (blk * fs->fs_bsize) */ \
145 	((off_t)(blk) << (fs)->e2fs_bshift)
146 
147 #define fsbtodoff(fs, blk)	/* calculates (blk * fs->fs_fsize) */ \
148 	((off_t)(blk) * (fs)->e2fs_fsize)
149 
150 #define dofftofsb(fs, blk)	/* calculates blk / fs->fs_fsize */   \
151 	((daddr_t)((blk) / (fs)->e2fs_fsize))
152 
153 #define dbtodoff(fs, b)		/* calculates diskblk * sectorsize */ \
154 	((off_t)(b) * ((fs)->e2fs_fsize >> (fs)->e2fs_fsbtodb))
155 
156 /* no fragments -> logical block number equal # of frags */
157 #define	numfrags(fs, loc)	/* calculates (loc / fs->fs_fsize) */ \
158 	((loc) >> (fs->e2fs_bshift))
159 
160 #define	fragroundup(fs, size)	/* calculates roundup(size, fs->fs_fsize) */ \
161 	roundup(size, fs->e2fs_fsize)
162 	/* was (((size) + (fs)->fs_qfmask) & (fs)->fs_fmask) */
163 
164 /*
165  * Determining the size of a file block in the file system.
166  * easy w/o fragments
167  */
168 #define	blksize(fs, ip, lbn) ((fs)->e2fs_fsize)
169 
170 /*
171  * INOPB is the number of inodes in a secondary storage block.
172  */
173 #define	INOPB(fs)	(fs->e2fs_ipb)
174 
175 /*
176  * NINDIR is the number of indirects in a file system block.
177  */
178 #define	NINDIR(fs)	(EXT2_ADDR_PER_BLOCK(fs))
179 
180 /*
181  * Use if additional debug logging is required.
182  */
183 /* #define EXT2FS_PRINT_EXTENTS */
184 
185 #endif	/* !_FS_EXT2FS_FS_H_ */
186