add new useful constants
[unix-history] / usr / src / sys / ufs / ffs / fs.h
CommitLineData
78c44263 1/* fs.h 4.6 83/01/02 */
1ef63481
KM
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 *
aca50d72
KM
15 * For file system fs, the offsets of the various blocks of interest
16 * are given in the super block as:
aca50d72
KM
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
6994bf5d 22 * the ``cgbase(fs, cg)'' macro.
b6407c9d 23 *
aca50d72 24 * The first boot and super blocks are given in absolute disk addresses.
b6407c9d 25 */
1df1dbcd 26#define BBSIZE 8192
80cc8328
KM
27#define SBSIZE 8192
28#define BBLOCK ((daddr_t)(0))
29#define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
1ef63481
KM
30
31/*
b6407c9d
KM
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.
1ef63481 37 *
b6407c9d
KM
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.
1ef63481
KM
45 *
46 * The file system records space availability at the fragment level;
47 * to determine block availability, aligned fragments are examined.
743f1ef7 48 *
80cc8328
KM
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 * MINFREE gives the minimum acceptable percentage of file system
63 * blocks which may be free. If the freelist drops below this level
64 * only the superuser may continue to allocate blocks. This may
65 * be set to 0 if no reserve of free blocks is deemed necessary,
66 * however severe performance degredations will be observed if the
67 * file system is run at greater than 90% full; thus the default
68 * value of fs_minfree is 10%.
69 *
70 * Empirically the best trade-off between block fragmentation and
71 * overall disk utilization at a loading of 90% comes with a
72 * fragmentation of 4, thus the default fragment size is a fourth
73 * of the block size.
74 */
75#define MINFREE 10
76#define DESFRAG 4
77
78/*
79 * Under current technology, most 300MB disks have 32 sectors and
26169eda 80 * 16 tracks, thus these are the defaults used for fs_nsect and
80cc8328
KM
81 * fs_ntrak respectively.
82 */
83#define DFLNSECT 32
26169eda 84#define DFLNTRAK 16
80cc8328
KM
85
86/*
87 * Cylinder group related limits.
88 *
743f1ef7
KM
89 * For each cylinder we keep track of the availability of blocks at different
90 * rotational positions, so that we can lay out the data to be picked
91 * up with minimum rotational latency. NRPOS is the number of rotational
92 * positions which we distinguish. With NRPOS 8 the resolution of our
93 * summary information is 2ms for a typical 3600 rpm drive.
80cc8328
KM
94 *
95 * ROTDELAY gives the minimum number of milliseconds to initiate
96 * another disk transfer on the same cylinder. It is used in
97 * determining the rotationally optimal layout for disk blocks
98 * within a file; the default of fs_rotdelay is 2ms.
743f1ef7 99 */
80cc8328
KM
100#define NRPOS 8 /* number distinct rotational positions */
101#define ROTDELAY 2
1ef63481
KM
102
103/*
104 * Each file system has a number of inodes statically allocated.
80cc8328 105 * We allocate one inode slot per NBPI bytes, expecting this
5ae9a796 106 * to be far more than we will ever need.
80cc8328
KM
107 *
108 * MAXIPG bounds the number of inodes per cylinder group, and
109 * is needed only to keep the structure simpler by having the
110 * only a single variable size element (the free bit map).
111 *
112 * N.B.: MAXIPG must be a multiple of INOPB(fs).
1ef63481 113 */
80cc8328
KM
114#define NBPI 2048
115#define MAXIPG 2048 /* max number inodes/cyl group */
1ef63481 116
b6407c9d
KM
117/*
118 * MINBSIZE is the smallest allowable block size.
119 * In order to insure that it is possible to create files of size
120 * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
121 * MINBSIZE must be big enough to hold a cylinder group block,
122 * thus changes to (struct cg) must keep its size within MINBSIZE.
123 * MAXCPG is limited only to dimension an array in (struct cg);
124 * it can be made larger as long as that structures size remains
125 * within the bounds dictated by MINBSIZE.
126 * Note that super blocks are always of size MAXBSIZE,
127 * and that MAXBSIZE must be >= MINBSIZE.
128 */
129#define MINBSIZE 4096
130#define DESCPG 16 /* desired fs_cpg */
131#define MAXCPG 32 /* maximum fs_cpg */
80cc8328 132
2c9afb08
KM
133/*
134 * The path name on which the file system is mounted is maintained
135 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
136 * the super block for this name.
1b807fc9 137 * The limit on the amount of summary information per file system
26169eda
KM
138 * is defined by MAXCSBUFS. It is currently parameterized for a
139 * maximum of two million cylinders.
2c9afb08 140 */
26169eda
KM
141#define MAXMNTLEN 512
142#define MAXCSBUFS 32
2c9afb08 143
1ef63481 144/*
80cc8328
KM
145 * Per cylinder group information; summarized in blocks allocated
146 * from first cylinder group data blocks. These blocks have to be
147 * read in from fs_csaddr (size fs_cssize) in addition to the
148 * super block.
1ef63481 149 *
80cc8328
KM
150 * N.B. sizeof(struct csum) must be a power of two in order for
151 * the ``fs_cs'' macro to work (see below).
1ef63481 152 */
80cc8328
KM
153struct csum {
154 long cs_ndir; /* number of directories */
155 long cs_nbfree; /* number of free blocks */
156 long cs_nifree; /* number of free inodes */
157 long cs_nffree; /* number of free frags */
158};
1ef63481 159
80cc8328
KM
160/*
161 * Super block for a file system.
162 */
b9b166c6 163#define FS_MAGIC 0x011954
1ef63481
KM
164struct fs
165{
b9b166c6
KM
166 struct fs *fs_link; /* linked list of file systems */
167 struct fs *fs_rlink; /* used for incore super blocks */
26169eda 168 daddr_t fs_sblkno; /* addr of super-block in filesys */
aca50d72
KM
169 daddr_t fs_cblkno; /* offset of cyl-block in filesys */
170 daddr_t fs_iblkno; /* offset of inode-blocks in filesys */
26169eda
KM
171 daddr_t fs_dblkno; /* offset of first data after cg */
172 long fs_cgoffset; /* cylinder group offset in cylinder */
173 long fs_cgmask; /* used to calc mod fs_ntrak */
1ef63481 174 time_t fs_time; /* last time written */
003319d1
KM
175 long fs_size; /* number of blocks in fs */
176 long fs_dsize; /* number of data blocks in fs */
177 long fs_ncg; /* number of cylinder groups */
b6407c9d
KM
178 long fs_bsize; /* size of basic blocks in fs */
179 long fs_fsize; /* size of frag blocks in fs */
26169eda
KM
180 long fs_frag; /* number of frags in a block in fs */
181 long fs_minfree; /* minimum percentage of free blocks */
182 long fs_rotdelay; /* num of ms for optimal next block */
183 long fs_rps; /* disk revolutions per second */
c1ab275c
KM
184 long fs_bmask; /* ``blkoff'' calc of blk offsets */
185 long fs_fmask; /* ``fragoff'' calc of frag offsets */
26169eda
KM
186 long fs_bshift; /* ``lblkno'' calc of logical blkno */
187 long fs_fshift; /* ``numfrags'' calc number of frags */
af0b24db
SL
188 long fs_maxcontig; /* max number of contiguous blks */
189 long fs_maxbpg; /* max number of blks per cyl group */
decc359e
KM
190 long fs_fragshift; /* block to frag shift */
191 long fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
192 long fs_sbsize; /* actual size of super block */
193 long fs_csmask; /* csum block offset */
194 long fs_csshift; /* csum block number */
195 long fs_nindir; /* value of NINDIR */
196 long fs_inopb; /* value of INOPB */
197 long fs_nspf; /* value of NSPF */
198 long fs_sparecon[6]; /* reserved for future constants */
1ef63481 199/* sizes determined by number of cylinder groups and their sizes */
b6407c9d 200 daddr_t fs_csaddr; /* blk addr of cyl grp summary area */
003319d1
KM
201 long fs_cssize; /* size of cyl grp summary area */
202 long fs_cgsize; /* cylinder group size */
1ef63481 203/* these fields should be derived from the hardware */
26169eda
KM
204 long fs_ntrak; /* tracks per cylinder */
205 long fs_nsect; /* sectors per track */
003319d1 206 long fs_spc; /* sectors per cylinder */
1ef63481 207/* this comes from the disk driver partitioning */
003319d1 208 long fs_ncyl; /* cylinders in file system */
1ef63481 209/* these fields can be computed from the others */
26169eda
KM
210 long fs_cpg; /* cylinders per group */
211 long fs_ipg; /* inodes per group */
b6407c9d 212 long fs_fpg; /* blocks per group * fs_frag */
003319d1
KM
213/* this data must be re-computed after crashes */
214 struct csum fs_cstotal; /* cylinder summary information */
1ef63481
KM
215/* these fields are cleared at mount time */
216 char fs_fmod; /* super block modified flag */
26169eda 217 char fs_clean; /* file system is clean flag */
1ef63481 218 char fs_ronly; /* mounted read-only flag */
26169eda 219 char fs_flags; /* currently unused flag */
2c9afb08 220 char fs_fsmnt[MAXMNTLEN]; /* name mounted on */
743f1ef7 221/* these fields retain the current block allocation info */
003319d1 222 long fs_cgrotor; /* last cg searched */
1b807fc9 223 struct csum *fs_csp[MAXCSBUFS];/* list of fs_cs info buffers */
26169eda 224 long fs_cpc; /* cyl per cycle in postbl */
aca50d72 225 short fs_postbl[MAXCPG][NRPOS];/* head of blocks for each rotation */
b9b166c6 226 long fs_magic; /* magic number */
aca50d72 227 u_char fs_rotbl[1]; /* list of blocks for each rotation */
743f1ef7 228/* actually longer */
1ef63481 229};
b6407c9d
KM
230
231/*
2c9afb08 232 * Convert cylinder group to base address of its global summary info.
80cc8328 233 *
b6407c9d
KM
234 * N.B. This macro assumes that sizeof(struct csum) is a power of two.
235 */
236#define fs_cs(fs, indx) \
237 fs_csp[(indx) / ((fs)->fs_bsize / sizeof(struct csum))] \
238 [(indx) % ((fs)->fs_bsize / sizeof(struct csum))]
1ef63481
KM
239
240/*
aca50d72
KM
241 * MAXBPC bounds the size of the rotational layout tables and
242 * is limited by the fact that the super block is of size SBSIZE.
243 * The size of these tables is INVERSELY proportional to the block
244 * size of the file system. It is aggravated by sector sizes that
245 * are not powers of two, as this increases the number of cylinders
246 * included before the rotational pattern repeats (fs_cpc).
247 * Its size is derived from the number of bytes remaining in (struct fs)
1ef63481 248 */
aca50d72 249#define MAXBPC (SBSIZE - sizeof (struct fs))
1ef63481
KM
250
251/*
80cc8328 252 * Cylinder group block for a file system.
1ef63481 253 */
b9b166c6 254#define CG_MAGIC 0x090255
1ef63481 255struct cg {
b9b166c6
KM
256 struct cg *cg_link; /* linked list of cyl groups */
257 struct cg *cg_rlink; /* used for incore cyl groups */
1ef63481 258 time_t cg_time; /* time last written */
003319d1 259 long cg_cgx; /* we are the cgx'th cylinder group */
1ef63481
KM
260 short cg_ncyl; /* number of cyl's this cg */
261 short cg_niblk; /* number of inode blocks this cg */
003319d1
KM
262 long cg_ndblk; /* number of data blocks this cg */
263 struct csum cg_cs; /* cylinder summary information */
264 long cg_rotor; /* position of last used block */
265 long cg_frotor; /* position of last used frag */
266 long cg_irotor; /* position of last used inode */
b6407c9d 267 long cg_frsum[MAXFRAG]; /* counts of available frags */
d37938c8 268 long cg_btot[MAXCPG]; /* block totals per cylinder */
1ef63481
KM
269 short cg_b[MAXCPG][NRPOS]; /* positions of free blocks */
270 char cg_iused[MAXIPG/NBBY]; /* used inode map */
b9b166c6 271 long cg_magic; /* magic number */
110d7a57 272 u_char cg_free[1]; /* free block map */
1ef63481
KM
273/* actually longer */
274};
80cc8328
KM
275
276/*
277 * MAXBPG bounds the number of blocks of data per cylinder group,
278 * and is limited by the fact that cylinder groups are at most one block.
279 * Its size is derived from the size of blocks and the (struct cg) size,
280 * by the number of remaining bits.
281 */
282#define MAXBPG(fs) \
283 (NBBY * ((fs)->fs_bsize - (sizeof (struct cg))) / (fs)->fs_frag)
284
285/*
286 * Turn file system block numbers into disk block addresses.
287 * This maps file system blocks to device size blocks.
288 */
289#define fsbtodb(fs, b) ((b) * ((fs)->fs_fsize / DEV_BSIZE))
290#define dbtofsb(fs, b) ((b) / ((fs)->fs_fsize / DEV_BSIZE))
291
292/*
293 * Cylinder group macros to locate things in cylinder groups.
26169eda 294 * They calc file system addresses of cylinder group data structures.
80cc8328 295 */
26169eda
KM
296#define cgbase(fs, c) ((daddr_t)((fs)->fs_fpg * (c)))
297#define cgstart(fs, c) \
298 (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
299#define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */
300#define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */
301#define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */
302#define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */
80cc8328
KM
303
304/*
2c9afb08
KM
305 * Macros for handling inode numbers:
306 * inode number to file system block offset.
307 * inode number to cylinder group number.
308 * inode number to file system block address.
80cc8328 309 */
6994bf5d
KM
310#define itoo(fs, x) ((x) % INOPB(fs))
311#define itog(fs, x) ((x) / (fs)->fs_ipg)
312#define itod(fs, x) \
313 ((daddr_t)(cgimin(fs, itog(fs, x)) + \
aca50d72 314 (x) % (fs)->fs_ipg / INOPB(fs) * (fs)->fs_frag))
80cc8328 315
80cc8328 316/*
2c9afb08
KM
317 * Give cylinder group number for a file system block.
318 * Give cylinder group block number for a file system block.
80cc8328 319 */
6994bf5d
KM
320#define dtog(fs, d) ((d) / (fs)->fs_fpg)
321#define dtogd(fs, d) ((d) % (fs)->fs_fpg)
80cc8328 322
aca50d72 323/*
2c9afb08
KM
324 * Extract the bits for a block from a map.
325 * Compute the cylinder and rotational position of a cyl block addr.
aca50d72 326 */
2c9afb08
KM
327#define blkmap(fs, map, loc) \
328 (((map)[loc / NBBY] >> (loc % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
aca50d72
KM
329#define cbtocylno(fs, bno) \
330 ((bno) * NSPF(fs) / (fs)->fs_spc)
331#define cbtorpos(fs, bno) \
332 ((bno) * NSPF(fs) % (fs)->fs_nsect * NRPOS / (fs)->fs_nsect)
333
c1ab275c
KM
334/*
335 * The following macros optimize certain frequently calculated
336 * quantities by using shifts and masks in place of divisions
337 * modulos and multiplications.
338 */
339#define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \
340 ((loc) & ~(fs)->fs_bmask)
341#define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \
342 ((loc) & ~(fs)->fs_fmask)
343#define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \
344 ((loc) >> (fs)->fs_bshift)
345#define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
346 ((loc) >> (fs)->fs_fshift)
347#define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \
348 (((size) + (fs)->fs_bsize - 1) & (fs)->fs_bmask)
349#define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \
350 (((size) + (fs)->fs_fsize - 1) & (fs)->fs_fmask)
351
80cc8328 352/*
2c9afb08 353 * Determining the size of a file block in the file system.
80cc8328
KM
354 */
355#define blksize(fs, ip, lbn) \
356 (((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) * (fs)->fs_bsize) \
c1ab275c
KM
357 ? (fs)->fs_bsize \
358 : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
80cc8328
KM
359#define dblksize(fs, dip, lbn) \
360 (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) * (fs)->fs_bsize) \
c1ab275c
KM
361 ? (fs)->fs_bsize \
362 : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
80cc8328
KM
363
364/*
2c9afb08 365 * Number of disk sectors per block; assumes DEV_BSIZE byte sector size.
80cc8328
KM
366 */
367#define NSPB(fs) ((fs)->fs_bsize / DEV_BSIZE)
368#define NSPF(fs) ((fs)->fs_fsize / DEV_BSIZE)
369
370/*
2c9afb08 371 * INOPB is the number of inodes in a secondary storage block.
80cc8328
KM
372 */
373#define INOPB(fs) ((fs)->fs_bsize / sizeof (struct dinode))
374#define INOPF(fs) ((fs)->fs_fsize / sizeof (struct dinode))
375
376/*
2c9afb08 377 * NINDIR is the number of indirects in a file system block.
80cc8328
KM
378 */
379#define NINDIR(fs) ((fs)->fs_bsize / sizeof (daddr_t))
1ef63481
KM
380
381#ifdef KERNEL
382struct fs *getfs();
1bbbd864 383struct fs *mountfs();
1ef63481 384#endif