cs399 new beginnings jonathan walpole. file systems

74
CS399 New Beginnings Jonathan Walpole

Upload: julia-mccoy

Post on 17-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS399 New Beginnings Jonathan Walpole. File Systems

CS399 New BeginningsJonathan Walpole

Page 2: CS399 New Beginnings Jonathan Walpole. File Systems

File Systems

Page 3: CS399 New Beginnings Jonathan Walpole. File Systems

Why Do We Need a File System?Must store large amounts of data

Data must survive the termination of the process that created itCalled “persistence”

Multiple processes must be able to access the information concurrently

Page 4: CS399 New Beginnings Jonathan Walpole. File Systems

What Is a File?Files can be structured or unstructured

- Unstructured: just a sequence of bytes- Structured: a sequence or tree of typed records

In Unix-based operating systems a file is an unstructured sequence of bytes

Page 5: CS399 New Beginnings Jonathan Walpole. File Systems

File Structure

asd

Sequenceof bytes

Sequenceof records

Treeof records

Page 6: CS399 New Beginnings Jonathan Walpole. File Systems

File ExtensionsEven though files are just a sequence of bytes, programs can

impose structure on them, by convention- Files with a certain standard structure imposed can be

identified using an extension to their name- Application programs may look for specific file extensions

to indicate the file’s type- But as far as the operating system is concerned its just a

sequence of bytes

Page 7: CS399 New Beginnings Jonathan Walpole. File Systems

Typical File Extensions

Page 8: CS399 New Beginnings Jonathan Walpole. File Systems

Executable FilesExecutable files

- The OS must understand the format of executable files in order to execute programs

- The exec system call needs this- Put program and data in process address space

Page 9: CS399 New Beginnings Jonathan Walpole. File Systems

Executable File Format

An executable file An archive

Page 10: CS399 New Beginnings Jonathan Walpole. File Systems

File AttributesVarious meta-data needs to be associated with files

- Owner- Creation time- Access permissions / protection- Size etc

This meta-data is called the file attributes- Maintained in file system data structures for each file

Page 11: CS399 New Beginnings Jonathan Walpole. File Systems

Example File Attributes

Page 12: CS399 New Beginnings Jonathan Walpole. File Systems

File Access ModelsSequential access

- Read all bytes/records from the beginning- Cannot jump around (but could rewind or back up)

convenient when medium was magnetic tape

Random access- Can read bytes (or records) in any order- Move position (seek), then read

Page 13: CS399 New Beginnings Jonathan Walpole. File Systems

File-Related System CallsCreate a fileDelete a fileOpenCloseRead (n bytes from current position)Write (n bytes to current position)Append (n bytes to end of file)Seek (move to new position)Get attributesSet/modify attributesRename file

Page 14: CS399 New Beginnings Jonathan Walpole. File Systems

File System Call Examplesfd = open (name, mode)byte_count = read (fd, buffer, buffer_size)byte_count = write (fd, buffer, num_bytes)close (fd)

Page 15: CS399 New Beginnings Jonathan Walpole. File Systems

File Storage on Disk

Page 16: CS399 New Beginnings Jonathan Walpole. File Systems

File Storage On DiskSector 0: “Master Boot Record” (MBR)

- Contains the partition mapRest of disk divided into “partitions”

- Partition: sequence of consecutive sectors.Each partition can hold its own file system

- Unix file system- Window file system- Apple file system

Every partition starts with a “boot block”- Contains a small program- This “boot program” reads in an OS from the file system in that

partitionOS Startup

- Bios reads MBR , then reads & execs a boot block

Page 17: CS399 New Beginnings Jonathan Walpole. File Systems

An Example Disk

Unix File System

Page 18: CS399 New Beginnings Jonathan Walpole. File Systems

File Bytes vs Disk SectorsFiles are sequences of bytes

- Granularity of file I/O is bytesDisks are arrays of sectors (512 bytes)

- Granularity of disk I/O is sectors- Files data must be stored in sectors

File systems define a block size- Block size = 2n * sector size- Contiguous sectors are allocated to a block

File systems view the disk as an array of blocks- Must allocate blocks to file- Must manage free space on disk

