xref: /original-bsd/sys/ufs/ffs/fs.h (revision 0b685140)
1 /* Copyright (c) 1981 Regents of the University of California */
2 
3 /*	fs.h	1.14	02/25/82	*/
4 
5 /*
6  * Each disk drive contains some number of file systems.
7  * A file system consists of a number of cylinder groups.
8  * Each cylinder group has inodes and data.
9  *
10  * A file system is described by its super-block, which in turn
11  * describes the cylinder groups.  The super-block is critical
12  * data and is replicated in each cylinder group to protect against
13  * catastrophic loss.  This is done at mkfs time and the critical
14  * super-block data does not change, so the copies need not be
15  * referenced further unless disaster strikes.
16  *
17  * For file system fs, the offsets of the various blocks of interest
18  * are given in the super block as:
19  *	[fs->fs_bblkno]		Boot sector
20  *	[fs->fs_sblkno]		Super-block
21  *	[fs->fs_cblkno]		Cylinder group block
22  *	[fs->fs_iblkno]		Inode blocks
23  *	[fs->fs_dblkno]		Data blocks
24  * The beginning of cylinder group cg in fs, is given by
25  * the ``cgbase(fs, cg)'' macro.
26  *
27  * The first boot and super blocks are given in absolute disk addresses.
28  */
29 #define BBSIZE		1024
30 #define SBSIZE		8192
31 #define	BBLOCK		((daddr_t)(0))
32 #define	SBLOCK		((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
33 
34 /*
35  * Addresses stored in inodes are capable of addressing fragments
36  * of `blocks'. File system blocks of at most size MAXBSIZE can
37  * be optionally broken into 2, 4, or 8 pieces, each of which is
38  * addressible; these pieces may be DEV_BSIZE, or some multiple of
39  * a DEV_BSIZE unit.
40  *
41  * Large files consist of exclusively large data blocks.  To avoid
42  * undue wasted disk space, the last data block of a small file may be
43  * allocated as only as many fragments of a large block as are
44  * necessary.  The file system format retains only a single pointer
45  * to such a fragment, which is a piece of a single large block that
46  * has been divided.  The size of such a fragment is determinable from
47  * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
48  *
49  * The file system records space availability at the fragment level;
50  * to determine block availability, aligned fragments are examined.
51  *
52  * The root inode is the root of the file system.
53  * Inode 0 can't be used for normal purposes and
54  * historically bad blocks were linked to inode 1,
55  * thus the root inode is 2. (inode 1 is no longer used for
56  * this purpose, however numerous dump tapes make this
57  * assumption, so we are stuck with it)
58  * The lost+found directory is given the next available
59  * inode when it is created by ``mkfs''.
60  */
61 #define	ROOTINO		((ino_t)2)	/* i number of all roots */
62 #define LOSTFOUNDINO	(ROOTINO + 1)
63 
64 /*
65  * MINFREE gives the minimum acceptable percentage of file system
66  * blocks which may be free. If the freelist drops below this level
67  * only the superuser may continue to allocate blocks. This may
68  * be set to 0 if no reserve of free blocks is deemed necessary,
69  * however severe performance degredations will be observed if the
70  * file system is run at greater than 90% full; thus the default
71  * value of fs_minfree is 10%.
72  *
73  * Empirically the best trade-off between block fragmentation and
74  * overall disk utilization at a loading of 90% comes with a
75  * fragmentation of 4, thus the default fragment size is a fourth
76  * of the block size.
77  */
78 #define MINFREE		10
79 #define DESFRAG		4
80 
81 /*
82  * Under current technology, most 300MB disks have 32 sectors and
83  * 19 tracks, thus these are the defaults used for fs_nsect and
84  * fs_ntrak respectively.
85  */
86 #define DFLNSECT	32
87 #define DFLNTRAK	19
88 
89 /*
90  * Cylinder group related limits.
91  *
92  * For each cylinder we keep track of the availability of blocks at different
93  * rotational positions, so that we can lay out the data to be picked
94  * up with minimum rotational latency.  NRPOS is the number of rotational
95  * positions which we distinguish.  With NRPOS 8 the resolution of our
96  * summary information is 2ms for a typical 3600 rpm drive.
97  *
98  * ROTDELAY gives the minimum number of milliseconds to initiate
99  * another disk transfer on the same cylinder. It is used in
100  * determining the rotationally optimal layout for disk blocks
101  * within a file; the default of fs_rotdelay is 2ms.
102  */
103 #define	NRPOS		8	/* number distinct rotational positions */
104 #define ROTDELAY	2
105 
106 /*
107  * Each file system has a number of inodes statically allocated.
108  * We allocate one inode slot per NBPI bytes, expecting this
109  * to be far more than we will ever need.
110  *
111  * MAXIPG bounds the number of inodes per cylinder group, and
112  * is needed only to keep the structure simpler by having the
113  * only a single variable size element (the free bit map).
114  *
115  * N.B.: MAXIPG must be a multiple of INOPB(fs).
116  */
117 #define	NBPI		2048
118 #define	MAXIPG		2048	/* max number inodes/cyl group */
119 
120 /*
121  * MINBSIZE is the smallest allowable block size.
122  * In order to insure that it is possible to create files of size
123  * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
124  * MINBSIZE must be big enough to hold a cylinder group block,
125  * thus changes to (struct cg) must keep its size within MINBSIZE.
126  * MAXCPG is limited only to dimension an array in (struct cg);
127  * it can be made larger as long as that structures size remains
128  * within the bounds dictated by MINBSIZE.
129  * Note that super blocks are always of size MAXBSIZE,
130  * and that MAXBSIZE must be >= MINBSIZE.
131  */
132 #define MINBSIZE	4096
133 #define	DESCPG		16	/* desired fs_cpg */
134 #define	MAXCPG		32	/* maximum fs_cpg */
135 
136 /*
137  * Per cylinder group information; summarized in blocks allocated
138  * from first cylinder group data blocks.  These blocks have to be
139  * read in from fs_csaddr (size fs_cssize) in addition to the
140  * super block.
141  *
142  * N.B. sizeof(struct csum) must be a power of two in order for
143  * the ``fs_cs'' macro to work (see below).
144  */
145 struct csum {
146 	long	cs_ndir;	/* number of directories */
147 	long	cs_nbfree;	/* number of free blocks */
148 	long	cs_nifree;	/* number of free inodes */
149 	long	cs_nffree;	/* number of free frags */
150 };
151 
152 /*
153  * Super block for a file system.
154  */
155 #define	FS_MAGIC	0x110854
156 struct	fs
157 {
158 	long	fs_magic;		/* magic number */
159 	daddr_t	fs_bblkno;		/* abs addr of boot-block in filesys */
160 	daddr_t	fs_sblkno;		/* abs addr of super-block in filesys */
161 	daddr_t	fs_cblkno;		/* offset of cyl-block in filesys */
162 	daddr_t	fs_iblkno;		/* offset of inode-blocks in filesys */
163 	daddr_t	fs_dblkno;		/* offset of data-blocks in filesys */
164 	time_t 	fs_time;    		/* last time written */
165 	long	fs_size;		/* number of blocks in fs */
166 	long	fs_dsize;		/* number of data blocks in fs */
167 	long	fs_ncg;			/* number of cylinder groups */
168 	long	fs_bsize;		/* size of basic blocks in fs */
169 	long	fs_fsize;		/* size of frag blocks in fs */
170 	short	fs_frag;		/* number of frags in a block in fs */
171 	short	fs_minfree;		/* minimum percentage of free blocks */
172 	short	fs_rotdelay;		/* num of ms for optimal next block */
173 	short	fs_rps;			/* disk revolutions per second */
174 	long	fs_bmask;		/* ``blkoff'' calc of blk offsets */
175 	long	fs_fmask;		/* ``fragoff'' calc of frag offsets */
176 	short	fs_bshift;		/* ``lblkno'' calc of logical blkno */
177 	short	fs_fshift;		/* ``numfrags'' calc number of frags */
178 /* sizes determined by number of cylinder groups and their sizes */
179 	daddr_t fs_csaddr;		/* blk addr of cyl grp summary area */
180 	long	fs_cssize;		/* size of cyl grp summary area */
181 	long	fs_cgsize;		/* cylinder group size */
182 /* these fields should be derived from the hardware */
183 	short	fs_ntrak;		/* tracks per cylinder */
184 	short	fs_nsect;		/* sectors per track */
185 	long  	fs_spc;   		/* sectors per cylinder */
186 /* this comes from the disk driver partitioning */
187 	long	fs_ncyl;   		/* cylinders in file system */
188 /* these fields can be computed from the others */
189 	short	fs_cpg;			/* cylinders per group */
190 	short	fs_ipg;			/* inodes per group */
191 	long	fs_fpg;			/* blocks per group * fs_frag */
192 /* this data must be re-computed after crashes */
193 	struct	csum fs_cstotal;	/* cylinder summary information */
194 /* these fields are cleared at mount time */
195 	char   	fs_fmod;    		/* super block modified flag */
196 	char   	fs_ronly;   		/* mounted read-only flag */
197 	char	fs_fsmnt[34];		/* name mounted on */
198 /* these fields retain the current block allocation info */
199 	long	fs_cgrotor;		/* last cg searched */
200 	struct	csum *fs_csp[NBUF];	/* list of fs_cs info buffers */
201 	short	fs_cpc;			/* cyl per cycle in postbl */
202 	short	fs_postbl[MAXCPG][NRPOS];/* head of blocks for each rotation */
203 	u_char	fs_rotbl[1];		/* list of blocks for each rotation */
204 /* actually longer */
205 };
206 
207 /*
208  * convert cylinder group to base address of its global summary info.
209  *
210  * N.B. This macro assumes that sizeof(struct csum) is a power of two.
211  */
212 #define fs_cs(fs, indx) \
213 	fs_csp[(indx) / ((fs)->fs_bsize / sizeof(struct csum))] \
214 	[(indx) % ((fs)->fs_bsize / sizeof(struct csum))]
215 
216 /*
217  * MAXBPC bounds the size of the rotational layout tables and
218  * is limited by the fact that the super block is of size SBSIZE.
219  * The size of these tables is INVERSELY proportional to the block
220  * size of the file system. It is aggravated by sector sizes that
221  * are not powers of two, as this increases the number of cylinders
222  * included before the rotational pattern repeats (fs_cpc).
223  * Its size is derived from the number of bytes remaining in (struct fs)
224  */
225 #define	MAXBPC	(SBSIZE - sizeof (struct fs))
226 
227 /*
228  * Cylinder group block for a file system.
229  */
230 #define	CG_MAGIC	0x092752
231 struct	cg {
232 	long	cg_magic;		/* magic number */
233 	time_t	cg_time;		/* time last written */
234 	long	cg_cgx;			/* we are the cgx'th cylinder group */
235 	short	cg_ncyl;		/* number of cyl's this cg */
236 	short	cg_niblk;		/* number of inode blocks this cg */
237 	long	cg_ndblk;		/* number of data blocks this cg */
238 	struct	csum cg_cs;		/* cylinder summary information */
239 	long	cg_rotor;		/* position of last used block */
240 	long	cg_frotor;		/* position of last used frag */
241 	long	cg_irotor;		/* position of last used inode */
242 	long	cg_frsum[MAXFRAG];	/* counts of available frags */
243 	long	cg_btot[MAXCPG];	/* block totals per cylinder */
244 	short	cg_b[MAXCPG][NRPOS];	/* positions of free blocks */
245 	char	cg_iused[MAXIPG/NBBY];	/* used inode map */
246 	char	cg_free[1];		/* free block map */
247 /* actually longer */
248 };
249 
250 /*
251  * MAXBPG bounds the number of blocks of data per cylinder group,
252  * and is limited by the fact that cylinder groups are at most one block.
253  * Its size is derived from the size of blocks and the (struct cg) size,
254  * by the number of remaining bits.
255  */
256 #define	MAXBPG(fs) \
257 	(NBBY * ((fs)->fs_bsize - (sizeof (struct cg))) / (fs)->fs_frag)
258 
259 /*
260  * Turn file system block numbers into disk block addresses.
261  * This maps file system blocks to device size blocks.
262  */
263 #define fsbtodb(fs, b)	((b) * ((fs)->fs_fsize / DEV_BSIZE))
264 #define	dbtofsb(fs, b)	((b) / ((fs)->fs_fsize / DEV_BSIZE))
265 
266 /*
267  * Cylinder group macros to locate things in cylinder groups.
268  *
269  * cylinder group to disk block address of spare boot block
270  * and super block
271  * Note that these are in absolute addresses, and can NOT
272  * in general be expressable in terms of file system addresses.
273  */
274 #define	cgbblock(fs, c)	(fsbtodb(fs, cgbase(fs, c)) + (fs)->fs_bblkno)
275 #define	cgsblock(fs, c)	(fsbtodb(fs, cgbase(fs, c)) + (fs)->fs_sblkno)
276 
277 /*
278  * file system addresses of cylinder group data structures
279  */
280 #define	cgbase(fs, c)	((daddr_t)((fs)->fs_fpg * (c)))		/* base addr */
281 #define	cgtod(fs, c)	(cgbase(fs, c) + (fs)->fs_cblkno)	/* cg block */
282 #define	cgimin(fs, c)	(cgbase(fs, c) + (fs)->fs_iblkno)	/* inode blk */
283 #define	cgdmin(fs, c)	(cgbase(fs, c) + (fs)->fs_dblkno)	/* 1st data */
284 
285 /*
286  * macros for handling inode numbers
287  *     inode number to file system block offset
288  *     inode number to cylinder group number
289  *     inode number to file system block address
290  */
291 #define	itoo(fs, x)	((x) % INOPB(fs))
292 #define	itog(fs, x)	((x) / (fs)->fs_ipg)
293 #define	itod(fs, x) \
294 	((daddr_t)(cgimin(fs, itog(fs, x)) + \
295 	(x) % (fs)->fs_ipg / INOPB(fs) * (fs)->fs_frag))
296 
297 /*
298  * give cylinder group number for a file system block
299  * give cylinder group block number for a file system block
300  */
301 #define	dtog(fs, d)	((d) / (fs)->fs_fpg)
302 #define	dtogd(fs, d)	((d) % (fs)->fs_fpg)
303 
304 /*
305  * compute the cylinder and rotational position of a cyl block addr
306  */
307 #define cbtocylno(fs, bno) \
308 	((bno) * NSPF(fs) / (fs)->fs_spc)
309 #define cbtorpos(fs, bno) \
310 	((bno) * NSPF(fs) % (fs)->fs_nsect * NRPOS / (fs)->fs_nsect)
311 
312 /*
313  * The following macros optimize certain frequently calculated
314  * quantities by using shifts and masks in place of divisions
315  * modulos and multiplications.
316  */
317 #define blkoff(fs, loc)		/* calculates (loc % fs->fs_bsize) */ \
318 	((loc) & ~(fs)->fs_bmask)
319 #define fragoff(fs, loc)	/* calculates (loc % fs->fs_fsize) */ \
320 	((loc) & ~(fs)->fs_fmask)
321 #define lblkno(fs, loc)		/* calculates (loc / fs->fs_bsize) */ \
322 	((loc) >> (fs)->fs_bshift)
323 #define numfrags(fs, loc)	/* calculates (loc / fs->fs_fsize) */ \
324 	((loc) >> (fs)->fs_fshift)
325 #define blkroundup(fs, size)	/* calculates roundup(size, fs->fs_bsize) */ \
326 	(((size) + (fs)->fs_bsize - 1) & (fs)->fs_bmask)
327 #define fragroundup(fs, size)	/* calculates roundup(size, fs->fs_fsize) */ \
328 	(((size) + (fs)->fs_fsize - 1) & (fs)->fs_fmask)
329 
330 /*
331  * determining the size of a file block in the file system
332  */
333 #define blksize(fs, ip, lbn) \
334 	(((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) * (fs)->fs_bsize) \
335 	    ? (fs)->fs_bsize \
336 	    : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
337 #define dblksize(fs, dip, lbn) \
338 	(((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) * (fs)->fs_bsize) \
339 	    ? (fs)->fs_bsize \
340 	    : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
341 
342 /*
343  * number of disk sectors per block; assumes DEV_BSIZE byte sector size
344  */
345 #define	NSPB(fs)	((fs)->fs_bsize / DEV_BSIZE)
346 #define	NSPF(fs)	((fs)->fs_fsize / DEV_BSIZE)
347 
348 /*
349  * INOPB is the number of inodes in a secondary storage block
350  */
351 #define	INOPB(fs)	((fs)->fs_bsize / sizeof (struct dinode))
352 #define	INOPF(fs)	((fs)->fs_fsize / sizeof (struct dinode))
353 
354 /*
355  * NINDIR is the number of indirects in a file system block
356  */
357 #define	NINDIR(fs)	((fs)->fs_bsize / sizeof (daddr_t))
358 
359 #ifdef KERNEL
360 struct	fs *getfs();
361 #endif
362