Week 7
File Systems
IN 1011 Operating Systems
File Systems
• File Concept
• File Operations
• Disk and Directory Structure
• File-System Mounting
• File Sharing
• Protection
File Systems
• File Concept
• File Operations
• Disk and Directory Structure
• File-System Mounting
• File Sharing
• Protection
These are not file systems!
• File System refers to the logical structure (i.e.
software) and not to the hardware in which the
data is stored.
File Systems
• A file system allows data to be formatted, stored
(e.g. on a hard disk) and accessed in a disciplined
and convenient manner
• The users of a file system do not need to
understand how the underlying physical storage
media work
• The same file system can be implemented on
many different devices (even in RAM)
File Attributes 1
• Files contain data but also have other attributes:
– Name – only information kept in human-readable
form, this is what users see
– Identifier – unique tag (number) which identifies
each file within a file system (for Unix these are
called i-numbers)
Student number
0100102323
File Attributes 2
• In Unix:
– file names are only stored in directories, they are not
attributes of files themselves
– a directory contains a mapping from names to i-numbers
– more than one name can be mapped to the same i-
number
Student number
0100102323
File Attributes 3
• Files contain data but also have other attributes:
– Type – (in systems that support different file types)
– Location – pointer to file location on device (e.g. disk)
– Size – current file size
– Protection – controls who can read, write, execute
– Time, date, and user identification – data for
ownership, protection, security, and auditing
Examples of File Info
Windows, Unix, Mac OS X
Examples of File Info
Windows, Unix, Mac OS X
File Systems
• File Concept
• File Operations
• Disk and Directory Structure
• File-System Mounting
• File Sharing
• Protection
File Operations
• File is an abstract data type (i.e. a group of
objects defined by its behaviour, the behaviour is
defined by a values and operations)
Create Read Open
Delete Write Close
File Operations 2
• Further operations
– Reposition (a pointer) within file
• Write – at write pointer location
• Read – at read pointer location
– Truncate
– Append
– Get attribute
– Set attribute
– Rename
Open Files
• Several pieces of data are needed to manage
open files:
– Open-file table: tracks open files
– File pointer: pointer to last read/write location, per
process that has the file open
– File-open count: counter of number of times a file is
open – to allow removal of data from open-file table
when last processes closes it
– Disk location of the file: e.g. block on disk
– Access rights: per-process access mode information
Open File Locking
• Provided by some operating systems and
file systems
• Mediates access to a file
• Mandatory or advisory:
– Mandatory – access is denied depending on
locks held and requested
– Advisory – processes can find status of locks
and decide what to do
File Types
• An OS may distinguish between a range of different
types
– e.g. text, binary, sound, video
– in such a typed system, copy binary to text would not
be permitted (cf int x = true)
• Or (more common) all files are just a sequence of bytes
–OS and user applications
may use file name extensions
as hints about data formats
File Types – Name,
Extension
Access Methods
• Sequential Access
– read next
– write next
– reset
• Direct Access – file is fixed length logical records
– read n
– write n
– position to n
– read next
– write next
Sequential-access File
Simulation of Sequential Access
on Direct-access File
File Open and Read in C
#include <stdlib.h>
#define BUFSIZE 1024 Standard library for I/O
#define O_RDONLY 00
int main () Buffer Size
{
char buffer [BUFSIZE]; Predefined Read Only
int length;
int fd; Variables char, int
if ( ( fd = open ("[Link]", O_RDONLY) ) < 0 ) {
perror("Unable to open data");
exit (1);
}
while ( (length = read (fd, buffer, BUFSIZE)) > 0 ) {
write (1, buffer, length);
}
close (fd);
exit (0);
}
File Open and Read in C
#include <stdlib.h>
#define BUFSIZE 1024 Syscall open returns a
#define O_RDONLY 00 file descriptor (positive int)
int main ()
{ Name of the file
char buffer [BUFSIZE];
int length; Predefined Read Only
int fd;
if ( ( fd = open ("[Link]", O_RDONLY) ) < 0 ) {
perror("Unable to open data"); Is this sequential or
exit (1);
direct access?
}
while ( (length = read (fd, buffer, BUFSIZE)) > 0 ) {
write (1, buffer, length);
}
file descriptor 1 is the
close (fd);
exit (0);
“standard output”
}
Extract from Alice in
Wonderland
• [Link]
Run the file
gcc –o readFile readF.c
./readFile
Change the Buffer
Change the Buffer
File Open and Read in Java
File Systems
• File Concept
• File Operations
• Disk and Directory Structure
• File-System Mounting
• File Sharing
• Protection
Operations Performed
on Directory
• Search for a file
• Create a file
• Delete a file
• List a directory
• Rename a file
• Traverse the file system
Disk Structure
• Disk can be subdivided into partitions
• Disk or partition can be used raw –
without a file system, or formatted with
a file system
– Different partitions on the same disk may
be formatted as different types of file
system
– Linux supports many: ext2, ext3, ext4,
ReiserFS, ...
Disk Structure
• Each partition containing a file system will store info
relating to that file system in a device directory or a
volume table of contents
A Typical File-system
Organization
RAID
• Disks or partitions can be RAID protected
against failure
• Redundant Array of Inexpensive
(Independent) Disks
– Use multiple smaller disks (c.f. one large disk)
– Parallelism improves performance
– Plus extra disk(s) for redundant data storage
• Provides fault tolerant storage system
– Especially if failed disks can be “hot swapped”
RAID 1
• RAID 1 Mirroring: N + N disks, replicate data
• Write data to both data disk and mirror disk
• On disk failure, read from mirror
Disk 1 Data Disk 2 Mirror
RAID 5: Distributed
Parity
• N + 1 disks
– Data striped across N disks at block level
– Redundant disk stores parity for a group of blocks,
but parity blocks distributed across disks
Directory Organization
• The directory is organized logically to
obtain
– Efficiency – locating a file quickly
– Naming – convenient to users
• Two users can have same name for different files
• The same file can have several different names
– Grouping – logical grouping of files by
properties, (e.g., all Java programs, all games, …)
Directory Structure
• A collection of nodes containing information
about all files For Unix these
will be i-numbers”
Directory
(Not a folder)
Files
F1 F2 F4
F3
Fn
Both the directory structure and the files reside on disk
Additional File-system Structure
• Different file systems provide different ways of
structuring file access to make life easier for users
and applications
• From simple to sophisticated:
– Single-level folder structure
– Two-level folder structure
– Tree-structured
– Graph-structured
Single-Level Directory
• A single directory for all users
• Naming problem
• Grouping problem
Two-Level Directory
• Separate directory for each user
Path name
Can have the same file name for different user
Efficient searching
No grouping capability
Tree-Structured
Directories
Tree-Structured
Directories
Tree-Structured
Directories (Cont)
• Absolute or relative path name
– Unix absolute path starts with /
– Windows absolute path starts C:, F:
/
• Example
– mkdir /home/troy/fruit home
– cd /home/troy/fruit troy
– touch apples
fruit veg
– touch pears
– mkdir ../veg apples pears
Tree Structure
• Assuming user troy has /
a clean file system home
troy@bigears:~$ mkdir food troy
troy@bigears:~$ mkdir clothes
food clothes
troy@bigears:~$ cd food coat
troy@bigears:~/food$ touch ../clothes/coat
Tree Structure
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes /
troy@bigears:~$ cd clothes
troy@bigears:~/food$ touch ../food/coat home
troy
food clothes
coat
Tree Structure
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes /
troy@bigears:~$ cd clothes
troy@bigears:~/food$ touch ../food/coat home
troy
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes food clothes
troy@bigears:~$ cd food
troy@bigears:~/food :~$ touch coat coat
Tree Structure
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes /
troy@bigears:~$ cd clothes
troy@bigears:~/food$ touch ../food/coat home
troy
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes food clothes coat
troy@bigears:~$ cd food
troy@bigears:~/food :~$ touch coat
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes
troy@bigears:~$ cd food
troy@bigears:~/food$ touch ../coat
Tree Structure
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes /
troy@bigears:~$ cd clothes
troy@bigears:~/food$ touch ../food/coat home
troy
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes food clothes
troy@bigears:~$ cd food
troy@bigears:~/food :~$ touch coat coat
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes
troy@bigears:~$ cd food
troy@bigears:~/food$ touch ../coat
troy@bigears:~$ mkdir food
troy@bigears:~$ mkdir clothes
troy@bigears:~$ cd food
troy@bigears:~/food$ mkdir coat
Tree-Structured
Directories (Cont.)
• Efficient searching
• Grouping Capability
• Current directory (working directory)
– $ nedit /home/troy/[Link]
• OR
– $ cd /home/troy
– $ nedit [Link]
Acyclic-Graph Directories
• Have shared subdirectories and files
Acyclic-Graph Directories
• Two different names (aliasing)
• If dict deletes count ⇒ dangling pointer
• Various more or less complicated solutions:
• Unix: only delete the file (inode) when last ref
(name) is removed
• File system must keep track of number of references
Acyclic-Graph Directories
• Two different names (aliasing)
• If dict deletes count ⇒ dangling pointer
• New directory entry type: symbolic link
• really just another file containing a string which is a
path (pointer) to an existing file
• Resolve the link – follow pointer to locate the file
• Deleting the link has no effect on the file or vice
versa
General Graph
Directory
General Graph
Directory (Cont.)
• How do we guarantee no cycles?
– Allow only links to file not subdirectories
– Every time a new link is added use a cycle detection
algorithm to determine whether it is OK
File Systems
• File Concept
• File Operations
• Disk and Directory Structure
• File-System Mounting
• File Sharing
• Protection
File System Mounting
• A file system must be mounted
before it can be accessed
• Mounting makes the file system
available at a mount point within
a previously mounted file system
• Need some special mechanism
to create the root file system
File System Mounting
File System Mounting
• Illustration of mounted and unmounted file systems
/dev/sdb1 root
/mnt
An unmounted system A mounted system
File System Mounting
File System Mounting
• Illustration of mounted and unmounted file systems
/dev/sdb1
/mnt
File Systems
• File Concept
• File Operations
• Disk and Directory Structure
• File-System Mounting
• File Sharing
• Protection
File Sharing
• Sharing of files on multi-user systems is desirable
• Sharing may be done through a protection
scheme
• On distributed systems, files may be shared
across a network
• Network File System (NFS) is a common
distributed file-sharing method
File Sharing
• If multi-user system
– User IDs identify users, allowing permissions and
protections to be per-user
Group IDs allow users to be in groups, permitting
group access rights
– Owner of a file / directory
– Group of a file / directory
File Sharing – Remote
File Systems
• Uses networking to allow file system access
between systems
– Manually via programs like FTP
– Automatically, seamlessly using distributed file
systems
– Semi automatically via the world wide web
File Systems
• File Concept
• File Operations
• Disk and Directory Structure
• File-System Mounting
• File Sharing
• Protection
Protection
• Operating system consists of a collection
of ‘objects’: hardware or software
• Each object has a unique name and can
be accessed through a well-defined set
of operations
• The Protection problem:
– Ensure that each object is accessed correctly
and only by those processes that are
allowed to do so.
Protection
• File owner/creator should be able to control:
– what can be done
– by whom
• Types of access
– Read
– Write
– Execute
– List and access children in the directories
– Delete
Access Lists and Groups
• Three mode of access: read (r), write (w), execute (x)
0 - – - means no permissions for anything 0 0 0
1 - – x means only execute 0 0 1
2 - w – means only write 0 1 0
3 - w x means write and execute 0 1 1
4 r – - means only read 1 0 0
5 r – x means read and execute 1 0 1
6 r w - means read and write 1 1 0
7 r w x means read, write and execute 1 1 1
• Notice the order in which these are listed
Access Lists and Groups
• Three classes of user: owner (u), group (g), other (o)
• There are permission for each of these users in the same
order
r w - r – x - w x
u g o
6 5 3
Access Lists and Groups
• Example
– a) owner access 7 ⇒ 1 1 1
– b) group access 6 ⇒ 1 1 0
– c) public access 1 ⇒ 0 0 1
• Or
– 111 110 001
Windows / Mac Access-Control
List Management
A Sample UNIX
Directory Listing
user group
file
folder
A Sample UNIX
Directory Listing
In this example the group
User/ group names and user names are the
folder
owner same, but a group is not a user
(Linux has created a default
group for each user).
file
A Sample UNIX
Directory Listing
file1 file2 file3 file4 dir
read read
troy read read read write write
execute execute
zapata read
execute
write read read read
read
helen read
execute
read read read
Unix: directories and the x bit
• Think of a directory as a special file containing a
list of file names
– You need r access to list the contents
– You need w access to change the contents (add a file,
rename a file or delete a file from the directory)
• The “execute” bit for directories is special:
– You need x access to “traverse” a directory in order
to access the files within it
– If you have x but not r you can still use the files
within if you know their names (ls can't help you)
Access Lists and Groups
• Users can change permissions for a particular file (say
game) or subdirectory, define an appropriate access.
$chmod 761 game
• A manager or root user can create and modify the owner
and group of a file, also add users to the group.
– Change/attach group:
$root $ chgrp G game
– Change owner and group of a file
$root $ chown user1:group2 game
Access Matrix:
Mechanism vs Policy
• Access matrix design separates mechanism from
policy
– Mechanism
– OS provides matrix structure + rules
– OS ensures that the matrix is only manipulated by
authorized agents and strictly enforces rules
• Policy
– Users dictate policy by setting entries in the matrix to
specify who can access what
File Systems
• File Concept
• File Operations
• Disk and Directory Structure
• File-System Mounting
• File Sharing
• Protection