File Types


Linux supports many types of files. The following sections discuss these types of files:

  • Ordinary files, directories, links, and inodes (discussed next)

  • Symbolic links (page 460)

  • Special files (page 460)

  • FIFO special file (named pipe) (page 462)

  • Sockets (page 462)

  • Block and character devices (page 463)

  • Raw devices (page 464)

Ordinary Files, Directories, Links, and Inodes

Ordinary and directory files


An ordinary file stores user data, such as textual information, programs, or images, such as a jpeg or tiff file. A directory is a standard-format disk file that stores information, including names, about ordinary files and other directory files.

Inodes


An inode is a data structure (page 1028), stored on disk, that defines a file's existence and is identified by an inode number. An inode contains critical information, such as the name of the owner of the file, where it is physically located on the disk, and how many hard links point to it. In addition, SELinux (page 400) stores extended information about files in inodes. A directory relates each of the filenames it stores to an inode.

When you move (mv) a file within a filesystem, you change the filename portion of the directory entry associated with the inode that describes the file. You do not create a new inode. If you move a file to another filesystem, mv first creates a new inode on the destination filesystem and then deletes the original inode. You can also use mv to move a directory recursively, in which case all files in the directory are copied and deleted.

When you make an additional hard link (ln, page 192) to a file, you create another reference (an additional filename) to the inode that describes the file. You do not create a new inode.

When you remove (rm) a file, you delete the directory entry that describes the file. When you remove the last hard link to a file, the operating system puts all blocks the inode pointed to back in the free list (the list of blocks that are available for use on the disk) and frees the inode to be used again.

The . and ... directory entries


Every directory contains at least two entries (. and ..). The . entry is a link to the directory itself. The .. entry is a link to the parent directory. In the case of the root directory, there is no parent and the .. entry is a link to the root directory itself. It is not possible to create hard links to directories.

Symbolic links


Because each filesystem has a separate set of inodes, you can create hard links to a file only from within the filesystem that holds that file. To get around this limitation, Linux provides symbolic links, which are files that point to other files. Files that are linked by a symbolic link do not share an inode. As a consequence, you can create a symbolic link to a file from any filesystem. You can also create a symbolic link to a directory, device, or other special file. For more information refer to "Symbolic Links" on page 194.

Special Files

Special files represent Linux kernel routines that provide access to an operating system feature. FIFO (first in, first out) special files allow unrelated programs to exchange information. Sockets allow unrelated processes on the same or different computers to exchange information. One type of socket, the UNIX domain socket, is a special file. Symbolic links are another type of special file.

Device files


Device files, which include both block and character special files, represent device drivers that let you communicate with peripheral devices, such as terminals, printers, and hard disks. By convention, device files appear in the /dev directory and its subdirectories. Each device file represents a device: You read from and write to the file to read from and write to the device it represents. For example, using cat to send an audio file to /dev/dsp plays the file. The following example shows part of the output that an ls l command produces for the /dev directory:

$ ls l /dev total 0 crwrw 1 root root     14,  12 Jan 25 08:33 adsp crw 1 root root     10, 175 Jan 25 08:33 agpgart crw 1 zach root    14,   4 Jan 25 08:33 audio drwxrxrx 3 root root          60 Jan 25 08:33 bus lrwxrwxrwx 1 root root          3 Jan 25 08:33 cdrom > hdb lrwxrwxrwx 1 root root          3 Jan 25 08:33 cdwriter > hdb crw 1 zach root     5,    1 Jan 25 08:33 console lrwxrwxrwx 1 root root          11 Jan 25 08:33 core > /proc/kcore drwxrxrx 6 root root         120 Jan 25 08:33 disk crw 1 zach root    14,    3 Jan 25 08:33 dsp lrwxrwxrwx 1 root root          13 Jan 25 08:33 fd > /proc/self/fd brwrw 1 zach floppy   2,    0 Jan 25 08:33 fd0 brwrw 1 zach floppy    2,  84 Jan 25 08:33 fd0u1040 brwrw 1 zach floppy    2,  88 Jan 25 08:33 fd0u1120 ... lrwxrwxrwx 1 root root          3 Jan 25 08:33 floppy > fd0 crwrwrw 1 root root     1,    7 Jan 25 08:33 full brwr 1 root disk     3,    0 Jan 25 00:33 hda brwr 1 root disk     3,    1 Jan 25 08:33 hda1 brwr 1 root disk     3,    2 Jan 25 08:33 hda2 brwr 1 root disk     3,    3 Jan 25 00:33 hda3 ...


The first character of each line is always , b, c, d, l, or p, representing ordinary (plain), block, character, directory, symbolic link, or named pipe (see the following section), respectively. The next nine characters identify the permissions for the file, followed by the number of hard links and the names of the owner and group. Where the number of bytes in a file would appear for an ordinary or directory file, a device file shows major and minor device numbers (page 463) separated by a comma. The rest of the line is the same as any other ls l listing (page 181).

udev


The udev utility manages device naming dynamically. It replaces the earlier devfs and moves the device naming functionality from the kernel to userspace. Because devices are added to and removed from a system infrequently, the performance penalty associated with this change is minimal. The benefit of the move is that a bug in udev cannot compromise or crash the kernel.

