Unix File System
S Ramachandram
Associate Professor
Dept of CSE,UCE,OU
File Systems
Abstraction to disk (convenience)
“The only thing friendly about a disk is that
it has persistent storage.”
Devices may be different: tape, IDE/SCSI,
NFS
Users
don’t care about detail
care about interface (won’t cover, assumed
knowledge)
OS
cares about implementation (efficiency)
File System Structures
Files - store the data
Directories - organize files
Partitions - separate collections of
directories (also called “volumes”)
all directory information kept in
partition
mount file system to access
Unix file system
Home directory
Where your files live
You always go there when you log in
Working directory
Whatever directory you are currently
in
4
Typical Unix file system
root (/)
bin dev users tmp usr var
classes csfac st fac staff spool
cs140 ugrad ms phd mail
001 (all users’ mail)
Typical Directories
bin: Software for the shell and most
common Unix commands
dev: Short for devices, holds the
software necessary to operate
peripherals such as printers and
terminals
etc: Contains various administrative
files such as lists of user names and
passwords
Typical Directories
usr/users: Contains the home
directories of users
tmp: A directory used to hold
temporary files
var: Files that contain information
that varies frequently; most
commonly, mail directories
File and Directory Names
Valid names can be made up of:
Uppercase letters (A to Z)
Lower case letters (a to z)
Numbers (0 to 9)
period (.), underscore (_), commas (,)
Should not contain spaces or the
following:
&*\|[]{}$<>()#?‘“/;^!~%
You should also avoid naming files or
directories with Unix commands
Absolute Pathnames
To use a file in Unix, all you need is its
name
To access a file in another directory,
you need to know its pathname
A pathname is an address that shows
the file’s position in the file system
Absolute path names give a files
location in relation to the top of the file
system structure
Example Directory
Structure
root (/)
bin dev etc home tmp usr var
jack jill
Bats marsupials
continents oceans
bandicoot opossum kangaroo wombat
wombat
Absolute Pathnames
All absolute path names begin with
a ‘/’
Also called full path names
Use the ~ as a shortcut for the full
path to a home directory
File and Directory Names
Valid names can be made up of:
Uppercase letters (A to Z)
Lower case letters (a to z)
Numbers (0 to 9)
period (.), underscore (_), commas (,)
Should not contain spaces or the
following:
&*\|[]{}$<>()#?‘“/;^!~%
You should also avoid naming files or
directories with Unix commands
Relative path names
Usually you are interested in a files
location relative to where you
currently are in the system
A relative pathnames start at the
working directory rather than the
root
Use ‘..’ to refer to the parent of the
current directory
Directories
Before reading file, must be
opened
Directory entry provides
information to get blocks
disk location (block, address)
i-node number
Map ascii name to the file
descriptor
Hierarchical Directory
(Unix)
Tree
Entry:
name
inode number (try “ls –I” or “ls –iad .”)
example:
60 /usr/bob/mbox
inode name
Unix Directory Example
Root Directory Block 132 Block 406
1 . 6 .
I-node 6 I-node 26 26 .
1 .. 1 ..
6 ..
4 bin 26 bob
12 grants
7 dev 17 jeff
81 books
14 lib 132 14 sue
406 60 mbox
9 etc 51 sam
17 Linux
6 usr 29 mark
8 tmp
Looking up Aha!
Looking up bob gives I-node 60
usr gives Relevant Data for has contents
I-node 26
I-node 6 data (bob) /usr/bob is of mbox
is in in block 406
block 132
File Attributes
Type – directory, regular file, FIFO, symbolic link,
special.
Reference count – number of hard links {link(),
unlink()}
size in bytes
device id – device files resides on
inode number - one inode per file, inodes are
unique within a disk partition (device id)
ownership - user and group id {chown()}
access modes - Permissions and modes {chmod()}
{read, write execute} for {owner, group or other}
timestamps – three different timestamps: last
access, last modify, last attributes modified.
{utime()}
Permissions and Modes
Three Mode Flags = {suid, sgid and sticky}
suid –
File: if set and executable then set the user’s effective user
id
Directory: Not used
sgid –
File: if set and executable then set the effective group id. If
sgid is set but not executable then mandatory file/record
locking
Directory: if set then new files inherit group of directory
otherwise group or creator.
sticky –
File: if set and executable file then keep copy of program in
swap area.
Directory: if set and directory writable then remove/rename
if EUID = owner of file/directory or if process has write
Permissions
The Permission Bit
9 permission bits used to determine 3 types
of accesses, READ, WRITE, EXECUTE.
Permission can be set based on GROUP,
OWNER, ANYONE ELSE.
Use chmod command to change permission
Binary 001 for EXECUTE
Binary 010 for WRITE
Binary 100 for READ
File System
Implementation
Which blocks with which file?
File descriptor implementations:
Contiguous
Linked List
Linked List with Index
I-nodes File
Descriptor
Disk Layout
tract sector heads
cylinder
Rotational speed platters
disk seek time
S5fs disk layout
bootarea superblock inode list data
Boot area - code to initialize bootstrap the system
Superblock - metadata for filesystem. Size of FS, size
of inode list, number of free blocks/inodes,
free block/inode list
inode list - linear array of 64byte inode structs
s5fs
inode
- some
name
details
8 . Di_mode (2)
45 .. di_nlinks (2)
0 “” di_uid (2)
123 myfile di_gid (2)
di_size (4)
di_addr (39)
di_gen (1)
di_atime (4)
di_mtime (4)
2 byte 14byte di_ctime (4)
directory On-disk inode
I-nodes
single
i-node indirect block Fast for small
attributes
files
Can hold big
Disk blocks
files
double indirect Size?
block
4 kbyte block
triple indirect
block
Locating file data blocks
Assume 1024 Byte Blocks
0
1
2
3
4
ks
5
c
6 blo
7
256
8 k s
l o c
9 b
36
10 - indirect 5 ,5
11 - double indirect 6
12 - triple indirect 16,777,216 blocks
Programmer Interface
System calls
Open()
Read()
Write()
Seek()
Overview
System calls
vnode interface
tmpfs swapfs UFS HSFS PCFS RFS /proc NFS
disk cdrom diskette Process
Anonymous
address
memory
space
Example: Unix open()
int open(char *path, int flags [, int mode])
path is name of file
flags is bitmap to set switch
O_RDONLY, O_WRONLY…
O_CREATE then use mode for perms
success, returns index
Unix open()
int fid = open(“blah”, flags);
read(fid, …);
User Space
System Space
0 stdin
1 stdout
2 stderr ...
3 File
File Structure
... Descriptor
... (where
(index) (attributes) blocks are)
(Per process) (Per device)
File System
Implementation
Process Open File File Descriptor Disk
Control Block Table Table
File sys info
Copy fd File
Open to mem descriptors
File
Pointer Directories
Array
(Device Info) (in memory Data
copy,
one per
device)
User View of Files
File Descriptors (open, dup, dup2, fork)
All I/O is through file descriptors
references the open file object
per process object
file descriptors may be dup’ed {dup(), dup2()},
copied on fork {fork()} or passed to unrelated
process {(see ioctl() or sendmsg(),
recvmsg()}permitting multiple descriptors to
reference one object.
File Object - holds context
created by an open() system call
stores file offset
reference to vnode
vnode - abstract representation of a file
How it works
fd = open(path, oflag, mode); lseek(), read(), write() affect offset
File Descriptors Open File Objects
{{0, uf_ofile} {*f_vnode,f_offset,f_count,...},
{1, uf_ofile} {*f_vnode,f_offset,f_count,...},
{2 , uf_ofile}
{*f_vnode,f_offset,f_count,...},
{3 , uf_ofile} {*f_vnode,f_offset,f_count,...},
{4 , uf_ofile} {*f_vnode,f_offset,f_count,...}}
{5 , uf_ofile}}
Vnode/vfsVnode/vfs
In-memory Vnode/vfsVnode/vfs
In-memory Vnode/vfs
representation In-memory In-memory
representation In-memory
representation
of file of file representation
representation
of file of file
of file