File System API
File System
- refers to a collection of files and the code in the operating system that manages the files
File
- an array of persistence bytes
Types of Names
- UID (Unique ID); called inode numbers in Unix/Linux
- Each file has its own inode number
- Files deleted will have their inode number recycled
- Only has to be unique for a given file system
- Can see inode numbers by running:
ls -i
- 1% of storage is inodes
- Path Name
- File Descriptor
inode Structure
- location: pointer to where the file is on disk
- holds a set of meta-data for the file
- Implementing inodes with
read(int inode, void* buf)
is hard to keep track of since an integer can easily be changed, misplaced, or just wrong.
Paths
- Instead of one level we can build a tree. So only the string name within a given directory has to be unique
- Directories are actually files with a list of file names mapping to inode numbers
mkdir
: gets a new inode and creates an empty file for the directory
- the filesystem can tell if it's a directory by the
d
indrwxr-xr-x
.
File Descriptor (FD)
- the inode only has to be fetched once leading to less directory traversals
- can track the offset in the file
Deleting Files
- There is no system call for deleting files
- When there are no references to a file the filesystem will recycle the inode and garbage collect the data blocks used to store the file
- Linux is limited to 16 file descriptors at a time
- If a file has an open file descriptor and the directory for the file is deleted the file block will not be deleted
Links
Hard Link ln
- Another path name we can use to access the file
- Each file is associated with the same inode number
- The system has to keep track of the hardlinks so the system knows when to garbage collect
- No hard links to directory (this would create a cycle)
- This is basically a (string, inode) pair in a directory file
Soft Link (Symbolic Link) ln -s
- This is essentially a file (it has it's own inode) that contains a path to another file
- If the file it points to is deleted, accessing the soft link will show an error
mount
- Attaches the file system we are mounting to the pre-exosting file system
- File systems will have unique inode numbers in themselves
- Command:
mount {device} {dir}
mount the device to the directory.
fsync
mv
: rename a file
- delete's one link to a file and creates a new link to a file
- This command is atomic so that a crash in the middle would either give you either:
- the old state of the file
- the new state of the file
Atomic File Update
- Make a temporary file
- Rename the temporary file