Page 19: CS399 New Beginnings Jonathan Walpole. File Systems

Contiguous Allocation

After deleting D and F...

Page 20: CS399 New Beginnings Jonathan Walpole. File Systems

Contiguous Allocation

After deleting D and F...

Page 21: CS399 New Beginnings Jonathan Walpole. File Systems

Contiguous AllocationAdvantages:

- Simple to implement (Need only starting sector & length of file)

- Performance is good (for sequential reading)

Page 22: CS399 New Beginnings Jonathan Walpole. File Systems

Contiguous AllocationAdvantages:

- Simple to implement (Need only starting sector & length of file)- Performance is good (for sequential reading)

Disadvantages:- After deletions, disk becomes fragmented- Will need periodic compaction (time-consuming)- Will need to manage free lists- If new file put at end of disk...

- No problem

- If new file is put into a “hole”...- Must know a file’s maximum possible size ... at the time it

is created!

Page 23: CS399 New Beginnings Jonathan Walpole. File Systems

Contiguous AllocationGood for CD-ROMs

- All file sizes are known in advance- Files are never deleted

Page 24: CS399 New Beginnings Jonathan Walpole. File Systems

Linked List Allocation

Each file is a sequence of blocksFirst word in each block contains number of next block

Page 25: CS399 New Beginnings Jonathan Walpole. File Systems

Linked List Allocation

Random access into the file is slow!

Page 26: CS399 New Beginnings Jonathan Walpole. File Systems

File Allocation Table (FAT)Keep a table in memoryOne entry per block on the diskEach entry contains the address of the “next” block

- End of file marker (-1)- A special value (-2) indicates the block is free

Page 27: CS399 New Beginnings Jonathan Walpole. File Systems
Page 28: CS399 New Beginnings Jonathan Walpole. File Systems
Page 29: CS399 New Beginnings Jonathan Walpole. File Systems
Page 30: CS399 New Beginnings Jonathan Walpole. File Systems
Page 31: CS399 New Beginnings Jonathan Walpole. File Systems
Page 32: CS399 New Beginnings Jonathan Walpole. File Systems
Page 33: CS399 New Beginnings Jonathan Walpole. File Systems
Page 34: CS399 New Beginnings Jonathan Walpole. File Systems
Page 35: CS399 New Beginnings Jonathan Walpole. File Systems

File Allocation Table (FAT)Random access...

