15.2 UFS On-Disk Format
UFS is built around the concept of a disk's geometry, which is described as the number of sectors in a track, the location of the head, and the number of tracks. UFS uses a hybrid block allocation strategy that allocates full blocks or smaller parts of the block called fragments. A block is a set of contigous fragments starting on a particular boundary. This boundary is determined by the size of a fragment and the number of fragments that constitute a block. For example, fragment 32 and block 32 both relate to the same physical location on disk. Although the next fragment on disk is 33 followed by 34, 35, 36, 37 and so on, the next block is at 40, which begins on fragment 40. This is true in the case of 8-Kbyte block size and 1-Kbyte fragment size, where 8 fragments constitutes a file system block.
15.2.1 On-Disk UFS Inodes
In UFS, all information pertaining to a file is stored in a special file index node called the inode (except for the name of the file, which is stored in the directory). There are two types of inodes: in-core and on-disk. The on-disk inodes, as the name implies, reside on disk, whereas the in-core inode is created only when a particular file is opened for reading or writing.
The on-disk inode is represented by struct icommon. It occupies exactly 128 bytes on disk and can also be found embedded in the in-core inode structure, as shown in Figure 15.1.
Figure 15.1 Embedded On-Disk in In-Core Inode
The structure of icommon looks like this.
struct icommon { o_mode_t ic_smode; /* 0: mode and type of file */ short ic_nlink; /* 2: number of links to file */ o_uid_t ic_suid; /* 4: owner's user id */ o_gid_t ic_sgid; /* 6: owner's group id */ u_offset_t ic_lsize; /* 8: number of bytes in file */ #ifdef _KERNEL struct timeval32 ic_atime; /* 16: time last accessed */ struct timeval32 ic_mtime; /* 24: time last modified */ struct timeval32 ic_ctime; /* 32: last time inode changed */ #else time32_t ic_atime; /* 16: time last accessed */ int32_t ic_atspare; time32_t ic_mtime; /* 24: time last modified */ int32_t ic_mtspare; time32_t ic_ctime; /* 32: last time inode changed */ int32_t ic_ctspare; #endif daddr32_t ic_db[NDADDR]; /* 40: disk block addresses */ daddr32_t ic_ib[NIADDR]; /* 88: indirect blocks */ int32_t ic_flags; /* 100: cflags */ int32_t ic_blocks; /* 104: 512 byte blocks actually held */ int32_t ic_gen; /* 108: generation number */ int32_t ic_shadow; /* 112: shadow inode */ uid_t ic_uid; /* 116: long EFT version of uid */ gid_t ic_gid; /* 120: long EFT version of gid */ uint32_t ic_oeftflag; /* 124: extended attr directory ino, 0 = none */ }; See usr/src/uts/common/sys/fs/ufs_inode.h
Most of the fields are self-explaining, but a couple of them need a bit of help:
- ic_smode. Indicates the type of inode. There are primarily four main types of inode: zero, special node (IFCHR, IFBLK, IFIFO, IFSOCK), symbolic link (IFLNK), a directory (IFDIR), a file (IFREG), or an extended metadata inode (IFSHAD, IFATTRDIR). Type zero indicates that the inode is not in use and ic_nlink should be zero, unless logging's reclaim_needed flag is set. With the special nodes, no data blocks are associated. They are used for character and block devices, pipes and sockets. The type file indicates where this inode is a directory, a regular file, a shadow inode, or an extended attribute directory.
- ic_nlink. Refers to the number of links to a file, that is, the number of names in the namespace that correspond to a specific file identifier. A regular file will have link count of 1 because only one name in the namespace corresponds to that particular file identifier. A directory link count has the value 2 by default: one is the name of the directory itself, and the other is the "." entry within the directory. Any subdirectory within a directory causes the link count to be incremented by 1 because of the ".." entry. The limit is 32,767 and hence, the limit for the number of subdirectories is 32,765 and also the total number of links. The ".." entry counts against the parent directory only.
- ic_db. Is an array that holds 12 pointers to data blocks. These are called the direct blocks. On a system with block size of 8192 bytes or 8 Kbytes, these can accommodate up to 98,304 bytes or 96 Kbytes. If the file consists entirely of direct blocks, then the last block for the file (not the last ic_db entry) may contain fragments. Note that if the file size exceeds the capacity of the ic_db array, then the block list for the file must consist entirely of full-sized file system blocks.
-
ic_ib. Is a small array of only three pointers but allows a file to be up to one terabyte. How does this work? Well, the first entry in ic_ib points to a block that stores 2048 block addresses. A file with a single indirect block can accommodate up to 8192 * (12 + 2048) bytes or 16 Mbytes. If more storage is required, another level of indirection is added and the second indirect block is used.
The second entry in ic_ib points to 2048 block addresses, and each of those 2048 entries points to another block containing 2048 entries that finally point to the data blocks. With two levels of indirection, a file can accommodate up to 8192 * 12 + 2048 + (2048 * 2048) bytes, or 32 Gbytes. A third level of indirection permits the file to be 8192 * 12 + 2048 + (2048 * 2048) + (2048 * 2048 * 2048) = 70,403,120,791,552 bytes long or—yes, you guessed it—64 Tbytes! However, since all addresses must be addressable as fragments, that is, a 31-bit count, the maximum is 2TB (2^31 * 1KB). Multi-terrabyte UFS (MTBUFS) enables 16TB filesystem sizes by enforcing the minimum frag size to be 8K, which gives you 2^31 * 2^10 * 8k, or 16 TB.
Figure 15.2 illustrates the layout.
Figure 15.2 UFS Block Layout
- ic_shadow. If non-zero, contains the number of an inode providing shadow metadata (usually, this data would be ACLs).
- ic_oeftflag. If non-zero, contains the number of an inode of type IFATTRDIR, which is a directory containing extended attribute files.
15.2.2 UFS Directories
The file name information and hierarchy information that constitute the directory structure of UFS are stored in directories. Each directory stores a list of file names and the inode number for each file; this information (stored in struct direct) allows the directory structure to relate file names to real disk files.
The directory itself is stored in a file as a series of chunks, which are groups of the directory entries. Earlier file systems like the System V file system had a fixed directory record length, which meant that a lot of space would be wasted if provision was made for long file names. In the UFS, each directory entry can be of variable length, thus providing a mechanism for long file names without a lot of wasted space. UFS file names can be up to 255 characters long.
The group of directory chunks that constitute a directory is stored as a special type of file. The notion of a directory as a type of file allows UFS to implement a hierarchical directory structure: Directories can contain files that are directories. For example, the root directory has a name, "/", and an inode number, 2, which holds a chunk of directory entries holding a number of files and directories. One of these directory entries, named etc, is another directory containing more files and directories. For traversal up and down the file system, the chdir system call opens the directory file in question and then sets the current working directory to point to the new directory file. Figure 15.3 illustrates the directory hierarchy.
Figure 15.3 UNIX Directory Hierarchy
Each directory contains two special files. The file named "." is a link to the directory itself; the file named ".." is a link to the parent directory. Thus, a change of directory to .. leads to the parent directory.
Now let's switch gears and see what the on-disk structures for directories look like.
The contents of a directory are broken up into DIRBLKSIZ chunks, also known as dirblks. Each of these contains one or more direct structures. DIRBLKSIZ was chosen to be the same as the size of a disk sector so that modifications to directory entries could be done atomically on the assumption that a sector write either completes successfully or fails (which can no longer be guaranteed with the advancement of cached hard drives).
Each directory entry is stored in a structure called direct that contains the inode number (d_ino), the length of the entry (d_reclen), the length of the name (d_namelen), and a null-terminated string for the name itself (d_name).
#define DIRBLKSIZ DEV_BSIZE #define MAXNAMLEN 255 struct direct { uint32_t d_ino; /* inode number of entry */ ushort_t d_reclen; /* length of this record */ ushort_t d_namlen; /* length of string in d_name */ char d_name[MAXNAMLEN + 1]; /* name must be no longer than this */ }; See usr/src/uts/common/sys/fs/ufs_fsdir.h
d_reclen includes the space consumed by all the fields in a directory entry, including d_name's trailing null character. This facilitates directory entry deletion because when an entry is deleted, if it is not the first entry in the current directory, the entry before it is grown to include the deleted one, that is, d_reclen is incremented to account for the size of the next entry. The procedure is relatively inexpensive and helps keep internal fragmentation down. Figure 15.4 illustrates the concept of directory deletion.
Figure 15.4 Deletion of a Directory Entry
15.2.3 UFS Hard Links
There is one inode for each file on disk; however, with hard links, each file can have multiple file names. With hard links, file names in multiple directories point to the same on-disk inode. The inode reference count field reflects the number of hard links to the inode. Figure 15.5 illustrates inode 1423 describing a file; two separate directory entries with different names both point to the same inode number. Note that the reference count, refcnt, has been incremented to 2.
Figure 15.5 UFS Links
15.2.4 Shadow Inodes
UFS allows storage of additional per-inode data through the use of shadow inodes. The implementation of a shadow inode is generic enough to permit storage of any arbitrary data. All that is needed are a tag to identify the data and functions to convert the appropriate data structures from on-disk to in-core, and vice versa. As of this writing (2005), only two data types are defined: FSD_ACL for identification of ACLs and FSD_DFACL for default ACLs. Only one shadow inode is permitted per inode today, and as a result both ACLs and default ACLs are stored in the same shadow inode.
typedef struct ufs_fsd { int fsd_type; /* type of data */ int fsd_size; /* size in bytes of ufs_fsd and data */ char fsd_data[1]; /* data */ } ufs_fsd_t; See usr/src/uts/common/sys/fs/ufs_acl.h
The way a shadow inode is laid out on disk is quite simple (see Figure 15.6). All the entries for the shadow inode contain one header that includes the type of data and the length of the whole record, data + header. Entries are then simply concatenated and stored to disk as a separate inode with the inode's ic_smode set to ISHAD. The parent's ic_shadow is then updated to point to this shadow inode.
Figure 15.6 On-Disk Shadow Inode Layout
15.2.5 The Boot Block
Figure 15.7 illustrates the UFS layout discussed in this section. At the start of the file system is the boot block. This is a spare sector reserved for the boot program when UFS is used as a root file system. At boot time, the boot firmware loads the first sector from the boot device and then starts executing code residing in that block. The firmware boot is file system independent, which means that the boot firmware has no knowledge about the file system. We rely on code in the file system boot block to mount the root file system. When the system starts, the UFS boot block is loaded and executed, which, in turn, mounts the UFS root file system. The boot program then passes control to a larger kernel loader, in /platform/ sun4[mud]/ufsboot, to load the UNIX kernel.
Figure 15.7 UFS Layout
The boot program is loaded onto the first sector of the file system at install time with the installboot(1M) command. The 512-byte install boot image resides in /usr/platform/sun4[mud]/lib/fs/ufs/bootblk in the platform-dependent directories.
15.2.6 The Superblock
The superblock contains all the information about the geometry and layout of the file system and is critical to the file system state. As a safety precaution, the superblock is replicated across the file system with each cylinder group so that the file system is not crippled if the superblock becomes corrupted. It is initially created by mkfs and updated by tunefs and mkfs (in case a file system is grown). The primary superblock starts at an offset of 8192 bytes into the partition slice and occupies one file system block (usually 8192 bytes, but can be 4096 bytes on x86 architectures). The superblock contains a variety of information, including the location of each cylinder group and a summary list of available free blocks. The major information in the superblock that identifies the file system geometry is listed below.
- fs_sblkno. Address of superblock in file system; defaults to block number 16.
- fs_cblkno. Offset of the first cylinder block in the file system.
- fs_iblkno. Offset of the first inode blocks in the file system.
- fs_dblkno. Offset of the first data blocks after the first cylinder group.
- fs_cgoffset. Cylinder group offset in the cylinder.
- fs_cgmask. Mask to obtain physical starting fragment number of the cylinder group.
- fs_time. Last time written.
- fs_size. Number of blocks in the file system.
- fs_dsize. Number of data blocks the in file system.
- fs_ncg. Number of cylinder groups.
- fs_cpg. Number of cylinders in a cylinder group.
- fs_ipg. Number of inodes in a cylinder group.
- fs_fpg. Number of fragments (including metadata) in a cylinder group.
- fs_bsize. Size of basic blocks in the file system.
- fs_fsize. Size of fragmented blocks in the file system.
- fs_frag. Number of fragments in a block in the file system.
- fs_magic. A magic number to validate the superblock.
The file system configuration parameters also reside in the superblock. The file system parameters include some of the following, which are configured at the time the file system is constructed. You can tune the parameters later with the tunefs command.
- fs_minfree. Minimum percentage of free blocks.
- fs_rotdelay. Number of milliseconds of rotational delay between sequential blocks. The rotational delay was used to implement block interleaving when the operating system could not keep up with reading contiguous blocks. Since this is no longer an issue, fs_rotdelay defaults to zero.
- fs_rps. Disk revolutions per second.
- fs_maxcontig. Maximum number of contiguous blocks, controls the number of read-ahead blocks.
- fs_maxbpg. Maximum number of data blocks per cylinder group.
- fs_optim. Optimization preference, space, or time.
And here are the significant logging related fields in the superblock:
- fs_rolled. Determines whether any data in the log still needs to be rolled back to the file system.
- fs_si. Indicates whether logging summary information is up to date or whether it needs to be recalculated from cylinder groups.
- fs_clean. Is set to FS_LOG for logging file system.
- fs_logbno. Is the disk block number of logging metadata.
- fs_reclaim: Is set to indicate if the reclaim thread is running or needs to be run.
See struct fs in usr/src/uts/common/sys/fs/ufs_fs.h for the complete superblock structure definition
15.2.7 The Cylinder Group
The cylinder group is made up of several logically distinct parts. At logical offset zero into the cylinder group is a backup copy of the file system's superblock. Following that, we have the cylinder group structure, the blktot array (indicating how many full blocks are available), the blks array (representing the full-sized blocks that are free in each rotational position), inode bitmap (marking which inodes are in use), and finally, the bitmap of which fragments are free. Next in the layout is the array of inodes whose size varies according to the number of inodes in a cylinder group (on-disk inode size is restricted to 128 bytes). And finally, the rest of the cylinder group is filled by the data blocks.
Figure 15.8 illustrates the layout.
Figure 15.8 Logical Layout of a Cylinder Group
The last cylinder group in a file system may be incomplete because the number of cylinders in a disk drive is usually not exactly rounded up to the cylinder groups. In this case, we simply reduce the number of data blocks available in the last cylinder group; however, the metadata portion of the cylinder group stays the same throughout the file system. The cg_ncyl and cg_nblk fields of the cylinder group structure guide us to the size so that we don't accidentally go out of bounds.
/* * Cylinder group block for a file system. * * Writable fields in the cylinder group are protected by the associated * super block lock fs->fs_lock. */ #define CG_MAGIC 0x090255 struct cg { uint32_t cg_link; /* NOT USED linked list of cyl groups */ int32_t cg_magic; /* magic number */ time32_t cg_time; /* time last written */ int32_t cg_cgx; /* we are the cgx'th cylinder group */ short cg_ncyl; /* number of cyl's this cg */ short cg_niblk; /* number of inode blocks this cg */ int32_t cg_ndblk; /* number of data blocks this cg */ struct csum cg_cs; /* cylinder summary information */ int32_t cg_rotor; /* position of last used block */ int32_t cg_frotor; /* position of last used frag */ int32_t cg_irotor; /* position of last used inode */ int32_t cg_frsum[MAXFRAG]; /* counts of available frags */ int32_t cg_btotoff; /* (int32_t)block totals per cylinder */ int32_t cg_boff; /* (short) free block positions */ int32_t cg_iusedoff; /* (char) used inode map */ int32_t cg_freeoff; /* (uchar_t) free block map */ int32_t cg_nextfreeoff; /* (uchar_t) next available space */ int32_t cg_sparecon[16]; /* reserved for future use */ uchar_t cg_space[1]; /* space for cylinder group maps */ /* actually longer */ }; See usr/src/uts/common/sys/fs/ufs_fs.h
15.2.8 Summary of UFS Architecture
Figure 15.9 puts it all together.
Figure 15.9 The UFS File System