This Lecture: Physical Reality (Disks) File System Abstraction
This Lecture: Physical Reality (Disks) File System Abstraction
n Reliability/durability Disk
n When system crashes, lose stuff in drivers n System’s view (inside OS):
memory, but want files to be durable n Collection of blocks
n A block is a logical transfer unit, while a sector is the physical
transfer unit. Block size >= sector size.
1
Data structures for a typical file
File operations system
n create
n write Process Open file
n read control table File descriptors
block (systemwide) (Metadata) File system
n reposition within file – file seek info
n delete File
descriptors
n truncate
n open(Fi) – search the directory structure on disk for entry Open Directories
Fi, and move the content of entry to memory. file
..
pointer
n close (Fi) – move the content of entry Fi in memory to array
.
directory structure on disk. File data
2
Contiguous allocation Linked files
n Request in advance for the size of the file n File header points to 1st
n Search bit map or linked list to locate a space block on disk
File header
n File header n A block points to the next
n first sector in file n Pros
n number of sectors n Can grow files dynamically
Free list is similar to a file
Pros
n
n
n No waste of space
Fast sequential access
Cons
n
n ...
n Easy random access
n Sequential access: slow
n Cons n random access: horrible
n External fragmentation n unreliable: losing a block
n Hard to grow files means losing the rest null
3
Multi-level indexed files Combined Scheme (Unix 4.1)
n 13 Pointers in a header
n 10 direct pointers data
n 11: 1-level indirect
n 12: 2-level indirect
n 13: 3-level indirect 1
data
2
Μ
n Pros & Cons ...
n In favor of small files 11 ..
12
. data
n Can grow 13
4
Naming and directories Directory structure
n Options n Approach 1: have a single directory for entire system.
n Use index (ask users specify inode number). Easier for system, not n put directory at known location on disk
as easy for users. n directory contains <name, index> pairs
n Text name (need to map to index) n if one user uses a name, no one else can
many older personal computers work this way.
Icon (need to map to index; or map to name then to index)
n
n
n Each directory is stored as a file, containing a (name, index) pair. n Approach 3: hierarchical name spaces
n Only OS permitted to modify directory n allow directory to map names to files or other dirs
n file system forms a tree (or graph, if links allowed)
n large name spaces tend to be hierarchical (ip addresses, domain
names, scoping in programming languages, etc.)
/
Hierarchical Unix Naming magic
afs bin cdrom dev sbin tmp
n Used since CTSS (1960s) n Bootstrapping: Where do you start looking?
n Unix picked up and used really nicely. awk chmod chown n Root directory
n Directories stored on disk just like regular files n inode #2 on the system
n inode contains special flag bit set n 0 and 1 used for other purposes
n <name, inode#>
users can read just like any other file n Special names:
n only special programs can write <afs, 1021> n Root directory: “/” (bootstrap name system for users)
<tmp, 1020> n Current directory: “.”
n file pointed to by the index may be <bin, 1022> n Parent directory: “..” (otherwise how to go up??)
another directory <cdrom, 4123> n user’s home directory: “~”
n makes FS into hierarchical tree <dev, 1001> n Using the given names, only need two operations to navigate
(what needed to make a DAG?) <sbin, 1011> the entire name space:
... n cd ‘name’: move into (change context to) directory “name”
n Simple. Plus speeding up file ops = speeding up dir ops! n ls : enumerate all names in current directory (context)
5
Creating synonyms: hard and soft Example: basic system calls in
links Unix
n More than one dir entry can refer to a given file What happens when you open and read a file?
n Unix stores count of pointers (“hard links”) to inode
foo bar n open
n to make: “ln foo bar” creates a
synonym (‘bar’) for ‘foo’ n read
ref = 2
Soft links: close
...
n n
n also point to a file (or dir), but object can be deleted from underneath n lseek
it (or never even exist). n create
n Unix builds like directories: normal file holds pointer to name, with
special “sym link” bit set n write
“baz” /bar
n When the file system encounters a symbolic link it automatically
translates it (if possible).
“double indirect” block and find the block’s address (“README”, disk address of inode) pair
Find an empty slot “fd” in the file descriptor table for the process;
n Otherwise, read the “triple indirect” block and find the block’s address }
n
n Read the block from the disk n Put the pointer to the inode in the slot “fd”;
n Copy the bytes in the block to the appropriate location in the buffer n Set the file pointer in the slot “fd” to 0;
Return “fd”;
5. The process calls close(fd); n
6
Example: create-write-close
(cont’d) Protection
3. The process calls write(fd, buffer, length); n Goals:
4. The kernel: n Prevent accidental and maliciously destructive behavior
n From “fd” find the file pointer;
n Ensure fair resource usage
n Based on the file system block size (let’s say 1 KB), find the blocks where
the bytes (file_pointer, file_pointer+length) lies;
n Read the inode
For (each block) {
A key distinction to make: policy vs. mechanism
n
n
n If the block is new, allocate a new disk block;
n Based on the block no, enter the block’s address to the appropriate
n Mechanism: how something is to be done
places in the inode or the indirect blocks; (the indirect blocks are n Policy: what is to be done
allocated as needed)
n Copy the bytes in buffer to the appropriate location in the block }
n Copy (I have the right to give someone else a copy of an access right I
The access control matrix is the “policy” we want to enforce;
have)
Mechanisms: (1) access control lists
n Control (I have the right to revoke someone else’s access rights)
(2) capability lists
n Capability lists (CAP): keep lists of access rights for each object with n ACLs can refer to users or groups
each domain
User A: File1: rw
File2: r n Change permissions on an object by modifying its ACL
…………
7
Revocation
n How does one revoke someone’s access rights to a particular object?
n Easy with ACLs: just remove entry from the list. Takes effects immediately
since the ACL is checked on each object access.
n Harder to do with CAPs since they are not stored with the object being
controlled.
Not so bad in a single machine: could keep all capability lists in a well-
known place (e.g., the OS capability table).
Very hard in distributed system, where remote hosts may have crashed
or may not cooperate.
(Solutions: expiration dates, back pointers to all CAPs that have been
handed out; maintain a revocation list that gets checked on every
access attempt)