1.\" $OpenBSD: fs.5,v 1.12 2003/06/06 13:28:13 jmc Exp $ 2.\" $NetBSD: fs.5,v 1.3 1994/11/30 19:31:17 jtc Exp $ 3.\" 4.\" Copyright (c) 1983, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)fs.5 8.2 (Berkeley) 4/19/94 32.\" 33.Dd April 19, 1994 34.Dt FS 5 35.Os 36.Sh NAME 37.Nm fs , 38.Nm inode 39.Nd format of file system volume 40.Sh SYNOPSIS 41.Fd #include <sys/types.h> 42.Fd #include <ufs/ufs/fs.h> 43.Fd #include <ufs/ufs/inode.h> 44.Sh DESCRIPTION 45The files 46.Aq Pa fs.h 47and 48.Aq Pa inode.h 49declare several structures and define variables and macros 50which are used to create and manage the underlying format of 51file system objects on random access devices (disks). 52.Pp 53The block size and number of blocks which 54comprise a file system are parameters of the file system. 55Sectors beginning at 56.Dv BBLOCK 57and continuing for 58.Dv BBSIZE 59are used 60for a disklabel and for some hardware primary 61and secondary bootstrapping programs. 62.Pp 63The actual file system begins at sector 64.Dv SBLOCK 65with the 66.Em super-block 67that is of size 68.Dv SBSIZE . 69The following structure describes the super-block and is 70from the file 71.Aq Pa ufs/fs.h : 72.Bd -literal 73#define FS_MAGIC 0x011954 74struct fs { 75 struct fs *fs_link; /* linked list of file systems */ 76 struct fs *fs_rlink; /* used for incore super blocks */ 77 daddr_t fs_sblkno; /* addr of super-block in filesys */ 78 daddr_t fs_cblkno; /* offset of cyl-block in filesys */ 79 daddr_t fs_iblkno; /* offset of inode-blocks in filesys */ 80 daddr_t fs_dblkno; /* offset of first data after cg */ 81 long fs_cgoffset; /* cylinder group offset in cylinder */ 82 long fs_cgmask; /* used to calc mod fs_ntrak */ 83 time_t fs_time; /* last time written */ 84 long fs_size; /* number of blocks in fs */ 85 long fs_dsize; /* number of data blocks in fs */ 86 long fs_ncg; /* number of cylinder groups */ 87 long fs_bsize; /* size of basic blocks in fs */ 88 long fs_fsize; /* size of frag blocks in fs */ 89 long fs_frag; /* number of frags in a block in fs */ 90/* these are configuration parameters */ 91 long fs_minfree; /* minimum percentage of free blocks */ 92 long fs_rotdelay; /* num of ms for optimal next block */ 93 long fs_rps; /* disk revolutions per second */ 94/* these fields can be computed from the others */ 95 long fs_bmask; /* ``blkoff'' calc of blk offsets */ 96 long fs_fmask; /* ``fragoff'' calc of frag offsets */ 97 long fs_bshift; /* ``lblkno'' calc of logical blkno */ 98 long fs_fshift; /* ``numfrags'' calc number of frags */ 99/* these are configuration parameters */ 100 long fs_maxcontig; /* max number of contiguous blks */ 101 long fs_maxbpg; /* max number of blks per cyl group */ 102/* these fields can be computed from the others */ 103 long fs_fragshift; /* block to frag shift */ 104 long fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */ 105 long fs_sbsize; /* actual size of super block */ 106 long fs_csmask; /* csum block offset */ 107 long fs_csshift; /* csum block number */ 108 long fs_nindir; /* value of NINDIR */ 109 long fs_inopb; /* value of INOPB */ 110 long fs_nspf; /* value of NSPF */ 111/* yet another configuration parameter */ 112 long fs_optim; /* optimization preference, see below */ 113/* these fields are derived from the hardware */ 114 long fs_npsect; /* # sectors/track including spares */ 115 long fs_interleave; /* hardware sector interleave */ 116 long fs_trackskew; /* sector 0 skew, per track */ 117 long fs_headswitch; /* head switch time, usec */ 118 long fs_trkseek; /* track-to-track seek, usec */ 119/* sizes determined by number of cylinder groups and their sizes */ 120 daddr_t fs_csaddr; /* blk addr of cyl grp summary area */ 121 long fs_cssize; /* size of cyl grp summary area */ 122 long fs_cgsize; /* cylinder group size */ 123/* these fields are derived from the hardware */ 124 long fs_ntrak; /* tracks per cylinder */ 125 long fs_nsect; /* sectors per track */ 126 long fs_spc; /* sectors per cylinder */ 127/* this comes from the disk driver partitioning */ 128 long fs_ncyl; /* cylinders in file system */ 129/* these fields can be computed from the others */ 130 long fs_cpg; /* cylinders per group */ 131 long fs_ipg; /* inodes per group */ 132 long fs_fpg; /* blocks per group * fs_frag */ 133/* this data must be re-computed after crashes */ 134 struct csum fs_cstotal; /* cylinder summary information */ 135/* these fields are cleared at mount time */ 136 char fs_fmod; /* super block modified flag */ 137 char fs_clean; /* file system is clean flag */ 138 char fs_ronly; /* mounted read-only flag */ 139 char fs_flags; /* currently unused flag */ 140 char fs_fsmnt[MAXMNTLEN]; /* name mounted on */ 141/* these fields retain the current block allocation info */ 142 long fs_cgrotor; /* last cg searched */ 143 struct csum *fs_csp[MAXCSBUFS]; /* list of fs_cs info buffers */ 144 long fs_cpc; /* cyl per cycle in postbl */ 145 short fs_opostbl[16][8]; /* old rotation block list head */ 146 long fs_sparecon[56]; /* reserved for future constants */ 147 quad fs_qbmask; /* ~fs_bmask - for use with quad size */ 148 quad fs_qfmask; /* ~fs_fmask - for use with quad size */ 149 long fs_postblformat; /* format of positional layout tables */ 150 long fs_nrpos; /* number of rotational positions */ 151 long fs_postbloff; /* (short) rotation block list head */ 152 long fs_rotbloff; /* (u_char) blocks for each rotation */ 153 long fs_magic; /* magic number */ 154 u_char fs_space[1]; /* list of blocks for each rotation */ 155/* actually longer */ 156}; 157.Ed 158.Pp 159Each disk drive contains some number of file systems. 160A file system consists of a number of cylinder groups. 161Each cylinder group has inodes and data. 162.Pp 163A file system is described by its super-block, which in turn 164describes the cylinder groups. 165The super-block is critical 166data and is replicated in each cylinder group to protect against 167catastrophic loss. 168This is done at file system creation time and the critical 169super-block data does not change, so the copies need not be 170referenced further unless disaster strikes. 171.Pp 172Addresses stored in inodes are capable of addressing fragments 173of 174.Dq blocks . 175File system blocks of at most size 176.Dv MAXBSIZE 177can 178be optionally broken into 2, 4, or 8 pieces, each of which is 179addressable; these pieces may be 180.Dv DEV_BSIZE , 181or some multiple of a 182.Dv DEV_BSIZE 183unit. 184.Pp 185Large files consist of exclusively large data blocks. 186To avoid undue wasted disk space, the last data block of a small file is 187allocated only as many fragments of a large block as are 188necessary. 189The file system format retains only a single pointer 190to such a fragment, which is a piece of a single large block that 191has been divided. 192The size of such a fragment is determinable from 193information in the inode, using the 194.Fn blksize 195macro. 196.Pp 197The file system records space availability at the fragment level; 198to determine block availability, aligned fragments are examined. 199.Pp 200The root inode is the root of the file system. 201Inode 0 can't be used for normal purposes and 202historically bad blocks were linked to inode 1 203(inode 1 is no longer used for 204this purpose; however, numerous dump tapes make this 205assumption, so we are stuck with it). 206Thus the root inode is 2. 207.Pp 208The 209.Fa fs_minfree 210element gives the minimum acceptable percentage of file system 211blocks that may be free. 212If the freelist drops below this level 213only the superuser may continue to allocate blocks. 214The 215.Fa fs_minfree 216element 217may be set to 0 if no reserve of free blocks is deemed necessary, 218although severe performance degradations will be observed if the 219file system is run at greater than 95% full; thus the default 220value of 221.Fa fs_minfree 222is 5%. 223.Pp 224Empirically the best trade-off between block fragmentation and 225overall disk utilization at a loading of 95% comes with a 226fragmentation of 8; thus the default fragment size is an eighth 227of the block size. 228.Pp 229The element 230.Fa fs_optim 231specifies whether the file system should try to minimize the time spent 232allocating blocks, or if it should attempt to minimize the space 233fragmentation on the disk. 234If the value of 235.Fa fs_minfree 236(see above) is less than 5%, 237then the file system defaults to optimizing for space to avoid 238running out of full sized blocks. 239If the value of 240.Fa fs_minfree 241is greater than or equal to 5%, 242fragmentation is unlikely to be problematical, and 243the file system defaults to optimizing for time. 244.Ss Cylinder group related limits 245Each cylinder keeps track of the availability of blocks at different 246rotational positions, so that sequential blocks can be laid out 247with minimum rotational latency. 248With the default of 8 distinguished 249rotational positions, the resolution of the 250summary information is 2ms for a typical 3600 rpm drive. 251.Pp 252The element 253.Fa fs_rotdelay 254gives the minimum number of milliseconds to initiate 255another disk transfer on the same cylinder. 256It is used in determining the rotationally optimal 257layout for disk blocks within a file; 258the default value for 259.Fa fs_rotdelay 260is 2ms. 261.Pp 262Each file system has a statically allocated number of inodes. 263An inode is allocated for each 264.Dv NBPI 265bytes of disk space. 266The inode allocation strategy is extremely conservative. 267.Pp 268.Dv MINBSIZE 269is the smallest allowable block size. 270With a 271.Dv MINBSIZE 272of 4096 273it is possible to create files of size 2742^32 with only two levels of indirection. 275.Dv MINBSIZE 276must be big enough to hold a cylinder group block, 277thus changes to 278.Fa struct cg 279must keep its size within 280.Dv MINBSIZE . 281Note that super-blocks are never more than size 282.Dv SBSIZE . 283.Pp 284The path name on which the file system is mounted is maintained in 285.Fa fs_fsmnt . 286.Dv MAXMNTLEN 287defines the amount of space allocated in 288the super-block for this name. 289The limit on the amount of summary information per file system 290is defined by 291.Dv MAXCSBUFS . 292For a 4096 byte block size, it is currently parameterized for a 293maximum of two million cylinders. 294.Pp 295Per cylinder group information is summarized in blocks allocated 296from the first cylinder group's data blocks. 297These blocks are read in from 298.Fa fs_csaddr 299(size 300.Fa fs_cssize ) 301in addition to the super-block. 302.Pp 303Note that 304.Fn sizeof "struct csum" 305must be a power of two in order for 306the 307.Fn fs_cs 308macro to work. 309.Ss Super-block for a file system 310The size of the rotational layout tables 311is limited by the fact that the super-block is of size 312.Dv SBSIZE . 313The size of these tables is inversely 314proportional to the block 315size of the file system. 316The size of the tables is 317increased when sector sizes are not powers of two, 318as this increases the number of cylinders 319included before the rotational pattern repeats 320.Pq Fa fs_cpc . 321The size of the rotational layout 322tables is derived from the number of bytes remaining in 323.Fa struct fs . 324.Pp 325The number of blocks of data per cylinder group 326is limited because cylinder groups are at most one block. 327The inode and free block tables 328must fit into a single block after deducting space for 329the cylinder group structure 330.Fa struct cg . 331.Ss Inodes 332The inode is the focus of all file activity in the 333.Tn UNIX 334file system. 335There is a unique inode allocated 336for each active file, 337each current directory, each mounted-on file, 338text file, and the root. 339An inode is 340.Dq named 341by its device/i-number pair. 342For further information, see the include file 343.Aq Pa ufs/ufs/inode.h . 344.Sh HISTORY 345A super-block structure named filsys appeared in 346.At v6 . 347The file system described in this manual appeared 348in 349.Bx 4.2 . 350