CS 355 - Systems Programming:
Unix/Linux with C

File Systems

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:

The inode in action

Creating a new file involves four steps:

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.