Linux File System
Presentor: Kho Tran and Phong
Le
© Copyright FPT Software – Level of Confidentiality 1
1 Introduction
2 File Operations
CONTENT
3 File Locking
4 Assignments
© Copyright FPT Software – Level of Confidentiality 2
01. Introduction
© Copyright FPT Software – Level of Confidentiality 3
Introduction
Linux has a philosophy:
Types of File on Linux
Every things in Linux is a
File
• Regular file Normal file such as text file,
executable file.
• Directories file File contain a list of other files.
• Character Device file This is special files
• Block Device file This is special files
• Link files Represent another file.
• Socket file Represent for a Socket.
• Pipe file Represent for a Pipe.
© Copyright FPT Software – Level of Confidentiality 4
Introduction
File Informations
ls
ls -l
ls –a
ls -h
© Copyright FPT Software – Level of Confidentiality 5
02. File Operations
© Copyright FPT Software – Level of Confidentiality 6
Introduction
Kernel provides a basic set of
system calls to perform reading, int open(const char *pathname, int flags, mode_t mode);
writing and manipulate files,
including: ssize_t read(int fd, void *buffer, size_t count);
open()
read() ssize_t write(int fd, void *buffer, size_t count);
write()
off_t lseek(int fd, off_t offset, int whence);
lseek()
close() int close(int fd);
© Copyright FPT Software – Level of Confidentiality 7
Page Cache
1. Kernel determines which page 1. Kernel wirtes content to page
to read cache
2. Kernel reads from page cache 2. The page cache will
periodically be written to
3. If the page is in the page physical memory or using the
cache, the information will be sync(), fsync() system call
read
4. If the page is not in the page
cache. Read from physical
memory to page cache then
read out to user space
© Copyright FPT Software – Level of Confidentiality 8
File Opening
The kernel controls the
interaction between the process
and the file through three tables:
File descriptor table
Open file table
I-node table
© Copyright FPT Software – Level of Confidentiality 9
File Descriptor Table
Belongs to each process (stored in the
Process Control Block).
Each entry is identified by a number called
a file descriptor (fd).
Each entry contains:
Fd Flags: operational modes of the file
description.
File pointer: index of the corresponding entry in
the Open File Table.
Data structure: struct fdtable (defined in
include/linux/fdtable.h).
© Copyright FPT Software – Level of Confidentiality
Open File Table
Shared among all processes in the
system.
Contains all information of files currently
opened.
Each entry contains:
Pointer value: current file offset.
Status flags: set when opening the file.
Access mode: read-only, write-only, read/write,
etc.
Index of the corresponding entry in the I-node
Table.
Data structure: struct file (defined in
include/linux/fs.h).
© Copyright FPT Software – Level of Confidentiality
I-node Table
Contains information about all files in the
filesystem.
Each entry contains:
File type, owner account, permissions, file size.
Number of hard links pointing to the file.
Pointer to the file’s data blocks.
Data structure: struct inode (defined in
include/linux/fs.h).
© Copyright FPT Software – Level of Confidentiality
File Open Process
open()
© Copyright FPT Software – Level of Confidentiality
Step 1: Locate the I-node
The Kernel finds the i-node number corresponding to the file name.
File directory stores the mapping between a file name and its corresponding i-
node number.
© Copyright FPT Software – Level of Confidentiality
Step 2: Update Open File Table
The Kernel adds a new entry to the Open File Table.
Entry values are set according to the parameters passed to open(),
including a pointer to the file’s i-node.
If the same file is opened multiple times (by the same or different
processes):
Multiple entries are created in the Open File Table.
All of them point to the same i-node entry.
The size of the Open File Table is limited → maximum number of files
that can be opened at the same time is constrained.
© Copyright FPT Software – Level of Confidentiality
Step 3: Update File Descriptor Table
The Kernel finds an unused entry in the process’s File Descriptor Table.
This entry is set to point to the new element in the Open File Table.
The return value of open() is the index of this File Descriptor Table entry.
© Copyright FPT Software – Level of Confidentiality
03. File Locking
© Copyright FPT Software – Level of Confidentiality 17
How it work?
File locking is used to manage multiple read/write processes to the same file (synchronization
problem)
How it works
Step 1: Write the lock state to the I-node of the file.
Step 2: If successful then read and write the file, otherwise the file is being used by
another process.
Step 3: After the read/write is done, remove the lock state from the I-node of the file.
© Copyright FPT Software – Level of Confidentiality 18
Flock() and Fcntl
Flock() Fcntl()
Simple Complex
Writing information to I-node is lock state Writing information to I-node is lock state,
lock area, lock execution process
Lock entire file Lock each area of file
Only process can read/write file at the same Multiple processes can read/write file at the
time same time
© Copyright FPT Software – Level of Confidentiality 19
Flock()
int flock(fd, operation);
Process A Process B
flock relies on file descriptor’s information to set lock state to
I-node table.
Parameters: Cann’t
Can set
fd: File descriptor LOCK_SH set
LOCK_SH
operation: LOCK_EX
LOCK_SH: Shared lock. More than one process may
hold a shared lock for a given file at a given time
LOCK_EX: Exclusive lock. Only one process may hold
an exclusive lock for a given file at a given time Cann’t Cann’t
LOCK_UN: Remove an existing lock held by this LOCK_EX set set
process LOCK_EX LOCK_SH
© Copyright FPT Software – Level of Confidentiality 20
Fcntl()
int fcntl(fd, cmd,
&flockstr);
fcntl() is more flexible than flock(). fcntl() allows to lock each part of
file. Even locking each byte.
Parameters:
fd: File descriptor
cmd:
F_SETLK
F_GETLK
flockstr: Lock information (lock state, lock area, lock process)
struct flock {
short l_type; /* Lock type: F_RDLCK, F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret 'l_start': SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Offset where the lock begins */
off_t l_len; /* Number of bytes to lock; 0 means "until EOF" */
pid_t l_pid; /* Process preventing our lock (F_GETLK only) */
}
© Copyright FPT Software – Level of Confidentiality 21
04. Assignments
© Copyright FPT Software – Level of Confidentiality 22
1. Exploring File Types
• Create the following files:
• A regular file: “touch [Link]”
• A directory: “mkdir dir1”
• A symbolic link: “ln -s [Link] link1”
• A named pipe: “mkfifo pipe1”
• Run “ls -l” and identify each file type based on the first character (“-”,
“d”, “l”, “p”, etc.).
23
2. File Permissions
• Create a file “File_Permissions.txt” and write some text inside it.
• Change its permissions so that:
• Owner: read, write, execute
• Group: read only
• Others: no permission
• Verify the changes using “ls -l”.
24
3. Using System Calls in C
• Write a C program that:
• Opens/creates a file “student_name.txt” using “open()”.
• Writes some text to the file using “write()”.
• Moves the file pointer back to the beginning using “lseek()”.
• Reads the content and prints it on the screen using “read()”.
• Closes the file with “close()”.
• Compile and run your program. Show the output.
25
4. Observing System Calls
• Run the command:
• strace -e openat,read,write,close cat /etc/passwd
• Identify at least three system calls made by the “cat” command.
• Explain the role of each system call you observed.
• Run the similar command with touch, cp, echo, less and find the
difference:
• Which commands mainly use write()?
• Which commands mainly use read()?
• Which commands use both read() and write()?
• How does touch differ from the others?
26
5. File Locking with flock()
• Open the same file “[Link]” in two different terminals.
• In the first terminal, run a C program that locks the file using “flock()”
before writing to it.
• In the second terminal, try to write to the same file without locking.
• Observe the difference. Then modify the second program to also use
“flock()” and show how file locking prevents conflicts.
27
6. Advanced File Locking with fcntl()
• Write a C program that:
• Opens a file “[Link]” with “open()”.
• Uses “fcntl()” to set a write lock (F_WRLCK) on the first 50 bytes of the file.
• Writes some text into the locked region.
• Keeps the lock active for 30 seconds (use sleep(30)).
• Releases the lock with F_UNLCK.
• While the program is running, try to run another program or use another terminal
to write into “[Link]”. Observe the behavior.
• Modify your program to use a read lock (F_RDLCK) instead, and demonstrate that
multiple processes can read simultaneously but cannot write to the locked region.
28