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