1*5e015e5eSjoerg.\" $NetBSD: fs.5,v 1.18 2010/03/22 18:58:32 joerg Exp $ 2b5930afcSjtc.\" 3b5930afcSjtc.\" Copyright (c) 1983, 1991, 1993 4b5930afcSjtc.\" The Regents of the University of California. All rights reserved. 561f28255Scgd.\" 661f28255Scgd.\" Redistribution and use in source and binary forms, with or without 761f28255Scgd.\" modification, are permitted provided that the following conditions 861f28255Scgd.\" are met: 961f28255Scgd.\" 1. Redistributions of source code must retain the above copyright 1061f28255Scgd.\" notice, this list of conditions and the following disclaimer. 1161f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright 1261f28255Scgd.\" notice, this list of conditions and the following disclaimer in the 1361f28255Scgd.\" documentation and/or other materials provided with the distribution. 14075022b3Sagc.\" 3. Neither the name of the University nor the names of its contributors 1561f28255Scgd.\" may be used to endorse or promote products derived from this software 1661f28255Scgd.\" without specific prior written permission. 1761f28255Scgd.\" 1861f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1961f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2061f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2161f28255Scgd.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2261f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2361f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2461f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2561f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2661f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2761f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2861f28255Scgd.\" SUCH DAMAGE. 2961f28255Scgd.\" 30b5930afcSjtc.\" @(#)fs.5 8.2 (Berkeley) 4/19/94 3161f28255Scgd.\" 32d31da81bSlukem.Dd July 27, 2001 3361f28255Scgd.Dt FS 5 347a1aea16Sgarbled.Os 3561f28255Scgd.Sh NAME 3661f28255Scgd.Nm fs , 3761f28255Scgd.Nm inode 3861f28255Scgd.Nd format of file system volume 3961f28255Scgd.Sh SYNOPSIS 40472351e1Swiz.In sys/param.h 41472351e1Swiz.In ufs/ffs/fs.h 42472351e1Swiz.In ufs/ufs/inode.h 4361f28255Scgd.Sh DESCRIPTION 4461f28255ScgdThe files 45*5e015e5eSjoerg.In ufs/ffs/fs.h 4661f28255Scgdand 47*5e015e5eSjoerg.In ufs/ufs/inode.h 48a31858b1Sjmmvdeclare several structures and define variables and macros 4961f28255Scgdwhich are used to create and manage the underlying format of 5061f28255Scgdfile system objects on random access devices (disks). 5161f28255Scgd.Pp 5261f28255ScgdThe block size and number of blocks which 5361f28255Scgdcomprise a file system are parameters of the file system. 5461f28255ScgdSectors beginning at 5561f28255Scgd.Dv BBLOCK 5661f28255Scgdand continuing for 5761f28255Scgd.Dv BBSIZE 5861f28255Scgdare used 5961f28255Scgdfor a disklabel and for some hardware primary 6061f28255Scgdand secondary bootstrapping programs. 6161f28255Scgd.Pp 6261f28255ScgdThe actual file system begins at sector 6361f28255Scgd.Dv SBLOCK 6461f28255Scgdwith the 6561f28255Scgd.Em super-block 6661f28255Scgdthat is of size 6761f28255Scgd.Dv SBSIZE . 6861f28255ScgdThe following structure described the super-block and is 6961f28255Scgdfrom the file 70*5e015e5eSjoerg.In ufs/ffs/fs.h : 7161f28255Scgd.Bd -literal 7261f28255Scgd#define FS_MAGIC 0x011954 7361f28255Scgdstruct fs { 74d31da81bSlukem int32_t fs_firstfield; /* historic file system linked list, */ 75d31da81bSlukem int32_t fs_unused_1; /* used for incore super blocks */ 760bd9d203Speter int32_t fs_sblkno; /* addr of super-block in filesys */ 770bd9d203Speter int32_t fs_cblkno; /* offset of cyl-block in filesys */ 780bd9d203Speter int32_t fs_iblkno; /* offset of inode-blocks in filesys */ 790bd9d203Speter int32_t fs_dblkno; /* offset of first data after cg */ 800bd9d203Speter int32_t fs_old_cgoffset; /* cylinder group offset in cylinder */ 810bd9d203Speter int32_t fs_old_cgmask; /* used to calc mod fs_ntrak */ 820bd9d203Speter int32_t fs_old_time; /* last time written */ 830bd9d203Speter int32_t fs_old_size; /* number of blocks in fs */ 840bd9d203Speter int32_t fs_old_dsize; /* number of data blocks in fs */ 85d31da81bSlukem int32_t fs_ncg; /* number of cylinder groups */ 86d31da81bSlukem int32_t fs_bsize; /* size of basic blocks in fs */ 87d31da81bSlukem int32_t fs_fsize; /* size of frag blocks in fs */ 88d31da81bSlukem int32_t fs_frag; /* number of frags in a block in fs */ 8961f28255Scgd/* these are configuration parameters */ 90d31da81bSlukem int32_t fs_minfree; /* minimum percentage of free blocks */ 910bd9d203Speter int32_t fs_old_rotdelay; /* num of ms for optimal next block */ 920bd9d203Speter int32_t fs_old_rps; /* disk revolutions per second */ 9361f28255Scgd/* these fields can be computed from the others */ 94d31da81bSlukem int32_t fs_bmask; /* ``blkoff'' calc of blk offsets */ 95d31da81bSlukem int32_t fs_fmask; /* ``fragoff'' calc of frag offsets */ 96d31da81bSlukem int32_t fs_bshift; /* ``lblkno'' calc of logical blkno */ 97d31da81bSlukem int32_t fs_fshift; /* ``numfrags'' calc number of frags */ 9861f28255Scgd/* these are configuration parameters */ 99d31da81bSlukem int32_t fs_maxcontig; /* max number of contiguous blks */ 100d31da81bSlukem int32_t fs_maxbpg; /* max number of blks per cyl group */ 10161f28255Scgd/* these fields can be computed from the others */ 102d31da81bSlukem int32_t fs_fragshift; /* block to frag shift */ 103d31da81bSlukem int32_t fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */ 104d31da81bSlukem int32_t fs_sbsize; /* actual size of super block */ 1050bd9d203Speter int32_t fs_spare1[2]; /* old fs_csmask */ 1060bd9d203Speter /* old fs_csshift */ 107d31da81bSlukem int32_t fs_nindir; /* value of NINDIR */ 108d31da81bSlukem int32_t fs_inopb; /* value of INOPB */ 1090bd9d203Speter int32_t fs_old_nspf; /* value of NSPF */ 11061f28255Scgd/* yet another configuration parameter */ 111d31da81bSlukem int32_t fs_optim; /* optimization preference, see below */ 11261f28255Scgd/* these fields are derived from the hardware */ 1130bd9d203Speter int32_t fs_old_npsect; /* # sectors/track including spares */ 1140bd9d203Speter int32_t fs_old_interleave; /* hardware sector interleave */ 1150bd9d203Speter int32_t fs_old_trackskew; /* sector 0 skew, per track */ 1160bd9d203Speter/* fs_id takes the space of unused fs_headswitch and fs_trkseek fields */ 1170bd9d203Speter int32_t fs_id[2]; /* unique file system id */ 11861f28255Scgd/* sizes determined by number of cylinder groups and their sizes */ 1190bd9d203Speter int32_t fs_old_csaddr; /* blk addr of cyl grp summary area */ 120d31da81bSlukem int32_t fs_cssize; /* size of cyl grp summary area */ 121d31da81bSlukem int32_t fs_cgsize; /* cylinder group size */ 12261f28255Scgd/* these fields are derived from the hardware */ 1230bd9d203Speter int32_t fs_spare2; /* old fs_ntrak */ 1240bd9d203Speter int32_t fs_old_nsect; /* sectors per track */ 1250bd9d203Speter int32_t fs_old_spc; /* sectors per cylinder */ 1260bd9d203Speter int32_t fs_old_ncyl; /* cylinders in file system */ 1270bd9d203Speter int32_t fs_old_cpg; /* cylinders per group */ 128d31da81bSlukem int32_t fs_ipg; /* inodes per group */ 129d31da81bSlukem int32_t fs_fpg; /* blocks per group * fs_frag */ 13061f28255Scgd/* this data must be re-computed after crashes */ 1310bd9d203Speter struct csum fs_old_cstotal; /* cylinder summary information */ 13261f28255Scgd/* these fields are cleared at mount time */ 133d31da81bSlukem int8_t fs_fmod; /* super block modified flag */ 134d31da81bSlukem int8_t fs_clean; /* file system is clean flag */ 135d31da81bSlukem int8_t fs_ronly; /* mounted read-only flag */ 1360bd9d203Speter uint8_t fs_old_flags; /* see FS_ flags below */ 137d31da81bSlukem u_char fs_fsmnt[MAXMNTLEN]; /* name mounted on */ 1380bd9d203Speter u_char fs_volname[MAXVOLLEN]; /* volume name */ 1390bd9d203Speter uint64_t fs_swuid; /* system-wide uid */ 1400bd9d203Speter int32_t fs_pad; 14161f28255Scgd/* these fields retain the current block allocation info */ 1420bd9d203Speter int32_t fs_cgrotor; /* last cg searched (UNUSED) */ 1430bd9d203Speter void *fs_ocsp[NOCSPTRS];/* padding; was list of fs_cs buffers */ 144fd18408bSperry uint8_t *fs_contigdirs; /* # of contiguously allocated dirs */ 1450bd9d203Speter struct csum *fs_csp; /* cg summary info buffer for fs_cs */ 146d31da81bSlukem int32_t *fs_maxcluster; /* max cluster in each cyl group */ 1470bd9d203Speter u_char *fs_active; /* used by snapshots to track fs */ 1480bd9d203Speter int32_t fs_old_cpc; /* cyl per cycle in postbl */ 1490bd9d203Speter/* this area is otherwise allocated unless fs_old_flags & FS_FLAGS_UPDATED */ 1500bd9d203Speter int32_t fs_maxbsize; /* maximum blocking factor permitted */ 1510bd9d203Speter int64_t fs_sparecon64[17]; /* old rotation block list head */ 1520bd9d203Speter int64_t fs_sblockloc; /* byte offset of standard superblock */ 1530bd9d203Speter struct csum_total fs_cstotal; /* cylinder summary information */ 1540bd9d203Speter int64_t fs_time; /* last time written */ 1550bd9d203Speter int64_t fs_size; /* number of blocks in fs */ 1560bd9d203Speter int64_t fs_dsize; /* number of data blocks in fs */ 1570bd9d203Speter int64_t fs_csaddr; /* blk addr of cyl grp summary area */ 1580bd9d203Speter int64_t fs_pendingblocks; /* blocks in process of being freed */ 1590bd9d203Speter int32_t fs_pendinginodes; /* inodes in process of being freed */ 1600bd9d203Speter int32_t fs_snapinum[FSMAXSNAP]; /* list of snapshot inode numbers */ 1610bd9d203Speter/* back to stuff that has been around a while */ 1620bd9d203Speter int32_t fs_avgfilesize; /* expected average file size */ 1630bd9d203Speter int32_t fs_avgfpdir; /* expected # of files per directory */ 1640bd9d203Speter int32_t fs_save_cgsize; /* save real cg size to use fs_bsize */ 1650bd9d203Speter int32_t fs_sparecon32[26]; /* reserved for future constants */ 1660bd9d203Speter uint32_t fs_flags; /* see FS_ flags below */ 1670bd9d203Speter/* back to stuff that has been around a while (again) */ 168d31da81bSlukem int32_t fs_contigsumsize; /* size of cluster summary array */ 1690bd9d203Speter int32_t fs_maxsymlinklen; /* max length of an internal symlink */ 1700bd9d203Speter int32_t fs_old_inodefmt; /* format of on-disk inodes */ 171fd18408bSperry uint64_t fs_maxfilesize; /* maximum representable file size */ 1720bd9d203Speter int64_t fs_qbmask; /* ~fs_bmask for use with 64-bit size */ 1730bd9d203Speter int64_t fs_qfmask; /* ~fs_fmask for use with 64-bit size */ 174d31da81bSlukem int32_t fs_state; /* validate fs_clean field (UNUSED) */ 1750bd9d203Speter int32_t fs_old_postblformat; /* format of positional layout tables */ 1760bd9d203Speter int32_t fs_old_nrpos; /* number of rotational positions */ 1770bd9d203Speter int32_t fs_spare5[2]; /* old fs_postbloff */ 1780bd9d203Speter /* old fs_rotbloff */ 179d31da81bSlukem int32_t fs_magic; /* magic number */ 18061f28255Scgd}; 18161f28255Scgd.Ed 18261f28255Scgd.Pp 18361f28255ScgdEach disk drive contains some number of file systems. 18461f28255ScgdA file system consists of a number of cylinder groups. 18561f28255ScgdEach cylinder group has inodes and data. 18661f28255Scgd.Pp 18761f28255ScgdA file system is described by its super-block, which in turn 188fa72f5deSfairdescribes the cylinder groups. 189fa72f5deSfairThe super-block is critical data and is replicated in each cylinder 190fa72f5deSfairgroup to protect against catastrophic loss. 191fa72f5deSfairThis is done at file system creation time and the critical super-block 192fa72f5deSfairdata does not change, so the copies need not be referenced further 193fa72f5deSfairunless disaster strikes. 19461f28255Scgd.Pp 19561f28255ScgdAddresses stored in inodes are capable of addressing fragments 196fa72f5deSfairof `blocks'. 197fa72f5deSfairFile system blocks of at most size 19861f28255Scgd.Dv MAXBSIZE 19961f28255Scgdcan 20061f28255Scgdbe optionally broken into 2, 4, or 8 pieces, each of which is 20161f28255Scgdaddressable; these pieces may be 20261f28255Scgd.Dv DEV_BSIZE , 20361f28255Scgdor some multiple of 20461f28255Scgda 20561f28255Scgd.Dv DEV_BSIZE 20661f28255Scgdunit. 20761f28255Scgd.Pp 208fa72f5deSfairLarge files consist of exclusively large data blocks. 209fa72f5deSfairTo avoid undue wasted disk space, the last data block of a small 210fa72f5deSfairfile is allocated as only as many fragments of a large block as 211fa72f5deSfairare necessary. 212fa72f5deSfairThe file system format retains only a single pointer to such a 213fa72f5deSfairfragment, which is a piece of a single large block that has been divided. 214fa72f5deSfairThe size of such a fragment is determinable from 21561f28255Scgdinformation in the inode, using the 21661f28255Scgd.Fn blksize fs ip lbn 21761f28255Scgdmacro. 21861f28255Scgd.Pp 21961f28255ScgdThe file system records space availability at the fragment level; 22061f28255Scgdto determine block availability, aligned fragments are examined. 22161f28255Scgd.Pp 22261f28255ScgdThe root inode is the root of the file system. 22361f28255ScgdInode 0 can't be used for normal purposes and 22461f28255Scgdhistorically bad blocks were linked to inode 1, 22561f28255Scgdthus the root inode is 2 (inode 1 is no longer used for 22661f28255Scgdthis purpose, however numerous dump tapes make this 22761f28255Scgdassumption, so we are stuck with it). 22861f28255Scgd.Pp 22961f28255ScgdThe 23061f28255Scgd.Fa fs_minfree 23161f28255Scgdelement gives the minimum acceptable percentage of file system 232fa72f5deSfairblocks that may be free. 233fa72f5deSfairIf the freelist drops below this level 23461f28255Scgdonly the super-user may continue to allocate blocks. 23561f28255ScgdThe 23661f28255Scgd.Fa fs_minfree 23761f28255Scgdelement 23861f28255Scgdmay be set to 0 if no reserve of free blocks is deemed necessary, 23961f28255Scgdhowever severe performance degradations will be observed if the 24061f28255Scgdfile system is run at greater than 90% full; thus the default 24161f28255Scgdvalue of 24261f28255Scgd.Fa fs_minfree 24361f28255Scgdis 10%. 24461f28255Scgd.Pp 24561f28255ScgdEmpirically the best trade-off between block fragmentation and 24661f28255Scgdoverall disk utilization at a loading of 90% comes with a 24761f28255Scgdfragmentation of 8, thus the default fragment size is an eighth 24861f28255Scgdof the block size. 24961f28255Scgd.Pp 25061f28255ScgdThe element 25161f28255Scgd.Fa fs_optim 25261f28255Scgdspecifies whether the file system should try to minimize the time spent 25361f28255Scgdallocating blocks, or if it should attempt to minimize the space 25461f28255Scgdfragmentation on the disk. 25561f28255ScgdIf the value of fs_minfree (see above) is less than 10%, 25661f28255Scgdthen the file system defaults to optimizing for space to avoid 25761f28255Scgdrunning out of full sized blocks. 25861f28255ScgdIf the value of minfree is greater than or equal to 10%, 25961f28255Scgdfragmentation is unlikely to be problematical, and 26061f28255Scgdthe file system defaults to optimizing for time. 26161f28255Scgd.Pp 26261f28255Scgd.Em Cylinder group related limits : 26361f28255ScgdEach cylinder keeps track of the availability of blocks at different 26461f28255Scgdrotational positions, so that sequential blocks can be laid out 265fa72f5deSfairwith minimum rotational latency. 266fa72f5deSfairWith the default of 8 distinguished 26761f28255Scgdrotational positions, the resolution of the 26861f28255Scgdsummary information is 2ms for a typical 3600 rpm drive. 26961f28255Scgd.Pp 27061f28255ScgdThe element 27161f28255Scgd.Fa fs_rotdelay 27261f28255Scgdgives the minimum number of milliseconds to initiate 27361f28255Scgdanother disk transfer on the same cylinder. 27461f28255ScgdIt is used in determining the rotationally optimal 27561f28255Scgdlayout for disk blocks within a file; 27661f28255Scgdthe default value for 27761f28255Scgd.Fa fs_rotdelay 27861f28255Scgdis 2ms. 27961f28255Scgd.Pp 2800bf14e7eSsommerfeEach file system has a statically allocated number of inodes, 2810bf14e7eSsommerfedetermined by its size and the desired number of file data bytes per 2820bf14e7eSsommerfeinode at the time it was created. See 2830bf14e7eSsommerfe.Xr newfs 8 2840bf14e7eSsommerfefor details on how to set this (and other) filesystem parameters. 2850bf14e7eSsommerfeBy default, the inode allocation strategy is extremely conservative. 28661f28255Scgd.Pp 28761f28255Scgd.Dv MINBSIZE 28861f28255Scgdis the smallest allowable block size. 28961f28255ScgdWith a 29061f28255Scgd.Dv MINBSIZE 29161f28255Scgdof 4096 29261f28255Scgdit is possible to create files of size 29361f28255Scgd2^32 with only two levels of indirection. 29461f28255Scgd.Dv MINBSIZE 29561f28255Scgdmust be big enough to hold a cylinder group block, 29661f28255Scgdthus changes to 29761f28255Scgd.Pq Fa struct cg 29861f28255Scgdmust keep its size within 29961f28255Scgd.Dv MINBSIZE . 30061f28255ScgdNote that super-blocks are never more than size 30161f28255Scgd.Dv SBSIZE . 30261f28255Scgd.Pp 30361f28255ScgdThe path name on which the file system is mounted is maintained in 30461f28255Scgd.Fa fs_fsmnt . 30561f28255Scgd.Dv MAXMNTLEN 30661f28255Scgddefines the amount of space allocated in 30761f28255Scgdthe super-block for this name. 30861f28255ScgdThe limit on the amount of summary information per file system 30961f28255Scgdis defined by 31061f28255Scgd.Dv MAXCSBUFS . 31161f28255ScgdFor a 4096 byte block size, it is currently parameterized for a 31261f28255Scgdmaximum of two million cylinders. 31361f28255Scgd.Pp 31461f28255ScgdPer cylinder group information is summarized in blocks allocated 31561f28255Scgdfrom the first cylinder group's data blocks. 31661f28255ScgdThese blocks are read in from 31761f28255Scgd.Fa fs_csaddr 31861f28255Scgd(size 31961f28255Scgd.Fa fs_cssize ) 32061f28255Scgdin addition to the super-block. 32161f28255Scgd.Pp 32261f28255Scgd.Sy N.B.: 323fa72f5deSfair.Fn sizeof "struct csum" 32461f28255Scgdmust be a power of two in order for 32561f28255Scgdthe 32661f28255Scgd.Fn fs_cs 32761f28255Scgdmacro to work. 32861f28255Scgd.Pp 32961f28255ScgdThe 33061f28255Scgd.Em "Super-block for a file system" : 33161f28255ScgdThe size of the rotational layout tables 33261f28255Scgdis limited by the fact that the super-block is of size 33361f28255Scgd.Dv SBSIZE . 33461f28255ScgdThe size of these tables is 33561f28255Scgd.Em inversely 336fa72f5deSfairproportional to the block size of the file system. 337fa72f5deSfairThe size of the tables is increased when sector sizes are not powers 338fa72f5deSfairof two, as this increases the number of cylinders included before 339fa72f5deSfairthe rotational pattern repeats 34061f28255Scgd.Pq Fa fs_cpc . 34161f28255ScgdThe size of the rotational layout 34261f28255Scgdtables is derived from the number of bytes remaining in 34361f28255Scgd.Pq Fa struct fs . 34461f28255Scgd.Pp 34561f28255ScgdThe number of blocks of data per cylinder group 34661f28255Scgdis limited because cylinder groups are at most one block. 34761f28255ScgdThe inode and free block tables 34861f28255Scgdmust fit into a single block after deducting space for 34961f28255Scgdthe cylinder group structure 35061f28255Scgd.Pq Fa struct cg . 35161f28255Scgd.Pp 35261f28255ScgdThe 35361f28255Scgd.Em Inode : 35461f28255ScgdThe inode is the focus of all file activity in the 355dc026e05Sfair.Ux 35661f28255Scgdfile system. 35761f28255ScgdThere is a unique inode allocated 35861f28255Scgdfor each active file, 35961f28255Scgdeach current directory, each mounted-on file, 36061f28255Scgdtext file, and the root. 36161f28255ScgdAn inode is `named' by its device/i-number pair. 36261f28255ScgdFor further information, see the include file 363*5e015e5eSjoerg.In ufs/ufs/inode.h . 3640bf14e7eSsommerfe.Sh SEE ALSO 3650bf14e7eSsommerfe.Xr newfs 8 36661f28255Scgd.Sh HISTORY 36761f28255ScgdA super-block structure named filsys appeared in 36861f28255Scgd.At v6 . 36961f28255ScgdThe file system described in this manual appeared 37061f28255Scgdin 37161f28255Scgd.Bx 4.2 . 372