Lecture Part3 4 Fs
Lecture Part3 4 Fs
ECE_3TC31_TP/INF107
Stefano Zacchiroli
2024
File-System Interface
Computers have varied forms of persistent, secondary storage: NVM, HDD, tapes, etc.
OSes provide a unified view over secondary storage, generally based on the notion of file
File
A file is a named collection of related information that is recorded on secondary storage
From a user’s perspective, a file is the smallest allotment of logical secondary storage
• That is, data cannot be written to secondary storage unless they are within a file
In addition to their content, files are associated to several files attributes, such as:
• Name – only information kept in human-readable form
• Identifier – unique tag (number) identifies file within file system
• Type – needed for systems that support different file types
• Location (physical) – pointer to file location on device
• Size – current file size
• Protection – controls who can do reading, writing, executing
• Time, date, and user identification – data for protection, security, and usage monitoring
Stored in the File Control Block (FCB) of each file
Many variations across OSes, including extended file attributes such as file checksum
In addition to their content, files are associated to several files attributes, such as:
• Name – only information kept in human-readable form
• Identifier – unique tag (number) identifies file within file system
• Type – needed for systems that support different file types
• Location (physical) – pointer to file location on device
• Size – current file size
• Protection – controls who can do reading, writing, executing
• Time, date, and user identification – data for protection, security, and usage monitoring
Stored in the File Control Block (FCB) of each file
Many variations across OSes, including extended file attributes such as file checksum
$ stat Makefile # Example using the UNIX stat command on the Makefile used to build these slides
File: Makefile
Size: 758 Blocks: 8 IO Block: 4096 regular file
Device: 254,1 Inode: 1582010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ zack) Gid: ( 1000/ zack)
Access: 2023-09-10 16:25:33.265416874 +0200
Modify: 2023-09-07 09:48:55.986798677 +0200
Change: 2023-09-07 09:48:55.986798677 +0200
Birth: 2023-09-07 09:48:55.986798677 +0200
4/1 2024 ECE_3TC31_TP/INF107 Operating Systems — File System
File Operation
We can see a file as an abstract data type (ADT), fully determined by the operations it supports
Common file operations that OS provides—usually as a set of corresponding system calls—are:
1. Create
2. Write — at write pointer location
3. Read — at read pointer location (can be the same as write pointer location)
4. Reposition within file — or seek
5. Delete
6. Truncate
The OS keeps track of open files in the system using several pieces of data
Two levels of open file tables
• Per-process open file table: one entry for each file opened by a process
– Contain process-specific file information, e.g., current file pointer for read/write operations
– Each entry also points to the relevant entry in the system-wide table of open files
• System-wide open file table: one entry for each open file in the entire system
– Contain process-independent file information, e.g., file physical location on mass storage, access rights
– Keeps a counter (open count) of processes having opened a file, for garbage collection of the entry when
it reaches 0
Based on the sequential access method, with one syscall per operation
Opening a file and obtaining an integer file descriptor as handle for future operations:
int open(const char *pathname, int flags, ... /* mode_t mode */);
// Returns file descriptor on success, or –1 on error
• read is very different from fread. read is a system call, which invokes a OS service; fread is a
library function (from the C standard library) which executes user code and eventually calls read
itself.
• fread entails a double copy, i.e., memory is copied twice: (1) from kernel buffer to stdlib buffer, (2)
from stdlib buffer to user process buffer.
read and write read/write starting from the position of a shared file pointer.1 Then, they
advance the pointer by the number of bytes read/written.
1
not to be confused with a C language pointer; a file pointer is just the current position in an open file
10/1 2024 ECE_3TC31_TP/INF107 Operating Systems — File System
Sequential Access on UNIX — Example
1 /* Based on copy.c from "The Linux Programming Interface" book, adapted for teaching purposes.
2 Copyright (C) Michael Kerrisk, 2010. License: GNU AGPL, version 3 or above.
3
4 Copy the file named argv[1] to a new file named in argv[2].
5 */
6
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12
13 #define BUF_SIZE 1024
14 #define errExit(msg) { fprintf(stderr, "%s", (msg)); exit(EXIT_FAILURE); }
15
16 int main(int argc, char *argv[]) {
17 int inputFd, outputFd;
18 ssize_t numRead;
19 char buf[BUF_SIZE];
20
21 if (argc != 3) errExit("Usage: copy OLD_FILE NEW_FILE");
Several different logical organizations for the directory structure are possible
• Single-level directory: all FS files in a single directory(!) — simple, but too prone to filename clashes
• Two-level directories — e.g., one home directory per user, no further nesting
Most used directory organizations are hierarchical, either trees or DAGs
• Paths are hierarchical file names used to navigate the hierarchical structure
Tree-structured directories Direct Acyclic Graph (DAG) directories
Full graph directories are possible, but avoided due to garbage collection complexity
14/1 2024 ECE_3TC31_TP/INF107 Operating Systems — File System
DAG Organization — UNIX example
Symbolic link (symlinks): files containing paths, followed transparently and by default by the FS
$ echo foo > foo.txt
• Symbolic links allow to create cycles (e.g., /home/zack/my_dir -> /), but the fact they have a
dedicated file type allows to skip them when performing FS traversals or removing files
Hard links (not a file type!) allow two separate directory entries to point to the same file (they are
forbidden for directories)
$ echo foo > foo.txt
$ ln foo.txt bar.txt # add bar.txt as a new name for foo.txt in current dir
$ ln foo.txt baz.txt # and another one! (baz.txt)
$ ls -l
-rw-r--r-- 3 zack zack 4 set 12 11:52 bar.txt
-rw-r--r-- 3 zack zack 4 set 12 11:52 baz.txt
-rw-r--r-- 3 zack zack 4 set 12 11:52 foo.txt
• All three directory entries point to the same FCB on the FS; none of them is the “original” file vs links
to it, as it happens with symlinks
Hard links (not a file type!) allow two separate directory entries to point to the same file (they are
forbidden for directories)
$ echo foo > foo.txt
$ ln foo.txt bar.txt # add bar.txt as a new name for foo.txt in current dir
$ ln foo.txt baz.txt # and another one! (baz.txt)
$ ls -l
-rw-r--r-- 3 zack zack 4 set 12 11:52 bar.txt
-rw-r--r-- 3 zack zack 4 set 12 11:52 baz.txt
-rw-r--r-- 3 zack zack 4 set 12 11:52 foo.txt
• All three directory entries point to the same FCB on the FS; none of them is the “original” file vs links
to it, as it happens with symlinks
Q: when can we free the disk space used by a file in a DAG FS?
A: when the number of (hard) links to it reaches 0
• In the example above, 3 denotes the number of inbound hard links to the file
• This is why the UNIX syscall to “remove” files is called unlink()
• (It is also why garbage collection is complicated in general graph FS.)
16/1 2024 ECE_3TC31_TP/INF107 Operating Systems — File System
File Protection
In a multi-user system, the OS needs to ensure that only valid accesses to files are permitted
• Users should be able to decide if/how/which other users can access their files
For each different type of file and directory access—read, write, execute, create, delete, etc.—the
OS will verify if it is permitted and, if not, terminate the syscall with an error (e.g., EPERM)
Most general mechanism: Access Control List (ACL)
• Each action is performed by a user via a syscall invocation
• Each FS object associated to owner user + list of ⟨user, action⟩ pairs of permitted actions & to whom
• Each action verified against the ACL; fail if there is no match
Problem: ACL length → FCB can no longer be fixed-size, making FS implementation complicated
• Common approach: group together actions and/or users in the ACL
• E.g., group together all actions performed by:
1. file owner,
2. users member of the same user group,
3. anyone else (“others”)
3 permission sets: file owner (“user”), members of the same group, anyone else (“others”)
3 permission bits for each set
• Read — ability to read the content of the object (data for files, list content for directories)
• Write — ability to change the content (note: directories need this to create/remove files)
• Execute
– For files: ability to execute the file as a program
– For directories: ability to resolve paths that contain the directory anywhere
Syscalls and commands for UNIX permission manipulation: chmod, chown, chgrp
• UNIX also support optional, variable-size extended ACLs; manipulated with: getfacl, setfacl
$ touch backup_password.txt # create empty file (set owner user implicity)
$ chgrp adm backup_password.txt # set its group to "adm" (user must be a member)
$ chmod u=rw,g=r,o= backup_password.txt # r/w for owner, read-only for admins, nothing for others
$ echo s3cr3t >> backup_password.txt # write password to file (quiz: why not before?)
$ ls -l backup_password.txt
-rw-r----- 1 zack adm 7 set 12 13:45 backup_password.txt
On disk In memory
Physical disks organized in partitions, Mount table listing all FS currently active
containing logical volumes, where filesystems (“mounted”) in the system
reside System-wide open-file table
Each volume contains Per-process open-file table
• (Optional) Boot control block with info Lots of caches!
needed to boot OS • E.g., copy of the FCB of each open file, copy
• Volume control block (called superblock on of the directory content of recently used
UNIX): total n. of blocks, n. of free blocks, directories, I/O buffers, etc.
pointers to free blocks, block size
• Directory structure
File Control Block (already discussed) for
each file
Context: secondary storage disks are accessible (for read/write) at the granularity of fixed-size
blocks (e.g., 512 bytes to 10 KiB), not individual bytes
An allocation method refers to how disk blocks are allocated to files
Three major allocation methods:
• Contiguous allocation
• Linked allocation
• Indexed allocation
struct stat {
dev_t st_dev; /* IDs of device on which file resides */
ino_t st_ino; /* I-node number of file */
mode_t st_mode; /* File type and permissions */
nlink_t st_nlink; /* Number of (hard) links to file */
uid_t st_uid; /* User ID of file owner */
gid_t st_gid; /* Group ID of file owner */
dev_t st_rdev; /* IDs for device special files */
off_t st_size; /* Total file size (bytes) */
blksize_t st_blksize; /* Optimal block size for I/O (bytes) */
blkcnt_t st_blocks; /* Number of (512B) blocks allocated */
time_t st_atime; /* Time of last file access */
time_t st_mtime; /* Time of last file modification */
time_t st_ctime; /* Time of last status change */
};
31/1 2024 ECE_3TC31_TP/INF107 Operating Systems — File System
A Closer Look to I-nodes (cont.)
Periodically use system programs to backup FS data to another storage device (magnetic tape,
other magnetic disk, optical)
• Recover lost file or disk by restoring data from backup
You should study on books, not slides! Reading material for this lecture is:
Credits:
Some of the material in these slides is reused (with modifications) from the official slides of the
book Operating System Concepts, Tenth Edition, as permitted by their copyright note.
How do you find a free block to allocate to a given file when needed?