previous contents up next

Unix for Advanced Users

12. The Unix Filesystem

12.3. The Virtual Filesystem

One of the unique things about Unix as an operating system is that regards everything as a file. Files can be divided into three categories; ordinary or plain files, directories, and special or device files. Device files include things such as disks, cdroms, tapes, terminals, serial ports, and sound cards. All files contain data of some kind.

12.3.1. The root of the filesystem and the Unix directory tree

The top level directory under Unix (denoted by a forward slash - /) is referred to as the root of the Unix (virtual) filesystem. Under the root directory are a number of directories who names depend somewhat on vendor preferences and on whether you are running a BSD or a SVR4 flavor of Unix:

BSD Style Directory Tree
(Click on a directory to see a description.)

SVR4 Style Directory Tree
(Click on a directory to see a description.)

12.3.2. Plain files

Ordinary or plain files are most likely to contain data that are meaningful to users. Plain files may contain data that users have entered into the computer, data that have been generated by programs, and even programs themselves. Indeed, all programs exist as files, and nearly all Unix commands result in a program file being retrieved and executed. This is an important feature and contributes to the flexibility of Unix.

12.3.3. Unix directory structure

Directories in Unix are properly known as directory files. They contain a list of file names and inode numbers. That is all! All other information about files is contained within inodes.

12.3.4. Links

Soft links are files that reference other files. The information in the file is the name of the file being referenced. When a user program attempts to read a file that is a soft link, the file system opens the symbolic link file, reads the reference, and then opens the file that is referenced. File management programs operate on the symbolic link file. For example, ls -l reads the symbolic link file and displays the name of the file being referenced, and rm deletes the symbolic link file, leaving untouched the file that is referenced by the link.

Hard links are not really files. They exist when more than one directory entry references the same inode. Hence, when a hard link is opened by a user program, the directory entry that is opened points directly to the inode of the data to be provided. And, when a hard link is deleted using rm, the directory entry is removed. Only when one deletes the last remaining directory entry that points to an inode are the data deleted.

12.3.5. Special

Special files are also known as device files. In Unix all physical devices are accessed via device files; they are what programs use to communicate with hardware. Device files contain information on location, type, and access mode for a specific device. When a device file is opened, the kernel uses the information in it to determine which physical device to access and how to access it.

There are two types of device files; character and block, as well as two modes of access. Block device files are used to access block device I/O. Block devices do buffered I/O, meaning that the the data is collected in a buffer until a full block can be transfered. Character device files are associated with character or raw device access. They are used for unbuffered data transfers to and from a device. Rather than transferring data in blocks the data is transfered character by character. One transfer can consist of multiple characters.

Some devices, such as disk partitions, may be accessed in block or character mode. Because each device file corresponds to a single access mode, physical devices that have more than one access mode will have more than one device file.

Device files are found in the /dev directory. Each device is assigned a major and minor device number. The major device number identifies the type of device, i.e. all SCSI devices would have the same number as would all the keyboards. The minor device number identifies a specific device, i.e. the keyboard attached to this workstation.

Device files are created using the mknod command. The form for this command is:

mknod device-name type major minor

The major and minor device numbers are indexed to device switches. There are two types of device switches; cdevsw for character devices and bdevsw for block devices. These switches are kernel structures that hold the names of all the control routines for a device and tell the kernel which driver module to execute. Device switches are actually tables that look something like this:

Using the ls command in the /dev directory will show entries that look like:

brw-r----- 1 root sys 1, 0 Aug 31 16:01 /dev/sd1a

The "b" before the permissions indicates that this is a block device file. When a user enters /dev/sd1a the kernel sees the file opening, realizes that it's major device number 1, and calls up the SCSIbus function to handle it.

previous contents up next