Commit | Line | Data |
---|---|---|
1ef63481 KM |
1 | /* Copyright (c) 1981 Regents of the University of California */ |
2 | ||
b6407c9d | 3 | /* fs.h 1.9 %G% */ |
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 | * | |
17 | * For file system fs and a cylinder group number cg: | |
18 | * [BBLOCK] Boot sector and bad block information | |
19 | * [SBLOCK] Super-block | |
b6407c9d KM |
20 | * [CBLOCK(fs)] Cylinder group block |
21 | * [IBLOCK(fs)..IBLOCK(fs)+fs.fs_ipg/INOPB(fs)) | |
1ef63481 | 22 | * Inode blocks |
b6407c9d | 23 | * [IBLOCK(fs)+fs.fs_ipg/INOPB(fs)..fs.fs_fpg/fs.fs_frag) |
1ef63481 | 24 | * Data blocks |
b6407c9d KM |
25 | * The beginning of data blocks for cg in fs is also given by |
26 | * the ``cgdmin(cg,fs)'' macro. | |
27 | * | |
28 | * The boot and super blocks are given in absolute disk addresses. | |
29 | */ | |
30 | #define BBLOCK ((daddr_t)(0 * (MAXBSIZE / DEV_BSIZE))) | |
31 | #define SBLOCK ((daddr_t)(1 * (MAXBSIZE / DEV_BSIZE))) | |
32 | /* | |
33 | * The cylinder group and inode blocks are given in file system | |
34 | * addresses, and hence must be converted to disk addresses by | |
35 | * the ``fsbtodb(fs, bno)'' macro. | |
1ef63481 | 36 | */ |
b6407c9d KM |
37 | #define CBLOCK(fs) ((daddr_t)(dbtofsb(fs, 2 * (MAXBSIZE / DEV_BSIZE)))) |
38 | #define IBLOCK(fs) ((daddr_t)(CBLOCK(fs) + (fs)->fs_frag)) | |
1ef63481 KM |
39 | |
40 | /* | |
b6407c9d KM |
41 | * Addresses stored in inodes are capable of addressing fragments |
42 | * of `blocks'. File system blocks of at most size MAXBSIZE can | |
43 | * be optionally broken into 2, 4, or 8 pieces, each of which is | |
44 | * addressible; these pieces may be DEV_BSIZE, or some multiple of | |
45 | * a DEV_BSIZE unit. | |
1ef63481 | 46 | * |
b6407c9d KM |
47 | * Large files consist of exclusively large data blocks. To avoid |
48 | * undue wasted disk space, the last data block of a small file may be | |
49 | * allocated as only as many fragments of a large block as are | |
50 | * necessary. The file system format retains only a single pointer | |
51 | * to such a fragment, which is a piece of a single large block that | |
52 | * has been divided. The size of such a fragment is determinable from | |
53 | * information in the inode, using the ``blksize(fs, ip, lbn)'' macro. | |
1ef63481 KM |
54 | * |
55 | * The file system records space availability at the fragment level; | |
56 | * to determine block availability, aligned fragments are examined. | |
743f1ef7 KM |
57 | * |
58 | * For each cylinder we keep track of the availability of blocks at different | |
59 | * rotational positions, so that we can lay out the data to be picked | |
60 | * up with minimum rotational latency. NRPOS is the number of rotational | |
61 | * positions which we distinguish. With NRPOS 8 the resolution of our | |
62 | * summary information is 2ms for a typical 3600 rpm drive. | |
63 | */ | |
64 | #define NRPOS 8 /* number distinct rotational positions */ | |
1ef63481 KM |
65 | |
66 | /* | |
67 | * Information per cylinder group summarized in blocks allocated | |
68 | * from first cylinder group data blocks. These blocks have to be | |
b6407c9d KM |
69 | * read in from fs_csaddr (size fs_cssize) in addition to the |
70 | * super block. | |
71 | * N.B. sizeof(struct csum) must be a power of two in order for | |
72 | * the ``fs_cs'' macro to work (see below). | |
1ef63481 KM |
73 | */ |
74 | struct csum { | |
003319d1 KM |
75 | long cs_ndir; /* number of directories */ |
76 | long cs_nbfree; /* number of free blocks */ | |
77 | long cs_nifree; /* number of free inodes */ | |
78 | long cs_nffree; /* number of free frags */ | |
1ef63481 | 79 | }; |
1ef63481 KM |
80 | |
81 | /* | |
82 | * Each file system has a number of inodes statically allocated. | |
83 | * We allocate one inode slot per NBPI data bytes, expecting this | |
5ae9a796 | 84 | * to be far more than we will ever need. |
1ef63481 KM |
85 | */ |
86 | #define NBPI 2048 | |
87 | ||
b6407c9d KM |
88 | /* |
89 | * MINBSIZE is the smallest allowable block size. | |
90 | * In order to insure that it is possible to create files of size | |
91 | * 2^32 with only two levels of indirection, MINBSIZE is set to 4096. | |
92 | * MINBSIZE must be big enough to hold a cylinder group block, | |
93 | * thus changes to (struct cg) must keep its size within MINBSIZE. | |
94 | * MAXCPG is limited only to dimension an array in (struct cg); | |
95 | * it can be made larger as long as that structures size remains | |
96 | * within the bounds dictated by MINBSIZE. | |
97 | * Note that super blocks are always of size MAXBSIZE, | |
98 | * and that MAXBSIZE must be >= MINBSIZE. | |
99 | */ | |
100 | #define MINBSIZE 4096 | |
101 | #define DESCPG 16 /* desired fs_cpg */ | |
102 | #define MAXCPG 32 /* maximum fs_cpg */ | |
1ef63481 KM |
103 | |
104 | /* | |
105 | * Super block for a file system. | |
106 | * | |
b6407c9d KM |
107 | * The super block is nominally located at disk block SBLOCK. |
108 | * Inode 0 can't be used for normal purposes, | |
109 | * historically bad blocks were linked to inode 1, | |
a8c2d86a KM |
110 | * thus the root inode is 2. (inode 1 is no longer used for |
111 | * this purpose, however numerous dump tapes make this | |
112 | * assumption, so we are stuck with it) | |
1ef63481 | 113 | */ |
a8c2d86a | 114 | #define ROOTINO ((ino_t)2) /* i number of all roots */ |
1ef63481 KM |
115 | |
116 | #define FS_MAGIC 0x110854 | |
117 | struct fs | |
118 | { | |
119 | long fs_magic; /* magic number */ | |
120 | daddr_t fs_sblkno; /* offset of super-block in filesys */ | |
121 | time_t fs_time; /* last time written */ | |
003319d1 KM |
122 | long fs_size; /* number of blocks in fs */ |
123 | long fs_dsize; /* number of data blocks in fs */ | |
124 | long fs_ncg; /* number of cylinder groups */ | |
b6407c9d KM |
125 | long fs_bsize; /* size of basic blocks in fs */ |
126 | long fs_fsize; /* size of frag blocks in fs */ | |
127 | long fs_frag; /* number of frags in a block in fs */ | |
128 | short fs_minfree; /* minimum percentage of free blocks */ | |
129 | short fs_rotdelay; /* num of ms for optimal next block */ | |
1ef63481 | 130 | /* sizes determined by number of cylinder groups and their sizes */ |
b6407c9d | 131 | daddr_t fs_csaddr; /* blk addr of cyl grp summary area */ |
003319d1 KM |
132 | long fs_cssize; /* size of cyl grp summary area */ |
133 | long fs_cgsize; /* cylinder group size */ | |
1ef63481 KM |
134 | /* these fields should be derived from the hardware */ |
135 | short fs_ntrak; /* tracks per cylinder */ | |
136 | short fs_nsect; /* sectors per track */ | |
003319d1 | 137 | long fs_spc; /* sectors per cylinder */ |
1ef63481 | 138 | /* this comes from the disk driver partitioning */ |
003319d1 | 139 | long fs_ncyl; /* cylinders in file system */ |
1ef63481 KM |
140 | /* these fields can be computed from the others */ |
141 | short fs_cpg; /* cylinders per group */ | |
1ef63481 | 142 | short fs_ipg; /* inodes per group */ |
b6407c9d | 143 | long fs_fpg; /* blocks per group * fs_frag */ |
003319d1 KM |
144 | /* this data must be re-computed after crashes */ |
145 | struct csum fs_cstotal; /* cylinder summary information */ | |
1ef63481 KM |
146 | /* these fields are cleared at mount time */ |
147 | char fs_fmod; /* super block modified flag */ | |
148 | char fs_ronly; /* mounted read-only flag */ | |
003319d1 | 149 | char fs_fsmnt[34]; /* name mounted on */ |
743f1ef7 | 150 | /* these fields retain the current block allocation info */ |
003319d1 | 151 | long fs_cgrotor; /* last cg searched */ |
743f1ef7 KM |
152 | struct csum *fs_csp[NBUF]; /* list of fs_cs info buffers */ |
153 | short fs_postbl[NRPOS]; /* head of blocks for each rotation */ | |
154 | short fs_rotbl[1]; /* list of blocks for each rotation */ | |
155 | /* actually longer */ | |
1ef63481 | 156 | }; |
b6407c9d KM |
157 | |
158 | /* | |
159 | * convert cylinder group to base address of its global summary info. | |
160 | * N.B. This macro assumes that sizeof(struct csum) is a power of two. | |
161 | */ | |
162 | #define fs_cs(fs, indx) \ | |
163 | fs_csp[(indx) / ((fs)->fs_bsize / sizeof(struct csum))] \ | |
164 | [(indx) % ((fs)->fs_bsize / sizeof(struct csum))] | |
1ef63481 KM |
165 | |
166 | /* | |
167 | * Cylinder group macros to locate things in cylinder groups. | |
168 | */ | |
169 | ||
170 | /* cylinder group to disk block at very beginning */ | |
171 | #define cgbase(c,fs) ((daddr_t)((fs)->fs_fpg*(c))) | |
172 | ||
173 | /* cylinder group to spare super block address */ | |
174 | #define cgsblock(c,fs) \ | |
b6407c9d | 175 | (cgbase(c,fs) + dbtofsb(fs, SBLOCK)) |
1ef63481 KM |
176 | |
177 | /* convert cylinder group to index of its cg block */ | |
178 | #define cgtod(c,fs) \ | |
b6407c9d | 179 | (cgbase(c,fs) + CBLOCK(fs)) |
1ef63481 KM |
180 | |
181 | /* give address of first inode block in cylinder group */ | |
182 | #define cgimin(c,fs) \ | |
b6407c9d | 183 | (cgbase(c,fs) + IBLOCK(fs)) |
1ef63481 KM |
184 | |
185 | /* give address of first data block in cylinder group */ | |
b6407c9d | 186 | #define cgdmin(c,fs) (cgimin(c,fs) + (fs)->fs_ipg / INOPF(fs)) |
1ef63481 KM |
187 | |
188 | /* turn inode number into cylinder group number */ | |
189 | #define itog(x,fs) ((x)/(fs)->fs_ipg) | |
190 | ||
b6407c9d KM |
191 | /* turn inode number into file system block address */ |
192 | #define itod(x,fs) ((daddr_t)(cgimin(itog(x,fs),fs)+(fs)->fs_frag*((x)%(fs)->fs_ipg/INOPB(fs)))) | |
1ef63481 | 193 | |
b6407c9d KM |
194 | /* turn inode number into file system block offset */ |
195 | #define itoo(x,fs) ((x)%INOPB(fs)) | |
1ef63481 | 196 | |
b6407c9d | 197 | /* give cylinder group number for a file system block */ |
1ef63481 KM |
198 | #define dtog(d,fs) ((d)/(fs)->fs_fpg) |
199 | ||
b6407c9d | 200 | /* give cylinder group block number for a file system block */ |
1ef63481 KM |
201 | #define dtogd(d,fs) ((d)%(fs)->fs_fpg) |
202 | ||
203 | /* | |
204 | * Cylinder group related limits. | |
205 | */ | |
206 | ||
1ef63481 KM |
207 | /* |
208 | * MAXIPG bounds the number of inodes per cylinder group, and | |
209 | * is needed only to keep the structure simpler by having the | |
210 | * only a single variable size element (the free bit map). | |
211 | * | |
212 | * N.B.: MAXIPG must be a multiple of INOPB. | |
213 | */ | |
214 | #define MAXIPG 2048 /* max number inodes/cyl group */ | |
215 | ||
216 | /* | |
217 | * MAXBPG bounds the number of blocks of data per cylinder group, | |
218 | * and is limited by the fact that cylinder groups are at most one block. | |
219 | * Its size is derived from the size of blocks and the (struct cg) size, | |
220 | * by the number of remaining bits. | |
221 | */ | |
b6407c9d KM |
222 | #define MAXBPG(fs) \ |
223 | (NBBY*((fs)->fs_bsize-(sizeof (struct cg)))/(fs)->fs_frag) | |
1ef63481 KM |
224 | |
225 | #define CG_MAGIC 0x092752 | |
226 | struct cg { | |
227 | long cg_magic; /* magic number */ | |
228 | time_t cg_time; /* time last written */ | |
003319d1 | 229 | long cg_cgx; /* we are the cgx'th cylinder group */ |
1ef63481 KM |
230 | short cg_ncyl; /* number of cyl's this cg */ |
231 | short cg_niblk; /* number of inode blocks this cg */ | |
003319d1 KM |
232 | long cg_ndblk; /* number of data blocks this cg */ |
233 | struct csum cg_cs; /* cylinder summary information */ | |
234 | long cg_rotor; /* position of last used block */ | |
235 | long cg_frotor; /* position of last used frag */ | |
236 | long cg_irotor; /* position of last used inode */ | |
b6407c9d | 237 | long cg_frsum[MAXFRAG]; /* counts of available frags */ |
1ef63481 KM |
238 | short cg_b[MAXCPG][NRPOS]; /* positions of free blocks */ |
239 | char cg_iused[MAXIPG/NBBY]; /* used inode map */ | |
240 | char cg_free[1]; /* free block map */ | |
241 | /* actually longer */ | |
242 | }; | |
243 | #define cgsize(fp) (sizeof (struct cg) + ((fp)->fs_fpg+NBBY-1)/NBBY) | |
244 | ||
245 | #ifdef KERNEL | |
246 | struct fs *getfs(); | |
247 | #endif |