maintaining and using cg_frsum to do intelligent allocation of FRAGs
[unix-history] / usr / src / sys / ufs / ffs / fs.h
CommitLineData
1ef63481
KM
1/* Copyright (c) 1981 Regents of the University of California */
2
f3c028b7 3/* fs.h 1.2 %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
20 * [CBLOCK] Cylinder group block
21 * [IBLOCK..IBLOCK+fs.fs_ipg/INOPB)
22 * Inode blocks
23 * [IBLOCK+fs.fs_ipg/INOPB..fs.fs_fpg/FRAG)
24 * Data blocks
25 * The beginning of data blocks for cg in fs is also given by cgdmin(cg,fs).
26 */
27#define BBLOCK ((daddr_t)(0*FRAG))
28#define SBLOCK ((daddr_t)(1*FRAG))
29#define CBLOCK ((daddr_t)(2*FRAG))
30#define IBLOCK ((daddr_t)(3*FRAG))
31
32/*
33 * Addresses stored in inodes are capable of addressing fragments of `blocks.'
34 * File system blocks of size BSIZE can be broken into FRAG pieces,
35 * each of which is addressible; these pieces may be sectors, or some
36 * multiple of a sector size (e.g. 1k byte units).
37 *
38 * Large files consist of exclusively large (BSIZE) data blocks. To avoid
39 * undue fragmentation, the last data block of a small file may be
40 * allocated as only as many pieces
41 * of a large block as are necessary. The file system format retains
42 * only a single pointer to such a fragment, which is a piece of a single
43 * BSIZE block which has been divided. The size of such a fragment is
44 * determinable from information in the inode.
45 *
46 * The file system records space availability at the fragment level;
47 * to determine block availability, aligned fragments are examined.
48 */
49
50/*
51 * Information per cylinder group summarized in blocks allocated
52 * from first cylinder group data blocks. These blocks have to be
53 * specially noticed in mkfs, icheck and fsck.
54 */
55struct csum {
56 short cs_ndir; /* number of directories */
57 short cs_nbfree; /* number of free blocks */
58 short cs_nifree; /* number of free inodes */
59 short cs_xx; /* for later use... */
60};
61#define cssize(fs) ((fs)->fs_ncg*sizeof(struct csum))
62#define csaddr(fs) (cgdmin(0, fs))
63
64/*
65 * Each file system has a number of inodes statically allocated.
66 * We allocate one inode slot per NBPI data bytes, expecting this
67 * to be far more than we will ever need. Actually, the directory
68 * structure has inode numbers kept in 16 bits, so no more than
69 * 65K inodes are possible, and this usually cuts off well above
70 * the number suggested by NBPI.
71 *
72 * THE DIRECTORY STRUCTURE SHOULD BE CHANGED SOON TO ALLOW
73 * LARGER INODE NUMBERS (SEE DIR.H).
74 */
75#define NBPI 2048
76
77#define DESCPG 8 /* desired fs_cpg */
78#define MAXCPG 16 /* maximum fs_cpg */
79/* MAXCPG is limited only to dimension an array in (struct cg); */
80/* it can be made larger as long as that structures size remains sane. */
81
82/*
83 * Super block for a file system.
84 *
85 * The super block is nominally located at disk block 1 although
86 * this is naive due to bad blocks. Inode 0 can't be used for normal
87 * purposes, thus the root inode is inode 1.
88 */
89#define ROOTINO ((ino_t)1) /* i number of all roots */
90
91#define FS_MAGIC 0x110854
92struct fs
93{
94 long fs_magic; /* magic number */
95 daddr_t fs_sblkno; /* offset of super-block in filesys */
96 time_t fs_time; /* last time written */
97 daddr_t fs_size; /* number of blocks in fs */
98 short fs_ncg; /* number of cylinder groups */
99/* sizes determined by number of cylinder groups and their sizes */
100 short fs_cssize; /* size of cyl grp summary area */
101 short fs_cgsize; /* cylinder group size */
102/* these fields should be derived from the hardware */
103 short fs_ntrak; /* tracks per cylinder */
104 short fs_nsect; /* sectors per track */
105 short fs_spc; /* sectors per cylinder */
106/* this comes from the disk driver partitioning */
107 short fs_ncyl; /* cylinders in file system */
108/* these fields can be computed from the others */
109 short fs_cpg; /* cylinders per group */
110 short fs_fpg; /* blocks per group*FRAG */
111 short fs_ipg; /* inodes per group */
112/* these fields must be re-computed after crashes */
113 daddr_t fs_nffree; /* total free fragments */
114 daddr_t fs_nbfree; /* free data blocks */
115 ino_t fs_nifree; /* total free inodes */
116/* these fields are cleared at mount time */
117 char fs_fmod; /* super block modified flag */
118 char fs_ronly; /* mounted read-only flag */
119 char fs_fsmnt[32]; /* name mounted on */
120 struct csum *fs_cs;
121};
122
123/*
124 * Cylinder group macros to locate things in cylinder groups.
125 */
126
127/* cylinder group to disk block at very beginning */
128#define cgbase(c,fs) ((daddr_t)((fs)->fs_fpg*(c)))
129
130/* cylinder group to spare super block address */
131#define cgsblock(c,fs) \
132 (cgbase(c,fs) + SBLOCK)
133
134/* convert cylinder group to index of its cg block */
135#define cgtod(c,fs) \
136 (cgbase(c,fs) + CBLOCK)
137
138/* give address of first inode block in cylinder group */
139#define cgimin(c,fs) \
140 (cgbase(c,fs) + IBLOCK)
141
142/* give address of first data block in cylinder group */
143#define cgdmin(c,fs) (cgimin(c,fs) + (fs)->fs_ipg / INOPF)
144
145/* turn inode number into cylinder group number */
146#define itog(x,fs) ((x)/(fs)->fs_ipg)
147
148/* turn inode number into disk block address */
149#define itod(x,fs) (cgimin(itog(x,fs),fs)+FRAG*((x)%(fs)->fs_ipg/INOPB))
150
151/* turn inode number into disk block offset */
152#define itoo(x) ((x)%INOPB)
153
154/* give cylinder group number for a disk block */
155#define dtog(d,fs) ((d)/(fs)->fs_fpg)
156
157/* give cylinder group block number for a disk block */
158#define dtogd(d,fs) ((d)%(fs)->fs_fpg)
159
160/*
161 * Cylinder group related limits.
162 */
163
164/*
165 * For each cylinder we keep track of the availability of blocks at different
166 * rotational positions, so that we can lay out the data to be picked
167 * up with minimum rotational latency. NRPOS is the number of rotational
168 * positions which we distinguish. With NRPOS 16 the resolution of our
169 * summary information is 1ms for a typical 3600 rpm drive.
170 */
171#define NRPOS 16 /* number distinct rotational positions */
172
173/*
174 * MAXIPG bounds the number of inodes per cylinder group, and
175 * is needed only to keep the structure simpler by having the
176 * only a single variable size element (the free bit map).
177 *
178 * N.B.: MAXIPG must be a multiple of INOPB.
179 */
180#define MAXIPG 2048 /* max number inodes/cyl group */
181
182/*
183 * MAXBPG bounds the number of blocks of data per cylinder group,
184 * and is limited by the fact that cylinder groups are at most one block.
185 * Its size is derived from the size of blocks and the (struct cg) size,
186 * by the number of remaining bits.
187 */
188#define MAXBPG (NBBY*(BSIZE-(sizeof (struct cg)))/FRAG)
189
190#define CG_MAGIC 0x092752
191struct cg {
192 long cg_magic; /* magic number */
193 time_t cg_time; /* time last written */
194 short cg_cgx; /* we are the cgx'th cylinder group */
195 short cg_ncyl; /* number of cyl's this cg */
196 short cg_niblk; /* number of inode blocks this cg */
197 short cg_ndblk; /* number of data blocks this cg */
198 short cg_nifree; /* free inodes */
199 short cg_ndir; /* allocated directories */
200 short cg_nffree; /* free block fragments */
201 short cg_nbfree; /* free blocks */
202 short cg_rotor; /* position of last used block */
203 short cg_irotor; /* position of last used inode */
f3c028b7
KM
204 short cg_frotor; /* position of last used frag */
205 short cg_frsum[FRAG]; /* counts of available frags */
1ef63481
KM
206 short cg_b[MAXCPG][NRPOS]; /* positions of free blocks */
207 char cg_iused[MAXIPG/NBBY]; /* used inode map */
208 char cg_free[1]; /* free block map */
209/* actually longer */
210};
211#define cgsize(fp) (sizeof (struct cg) + ((fp)->fs_fpg+NBBY-1)/NBBY)
212
213#ifdef KERNEL
214struct fs *getfs();
f3c028b7
KM
215int inside[], around[];
216char fragtbl[];
1ef63481 217#endif