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