The Linux Programming Interface
File Systems
(01)主要内容
The majority of this chapter is concern with file systems, which are organized collections of files and directories.
(02)设备解释,有必要的驱动,统一的接口,供上层调用。
A device special file corresponds to a device on the system. Within the kernel, each device type has a corresponding device driver, which handles all I/O requests for the device.
A device driver is an unit of kernel code that implements a set of operations that (normally) correspond to input and output actions on an associated piece of hardware.
The API provides a consistent interface, hiding the differences in operation of individual devices, allows for universality of I/O.
(03) /dev目录
Device files appear within the file system, just like other files, usually under the /dev directory.
The superuser can create a device file using the mknod command.
(04)设备ID
Each device file has a major ID number and a minor ID number.
The major ID identifies the general class of device, and is used by the kernel to look up the appropriate driver for this type of device.
(05)磁盘和分区
A hard disk drive is a mechanical device consisting of one or more platters that rotate at high speed.
Each disk is divided into one or more partitions.
(06)文件系统所包含的部分
boot block
superblock
I-node table
Data blocks
(07) I-nodes
A file system's i-node table contains one i-node for each file residing in the file system.
使用 ls -li 显示每个文件的i-nodes
(08)虚拟文件系统
The virtual file system is a kernel file system that resolves this problem by creating an abstraction layer for file-system operations.
The VFS interface includes operations corresponding to all of the usual system calls for working with file systems and directories, such as open(), read(), write(), lseek(), close(), truncate(), stat(), mount(), mmap(), link(), unlink(), symlink(), and remove().
(09) mount 命令
wang@wang:~$ mount /dev/sda1 on / type ext4 (rw,errors=remount-ro) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) none on /sys/fs/cgroup type tmpfs (rw) none on /sys/fs/fuse/connections type fusectl (rw) none on /sys/kernel/debug type debugfs (rw) none on /sys/kernel/security type securityfs (rw) udev on /dev type devtmpfs (rw,mode=0755) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620) tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755) none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880) none on /run/shm type tmpfs (rw,nosuid,nodev) none on /run/user type tmpfs (rw,noexec,nosuid,nodev,size=104857600,mode=0755) none on /sys/fs/pstore type pstore (rw) binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev) systemd on /sys/fs/cgroup/systemd type cgroup (rw,noexec,nosuid,nodev,none,name=systemd) gvfsd-fuse on /run/user/1000/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,user=wang) (10) 单文件目录
(11)mount 举例
mkdir /testfs /* create two directories for mount points */ mkdir /demo mount /dev/sda12 /testfs /* mount file system at one mount point */ mount /dev/sda12 /demo /* mount file system at second mount point mount | grep sda12 /dev/sda12 on /testfs type ext3 /dev/sda12 on /demo type ext3 touch /testfs/myfile /* make a change via first mount point */ ls /demo /* view files at second mount point */ lost+found myfile
(12) 虚拟文件 tmpfs
Linux also supports the notion of virtual file systems that reside in memory. To applications ,these look just like any other file system - the same operations (open(), read(), write(), link(), mkdir(), and so on) can be applied to files and directories in such file systems.
(13)获取文件系统信息
The statvfs() and fstatvfs() library functions obtain information about a mounted file system.
#include <sys/statvfs.h>
int statvfs(const char *pathname, struct statvfs *statvfsbuf);
int fstatvfs(int fd, struct statvfs *statvfsbuf);
(14)总结
Devices are represented by entries in the /dev directory. Each device has a corresponding device driver, which implements a standard set of operations, including those corresponding to the open(), read(), write(), and close() system calls. A device may be real, meaning that there is a corresponding hardware device, or virtual, meaning that no hardware device exists, but the kernel nevertheless provides a device driver that implements an API that is the same as a real device.
A hard disk is divided into one or more partitions, each of which may contain a file system. A file system is an organized collection of regular files and directories. Linux implements a wide variety of file systems, including the traditional ext2 file system. The ext2 file system is conceptually similar to early UNIX file systems, consisting of a boot block, a superblock, an i-node table, and a data area containing file data blocks. Each file has entry in the file system's i-node table. This entry contains various information about the file, including its type, size, link count, ownership, permissions, timestamps, and pointers to the file's data blocks.
Linux provides a range of journaling file systems, including Reiserfs, ext3, ext4, XFS, JFS, and Btrfs. A journaling file system records metadata updates to a log file before the actual file updates are performed. This means that in the event of a system crash, the log file can be replayed to quickly restore the file system to a consistent state. The key benifit of journaling file systems is that they avoid the lengthy file-system consistency checks required by conventional UNIX file systems after a system crash.
All file system on a Linux system are mounted under a single directory tree, with the directory / at this root. The location at which a file system is mounted in the directory tree is called its mount point.
A privileged process can mount and unmount a file system using the mount() and unmount() system calls. Information about a mounted file system can be retrieved using statvfs().
(14)习题