1 /* 2 * Copyright (c) 1982 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)fs.h 6.6 (Berkeley) 05/02/86 7 */ 8 9 /* 10 * Each disk drive contains some number of file systems. 11 * A file system consists of a number of cylinder groups. 12 * Each cylinder group has inodes and data. 13 * 14 * A file system is described by its super-block, which in turn 15 * describes the cylinder groups. The super-block is critical 16 * data and is replicated in each cylinder group to protect against 17 * catastrophic loss. This is done at mkfs time and the critical 18 * super-block data does not change, so the copies need not be 19 * referenced further unless disaster strikes. 20 * 21 * For file system fs, the offsets of the various blocks of interest 22 * are given in the super block as: 23 * [fs->fs_sblkno] Super-block 24 * [fs->fs_cblkno] Cylinder group block 25 * [fs->fs_iblkno] Inode blocks 26 * [fs->fs_dblkno] Data blocks 27 * The beginning of cylinder group cg in fs, is given by 28 * the ``cgbase(fs, cg)'' macro. 29 * 30 * The first boot and super blocks are given in absolute disk addresses. 31 */ 32 #define BBSIZE 8192 33 #define SBSIZE 8192 34 #define BBLOCK ((daddr_t)(0)) 35 #define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE)) 36 37 /* 38 * Addresses stored in inodes are capable of addressing fragments 39 * of `blocks'. File system blocks of at most size MAXBSIZE can 40 * be optionally broken into 2, 4, or 8 pieces, each of which is 41 * addressible; these pieces may be DEV_BSIZE, or some multiple of 42 * a DEV_BSIZE unit. 43 * 44 * Large files consist of exclusively large data blocks. To avoid 45 * undue wasted disk space, the last data block of a small file may be 46 * allocated as only as many fragments of a large block as are 47 * necessary. The file system format retains only a single pointer 48 * to such a fragment, which is a piece of a single large block that 49 * has been divided. The size of such a fragment is determinable from 50 * information in the inode, using the ``blksize(fs, ip, lbn)'' macro. 51 * 52 * The file system records space availability at the fragment level; 53 * to determine block availability, aligned fragments are examined. 54 * 55 * The root inode is the root of the file system. 56 * Inode 0 can't be used for normal purposes and 57 * historically bad blocks were linked to inode 1, 58 * thus the root inode is 2. (inode 1 is no longer used for 59 * this purpose, however numerous dump tapes make this 60 * assumption, so we are stuck with it) 61 * The lost+found directory is given the next available 62 * inode when it is created by ``mkfs''. 63 */ 64 #define ROOTINO ((ino_t)2) /* i number of all roots */ 65 #define LOSTFOUNDINO (ROOTINO + 1) 66 67 /* 68 * Cylinder group related limits. 69 * 70 * For each cylinder we keep track of the availability of blocks at different 71 * rotational positions, so that we can lay out the data to be picked 72 * up with minimum rotational latency. NRPOS is the number of rotational 73 * positions which we distinguish. With NRPOS 8 the resolution of our 74 * summary information is 2ms for a typical 3600 rpm drive. 75 */ 76 #define NRPOS 8 /* number distinct rotational positions */ 77 78 /* 79 * MAXIPG bounds the number of inodes per cylinder group, and 80 * is needed only to keep the structure simpler by having the 81 * only a single variable size element (the free bit map). 82 * 83 * N.B.: MAXIPG must be a multiple of INOPB(fs). 84 */ 85 #define MAXIPG 2048 /* max number inodes/cyl group */ 86 87 /* 88 * MINBSIZE is the smallest allowable block size. 89 * In order to insure that it is possible to create files of size 90 * 2^32 with only two levels of indirection, MINBSIZE is set to 4096. 91 * MINBSIZE must be big enough to hold a cylinder group block, 92 * thus changes to (struct cg) must keep its size within MINBSIZE. 93 * MAXCPG is limited only to dimension an array in (struct cg); 94 * it can be made larger as long as that structures size remains 95 * within the bounds dictated by MINBSIZE. 96 * Note that super blocks are always of size SBSIZE, 97 * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE. 98 */ 99 #define MINBSIZE 4096 100 #define MAXCPG 32 /* maximum fs_cpg */ 101 102 /* 103 * The path name on which the file system is mounted is maintained 104 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in 105 * the super block for this name. 106 * The limit on the amount of summary information per file system 107 * is defined by MAXCSBUFS. It is currently parameterized for a 108 * maximum of two million cylinders. 109 */ 110 #define MAXMNTLEN 512 111 #define MAXCSBUFS 32 112 113 /* 114 * Per cylinder group information; summarized in blocks allocated 115 * from first cylinder group data blocks. These blocks have to be 116 * read in from fs_csaddr (size fs_cssize) in addition to the 117 * super block. 118 * 119 * N.B. sizeof(struct csum) must be a power of two in order for 120 * the ``fs_cs'' macro to work (see below). 121 */ 122 struct csum { 123 long cs_ndir; /* number of directories */ 124 long cs_nbfree; /* number of free blocks */ 125 long cs_nifree; /* number of free inodes */ 126 long cs_nffree; /* number of free frags */ 127 }; 128 129 /* 130 * Super block for a file system. 131 */ 132 #define FS_MAGIC 0x011954 133 struct fs 134 { 135 struct fs *fs_link; /* linked list of file systems */ 136 struct fs *fs_rlink; /* used for incore super blocks */ 137 daddr_t fs_sblkno; /* addr of super-block in filesys */ 138 daddr_t fs_cblkno; /* offset of cyl-block in filesys */ 139 daddr_t fs_iblkno; /* offset of inode-blocks in filesys */ 140 daddr_t fs_dblkno; /* offset of first data after cg */ 141 long fs_cgoffset; /* cylinder group offset in cylinder */ 142 long fs_cgmask; /* used to calc mod fs_ntrak */ 143 time_t fs_time; /* last time written */ 144 long fs_size; /* number of blocks in fs */ 145 long fs_dsize; /* number of data blocks in fs */ 146 long fs_ncg; /* number of cylinder groups */ 147 long fs_bsize; /* size of basic blocks in fs */ 148 long fs_fsize; /* size of frag blocks in fs */ 149 long fs_frag; /* number of frags in a block in fs */ 150 /* these are configuration parameters */ 151 long fs_minfree; /* minimum percentage of free blocks */ 152 long fs_rotdelay; /* num of ms for optimal next block */ 153 long fs_rps; /* disk revolutions per second */ 154 /* these fields can be computed from the others */ 155 long fs_bmask; /* ``blkoff'' calc of blk offsets */ 156 long fs_fmask; /* ``fragoff'' calc of frag offsets */ 157 long fs_bshift; /* ``lblkno'' calc of logical blkno */ 158 long fs_fshift; /* ``numfrags'' calc number of frags */ 159 /* these are configuration parameters */ 160 long fs_maxcontig; /* max number of contiguous blks */ 161 long fs_maxbpg; /* max number of blks per cyl group */ 162 /* these fields can be computed from the others */ 163 long fs_fragshift; /* block to frag shift */ 164 long fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */ 165 long fs_sbsize; /* actual size of super block */ 166 long fs_csmask; /* csum block offset */ 167 long fs_csshift; /* csum block number */ 168 long fs_nindir; /* value of NINDIR */ 169 long fs_inopb; /* value of INOPB */ 170 long fs_nspf; /* value of NSPF */ 171 long fs_optim; /* optimization preference, see below */ 172 long fs_sparecon[5]; /* reserved for future constants */ 173 /* sizes determined by number of cylinder groups and their sizes */ 174 daddr_t fs_csaddr; /* blk addr of cyl grp summary area */ 175 long fs_cssize; /* size of cyl grp summary area */ 176 long fs_cgsize; /* cylinder group size */ 177 /* these fields should be derived from the hardware */ 178 long fs_ntrak; /* tracks per cylinder */ 179 long fs_nsect; /* sectors per track */ 180 long fs_spc; /* sectors per cylinder */ 181 /* this comes from the disk driver partitioning */ 182 long fs_ncyl; /* cylinders in file system */ 183 /* these fields can be computed from the others */ 184 long fs_cpg; /* cylinders per group */ 185 long fs_ipg; /* inodes per group */ 186 long 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 char fs_fmod; /* super block modified flag */ 191 char fs_clean; /* file system is clean flag */ 192 char fs_ronly; /* mounted read-only flag */ 193 char fs_flags; /* currently unused flag */ 194 char fs_fsmnt[MAXMNTLEN]; /* name mounted on */ 195 /* these fields retain the current block allocation info */ 196 long fs_cgrotor; /* last cg searched */ 197 struct csum *fs_csp[MAXCSBUFS];/* list of fs_cs info buffers */ 198 long fs_cpc; /* cyl per cycle in postbl */ 199 short fs_postbl[MAXCPG][NRPOS];/* head of blocks for each rotation */ 200 long fs_magic; /* magic number */ 201 u_char fs_rotbl[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 * Convert cylinder group to base address of its global summary info. 212 * 213 * N.B. This macro assumes that sizeof(struct csum) is a power of two. 214 */ 215 #define fs_cs(fs, indx) \ 216 fs_csp[(indx) >> (fs)->fs_csshift][(indx) & ~(fs)->fs_csmask] 217 218 /* 219 * MAXBPC bounds the size of the rotational layout tables and 220 * is limited by the fact that the super block is of size SBSIZE. 221 * The size of these tables is INVERSELY proportional to the block 222 * size of the file system. It is aggravated by sector sizes that 223 * are not powers of two, as this increases the number of cylinders 224 * included before the rotational pattern repeats (fs_cpc). 225 * Its size is derived from the number of bytes remaining in (struct fs) 226 */ 227 #define MAXBPC (SBSIZE - sizeof (struct fs)) 228 229 /* 230 * Cylinder group block for a file system. 231 */ 232 #define CG_MAGIC 0x090255 233 struct cg { 234 struct cg *cg_link; /* linked list of cyl groups */ 235 struct cg *cg_rlink; /* used for incore cyl groups */ 236 time_t cg_time; /* time last written */ 237 long cg_cgx; /* we are the cgx'th cylinder group */ 238 short cg_ncyl; /* number of cyl's this cg */ 239 short cg_niblk; /* number of inode blocks this cg */ 240 long cg_ndblk; /* number of data blocks this cg */ 241 struct csum cg_cs; /* cylinder summary information */ 242 long cg_rotor; /* position of last used block */ 243 long cg_frotor; /* position of last used frag */ 244 long cg_irotor; /* position of last used inode */ 245 long cg_frsum[MAXFRAG]; /* counts of available frags */ 246 long cg_btot[MAXCPG]; /* block totals per cylinder */ 247 short cg_b[MAXCPG][NRPOS]; /* positions of free blocks */ 248 char cg_iused[MAXIPG/NBBY]; /* used inode map */ 249 long cg_magic; /* magic number */ 250 u_char cg_free[1]; /* free block map */ 251 /* actually longer */ 252 }; 253 254 /* 255 * MAXBPG bounds the number of blocks of data per cylinder group, 256 * and is limited by the fact that cylinder groups are at most one block. 257 * Its size is derived from the size of blocks and the (struct cg) size, 258 * by the number of remaining bits. 259 */ 260 #define MAXBPG(fs) \ 261 (fragstoblks((fs), (NBBY * ((fs)->fs_bsize - (sizeof (struct cg)))))) 262 263 /* 264 * Turn file system block numbers into disk block addresses. 265 * This maps file system blocks to device size blocks. 266 */ 267 #define fsbtodb(fs, b) ((b) << (fs)->fs_fsbtodb) 268 #define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb) 269 270 /* 271 * Cylinder group macros to locate things in cylinder groups. 272 * They calc file system addresses of cylinder group data structures. 273 */ 274 #define cgbase(fs, c) ((daddr_t)((fs)->fs_fpg * (c))) 275 #define cgstart(fs, c) \ 276 (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask))) 277 #define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */ 278 #define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */ 279 #define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */ 280 #define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */ 281 282 /* 283 * Macros for handling inode numbers: 284 * inode number to file system block offset. 285 * inode number to cylinder group number. 286 * inode number to file system block address. 287 */ 288 #define itoo(fs, x) ((x) % INOPB(fs)) 289 #define itog(fs, x) ((x) / (fs)->fs_ipg) 290 #define itod(fs, x) \ 291 ((daddr_t)(cgimin(fs, itog(fs, x)) + \ 292 (blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs)))))) 293 294 /* 295 * Give cylinder group number for a file system block. 296 * Give cylinder group block number for a file system block. 297 */ 298 #define dtog(fs, d) ((d) / (fs)->fs_fpg) 299 #define dtogd(fs, d) ((d) % (fs)->fs_fpg) 300 301 /* 302 * Extract the bits for a block from a map. 303 * Compute the cylinder and rotational position of a cyl block addr. 304 */ 305 #define blkmap(fs, map, loc) \ 306 (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag))) 307 #define cbtocylno(fs, bno) \ 308 ((bno) * NSPF(fs) / (fs)->fs_spc) 309 #define cbtorpos(fs, bno) \ 310 ((bno) * NSPF(fs) % (fs)->fs_spc % (fs)->fs_nsect * NRPOS / (fs)->fs_nsect) 311 312 /* 313 * The following macros optimize certain frequently calculated 314 * quantities by using shifts and masks in place of divisions 315 * modulos and multiplications. 316 */ 317 #define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \ 318 ((loc) & ~(fs)->fs_bmask) 319 #define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \ 320 ((loc) & ~(fs)->fs_fmask) 321 #define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \ 322 ((loc) >> (fs)->fs_bshift) 323 #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \ 324 ((loc) >> (fs)->fs_fshift) 325 #define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \ 326 (((size) + (fs)->fs_bsize - 1) & (fs)->fs_bmask) 327 #define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \ 328 (((size) + (fs)->fs_fsize - 1) & (fs)->fs_fmask) 329 #define fragstoblks(fs, frags) /* calculates (frags / fs->fs_frag) */ \ 330 ((frags) >> (fs)->fs_fragshift) 331 #define blkstofrags(fs, blks) /* calculates (blks * fs->fs_frag) */ \ 332 ((blks) << (fs)->fs_fragshift) 333 #define fragnum(fs, fsb) /* calculates (fsb % fs->fs_frag) */ \ 334 ((fsb) & ((fs)->fs_frag - 1)) 335 #define blknum(fs, fsb) /* calculates rounddown(fsb, fs->fs_frag) */ \ 336 ((fsb) &~ ((fs)->fs_frag - 1)) 337 338 /* 339 * Determine the number of available frags given a 340 * percentage to hold in reserve 341 */ 342 #define freespace(fs, percentreserved) \ 343 (blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \ 344 (fs)->fs_cstotal.cs_nffree - ((fs)->fs_dsize * (percentreserved) / 100)) 345 346 /* 347 * Determining the size of a file block in the file system. 348 */ 349 #define blksize(fs, ip, lbn) \ 350 (((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) << (fs)->fs_bshift) \ 351 ? (fs)->fs_bsize \ 352 : (fragroundup(fs, blkoff(fs, (ip)->i_size)))) 353 #define dblksize(fs, dip, lbn) \ 354 (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \ 355 ? (fs)->fs_bsize \ 356 : (fragroundup(fs, blkoff(fs, (dip)->di_size)))) 357 358 /* 359 * Number of disk sectors per block; assumes DEV_BSIZE byte sector size. 360 */ 361 #define NSPB(fs) ((fs)->fs_nspf << (fs)->fs_fragshift) 362 #define NSPF(fs) ((fs)->fs_nspf) 363 364 /* 365 * INOPB is the number of inodes in a secondary storage block. 366 */ 367 #define INOPB(fs) ((fs)->fs_inopb) 368 #define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift) 369 370 /* 371 * NINDIR is the number of indirects in a file system block. 372 */ 373 #define NINDIR(fs) ((fs)->fs_nindir) 374 375 #ifdef KERNEL 376 struct fs *getfs(); 377 struct fs *mountfs(); 378 #endif 379