Lec14 Fsapi
Lec14 Fsapi
Operating Systems
CSCI3150 Intro to OS 2
Abstractions
File
A file is data with some properties (name, type, permissions, etc.)
Directory
A file; a list of <user-readable filename, low-level name> pairs
/ root directory
vaild files:
/foo/bar.txt
/bar/foo/bar.txt
foo bar
vaild directories:
bar.t /
bar foo /foo
xt
/bar
bar.t /bar/bar
xt /bar/foo/
CSCI3150 Intro to OS 3
File System Interface: Creating a file
CSCI3150 Intro to OS 4
Aside: A Few Tables for Files in Unix/Linux
CSCI3150 Intro to OS 5
Aside: A Few Tables for Files in Unix/Linux
Terms: Open file table entry, open file description (OFD) (POSIX)
inode table
System-wide, one entry per inode in the fs
CSCI3150 Intro to OS 6
Duplicated File Descriptors (intra-process)
CSCI3150 Intro to OS 7
Duplicated File Descriptors (inter-process)
CSCI3150 Intro to OS 8
Duplicated OFDs
Two processes may have fds referring to distinct OFDs, or open file
table entries, that refer to the same inode
They independently opened the same file
CSCI3150 Intro to OS 9
Interface: Reading and Writing Files
An Example of reading and writing ‘foo’ file.
prompt> echo hello > foo //save the output to the file foo
prompt> cat foo //dump the contents to the screen
hello
prompt>
open(): open file for reading with O_RDOLY and O_LARGEFILE flags.
returns file descriptor 3 ( 0,1,2, is for standard input/output/error)
read(): read bytes from the file.
write(): write buffer to standard output.
CSCI3150 Intro to OS 10
Reading and Writing Files (Cont.)
OFFSET
offset
CSCI3150 Intro to OS 11
Data structures
struct file {
CSCI3150 Intro to OS 12
Sample traces
CSCI3150 Intro to OS 13
fsync()
Persistency
write():write data to the buffer. Later, save it to the storage.
An Example of fsync().
CSCI3150 Intro to OS 14
A Simple File System
CSCI3150 Intro to OS 15
Overview
We will study...
How can we build a simple file system?
CSCI3150 Intro to OS 16
File System Implementation
CSCI3150 Intro to OS 17
Overall Organization
Let’s develop the overall organization of the file system data structure.
0 7 8 15 16 23 24 31
32 39 40 47 48 55 56 63
CSCI3150 Intro to OS 18
Data Region in a FS
D D D D D D D D D D D D D D D D D D D D D D D D
0 7 8 15 16 23 24 31
Data Region
D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D
32 39 40 47 48 55 56 63
The FS has to track which data blocks comprise a file, the size of the
file, its owner, etc.
CSCI3150 Intro to OS 19
inode Table
i d I I I I I D D D D D D D D D D D D D D D D D D D D D D D D
0 7 8 15 16 23 24 31
Data Region
D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D
32 39 40 47 48 55 56 63
CSCI3150 Intro to OS 20
Allocation Structures
i d I I I I I D D D D D D D D D D D D D D D D D D D D D D D D
0 7 8 15 16 23 24 31
Data Region
D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D
32 39 40 47 48 55 56 63
CSCI3150 Intro to OS 21
Super Block
S i d I I I I I D D D D D D D D D D D D D D D D D D D D D D D D
0 7 8 15 16 23 24 31
Data Region
D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D
32 39 40 47 48 55 56 63
Thus, when mounting a file system, OS will read the superblock first,
to initialize various information.
CSCI3150 Intro to OS 22
File Organization: The inode
Add start address of the inode table(12 KB) + inode region(8 KB) = 20 KB
4 5 6 7 20 21 22 23 36 37 38 39 52 53 54 55 68 69 70 71
Super i-bmap d-bmap 8 9 10 11 24 25 26 27 40 41 42 43 56 57 58 59 72 73 74 75
12 13 14 15 28 29 30 31 44 45 46 47 60 61 62 63 76 77 78 79
CSCI3150 Intro to OS 23
inode Structure: Indexed Allocation
Data
direct blocks . Contains the addresses of blocks
. . that contain pointers to the
. . actual data blocks
Data
.
. Data . Data
. .
single indirect . Data . . Data
double indirect .
triple indirect . . Data
.
. Data
Index block, contains
addresses of blocks that
contain data
Pointers to the actual data blocks
CSCI3150 Intro to OS 24
Directory Structure
Record String
length length
CSCI3150 Intro to OS 25
File Read: “/foo/bar”
Assumption: Super node is in memory; everything else is not
data inode root foo bar root foo bar bar bar
bitmap bitmap inode inode inode data data data[0] data[1] data[2]
read
read
open(bar) read
read
read
read
read() read
write
read
read() read
write
read
read() read
write
To find an inode, we need the i-number which usually is in its parent directory
The root has no parent; its i-number must be well-known (2 in most Unix FS)
CSCI3150 Intro to OS 26
File Creation
data inode root foo bar root foo bar bar bar
bitmap bitmap inode inode inode data data data[0] data[1] data[2]
read
read
read
read
create read
(/foo/bar) write
write
read
write
write
read
read
write() write
write
write
...
CSCI3150 Intro to OS 27
File Creation (Cont.)
data inode root foo bar root foo bar bar bar
bitmap bitmap inode inode inode data data data[0] data[1] data[2]
...
read
read
write() write
write
write
read
read
write() write
write
write
CSCI3150 Intro to OS 28
Caching and Buffering
Reading and writing are very IO-intensive
File open: two IOs for each directory component and one read for the data.
FD
CSCI3150 Intro to OS 29
Caching and Buffering
Page Cache
Merge virtual memory and buffer cache
A physical page frame can host either a page in the process address space or a file
block.
Dynamic partitioning
FD
Page cache
CSCI3150 Intro to OS 30
Summary
CSCI3150 Intro to OS 31
How to Make it Faster?
CSCI3150 Intro to OS 32
Problem of the VSFS
A1 A2 B1 B2 C1 C2 D1 D2
CSCI3150 Intro to OS 33
FFS: Disk awareness is the solution
CSCI3150 Intro to OS 34
Cylinder Groups
Accessing one after the other will not be long seeks across the disk.
FFS needs to allocate the files and directories within each of these groups.
S ib db Inodes Data
inode bitmap and data bitmap to track free inode and data block.
CSCI3150 Intro to OS 35
Cylinder Group (Cont.)
Cylinder: Tracks at same distance from center of drive across different surfaces.
All tracks with same color
CSCI3150 Intro to OS 36
How To Allocate Files and Directories?
CSCI3150 Intro to OS 37
Summary
CSCI3150 Intro to OS 38