Reference: Molay, Understanding Unix/Linux Programming, Chapter 4
A user's view of the file system
A directory structure appears as a tree. Each directory can contain files and/or other directories.
Linux commands to manipulate directories: mkdir, rmdir, cd, pwd, ls.
Linux commands to manipulate files: cp, rm, chmod.
Internal structure of the Linux file system
A disk is a stack of magnetic platters. Several levels of abstraction convert the stack of platters into the file system accessible to the user:
- Platters to partitions. Each partition is treated as a separate disk.
- Partitions to an array of blocks. Surface of each platter is organized into tracks (concentric circles). Each track is divided into sectors. Each sector stores a fixed number of bytes (e.g. 512). Each sector (a.k.a. disk block) is numbered sequentially and can be viewed as an addressable array.
- An array of blocks to three regions, the data area, the inode table, and the superblock. This three-part structure imposed on a numbered sequence of blocks is called a file system.
- The superblock: contains information about the organization of the file system itself.
- The inode table: inode is a structure with the data about each file (size, owner UID, last modification time, etc.); each inode is identified by its position in the inode table.
- The data area: contains the actual file data.
Creating a new file involves four steps:
- Store properties: kernel allocates a free inode and uses it to record the file metadata.
- Store data: the kernel allocates as many free disk blocks as needed to store all file data and copies the file data into these blocks.
- Record allocation: the kernel records the numbers of the blocks in the disk allocation section of the inode.
- Add filename to directory: the kernel adds a corresponding entry (inode_number, file_name) into the directory.
ls -i lists inode numbers of all files. stat shows much of the information stored in the inode:
$stat -x myfile
File: "myfile"
Size: 1040
FileType: Regular File
Mode: (0644/-rw-r--r--)
Uid: (1858866695/stan)
Gid: (1693310884/CCSU_COMP_SRV\Domain Users)
Device: 1,2 Inode: 4022843 Links: 1
Access: Sat Oct 11 20:51:54 2014
Modify: Mon Sep 29 11:55:04 2014
Change: Sat Oct 11 20:51:54 2014
inode's disk allocation section contains a enough space for 13 block numbers. The first 10 entries contain regular block numbers containing the file data. Blocks 11, 12, and 13 are indirect blocks, as shown below:
System calls for directory trees
- int mkdir(char *dname, mode_t mode);
- Creates a directory called dname with the mask of permission bits specified in mode. Returns 0 if success, or -1 in case of an error.
- int rmdir(char *dname);
- Removes a directory node called dname. Returns 0 if success, or -1 in case of an error.
- int unlink(char *path);
- Deletes a directory entry located at path from the directory structure; decrements the link count; if the link count for the in ode becomes 0, frees the data blocks and the inode.
- int link(char *orig_link, char *new_link);
- Creates a new link to an inode.
- int rename(char *from, char *to);
- Changes the name or location of a file or directory.
- int chdir(char *path);
- Changes the current directory of the running process.