ufs(4) ufs(4)NAME ufs - UFS file-system format SYNOPSIS #include <sys/types.h> #include <sys/time.h> #include <sys/vnode.h> #include <ufs/fs.h> #include <ufs/inode.h> DESCRIPTION Each Berkeley 4.2 file system (UFS), whether on a hard disk or a floppy disk, shares a common format. Information about that format is stored in the file system in an area known as the superblock. The file system is divided into a certain number of blocks. The size of each block is a file system parameter that is stored in the superblock. Sectors 0 to 7 of a file system contain bootstrap programs. Sector 8 contains the first superblock of the file system. Sector 16 contains the first alternate superblock. Here is the layout of the superblock as defined by <ufs/fs.h>: #define DEVB_SIZE512 #define FS_MAGIC0x011954 #define MAXBSIZE8192 #define MAXCPG32 #define MAXCSBUFS32 #define MAXIPG2048 #define MAXMNTLEN(512-12) #define MINBSIZE4096 #define NPROS8 #define SBSIZE4096 struct fs { struct fs *fs_link;/* linked list of file systems */ struct fs *fs_rlink;/* used for incore superblocks */ daddr_t fs_sblkno;/* addr of superblock in filesys */ daddr_t fs_cblkno;/* offset of cyl-block in filesys */ daddr_t fs_iblkno;/* offset of inode-blocks in filesys */ daddr_t fs_dblkno;/* offset of first data after cg */ long fs_cgoffset;/* cylinder group offset in cylinder */ long fs_cgmask;/* used to calc mod fs_ntrak */ time_t fs_time;/* last time written */ long fs_size;/* number of blocks in fs */ long fs_dsize;/* number of data blocks in fs */ long fs_ncg;/* number of cylinder groups */ long fs_bsize;/* size of basic blocks in fs */ long fs_fsize;/* size of frag blocks in fs */ long fs_frag;/* number of frags in a block in fs */ /* these are configuration parameters */ long fs_minfree;/* minimum percentage of free blocks */ long fs_rotdelay;/* num of ms for optimal next block */ long fs_rps;/* disk revolutions per second */ /* these fields can be computed from the others */ January 1992 1
ufs(4) ufs(4)long fs_bmask;/* ``blkoff'' calc of blk offsets */ long fs_fmask;/* ``fragoff'' calc of frag offsets */ long fs_bshift;/* ``lblkno'' calc of logical blkno */ long fs_fshift;/* ``numfrags'' calc number of frags */ /* these are configuration parameters */ long fs_maxcontig;/* max number of contiguous blks */ long fs_maxbpg;/* max number of blks per cyl group */ /* these fields can be computed from the others */ long fs_fragshift;/* block to frag shift */ long fs_fsbtodb;/* fsbtodb and dbtofsb shift constant */ long fs_sbsize;/* actual size of superblock */ long fs_csmask;/* csum block offset */ long fs_csshift;/* csum block number */ long fs_nindir;/* value of NINDIR */ long fs_inopb;/* value of INOPB */ long fs_nspf;/* value of NSPF */ long fs_optim;/* optimization preference */ long fs_sparecon[2]; /* reserved for future constants */ long fs_state;/* file-system state */ long fs_id[2];/* file-system id */ /* sizes determined by number of cylinder groups and their sizes */ daddr_t fs_csaddr;/* blk addr of cyl grp summary area */ long fs_cssize;/* size of cyl grp summary area */ long fs_cgsize;/* cylinder group size */ /* these fields should be derived from the hardware */ long fs_ntrak;/* tracks per cylinder */ long fs_nsect;/* sectors per track */ long fs_spc;/* sectors per cylinder */ /* this comes from the disk driver partitioning */ long fs_ncyl;/* cylinders in file system */ /* these fields can be computed from the others */ long fs_cpg;/* cylinders per group */ long fs_ipg;/* inodes per group */ long fs_fpg;/* blocks per group * fs_frag */ /* this data must be recomputed after crashes */ struct csum fs_cstotal; /* cylinder summary information */ /* these fields are cleared at mount time */ char fs_fmod;/* superblock modified flag */ char fs_clean;/* file system is clean flag */ char fs_ronly;/* mounted read-only flag */ char fs_flags;/* currently unused flag */ char fs_fsmnt[MAXMNTLEN]; /* name mounted on */ char fs_fsname[6];/* file-system name */ char fs_fpack[6];/* file-system pack name */ /* these fields retain the current block allocation info */ long fs_cgrotor;/* last cg searched */ struct csum *fs_csp[MAXCSBUFS]; /* list of fs_cs info buffers */ long fs_cpc;/* cyl per cycle in postbl */ short fs_postbl[MAXCPG][NRPOS]; /* head of blocks for each rotation */ 2 January 1992
ufs(4) ufs(4)long fs_magic;/* magic number */ u_char fs_rotbl[1];/* list of blocks for each rotation */ /* actually longer */ }; Disk Layout A disk can contain one or more partitions. A disk partition can contain at most one file system. A UFS file system consists of a number of cylinder groups. The superblock of a UFS file system describes the cylinder groups. The superblock contains critical data that is replicated in each cylinder group to protect against catastrophic loss. This replication is made when the file system is created. In addition, because critical superblock data does not change, the copies remain useful even when the file system has been used. Each cylinder group has inodes and data. The inode is the focus of all file activity in the UFS file system. A unique inode is allocated for each active file, each current directory, each directory that serves as a mount point, text file, and the root. An inode is named by its device file and inumber pair. For further information, see <ufs/inode.h>. Addresses stored in inodes can be used for addressing fragments of blocks. File-system blocks, whose maximum size is specified by the value of MAXBSIZE, can be broken into two, four, or eight fragments, each of which is addressable. The size of each of these pieces must be either the value of DEV_BSIZE or a multiple of that value. Large files consist exclusively of large data blocks. So that disk space is not wasted, the last data block of a small file is allocated only as many fragments of a large block as are necessary. The file-system format retains only a single pointer to such a fragment. You can determine the size of such a fragment from information in the inode by using the blksize macro in <ufs/fs.h>. The file system records space availability at the fragment level. To determine block availability, the ufs module in the kernel examines aligned fragments. Inodes 0 and 1 of the UFS file system are reserved, and inode 2 is the root inode. The root inode is the root of the file system. (Early versions of the UFS file system linked bad blocks to inode 1, but this is no longer done. To maintain compatibility with dump tapes of such file systems, inode 1 is reserved.) When making a UFS file system, the /etc/fs/ufs/mkfs command, which is called by newfs, gives the next available inode to the lost+found directory. January 1992 3
ufs(4) ufs(4)The fs_minfree member of the fs structure specifies the minimum acceptable percentage of file-system blocks that must be free. If the free list drops below this level, only users who are logged in as root can continue to allocate blocks. The fs_mintree member can be set to 0 if no reserve of free blocks is deemed necessary; however, severe performance degradations occur if the file system is more than 90 percent full. The default value of fs_minfree is 10 percent. Empirically, the best tradeoff between block fragmentation and overall disk utilization at a loading of 90 percent comes with a fragmentation of 4; thus the default value of the fs_frag member of the fs structure is one-fourth of the block size. Limits Related to the Cylinder Group Each cylinder keeps track of the availability of blocks at different rotational positions so that sequential blocks can be laid out with minimum rotational latency. The symbolic constant NRPOS is the number of distinguishable rotational positions. When NRPOS is defined as 8, the resolution of the summary information is 2 milliseconds for a typical 3600 rpm drive. The fs_rotdelay member of the fs structure specifies the minimum number of milliseconds that it takes to initiate another disk transfer on the same cylinder. The value of fs_rotdelay is used to determine the rotationally optimal layout for disk blocks within a file. The default value for fs_rotdelay is 2 milliseconds. Each file system has a statically allocated number of inodes. An inode is allocated for each 2048 bytes of disk space. The inode allocation strategy is extremely conservative. The symbolic constant MAXIPG specifies the number of inodes per cylinder group and simplifies the cg structure so that only one member, cg_free (the free bit map), varies in size. Note that the value of MAXIPG must be a multiple of the value returned by the INOPB macro. The symbolic constant MINBSIZE is the smallest allowable block size. With a MINBSIZE of 4096, you can create files32of 2 bytes with only two levels of indirection. Thevalue of MINBSIZE must be big enough to accommodate a cylinder group block; therefore, changes to the cg structure must not cause the size of that structure to exceed the value of MINBSIZE. The symbolic constant MAXCPG specifies the dimension of an array in the cg structure. You can increase the size of MAXCPG as long as the size of the cg 4 January 1992
ufs(4) ufs(4)structure does not exceed the value of MINBSIZE. The symbolic constant SBSIZE specifies the maximum size of the superblock. The pathname on which the file system is mounted is stored in the fs_fsmnt member of the fs structure. The symbolic constant MAXMNTLEN defines the amount of space allocated in the superblock for this name. The symbolic constant MAXCSBUFS limits the amount of summary information per file system. It is currently set for a maximum of two million cylinders. Per-cylinder-group information is summarized in blocks allocated from the data blocks of the first cylinder. When the superblock is read into the kernel, the summary of per- cylinder-group information is read from the location indicated by the fs_csaddr member of the fs structure. The size of the summary information is given by the fs_cssize member. Note that the fs_cs macro, defined in <ufs/fs.h>, will not work unless the size of the csum structure is a power of 2. Limits Related to the Superblock MAXBPC bounds the size of the rotational layout tables and is limited by the fact that the superblock is of size SBSIZE. The size of these tables is inversely proportional to the block size of the file system. The size of the tables increases when sector sizes are not powers of 2. Failure to use powers of 2 increases the number of cylinders included before the rotational pattern is repeated, as specified by fs_cpc. The size of the rotational layout tables is derived from the number of bytes remaining in the fs structure. The symbolic constant MAXBPG specifies the number of blocks of data per cylinder group and is limited by the fact that cylinder groups are at most one block. The size of the free-block table is derived from the size of blocks and the number of remaining bytes in the cg structure. SEE ALSO svfs(4) newfs(1M), tunefs(1M) in A/UX System Administrator's Reference January 1992 5