- Search the linked list (but all in memory

Directory entry needs only one number- Starting block number

Page 36: CS399 New Beginnings Jonathan Walpole. File Systems

File Allocation Table (FAT)Random access...

- Search the linked list (but all in memory)

Directory Entry needs only one number- Starting block number

Disadvantage:- Entire table must be in memory all at once!- A problem for large file systems

Page 37: CS399 New Beginnings Jonathan Walpole. File Systems

File Allocation Table (FAT)Random access...

- Search the linked list (but all in memory)

Directory Entry needs only one number- Starting block number

Disadvantage:- Entire table must be in memory all at once!- Example:

200 GB = disk size1 KB = block size4 bytes = FAT entry size800 MB of memory used to store the FAT

Page 38: CS399 New Beginnings Jonathan Walpole. File Systems

I-nodesEach I-node (“index-node”) is a structure containing info about the file

- Attributes and location of the blocks containing the file

Otherattributes

Blockson disk

I-node

Page 39: CS399 New Beginnings Jonathan Walpole. File Systems

I-nodes

Enough spacefor 10 pointers

Otherattributes

Blockson disk

I-node

Page 40: CS399 New Beginnings Jonathan Walpole. File Systems

I-nodes

Enough spacefor 10 pointers Blocks

on disk

Otherattributes

I-node

Page 41: CS399 New Beginnings Jonathan Walpole. File Systems

The UNIX I-node

Page 42: CS399 New Beginnings Jonathan Walpole. File Systems

The UNIX File System

Page 43: CS399 New Beginnings Jonathan Walpole. File Systems

The UNIX File System

The layout of the disk (for early Unix systems):

Page 44: CS399 New Beginnings Jonathan Walpole. File Systems

Naming FilesHow do we find a file given its name?How can we ensure that file names are unique?

Page 45: CS399 New Beginnings Jonathan Walpole. File Systems

Single Level DirectoriesSometimes called foldersEarly OSs had single-Level Directory Systems

- Sharing amongst users was a problemAppropriate for small, embedded systems

Root Directory

c da b

Page 46: CS399 New Beginnings Jonathan Walpole. File Systems

Two-Level DirectoriesEach user has a directory

/peter/g

Root Directory

harry

ca b

peter

cd e

todd

dg a

micah

eb

Page 47: CS399 New Beginnings Jonathan Walpole. File Systems

Hierarchical DirectoriesA tree of directoriesInterior nodes: DirectoriesLeaves: Files

/

ED

CBA

F

G H

i j

m n

o

k l

p q

Page 48: CS399 New Beginnings Jonathan Walpole. File Systems

A tree of directoriesInterior nodes: DirectoriesLeaves: Files

/

ED

CBA

F

G H

i j

m n

o

k l

p q

User’s Directories

Root Directory

Sub-directories

Hierarchical Directories

Page 49: CS399 New Beginnings Jonathan Walpole. File Systems

Path NamesWindows

\usr\jon\mailboxUnix

/usr/jon/mailbox

Absolute Path Name/usr/jon/mailbox

Relative Path Name- “working directory” (or “current directory”)- Each process has its own working directory

Page 50: CS399 New Beginnings Jonathan Walpole. File Systems

A Unix directory tree. is the “current directory”.. is the parent

Page 51: CS399 New Beginnings Jonathan Walpole. File Systems

Typical Directory OperationsCreate ... a new directoryDelete ... a directoryOpen ... a directory for readingCloseReaddir ... return next entry in the directoryRename ... a directoryLink ... add this directory as a sub directory in

anotherUnlink ... remove a “hard link”

Page 52: CS399 New Beginnings Jonathan Walpole. File Systems

Implementing DirectoriesList of files

- File name- File Attributes

Simple Approach: - Put all attributes in the directory

Page 53: CS399 New Beginnings Jonathan Walpole. File Systems

Implementing DirectoriesSimple approach

“Kernel.h”

“Kernel.c”

“Main.c”

“Proj7.pdf”

“temp”

“os”

attributes

attributes

attributes

attributes

attributes

attributes

•••

•••

Page 54: CS399 New Beginnings Jonathan Walpole. File Systems

Implementing DirectoriesList of files

- File name- File Attributes

Unix Approach:- Directory contains

- File name- I-Node number

- I-Node contains- File Attributes

Page 55: CS399 New Beginnings Jonathan Walpole. File Systems

Implementing DirectoriesUnix approach

“Kernel.h”

“Kernel.c”

“Main.c”

“Proj7.pdf”

“temp”

“os”

i-node

i-node

i-node

i-node

i-node

i-node•••

•••

Page 56: CS399 New Beginnings Jonathan Walpole. File Systems

Implementing FilenamesShort, Fixed Length Names

- MS-DOS/Windows8 + 3 “FILE3.BAK”Each directory entry has 11 bytes for the name

- Unix (original)Max 14 chars

Page 57: CS399 New Beginnings Jonathan Walpole. File Systems

Implementing FilenamesShort, Fixed Length Names

- MS-DOS/Windows8 + 3 “FILE3.BAK”Each directory entry has 11 bytes for the name

- Unix (original)Max 14 chars

Variable Length Names- Unix (today)

Max 255 charsDirectory structure gets more complex

Page 58: CS399 New Beginnings Jonathan Walpole. File Systems

Variable-Length Filenames

Page 59: CS399 New Beginnings Jonathan Walpole. File Systems

Variable-Length Filenames

Page 60: CS399 New Beginnings Jonathan Walpole. File Systems

Sharing FilesOne file appears in several directoriesTree DAG /

ED

CBA

F

G H

i j

m

n o

k l

p q

Page 61: CS399 New Beginnings Jonathan Walpole. File Systems

Sharing FilesOne file appears in several directoriesTree DAG (Directed Acyclic Graph) /

ED

CBA

F

G H

i j

m

n o

k l

p q

Page 62: CS399 New Beginnings Jonathan Walpole. File Systems

Sharing FilesOne file appears in several directoriesTree DAG (Directed Acyclic Graph) /

ED

CBA

F

G H

i j

m

n o

k l

p q

What if the file changes?New disk blocks are used.Better not store this infoin the directories!!!

Page 63: CS399 New Beginnings Jonathan Walpole. File Systems

Hard Links and Symbolic LinksIn Unix:

Hard links- Both directories point to the same i-node

Symbolic links- One directory points to the file’s i-node- Other directory contains the “path”

Page 64: CS399 New Beginnings Jonathan Walpole. File Systems

Hard Links

/

ED

CBA

F

G H

i j

m

n o

k l

p q

Page 65: CS399 New Beginnings Jonathan Walpole. File Systems

Hard LinksAssume i-node number of “n” is 45

/

ED

CBA

F

G H

i j

m

n o

k l

p q

Page 66: CS399 New Beginnings Jonathan Walpole. File Systems

Hard LinksAssume i-node number of “n” is 45

/

ED

CBA

F

G H

i j

m

n o

k l

p q

“m ”

“n ”123

45•••

•••

“n ”“o ”

45

87•••

•••

Directory “D ”

Directory “G ”

Page 67: CS399 New Beginnings Jonathan Walpole. File Systems

Hard LinksAssume i-node number of “n” is 45.

/

ED

CBA

F

G H

i j

m

n o

k l

p q

“m ”

“n ”123

45•••

•••

“n ”“o ”

45

87•••

•••

Directory “D ”

Directory “G ”

The file may have adifferent name in

each directory/B/D/n1/C/F/G/n2

Page 68: CS399 New Beginnings Jonathan Walpole. File Systems

Symbolic LinksAssume i-node number of “n” is 45.

/

ED

CBA

F

G H

i j

m

n o

k l

p q

Hard Link

Symbolic Link

Page 69: CS399 New Beginnings Jonathan Walpole. File Systems

Symbolic LinksAssume i-node number of “n” is 45.

/

ED

CBA

F

G H

i j

m

n o

k l

p q

“m ”

“n ”123

45•••

•••

Directory “D ”

Hard Link

Symbolic Link

Page 70: CS399 New Beginnings Jonathan Walpole. File Systems

Symbolic LinksAssume i-node number of “n” is 45.

/

ED

CBA

F

G H

i j

m

n o

k l

p q

“m ”

“n ”123

45•••

•••

“n ”“o ”

/B/D/n

87•••

•••

Directory “D ”

Directory “G ”

Hard Link

Symbolic Link

Page 71: CS399 New Beginnings Jonathan Walpole. File Systems

Symbolic LinksAssume i-node number of “n” is 45.

/

ED

CBA

F

G H

i j

m

o

k l

p q

“m ”

“n ”123

45•••

•••

“n ”“o ”

91

87•••

•••

Directory “D ”

Directory “G ”

Symbolic Link

n

“/B/D/n”Separate filei-node = 91

Page 72: CS399 New Beginnings Jonathan Walpole. File Systems

Deleting a FileDirectory entry is removed from directoryAll blocks in file are returned to free list

What about sharing???- Multiple links to one file (in Unix)

Hard Links- Put a “reference count” field in each i-node- Counts number of directories that point to the file- When removing file from directory, decrement count- When count goes to zero, reclaim all blocks in the file

Symbolic Link- Remove the real file... (normal file deletion)- Symbolic link becomes “broken”

Page 73: CS399 New Beginnings Jonathan Walpole. File Systems

Example: open,read,closefd = open (filename,mode)

- Traverse directory tree- find i-node- Check permissions- Set up open file table entry and return fd

byte_count = read (fd, buffer, num_bytes)- figure out which block(s) to read- copy data to user buffer- return number of bytes read

close (fd)- reclaim resources

Page 74: CS399 New Beginnings Jonathan Walpole. File Systems

Example: open,write,closebyte_count = write (fd, buffer, num_bytes)

- figure out how many and which block(s) to write- Read them from disk into kernel buffer(s)- copy data from user buffer- send modified blocks back to disk- adjust i-node entries- return number of bytes written