File System
A file is a uniquely named collection of related information that is stored on the secondary storage. It acts as a logical storage unit in the computer, defined by the OS.
Two types of entities in a file system:
- Directories - has a structured namespace
- Files
A file consists of metadata and attributes (stored in the inode) such as name, size, datetime of creation, userId, etc. Its content is a group of data bytes
A file can have a file extension which (usually) indicates the type of file that it is.
File Interpreters¶
NTFS, FAT, EXT4
- OS Kernels:
- unix based OS implement directories as special files.
- it knows the difference between directories and regular files\
- supports executable files
- Doesn't understand any other formats
- System programs
- Compilers (.c) , Linkers and Loaders (elf)
- User applications
- VLC (mp3, mp4)
- MS Word (.docx) etc.
File Attrs¶
A file has the following attrs:
- Name
- Identifier: usually a number (stored in inode) that identifies the file within the file system. It is the non-human-readable name for the file
- Type/Format
- Location (pointer)
- Size
- Protection and Access control information
- Time and date of creation/modification, owner
ls -ali
shows the files with their permissions and identifiers
File Interface¶
- create
- read
- write
- delete
- reposition
- truncate (
file.length = 0
)
Creation¶
- free space is available
- An entry for the new file must be made in the directory (OS)
Read/Write¶
open()
(must have perms)read()/write()
on file pointer (byte by bye)close()
File Deletion¶
- free the space occupied by the file
- Remove its entry from the directory
Truncation¶
- We keep the file's attributes, but its lengnth is set to 0
- The space for the file is released
Reposition¶
open()
- move the file pointer to another portion of the file (say byte
N
)lseek()
Complex operations¶
copy
can be performed by create
ing a new file, opening
and read
ing the old file and write
ing to the new file
The File System¶
Maintains and organises a physical secondary storage
A file system controls how data is stored and retrieved in a system.
It is a set of rules (and features) used to determine the way data is stored.
FS logically separates the segments of data on a disk and gives each piece a unique name. Each group of data is the contents of a file, and file attributes may be stored elsewhere. The structural and logical rules used to manage these groups of data are called a "file system".
FSs may vary between OSs, but there are a few that are widely used:
- File Allocation Table (FAT) (Win)
- NTFS (default on Win)
- ext4 (linux)
- Universal Disk Format UDF (DVDs)
- Hierarchical File System (HFS) (macOS)
- Apple File System (AFS) (macOS)
Unix file system data structures¶
- File Descriptor Table: FD table per process -> Contains file descriptors which are numbers that uniquely (per proc) identify an open file in a computer's OS. Each FD is associated with a file pointer that points to the SWOFT.
- System Wide Open File Table (SWOFT): Contains a list of all opened files, sockets, devices (anything that uses
open()
)cp
: current pointer (pointer to the specific byte of the file being read)Access status
: such as read/write/append/execute etc.Open count
: How many FD table entries point to it. We cannot remove open file entries unless their ref count reaches 0Inode pointer
: A pointer to a Unix inode table.
- Inode Table: short for Index Node Table or File Contorl Block (FCB): database of all file attributes and the locations of their contents
- Does not store filename
File System Mapping¶
-
Multiple FD table entries can point to the same swoft
- FD pass to another proc by sockets
- FD inherited from parent after
fork()
- Single process can have two or more FDs that ref the same file.
dup()
ordup2()
. Two FDs created this way affect each other
-
Multiple swoft entries can point to the same inode. This happens when multiple files call
open()
on the same file. Two FDs do not affect each other this way.
Appendix¶
Inode¶
The physical fs¶
logical fs is the interface. this is what happens on bare metal
- Boot Control Block per Volume. Contains the information needed by the system to boot an OS from that volume
- Volume Control Block. contains volume details, such as number of blocks, block size, free block count, free block pointers, free FCB count and FCB pointers.
- Directory Structure. used to organise files
- FCB. one entry per file, contains attrs, has a unique id and associated with a directory entry.
Volume vs Partition:
- A single volume can span multiple disks, a partition is created on a single disk.
- Volumes have names (C:) partitions are only identified by numbers.
- Partitions are more sutited for individual devices, volumes are more suited for network attached storages
Mounting fs during boot¶
root partition which contain OS kernel and sometimes other system files, is mounted at boot time. The system has an in-memory mount table that contains information about each mounted volume. Other volumes can be automatically mounted at boot time or later, depending on the operating system.
The mount procedure:
- The OS is given the name of the device and the mount point - the location w/i the file structure where the fs is to be attached
- Typically, the mount point is an empty directory.
In memory information about the fs is loaded at mount time and there are three data structs related to this:
- Mount Table:
- Inode Table: contains directory structure and file pointers to the actual data on secondary storage or cached memory.
- swoft
- FDT
Searching for a file¶
Since a directory is also a file, it has its own inode number. Its contents are other filenames (can also be dirs, subdirs) and their inode numbers.
Each volume/partition also contains information about its own fs. The volume table of contents is a list of entries in the device directory that have info like filenames, size, etc.
Purpose¶
- Locate: a (group of) files quickly
- Naming:
- same name for different files in different directories
- same name for different extensions
- same file can have different names (links)
- Organisation
Folder¶
A folder is a GUI concept, because of the icons. The term directory is more broad and refers to a general collection of files
Directory operations¶
All of these involve modifying the dir:
create/delete
: create/delete a file or subdilist
rename
: a filesearch
: for a filetraverse
:cd
Directory structure¶
Single level¶
All files in one folder
Two level¶
Separate folder per user
Tree level¶
Unique path to reach file (no links)
File Links¶
Different filenames may point to the same inode entry. These are formally called links
Hard Link¶
Extra names mapped to the same inode entry. Multiple "files" can point to the same content. Each hard link increases the ref count of the file in the inode entry.
.
and ..
are hard links!
In unix, directories cannot be hard linked, this is done to keep the directory structure acyclic. Symbolic links should be used instead
Symlinks¶
Symbolic Links are simply files whose content is a text string (reference to another file/dir) which is automatically interpreted and followed by the OS as a path to another file/dir.
Symlinks aka soft links. dirs can be linked with symlinks.
Symlinks do not increase the ref count. Will break if the original file that it is pointing to is deleted (although this is not a consequence of ref count not being ++
). The link will work again if the deleted file is recreated
Graph Directory Structure¶
Acyclic¶
multiple paths can reach the same file (links)
General¶
Self referencing can cause infinite loops. Reference counting does not work if there are self references. Performance issues with searching and traversal