1 /* 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)fs.h 8.8 (Berkeley) 07/08/94 8 */ 9 10 /* 11 * Each disk drive contains some number of file systems. 12 * A file system consists of a number of cylinder groups. 13 * Each cylinder group has inodes and data. 14 * 15 * A file system is described by its super-block, which in turn 16 * describes the cylinder groups. The super-block is critical 17 * data and is replicated in each cylinder group to protect against 18 * catastrophic loss. This is done at `newfs' time and the critical 19 * super-block data does not change, so the copies need not be 20 * referenced further unless disaster strikes. 21 * 22 * For file system fs, the offsets of the various blocks of interest 23 * are given in the super block as: 24 * [fs->fs_sblkno] Super-block 25 * [fs->fs_cblkno] Cylinder group block 26 * [fs->fs_iblkno] Inode blocks 27 * [fs->fs_dblkno] Data blocks 28 * The beginning of cylinder group cg in fs, is given by 29 * the ``cgbase(fs, cg)'' macro. 30 * 31 * The first boot and super blocks are given in absolute disk addresses. 32 * The byte-offset forms are preferred, as they don't imply a sector size. 33 */ 34 #define BBSIZE 8192 35 #define SBSIZE 8192 36 #define BBOFF ((off_t)(0)) 37 #define SBOFF ((off_t)(BBOFF + BBSIZE)) 38 #define BBLOCK ((daddr_t)(0)) 39 #define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE)) 40 41 /* 42 * Addresses stored in inodes are capable of addressing fragments 43 * of `blocks'. File system blocks of at most size MAXBSIZE can 44 * be optionally broken into 2, 4, or 8 pieces, each of which is 45 * addressible; these pieces may be DEV_BSIZE, or some multiple of 46 * a DEV_BSIZE unit. 47 * 48 * Large files consist of exclusively large data blocks. To avoid 49 * undue wasted disk space, the last data block of a small file may be 50 * allocated as only as many fragments of a large block as are 51 * necessary. The file system format retains only a single pointer 52 * to such a fragment, which is a piece of a single large block that 53 * has been divided. The size of such a fragment is determinable from 54 * information in the inode, using the ``blksize(fs, ip, lbn)'' macro. 55 * 56 * The file system records space availability at the fragment level; 57 * to determine block availability, aligned fragments are examined. 58 */ 59 60 /* 61 * MINBSIZE is the smallest allowable block size. 62 * In order to insure that it is possible to create files of size 63 * 2^32 with only two levels of indirection, MINBSIZE is set to 4096. 64 * MINBSIZE must be big enough to hold a cylinder group block, 65 * thus changes to (struct cg) must keep its size within MINBSIZE. 66 * Note that super blocks are always of size SBSIZE, 67 * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE. 68 */ 69 #define MINBSIZE 4096 70 71 /* 72 * The path name on which the file system is mounted is maintained 73 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in 74 * the super block for this name. 75 */ 76 #define MAXMNTLEN 512 77 78 /* 79 * The limit on the amount of summary information per file system 80 * is defined by MAXCSBUFS. It is currently parameterized for a 81 * size of 128 bytes (2 million cylinder groups on machines with 82 * 32-bit pointers, and 1 million on 64-bit machines). 83 */ 84 #define MAXCSBUFS (128 / sizeof(void *)) 85 86 /* 87 * A summary of contiguous blocks of various sizes is maintained 88 * in each cylinder group. Normally this is set by the initial 89 * value of fs_maxcontig. To conserve space, a maximum summary size 90 * is set by FS_MAXCONTIG. 91 */ 92 #define FS_MAXCONTIG 16 93 94 /* 95 * MINFREE gives the minimum acceptable percentage of file system 96 * blocks which may be free. If the freelist drops below this level 97 * only the superuser may continue to allocate blocks. This may 98 * be set to 0 if no reserve of free blocks is deemed necessary, 99 * however throughput drops by fifty percent if the file system 100 * is run at between 95% and 100% full; thus the minimum default 101 * value of fs_minfree is 5%. However, to get good clustering 102 * performance, 10% is a better choice. hence we use 10% as our 103 * default value. With 10% free space, fragmentation is not a 104 * problem, so we choose to optimize for time. 105 */ 106 #define MINFREE 5 107 #define DEFAULTOPT FS_OPTTIME 108 109 /* 110 * Per cylinder group information; summarized in blocks allocated 111 * from first cylinder group data blocks. These blocks have to be 112 * read in from fs_csaddr (size fs_cssize) in addition to the 113 * super block. 114 * 115 * N.B. sizeof(struct csum) must be a power of two in order for 116 * the ``fs_cs'' macro to work (see below). 117 */ 118 struct csum { 119 int32_t cs_ndir; /* number of directories */ 120 int32_t cs_nbfree; /* number of free blocks */ 121 int32_t cs_nifree; /* number of free inodes */ 122 int32_t cs_nffree; /* number of free frags */ 123 }; 124 125 /* 126 * Super block for an FFS file system. 127 */ 128 struct fs { 129 int32_t unused_1; /* historic file system linked list, */ 130 int32_t unused_2; /* used for incore super blocks */ 131 daddr_t fs_sblkno; /* addr of super-block in filesys */ 132 daddr_t fs_cblkno; /* offset of cyl-block in filesys */ 133 daddr_t fs_iblkno; /* offset of inode-blocks in filesys */ 134 daddr_t fs_dblkno; /* offset of first data after cg */ 135 int32_t fs_cgoffset; /* cylinder group offset in cylinder */ 136 int32_t fs_cgmask; /* used to calc mod fs_ntrak */ 137 time_t fs_time; /* last time written */ 138 int32_t fs_size; /* number of blocks in fs */ 139 int32_t fs_dsize; /* number of data blocks in fs */ 140 int32_t fs_ncg; /* number of cylinder groups */ 141 int32_t fs_bsize; /* size of basic blocks in fs */ 142 int32_t fs_fsize; /* size of frag blocks in fs */ 143 int32_t fs_frag; /* number of frags in a block in fs */ 144 /* these are configuration parameters */ 145 int32_t fs_minfree; /* minimum percentage of free blocks */ 146 int32_t fs_rotdelay; /* num of ms for optimal next block */ 147 int32_t fs_rps; /* disk revolutions per second */ 148 /* these fields can be computed from the others */ 149 int32_t fs_bmask; /* ``blkoff'' calc of blk offsets */ 150 int32_t fs_fmask; /* ``fragoff'' calc of frag offsets */ 151 int32_t fs_bshift; /* ``lblkno'' calc of logical blkno */ 152 int32_t fs_fshift; /* ``numfrags'' calc number of frags */ 153 /* these are configuration parameters */ 154 int32_t fs_maxcontig; /* max number of contiguous blks */ 155 int32_t fs_maxbpg; /* max number of blks per cyl group */ 156 /* these fields can be computed from the others */ 157 int32_t fs_fragshift; /* block to frag shift */ 158 int32_t fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */ 159 int32_t fs_sbsize; /* actual size of super block */ 160 int32_t fs_csmask; /* csum block offset */ 161 int32_t fs_csshift; /* csum block number */ 162 int32_t fs_nindir; /* value of NINDIR */ 163 int32_t fs_inopb; /* value of INOPB */ 164 int32_t fs_nspf; /* value of NSPF */ 165 /* yet another configuration parameter */ 166 int32_t fs_optim; /* optimization preference, see below */ 167 /* these fields are derived from the hardware */ 168 int32_t fs_npsect; /* # sectors/track including spares */ 169 int32_t fs_interleave; /* hardware sector interleave */ 170 int32_t fs_trackskew; /* sector 0 skew, per track */ 171 int32_t fs_headswitch; /* head switch time, usec */ 172 int32_t fs_trkseek; /* track-to-track seek, usec */ 173 /* sizes determined by number of cylinder groups and their sizes */ 174 daddr_t fs_csaddr; /* blk addr of cyl grp summary area */ 175 int32_t fs_cssize; /* size of cyl grp summary area */ 176 int32_t fs_cgsize; /* cylinder group size */ 177 /* these fields are derived from the hardware */ 178 int32_t fs_ntrak; /* tracks per cylinder */ 179 int32_t fs_nsect; /* sectors per track */ 180 int32_t fs_spc; /* sectors per cylinder */ 181 /* this comes from the disk driver partitioning */ 182 int32_t fs_ncyl; /* cylinders in file system */ 183 /* these fields can be computed from the others */ 184 int32_t fs_cpg; /* cylinders per group */ 185 int32_t fs_ipg; /* inodes per group */ 186 int32_t fs_fpg; /* blocks per group * fs_frag */ 187 /* this data must be re-computed after crashes */ 188 struct csum fs_cstotal; /* cylinder summary information */ 189 /* these fields are cleared at mount time */ 190 int8_t fs_fmod; /* super block modified flag */ 191 int8_t fs_clean; /* file system is clean flag */ 192 int8_t fs_ronly; /* mounted read-only flag */ 193 int8_t fs_flags; /* currently unused flag */ 194 u_char fs_fsmnt[MAXMNTLEN]; /* name mounted on */ 195 /* these fields retain the current block allocation info */ 196 int32_t fs_cgrotor; /* last cg searched */ 197 struct csum *fs_csp[MAXCSBUFS];/* list of fs_cs info buffers */ 198 int32_t fs_cpc; /* cyl per cycle in postbl */ 199 int16_t fs_opostbl[16][8]; /* old rotation block list head */ 200 int32_t fs_sparecon[50]; /* reserved for future constants */ 201 int32_t fs_contigsumsize; /* size of cluster summary array */ 202 int32_t fs_maxsymlinklen; /* max length of an internal symlink */ 203 int32_t fs_inodefmt; /* format of on-disk inodes */ 204 u_quad_t fs_maxfilesize; /* maximum representable file size */ 205 quad_t fs_qbmask; /* ~fs_bmask - for use with quad size */ 206 quad_t fs_qfmask; /* ~fs_fmask - for use with quad size */ 207 int32_t fs_state; /* validate fs_clean field */ 208 int32_t fs_postblformat; /* format of positional layout tables */ 209 int32_t fs_nrpos; /* number of rotational positions */ 210 int32_t fs_postbloff; /* (u_int16) rotation block list head */ 211 int32_t fs_rotbloff; /* (u_int8) blocks for each rotation */ 212 int32_t fs_magic; /* magic number */ 213 u_int8_t fs_space[1]; /* list of blocks for each rotation */ 214 /* actually longer */ 215 }; 216 217 /* 218 * Filesystem identification 219 */ 220 #define FS_MAGIC 0x011954 /* the fast filesystem magic number */ 221 #define FS_OKAY 0x7c269d38 /* superblock checksum */ 222 #define FS_42INODEFMT -1 /* 4.2BSD inode format */ 223 #define FS_44INODEFMT 2 /* 4.4BSD inode format */ 224 /* 225 * Preference for optimization. 226 */ 227 #define FS_OPTTIME 0 /* minimize allocation time */ 228 #define FS_OPTSPACE 1 /* minimize disk fragmentation */ 229 230 /* 231 * Rotational layout table format types 232 */ 233 #define FS_42POSTBLFMT -1 /* 4.2BSD rotational table format */ 234 #define FS_DYNAMICPOSTBLFMT 1 /* dynamic rotational table format */ 235 /* 236 * Macros for access to superblock array structures 237 */ 238 #define fs_postbl(fs, cylno) \ 239 (((fs)->fs_postblformat == FS_42POSTBLFMT) \ 240 ? ((fs)->fs_opostbl[cylno]) \ 241 : ((int16_t *)((u_int8_t *)(fs) + \ 242 (fs)->fs_postbloff) + (cylno) * (fs)->fs_nrpos)) 243 #define fs_rotbl(fs) \ 244 (((fs)->fs_postblformat == FS_42POSTBLFMT) \ 245 ? ((fs)->fs_space) \ 246 : ((u_int8_t *)((u_int8_t *)(fs) + (fs)->fs_rotbloff))) 247 248 /* 249 * The size of a cylinder group is calculated by CGSIZE. The maximum size 250 * is limited by the fact that cylinder groups are at most one block. 251 * Its size is derived from the size of the maps maintained in the 252 * cylinder group and the (struct cg) size. 253 */ 254 #define CGSIZE(fs) \ 255 /* base cg */ (sizeof(struct cg) + sizeof(int32_t) + \ 256 /* blktot size */ (fs)->fs_cpg * sizeof(int32_t) + \ 257 /* blks size */ (fs)->fs_cpg * (fs)->fs_nrpos * sizeof(int16_t) + \ 258 /* inode map */ howmany((fs)->fs_ipg, NBBY) + \ 259 /* block map */ howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY) +\ 260 /* if present */ ((fs)->fs_contigsumsize <= 0 ? 0 : \ 261 /* cluster sum */ (fs)->fs_contigsumsize * sizeof(int32_t) + \ 262 /* cluster map */ howmany((fs)->fs_cpg * (fs)->fs_spc / NSPB(fs), NBBY))) 263 264 /* 265 * Convert cylinder group to base address of its global summary info. 266 * 267 * N.B. This macro assumes that sizeof(struct csum) is a power of two. 268 */ 269 #define fs_cs(fs, indx) \ 270 fs_csp[(indx) >> (fs)->fs_csshift][(indx) & ~(fs)->fs_csmask] 271 272 /* 273 * Cylinder group block for a file system. 274 */ 275 #define CG_MAGIC 0x090255 276 struct cg { 277 int32_t unused_1; /* historic cyl groups linked list */ 278 int32_t cg_magic; /* magic number */ 279 time_t cg_time; /* time last written */ 280 int32_t cg_cgx; /* we are the cgx'th cylinder group */ 281 int16_t cg_ncyl; /* number of cyl's this cg */ 282 int16_t cg_niblk; /* number of inode blocks this cg */ 283 int32_t cg_ndblk; /* number of data blocks this cg */ 284 struct csum cg_cs; /* cylinder summary information */ 285 int32_t cg_rotor; /* position of last used block */ 286 int32_t cg_frotor; /* position of last used frag */ 287 int32_t cg_irotor; /* position of last used inode */ 288 int32_t cg_frsum[MAXFRAG]; /* counts of available frags */ 289 int32_t cg_btotoff; /* (int32) block totals per cylinder */ 290 int32_t cg_boff; /* (u_int16) free block positions */ 291 int32_t cg_iusedoff; /* (u_int8) used inode map */ 292 int32_t cg_freeoff; /* (u_int8) free block map */ 293 int32_t cg_nextfreeoff; /* (u_int8) next available space */ 294 int32_t cg_clustersumoff; /* (u_int32) counts of avail clusters */ 295 int32_t cg_clusteroff; /* (u_int8) free cluster map */ 296 int32_t cg_nclusterblks; /* number of clusters this cg */ 297 int32_t cg_sparecon[13]; /* reserved for future use */ 298 u_int8_t cg_space[1]; /* space for cylinder group maps */ 299 /* actually longer */ 300 }; 301 302 /* 303 * Macros for access to cylinder group array structures 304 */ 305 #define cg_blktot(cgp) \ 306 (((cgp)->cg_magic != CG_MAGIC) \ 307 ? (((struct ocg *)(cgp))->cg_btot) \ 308 : ((int32_t *)((u_int8_t *)(cgp) + (cgp)->cg_btotoff))) 309 #define cg_blks(fs, cgp, cylno) \ 310 (((cgp)->cg_magic != CG_MAGIC) \ 311 ? (((struct ocg *)(cgp))->cg_b[cylno]) \ 312 : ((int16_t *)((u_int8_t *)(cgp) + \ 313 (cgp)->cg_boff) + (cylno) * (fs)->fs_nrpos)) 314 #define cg_inosused(cgp) \ 315 (((cgp)->cg_magic != CG_MAGIC) \ 316 ? (((struct ocg *)(cgp))->cg_iused) \ 317 : ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_iusedoff))) 318 #define cg_blksfree(cgp) \ 319 (((cgp)->cg_magic != CG_MAGIC) \ 320 ? (((struct ocg *)(cgp))->cg_free) \ 321 : ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_freeoff))) 322 #define cg_chkmagic(cgp) \ 323 ((cgp)->cg_magic == CG_MAGIC || ((struct ocg *)(cgp))->cg_magic == CG_MAGIC) 324 #define cg_clustersfree(cgp) \ 325 ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_clusteroff)) 326 #define cg_clustersum(cgp) \ 327 ((int32_t *)((u_int8_t *)(cgp) + (cgp)->cg_clustersumoff)) 328 329 /* 330 * The following structure is defined 331 * for compatibility with old file systems. 332 */ 333 struct ocg { 334 int32_t unused_1; /* historic linked list of cyl groups */ 335 int32_t unused_2; /* used for incore cyl groups */ 336 time_t cg_time; /* time last written */ 337 int32_t cg_cgx; /* we are the cgx'th cylinder group */ 338 int16_t cg_ncyl; /* number of cyl's this cg */ 339 int16_t cg_niblk; /* number of inode blocks this cg */ 340 int32_t cg_ndblk; /* number of data blocks this cg */ 341 struct csum cg_cs; /* cylinder summary information */ 342 int32_t cg_rotor; /* position of last used block */ 343 int32_t cg_frotor; /* position of last used frag */ 344 int32_t cg_irotor; /* position of last used inode */ 345 int32_t cg_frsum[8]; /* counts of available frags */ 346 int32_t cg_btot[32]; /* block totals per cylinder */ 347 int16_t cg_b[32][8]; /* positions of free blocks */ 348 u_int8_t cg_iused[256]; /* used inode map */ 349 int32_t cg_magic; /* magic number */ 350 u_int8_t cg_free[1]; /* free block map */ 351 /* actually longer */ 352 }; 353 354 /* 355 * Turn file system block numbers into disk block addresses. 356 * This maps file system blocks to device size blocks. 357 */ 358 #define fsbtodb(fs, b) ((b) << (fs)->fs_fsbtodb) 359 #define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb) 360 361 /* 362 * Cylinder group macros to locate things in cylinder groups. 363 * They calc file system addresses of cylinder group data structures. 364 */ 365 #define cgbase(fs, c) ((daddr_t)((fs)->fs_fpg * (c))) 366 #define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */ 367 #define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */ 368 #define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */ 369 #define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */ 370 #define cgstart(fs, c) \ 371 (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask))) 372 373 /* 374 * Macros for handling inode numbers: 375 * inode number to file system block offset. 376 * inode number to cylinder group number. 377 * inode number to file system block address. 378 */ 379 #define ino_to_cg(fs, x) ((x) / (fs)->fs_ipg) 380 #define ino_to_fsba(fs, x) \ 381 ((daddr_t)(cgimin(fs, ino_to_cg(fs, x)) + \ 382 (blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs)))))) 383 #define ino_to_fsbo(fs, x) ((x) % INOPB(fs)) 384 385 /* 386 * Give cylinder group number for a file system block. 387 * Give cylinder group block number for a file system block. 388 */ 389 #define dtog(fs, d) ((d) / (fs)->fs_fpg) 390 #define dtogd(fs, d) ((d) % (fs)->fs_fpg) 391 392 /* 393 * Extract the bits for a block from a map. 394 * Compute the cylinder and rotational position of a cyl block addr. 395 */ 396 #define blkmap(fs, map, loc) \ 397 (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag))) 398 #define cbtocylno(fs, bno) \ 399 ((bno) * NSPF(fs) / (fs)->fs_spc) 400 #define cbtorpos(fs, bno) \ 401 (((bno) * NSPF(fs) % (fs)->fs_spc / (fs)->fs_nsect * (fs)->fs_trackskew + \ 402 (bno) * NSPF(fs) % (fs)->fs_spc % (fs)->fs_nsect * (fs)->fs_interleave) % \ 403 (fs)->fs_nsect * (fs)->fs_nrpos / (fs)->fs_npsect) 404 405 /* 406 * The following macros optimize certain frequently calculated 407 * quantities by using shifts and masks in place of divisions 408 * modulos and multiplications. 409 */ 410 #define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \ 411 ((loc) & (fs)->fs_qbmask) 412 #define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \ 413 ((loc) & (fs)->fs_qfmask) 414 #define lblktosize(fs, blk) /* calculates (blk * fs->fs_bsize) */ \ 415 ((blk) << (fs)->fs_bshift) 416 #define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \ 417 ((loc) >> (fs)->fs_bshift) 418 #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \ 419 ((loc) >> (fs)->fs_fshift) 420 #define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \ 421 (((size) + (fs)->fs_qbmask) & (fs)->fs_bmask) 422 #define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \ 423 (((size) + (fs)->fs_qfmask) & (fs)->fs_fmask) 424 #define fragstoblks(fs, frags) /* calculates (frags / fs->fs_frag) */ \ 425 ((frags) >> (fs)->fs_fragshift) 426 #define blkstofrags(fs, blks) /* calculates (blks * fs->fs_frag) */ \ 427 ((blks) << (fs)->fs_fragshift) 428 #define fragnum(fs, fsb) /* calculates (fsb % fs->fs_frag) */ \ 429 ((fsb) & ((fs)->fs_frag - 1)) 430 #define blknum(fs, fsb) /* calculates rounddown(fsb, fs->fs_frag) */ \ 431 ((fsb) &~ ((fs)->fs_frag - 1)) 432 433 /* 434 * Determine the number of available frags given a 435 * percentage to hold in reserve 436 */ 437 #define freespace(fs, percentreserved) \ 438 (blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \ 439 (fs)->fs_cstotal.cs_nffree - ((fs)->fs_dsize * (percentreserved) / 100)) 440 441 /* 442 * Determining the size of a file block in the file system. 443 */ 444 #define blksize(fs, ip, lbn) \ 445 (((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) << (fs)->fs_bshift) \ 446 ? (fs)->fs_bsize \ 447 : (fragroundup(fs, blkoff(fs, (ip)->i_size)))) 448 #define dblksize(fs, dip, lbn) \ 449 (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \ 450 ? (fs)->fs_bsize \ 451 : (fragroundup(fs, blkoff(fs, (dip)->di_size)))) 452 453 454 /* 455 * Number of disk sectors per block/fragment; assumes DEV_BSIZE byte 456 * sector size. 457 */ 458 #define NSPB(fs) ((fs)->fs_nspf << (fs)->fs_fragshift) 459 #define NSPF(fs) ((fs)->fs_nspf) 460 461 /* 462 * Number of inodes in a secondary storage block/fragment. 463 */ 464 #define INOPB(fs) ((fs)->fs_inopb) 465 #define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift) 466 467 /* 468 * Number of indirects in a file system block. 469 */ 470 #define NINDIR(fs) ((fs)->fs_nindir) 471 472 extern int inside[], around[]; 473 extern u_char *fragtbl[]; 474