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