patch two holes in rename
[unix-history] / usr / src / sys / ufs / ffs / fs.h
CommitLineData
7e2a7a5c 1/* fs.h 4.7 83/01/09 */
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
80cc8328
KM
61/*
62 * Cylinder group related limits.
63 *
743f1ef7
KM
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 */
80cc8328 70#define NRPOS 8 /* number distinct rotational positions */
1ef63481
KM
71
72/*
80cc8328
KM
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).
1ef63481 78 */
80cc8328 79#define MAXIPG 2048 /* max number inodes/cyl group */
1ef63481 80
b6407c9d
KM
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 MAXBSIZE,
91 * and that MAXBSIZE must be >= MINBSIZE.
92 */
93#define MINBSIZE 4096
b6407c9d 94#define MAXCPG 32 /* maximum fs_cpg */
80cc8328 95
2c9afb08
KM
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.
1b807fc9 100 * The limit on the amount of summary information per file system
26169eda
KM
101 * is defined by MAXCSBUFS. It is currently parameterized for a
102 * maximum of two million cylinders.
2c9afb08 103 */
26169eda
KM
104#define MAXMNTLEN 512
105#define MAXCSBUFS 32
2c9afb08 106
1ef63481 107/*
80cc8328
KM
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.
1ef63481 112 *
80cc8328
KM
113 * N.B. sizeof(struct csum) must be a power of two in order for
114 * the ``fs_cs'' macro to work (see below).
1ef63481 115 */
80cc8328
KM
116struct 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};
1ef63481 122
80cc8328
KM
123/*
124 * Super block for a file system.
125 */
b9b166c6 126#define FS_MAGIC 0x011954
1ef63481
KM
127struct fs
128{
b9b166c6
KM
129 struct fs *fs_link; /* linked list of file systems */
130 struct fs *fs_rlink; /* used for incore super blocks */
26169eda 131 daddr_t fs_sblkno; /* addr of super-block in filesys */
aca50d72
KM
132 daddr_t fs_cblkno; /* offset of cyl-block in filesys */
133 daddr_t fs_iblkno; /* offset of inode-blocks in filesys */
26169eda
KM
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 */
1ef63481 137 time_t fs_time; /* last time written */
003319d1
KM
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 */
b6407c9d
KM
141 long fs_bsize; /* size of basic blocks in fs */
142 long fs_fsize; /* size of frag blocks in fs */
26169eda 143 long fs_frag; /* number of frags in a block in fs */
7e2a7a5c 144/* these are configuration parameters */
26169eda
KM
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 */
7e2a7a5c 148/* these fields can be computed from the others */
c1ab275c
KM
149 long fs_bmask; /* ``blkoff'' calc of blk offsets */
150 long fs_fmask; /* ``fragoff'' calc of frag offsets */
26169eda
KM
151 long fs_bshift; /* ``lblkno'' calc of logical blkno */
152 long fs_fshift; /* ``numfrags'' calc number of frags */
7e2a7a5c 153/* these are configuration parameters */
af0b24db
SL
154 long fs_maxcontig; /* max number of contiguous blks */
155 long fs_maxbpg; /* max number of blks per cyl group */
7e2a7a5c 156/* these fields can be computed from the others */
decc359e
KM
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 */
1ef63481 166/* sizes determined by number of cylinder groups and their sizes */
b6407c9d 167 daddr_t fs_csaddr; /* blk addr of cyl grp summary area */
003319d1
KM
168 long fs_cssize; /* size of cyl grp summary area */
169 long fs_cgsize; /* cylinder group size */
1ef63481 170/* these fields should be derived from the hardware */
26169eda
KM
171 long fs_ntrak; /* tracks per cylinder */
172 long fs_nsect; /* sectors per track */
003319d1 173 long fs_spc; /* sectors per cylinder */
1ef63481 174/* this comes from the disk driver partitioning */
003319d1 175 long fs_ncyl; /* cylinders in file system */
1ef63481 176/* these fields can be computed from the others */
26169eda
KM
177 long fs_cpg; /* cylinders per group */
178 long fs_ipg; /* inodes per group */
b6407c9d 179 long fs_fpg; /* blocks per group * fs_frag */
003319d1
KM
180/* this data must be re-computed after crashes */
181 struct csum fs_cstotal; /* cylinder summary information */
1ef63481
KM
182/* these fields are cleared at mount time */
183 char fs_fmod; /* super block modified flag */
26169eda 184 char fs_clean; /* file system is clean flag */
1ef63481 185 char fs_ronly; /* mounted read-only flag */
26169eda 186 char fs_flags; /* currently unused flag */
2c9afb08 187 char fs_fsmnt[MAXMNTLEN]; /* name mounted on */
743f1ef7 188/* these fields retain the current block allocation info */
003319d1 189 long fs_cgrotor; /* last cg searched */
1b807fc9 190 struct csum *fs_csp[MAXCSBUFS];/* list of fs_cs info buffers */
26169eda 191 long fs_cpc; /* cyl per cycle in postbl */
aca50d72 192 short fs_postbl[MAXCPG][NRPOS];/* head of blocks for each rotation */
b9b166c6 193 long fs_magic; /* magic number */
aca50d72 194 u_char fs_rotbl[1]; /* list of blocks for each rotation */
743f1ef7 195/* actually longer */
1ef63481 196};
b6407c9d
KM
197
198/*
2c9afb08 199 * Convert cylinder group to base address of its global summary info.
80cc8328 200 *
b6407c9d
KM
201 * N.B. This macro assumes that sizeof(struct csum) is a power of two.
202 */
203#define fs_cs(fs, indx) \
7e2a7a5c 204 fs_csp[(indx) >> (fs)->fs_csshift][(indx) & ~(fs)->fs_csmask]
1ef63481
KM
205
206/*
aca50d72
KM
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)
1ef63481 214 */
aca50d72 215#define MAXBPC (SBSIZE - sizeof (struct fs))
1ef63481
KM
216
217/*
80cc8328 218 * Cylinder group block for a file system.
1ef63481 219 */
b9b166c6 220#define CG_MAGIC 0x090255
1ef63481 221struct cg {
b9b166c6
KM
222 struct cg *cg_link; /* linked list of cyl groups */
223 struct cg *cg_rlink; /* used for incore cyl groups */
1ef63481 224 time_t cg_time; /* time last written */
003319d1 225 long cg_cgx; /* we are the cgx'th cylinder group */
1ef63481
KM
226 short cg_ncyl; /* number of cyl's this cg */
227 short cg_niblk; /* number of inode blocks this cg */
003319d1
KM
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 */
b6407c9d 233 long cg_frsum[MAXFRAG]; /* counts of available frags */
d37938c8 234 long cg_btot[MAXCPG]; /* block totals per cylinder */
1ef63481
KM
235 short cg_b[MAXCPG][NRPOS]; /* positions of free blocks */
236 char cg_iused[MAXIPG/NBBY]; /* used inode map */
b9b166c6 237 long cg_magic; /* magic number */
110d7a57 238 u_char cg_free[1]; /* free block map */
1ef63481
KM
239/* actually longer */
240};
80cc8328
KM
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) \
7e2a7a5c 249 (NBBY * ((fs)->fs_bsize - (sizeof (struct cg))) >> (fs)->fs_fragshift)
80cc8328
KM
250
251/*
252 * Turn file system block numbers into disk block addresses.
253 * This maps file system blocks to device size blocks.
254 */
7e2a7a5c
KM
255#define fsbtodb(fs, b) ((b) << (fs)->fs_fsbtodb)
256#define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
80cc8328
KM
257
258/*
259 * Cylinder group macros to locate things in cylinder groups.
26169eda 260 * They calc file system addresses of cylinder group data structures.
80cc8328 261 */
26169eda
KM
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 */
80cc8328
KM
269
270/*
2c9afb08
KM
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.
80cc8328 275 */
6994bf5d
KM
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)) + \
7e2a7a5c 280 ((((x) % (fs)->fs_ipg) / INOPB(fs)) << (fs)->fs_fragshift)))
80cc8328 281
80cc8328 282/*
2c9afb08
KM
283 * Give cylinder group number for a file system block.
284 * Give cylinder group block number for a file system block.
80cc8328 285 */
6994bf5d
KM
286#define dtog(fs, d) ((d) / (fs)->fs_fpg)
287#define dtogd(fs, d) ((d) % (fs)->fs_fpg)
80cc8328 288
aca50d72 289/*
2c9afb08
KM
290 * Extract the bits for a block from a map.
291 * Compute the cylinder and rotational position of a cyl block addr.
aca50d72 292 */
2c9afb08
KM
293#define blkmap(fs, map, loc) \
294 (((map)[loc / NBBY] >> (loc % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
aca50d72
KM
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
c1ab275c
KM
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
80cc8328 318/*
2c9afb08 319 * Determining the size of a file block in the file system.
80cc8328
KM
320 */
321#define blksize(fs, ip, lbn) \
7e2a7a5c 322 (((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) << (fs)->fs_bshift) \
c1ab275c
KM
323 ? (fs)->fs_bsize \
324 : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
80cc8328 325#define dblksize(fs, dip, lbn) \
7e2a7a5c 326 (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \
c1ab275c
KM
327 ? (fs)->fs_bsize \
328 : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
80cc8328
KM
329
330/*
2c9afb08 331 * Number of disk sectors per block; assumes DEV_BSIZE byte sector size.
80cc8328 332 */
7e2a7a5c
KM
333#define NSPB(fs) ((fs)->fs_nspf << (fs)->fs_fragshift)
334#define NSPF(fs) ((fs)->fs_nspf)
80cc8328
KM
335
336/*
2c9afb08 337 * INOPB is the number of inodes in a secondary storage block.
80cc8328 338 */
7e2a7a5c
KM
339#define INOPB(fs) ((fs)->fs_inopb)
340#define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift)
80cc8328
KM
341
342/*
2c9afb08 343 * NINDIR is the number of indirects in a file system block.
80cc8328 344 */
7e2a7a5c 345#define NINDIR(fs) ((fs)->fs_nindir)
1ef63481
KM
346
347#ifdef KERNEL
348struct fs *getfs();
1bbbd864 349struct fs *mountfs();
1ef63481 350#endif