The udev utility is part of the hotplug system (discussed next). When a device is added to or removed from the system, the kernel creates a device name in the /sys pseudofilesystem and notifies hotplug of the event, which is received by udev. The udev utility then creates the device file, usually in the /dev directory, or removes the device file from the system. The udev utility can also rename network interfaces. See fedora.redhat.com/docs/udev and www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html for more information.

Hotplug


The hotplug system allows you to plug a device into the system and use it immediately. Although hotplug was available in the Linux 2.4 kernel, the 2.6 kernel integrates hotplug with the unified device driver model framework (the driver model core) so that any bus can report an event when a device is added to or removed from the system. User software can be notified of the event so it can take appropriate action. See linux-hotplug.sourceforge.net for more information.

FIFO Special File (Named Pipe)

A FIFO special file, also called a named pipe, represents a pipe: You read from and write to the file to read from and write to the pipe. The term FIFO stands for first in, first outthe way any pipe works. In other words, the first information that you put in one end is the first information that comes out the other end. When you use a pipe on a command line to send the output of a program to the printer, the printer outputs the information in the same order that the program produced it and sent it to the pipe.

Unless you are writing sophisticated programs, you will not be working with FIFO special files. However, programs that you use on Linux use named pipes for interprocess communication. You can create a pipe using mkfifo:

$ mkfifo AA $ ls -l AA prwrwr   1 zach zach 0 Apr 26 13:11 AA


The p at the left end of the output of ls l indicates that the file is a pipe.

The UNIX and Linux systems have included pipes for many generations. Without named pipes, only processes that were children of the same ancestor could use pipes to exchange information. Using named pipes, any two processes on a single system can exchange information. When one program writes to a FIFO special file, another program can read from the same file. The programs do not have to run at the same time or be aware of each other's activity. The operating system handles all buffering and information storage. This type of communication is termed asynchronous (async) because programs on the ends of the pipe do not have to be synchronized.

Sockets

Like a FIFO special file, a socket allows asynchronous processes that are not children of the same ancestor to exchange information. Sockets are the central mechanism of the interprocess communication that forms the basis of the networking facility. When you use networking utilities, pairs of cooperating sockets manage the communication between the processes on the local computer and the remote computer. Sockets form the basis of such utilities as ssh and scp.

Major and Minor Device Numbers

A major device number points to a driver in the kernel that works with a class of hardware devices: terminal, printer, tape drive, hard disk, and so on. In the list of the /dev directory on page 461, all of the hard disk partitions have a major device number of 3.

A minor device number represents a particular piece of hardware within a class. Although all hard disk partitions are grouped together by their major device number, each has a different minor device number (hda1 is 1, hda2 is 2, and so on). This setup allows one piece of software (the device driver) to service all similar hardware yet to be able to distinguish among different physical units.

Block and Character Devices

This section describes typical device drivers. Because device drivers can be changed to suit a particular purpose, the descriptions in this section do not pertain to every system.

A block device is an I/O (input/output) device that is characterized by

  • Being able to perform random access reads.

  • Having a specific block size.

  • Handling only single blocks of data at a time.

  • Accepting only transactions that involve whole blocks of data.

  • Being able to have a filesystem mounted on it.

  • Having the Linux kernel buffer its input and output.

  • Appearing to the operating system as a series of blocks numbered from 0 through n 1, where n is the number of blocks on the device.

Block devices commonly found on a Linux system include hard disks, floppy diskettes, and CD-ROMs.

A character device is any device that is not a block device. Examples of character devices include printers, terminals, tape drives, and modems.

The device driver for a character device determines how a program reads from and writes to that device. For example, the device driver for a terminal allows a program to read the information you type on the terminal in two ways. First, a program can read single characters from a terminal in raw modethat is, without the driver doing any interpretation of the characters. (This mode has nothing to do with the raw device described in the following section.) Alternatively, a program can read one line at a time. When a program reads one line at a time, the driver handles the erase and kill characters so the program never sees typing mistakes that have been corrected. In this case, the program reads everything from the beginning of a line to the RETURN that ends a line; the number of characters in a line can vary.

Raw Devices

Device driver programs for block devices usually have two entry points so they can be used in two ways: as block devices or as character devices. The character device form of a block device is called a raw device. A raw device is characterized by

  • Direct I/O (no buffering through the Linux kernel).

  • A one-to-one correspondence between system calls and hardware requests.

  • Device-dependent restrictions on I/O.

An example of a utility that uses a raw device is fsck. It is more efficient for fsck to operate on the disk as a raw device, rather than being restricted by the fixed size of blocks in the block device interface. Because it has full knowledge of the underlying filesystem structure, fsck can operate on the raw device using the largest possible units. When a filesystem is mounted, processes normally access the disk through the block device interface, which explains why it is important to allow fsck to modify only an unmounted filesystem. On a mounted filesystem, there is the danger that, while fsck is rearranging the underlying structure through the raw device, another process could change a disk block using the block device, resulting in a corrupted filesystem.




A Practical Guide to Red Hat Linux
A Practical Guide to Red HatВ® LinuxВ®: Fedoraв„ў Core and Red Hat Enterprise Linux (3rd Edition)
ISBN: 0132280272
EAN: 2147483647
Year: 2006
Pages: 383

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net