This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / sys / ufs / fs.h
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
78ed81a3 33 * from: @(#)fs.h 7.12 (Berkeley) 5/8/91
34 * $Id$
15637ed4
RG
35 */
36
37/*
38 * Each disk drive contains some number of file systems.
39 * A file system consists of a number of cylinder groups.
40 * Each cylinder group has inodes and data.
41 *
42 * A file system is described by its super-block, which in turn
43 * describes the cylinder groups. The super-block is critical
44 * data and is replicated in each cylinder group to protect against
45 * catastrophic loss. This is done at `newfs' time and the critical
46 * super-block data does not change, so the copies need not be
47 * referenced further unless disaster strikes.
48 *
49 * For file system fs, the offsets of the various blocks of interest
50 * are given in the super block as:
51 * [fs->fs_sblkno] Super-block
52 * [fs->fs_cblkno] Cylinder group block
53 * [fs->fs_iblkno] Inode blocks
54 * [fs->fs_dblkno] Data blocks
55 * The beginning of cylinder group cg in fs, is given by
56 * the ``cgbase(fs, cg)'' macro.
57 *
58 * The first boot and super blocks are given in absolute disk addresses.
59 * The byte-offset forms are preferred, as they don't imply a sector size.
60 */
61#define BBSIZE 8192
62#define SBSIZE 8192
63#define BBOFF ((off_t)(0))
64#define SBOFF ((off_t)(BBOFF + BBSIZE))
65#define BBLOCK ((daddr_t)(0))
66#define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
67
68/*
69 * Addresses stored in inodes are capable of addressing fragments
70 * of `blocks'. File system blocks of at most size MAXBSIZE can
71 * be optionally broken into 2, 4, or 8 pieces, each of which is
72 * addressible; these pieces may be DEV_BSIZE, or some multiple of
73 * a DEV_BSIZE unit.
74 *
75 * Large files consist of exclusively large data blocks. To avoid
76 * undue wasted disk space, the last data block of a small file may be
77 * allocated as only as many fragments of a large block as are
78 * necessary. The file system format retains only a single pointer
79 * to such a fragment, which is a piece of a single large block that
80 * has been divided. The size of such a fragment is determinable from
81 * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
82 *
83 * The file system records space availability at the fragment level;
84 * to determine block availability, aligned fragments are examined.
85 *
86 * The root inode is the root of the file system.
87 * Inode 0 can't be used for normal purposes and
88 * historically bad blocks were linked to inode 1,
89 * thus the root inode is 2. (inode 1 is no longer used for
90 * this purpose, however numerous dump tapes make this
91 * assumption, so we are stuck with it)
92 */
93#define ROOTINO ((ino_t)2)
94
95/*
96 * MINBSIZE is the smallest allowable block size.
97 * In order to insure that it is possible to create files of size
98 * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
99 * MINBSIZE must be big enough to hold a cylinder group block,
100 * thus changes to (struct cg) must keep its size within MINBSIZE.
101 * Note that super blocks are always of size SBSIZE,
102 * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
103 */
104#define MINBSIZE 4096
105
106/*
107 * The path name on which the file system is mounted is maintained
108 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
109 * the super block for this name.
110 * The limit on the amount of summary information per file system
111 * is defined by MAXCSBUFS. It is currently parameterized for a
112 * maximum of two million cylinders.
113 */
114#define MAXMNTLEN 512
115#define MAXCSBUFS 32
116
117/*
118 * Per cylinder group information; summarized in blocks allocated
119 * from first cylinder group data blocks. These blocks have to be
120 * read in from fs_csaddr (size fs_cssize) in addition to the
121 * super block.
122 *
123 * N.B. sizeof(struct csum) must be a power of two in order for
124 * the ``fs_cs'' macro to work (see below).
125 */
126struct csum {
127 long cs_ndir; /* number of directories */
128 long cs_nbfree; /* number of free blocks */
129 long cs_nifree; /* number of free inodes */
130 long cs_nffree; /* number of free frags */
131};
132
133/*
134 * Super block for a file system.
135 */
136#define FS_MAGIC 0x011954
137#define FSOKAY 0x7c269d38
138struct fs
139{
140 struct fs *fs_link; /* linked list of file systems */
141 struct fs *fs_rlink; /* used for incore super blocks */
142 daddr_t fs_sblkno; /* addr of super-block in filesys */
143 daddr_t fs_cblkno; /* offset of cyl-block in filesys */
144 daddr_t fs_iblkno; /* offset of inode-blocks in filesys */
145 daddr_t fs_dblkno; /* offset of first data after cg */
146 long fs_cgoffset; /* cylinder group offset in cylinder */
147 long fs_cgmask; /* used to calc mod fs_ntrak */
148 time_t fs_time; /* last time written */
149 long fs_size; /* number of blocks in fs */
150 long fs_dsize; /* number of data blocks in fs */
151 long fs_ncg; /* number of cylinder groups */
152 long fs_bsize; /* size of basic blocks in fs */
153 long fs_fsize; /* size of frag blocks in fs */
154 long fs_frag; /* number of frags in a block in fs */
155/* these are configuration parameters */
156 long fs_minfree; /* minimum percentage of free blocks */
157 long fs_rotdelay; /* num of ms for optimal next block */
158 long fs_rps; /* disk revolutions per second */
159/* these fields can be computed from the others */
160 long fs_bmask; /* ``blkoff'' calc of blk offsets */
161 long fs_fmask; /* ``fragoff'' calc of frag offsets */
162 long fs_bshift; /* ``lblkno'' calc of logical blkno */
163 long fs_fshift; /* ``numfrags'' calc number of frags */
164/* these are configuration parameters */
165 long fs_maxcontig; /* max number of contiguous blks */
166 long fs_maxbpg; /* max number of blks per cyl group */
167/* these fields can be computed from the others */
168 long fs_fragshift; /* block to frag shift */
169 long fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
170 long fs_sbsize; /* actual size of super block */
171 long fs_csmask; /* csum block offset */
172 long fs_csshift; /* csum block number */
173 long fs_nindir; /* value of NINDIR */
174 long fs_inopb; /* value of INOPB */
175 long fs_nspf; /* value of NSPF */
176/* yet another configuration parameter */
177 long fs_optim; /* optimization preference, see below */
178/* these fields are derived from the hardware */
179 long fs_npsect; /* # sectors/track including spares */
180 long fs_interleave; /* hardware sector interleave */
181 long fs_trackskew; /* sector 0 skew, per track */
182 long fs_headswitch; /* head switch time, usec */
183 long fs_trkseek; /* track-to-track seek, usec */
184/* sizes determined by number of cylinder groups and their sizes */
185 daddr_t fs_csaddr; /* blk addr of cyl grp summary area */
186 long fs_cssize; /* size of cyl grp summary area */
187 long fs_cgsize; /* cylinder group size */
188/* these fields are derived from the hardware */
189 long fs_ntrak; /* tracks per cylinder */
190 long fs_nsect; /* sectors per track */
191 long fs_spc; /* sectors per cylinder */
192/* this comes from the disk driver partitioning */
193 long fs_ncyl; /* cylinders in file system */
194/* these fields can be computed from the others */
195 long fs_cpg; /* cylinders per group */
196 long fs_ipg; /* inodes per group */
197 long fs_fpg; /* blocks per group * fs_frag */
198/* this data must be re-computed after crashes */
199 struct csum fs_cstotal; /* cylinder summary information */
200/* these fields are cleared at mount time */
201 char fs_fmod; /* super block modified flag */
202 char fs_clean; /* file system is clean flag */
203 char fs_ronly; /* mounted read-only flag */
204 char fs_flags; /* currently unused flag */
205 char fs_fsmnt[MAXMNTLEN]; /* name mounted on */
206/* these fields retain the current block allocation info */
207 long fs_cgrotor; /* last cg searched */
208 struct csum *fs_csp[MAXCSBUFS];/* list of fs_cs info buffers */
209 long fs_cpc; /* cyl per cycle in postbl */
210 short fs_opostbl[16][8]; /* old rotation block list head */
211 long fs_sparecon[55]; /* reserved for future constants */
212 long fs_state; /* validate fs_clean field */
213 quad fs_qbmask; /* ~fs_bmask - for use with quad size */
214 quad fs_qfmask; /* ~fs_fmask - for use with quad size */
215 long fs_postblformat; /* format of positional layout tables */
216 long fs_nrpos; /* number of rotaional positions */
217 long fs_postbloff; /* (short) rotation block list head */
218 long fs_rotbloff; /* (u_char) blocks for each rotation */
219 long fs_magic; /* magic number */
220 u_char fs_space[1]; /* list of blocks for each rotation */
221/* actually longer */
222};
223/*
224 * Preference for optimization.
225 */
226#define FS_OPTTIME 0 /* minimize allocation time */
227#define FS_OPTSPACE 1 /* minimize disk fragmentation */
228
229/*
230 * Rotational layout table format types
231 */
232#define FS_42POSTBLFMT -1 /* 4.2BSD rotational table format */
233#define FS_DYNAMICPOSTBLFMT 1 /* dynamic rotational table format */
234/*
235 * Macros for access to superblock array structures
236 */
237#define fs_postbl(fs, cylno) \
238 (((fs)->fs_postblformat == FS_42POSTBLFMT) \
239 ? ((fs)->fs_opostbl[cylno]) \
240 : ((short *)((char *)(fs) + (fs)->fs_postbloff) + (cylno) * (fs)->fs_nrpos))
241#define fs_rotbl(fs) \
242 (((fs)->fs_postblformat == FS_42POSTBLFMT) \
243 ? ((fs)->fs_space) \
244 : ((u_char *)((char *)(fs) + (fs)->fs_rotbloff)))
245
246/*
247 * Convert cylinder group to base address of its global summary info.
248 *
249 * N.B. This macro assumes that sizeof(struct csum) is a power of two.
250 */
251#define fs_cs(fs, indx) \
252 fs_csp[(indx) >> (fs)->fs_csshift][(indx) & ~(fs)->fs_csmask]
253
254/*
255 * Cylinder group block for a file system.
256 */
257#define CG_MAGIC 0x090255
258struct cg {
259 struct cg *cg_link; /* linked list of cyl groups */
260 long cg_magic; /* magic number */
261 time_t cg_time; /* time last written */
262 long cg_cgx; /* we are the cgx'th cylinder group */
263 short cg_ncyl; /* number of cyl's this cg */
264 short cg_niblk; /* number of inode blocks this cg */
265 long cg_ndblk; /* number of data blocks this cg */
266 struct csum cg_cs; /* cylinder summary information */
267 long cg_rotor; /* position of last used block */
268 long cg_frotor; /* position of last used frag */
269 long cg_irotor; /* position of last used inode */
270 long cg_frsum[MAXFRAG]; /* counts of available frags */
271 long cg_btotoff; /* (long) block totals per cylinder */
272 long cg_boff; /* (short) free block positions */
273 long cg_iusedoff; /* (char) used inode map */
274 long cg_freeoff; /* (u_char) free block map */
275 long cg_nextfreeoff; /* (u_char) next available space */
276 long cg_sparecon[16]; /* reserved for future use */
277 u_char cg_space[1]; /* space for cylinder group maps */
278/* actually longer */
279};
280/*
281 * Macros for access to cylinder group array structures
282 */
283#define cg_blktot(cgp) \
284 (((cgp)->cg_magic != CG_MAGIC) \
285 ? (((struct ocg *)(cgp))->cg_btot) \
286 : ((long *)((char *)(cgp) + (cgp)->cg_btotoff)))
287#define cg_blks(fs, cgp, cylno) \
288 (((cgp)->cg_magic != CG_MAGIC) \
289 ? (((struct ocg *)(cgp))->cg_b[cylno]) \
290 : ((short *)((char *)(cgp) + (cgp)->cg_boff) + (cylno) * (fs)->fs_nrpos))
291#define cg_inosused(cgp) \
292 (((cgp)->cg_magic != CG_MAGIC) \
293 ? (((struct ocg *)(cgp))->cg_iused) \
294 : ((char *)((char *)(cgp) + (cgp)->cg_iusedoff)))
295#define cg_blksfree(cgp) \
296 (((cgp)->cg_magic != CG_MAGIC) \
297 ? (((struct ocg *)(cgp))->cg_free) \
298 : ((u_char *)((char *)(cgp) + (cgp)->cg_freeoff)))
299#define cg_chkmagic(cgp) \
300 ((cgp)->cg_magic == CG_MAGIC || ((struct ocg *)(cgp))->cg_magic == CG_MAGIC)
301
302/*
303 * The following structure is defined
304 * for compatibility with old file systems.
305 */
306struct ocg {
307 struct ocg *cg_link; /* linked list of cyl groups */
308 struct ocg *cg_rlink; /* used for incore cyl groups */
309 time_t cg_time; /* time last written */
310 long cg_cgx; /* we are the cgx'th cylinder group */
311 short cg_ncyl; /* number of cyl's this cg */
312 short cg_niblk; /* number of inode blocks this cg */
313 long cg_ndblk; /* number of data blocks this cg */
314 struct csum cg_cs; /* cylinder summary information */
315 long cg_rotor; /* position of last used block */
316 long cg_frotor; /* position of last used frag */
317 long cg_irotor; /* position of last used inode */
318 long cg_frsum[8]; /* counts of available frags */
319 long cg_btot[32]; /* block totals per cylinder */
320 short cg_b[32][8]; /* positions of free blocks */
321 char cg_iused[256]; /* used inode map */
322 long cg_magic; /* magic number */
323 u_char cg_free[1]; /* free block map */
324/* actually longer */
325};
326
327/*
328 * Turn file system block numbers into disk block addresses.
329 * This maps file system blocks to device size blocks.
330 */
331#define fsbtodb(fs, b) ((b) << (fs)->fs_fsbtodb)
332#define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
333
334/*
335 * Cylinder group macros to locate things in cylinder groups.
336 * They calc file system addresses of cylinder group data structures.
337 */
338#define cgbase(fs, c) ((daddr_t)((fs)->fs_fpg * (c)))
339#define cgstart(fs, c) \
340 (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
341#define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */
342#define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */
343#define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */
344#define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */
345
346/*
347 * Macros for handling inode numbers:
348 * inode number to file system block offset.
349 * inode number to cylinder group number.
350 * inode number to file system block address.
351 */
352#define itoo(fs, x) ((x) % INOPB(fs))
353#define itog(fs, x) ((x) / (fs)->fs_ipg)
354#define itod(fs, x) \
355 ((daddr_t)(cgimin(fs, itog(fs, x)) + \
356 (blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs))))))
357
358/*
359 * Give cylinder group number for a file system block.
360 * Give cylinder group block number for a file system block.
361 */
362#define dtog(fs, d) ((d) / (fs)->fs_fpg)
363#define dtogd(fs, d) ((d) % (fs)->fs_fpg)
364
365/*
366 * Extract the bits for a block from a map.
367 * Compute the cylinder and rotational position of a cyl block addr.
368 */
369#define blkmap(fs, map, loc) \
370 (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
371#define cbtocylno(fs, bno) \
372 ((bno) * NSPF(fs) / (fs)->fs_spc)
373#define cbtorpos(fs, bno) \
374 (((bno) * NSPF(fs) % (fs)->fs_spc / (fs)->fs_nsect * (fs)->fs_trackskew + \
375 (bno) * NSPF(fs) % (fs)->fs_spc % (fs)->fs_nsect * (fs)->fs_interleave) % \
376 (fs)->fs_nsect * (fs)->fs_nrpos / (fs)->fs_npsect)
377
378/*
379 * The following macros optimize certain frequently calculated
380 * quantities by using shifts and masks in place of divisions
381 * modulos and multiplications.
382 */
383#define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \
384 ((loc) & ~(fs)->fs_bmask)
385#define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \
386 ((loc) & ~(fs)->fs_fmask)
387#define lblktosize(fs, blk) /* calculates (blk * fs->fs_bsize) */ \
388 ((blk) << (fs)->fs_bshift)
389#define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \
390 ((loc) >> (fs)->fs_bshift)
391#define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
392 ((loc) >> (fs)->fs_fshift)
393#define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \
394 (((size) + (fs)->fs_bsize - 1) & (fs)->fs_bmask)
395#define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \
396 (((size) + (fs)->fs_fsize - 1) & (fs)->fs_fmask)
397#define fragstoblks(fs, frags) /* calculates (frags / fs->fs_frag) */ \
398 ((frags) >> (fs)->fs_fragshift)
399#define blkstofrags(fs, blks) /* calculates (blks * fs->fs_frag) */ \
400 ((blks) << (fs)->fs_fragshift)
401#define fragnum(fs, fsb) /* calculates (fsb % fs->fs_frag) */ \
402 ((fsb) & ((fs)->fs_frag - 1))
403#define blknum(fs, fsb) /* calculates rounddown(fsb, fs->fs_frag) */ \
404 ((fsb) &~ ((fs)->fs_frag - 1))
405
406/*
407 * Determine the number of available frags given a
408 * percentage to hold in reserve
409 */
410#define freespace(fs, percentreserved) \
411 (blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
412 (fs)->fs_cstotal.cs_nffree - ((fs)->fs_dsize * (percentreserved) / 100))
413
414/*
415 * Determining the size of a file block in the file system.
416 */
417#define blksize(fs, ip, lbn) \
418 (((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) << (fs)->fs_bshift) \
419 ? (fs)->fs_bsize \
420 : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
421#define dblksize(fs, dip, lbn) \
422 (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \
423 ? (fs)->fs_bsize \
424 : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
425
426/*
427 * Number of disk sectors per block; assumes DEV_BSIZE byte sector size.
428 */
429#define NSPB(fs) ((fs)->fs_nspf << (fs)->fs_fragshift)
430#define NSPF(fs) ((fs)->fs_nspf)
431
432/*
433 * INOPB is the number of inodes in a secondary storage block.
434 */
435#define INOPB(fs) ((fs)->fs_inopb)
436#define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift)
437
438/*
439 * NINDIR is the number of indirects in a file system block.
440 */
441#define NINDIR(fs) ((fs)->fs_nindir)