Home > Articles > Security > Network Security

This chapter is from the book

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.

15fig15_01.gif

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.

    ufs_block_layout.gif

    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.

ufs_03.gif

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.

ufs_04.gif

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.

ufs_05.gif

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.

ufs_06.gif

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.

ufs_layout15_7.gif

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.

ufs_08.gif

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.

15fig15_09.gif

Figure 15.9 The UFS File System

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020