CS 134: Operating Systems File System Implementation 1 / 34 - - PowerPoint PPT Presentation

cs 134 operating systems
SMART_READER_LITE
LIVE PREVIEW

CS 134: Operating Systems File System Implementation 1 / 34 - - PowerPoint PPT Presentation

CS34 2013-05-17 CS 134: Operating Systems File System Implementation CS 134: Operating Systems File System Implementation 1 / 34 Overview CS34 Overview 2013-05-17 Implementation Issues Caching Failures Disk Scheduling Overview API


slide-1
SLIDE 1

CS 134: Operating Systems

File System Implementation

1 / 34

CS 134: Operating Systems

File System Implementation

2013-05-17

CS34

slide-2
SLIDE 2

Overview

Implementation Issues Caching Failures Disk Scheduling API What Goes in an API? Two Strange APIs Consistency Other File Operations

2 / 34

Overview

Implementation Issues Caching Failures Disk Scheduling API What Goes in an API? Two Strange APIs Consistency Other File Operations

2013-05-17

CS34 Overview

slide-3
SLIDE 3

Implementation Issues Caching

Disk Caching—Class Exercise

If OS caches blocks of a file in memory,

◮ How should it track what it’s caching? ◮ How should it decide what to cache?

3 / 34

Disk Caching—Class Exercise

If OS caches blocks of a file in memory,

◮ How should it track what it’s caching? ◮ How should it decide what to cache?

2013-05-17

CS34 Implementation Issues Caching Disk Caching—Class Exercise

There’s a close relationship to paging algorithms.

slide-4
SLIDE 4

Implementation Issues Caching

Disk Buffering

Allow writes to return immediately

◮ Copy data into a buffer ◮ Write out to the disk

Buffers vs. Caches

Which do we actually need?

4 / 34

Disk Buffering

Allow writes to return immediately

◮ Copy data into a buffer ◮ Write out to the disk

Buffers vs. Caches

Which do we actually need?

2013-05-17

CS34 Implementation Issues Caching Disk Buffering

We need some kind of buffer (the write may not be the same size as the block). We don’t need the cache; it just improves performance.

slide-5
SLIDE 5

Implementation Issues Caching

Buffer Cache

Store disk blocks waiting to write in buffer cache

◮ “Free” buffers used as cache for blocks recently read ◮ Dirty buffers will eventually be written to disk ◮ Allow buffer cache to use any free memory in the machine ◮ Ensure that a certain number of buffers are always available

Class Exercises & Reminders

When should we write the dirty buffers? Per-process or system-wide? Remind you of anything?

5 / 34

Buffer Cache

Store disk blocks waiting to write in buffer cache

◮ “Free” buffers used as cache for blocks recently read ◮ Dirty buffers will eventually be written to disk ◮ Allow buffer cache to use any free memory in the machine ◮ Ensure that a certain number of buffers are always available

Class Exercises & Reminders

When should we write the dirty buffers? Per-process or system-wide? Remind you of anything?

2013-05-17

CS34 Implementation Issues Caching Buffer Cache

slide-6
SLIDE 6

Implementation Issues Caching

Buffer Cache (cont.)

One buffer cache for all processes and all block devices

◮ Local disks ◮ Remote disks ◮ Tapes, CD-ROMs, etc.

6 / 34

Buffer Cache (cont.)

One buffer cache for all processes and all block devices

◮ Local disks ◮ Remote disks ◮ Tapes, CD-ROMs, etc.

2013-05-17

CS34 Implementation Issues Caching Buffer Cache (cont.)

slide-7
SLIDE 7

Implementation Issues Failures

Dealing with System Failure—Class Exercise

Extending the file by one block. . . In what order should we update the structures on disk to cause minimum damage if the system crashes? (Assume disk will never leave a block half-written. . . ) What about

◮ File creation? ◮ File deletion?

7 / 34

Dealing with System Failure—Class Exercise

Extending the file by one block. . . In what order should we update the structures on disk to cause minimum damage if the system crashes? (Assume disk will never leave a block half-written. . . ) What about

◮ File creation? ◮ File deletion?

2013-05-17

CS34 Implementation Issues Failures Dealing with System Failure—Class Exercise

slide-8
SLIDE 8

Implementation Issues Failures

Recovering from System Failure

Before system failure. . .

◮ Backups!

After system failure. . .

◮ Run consistency checker—compares data in directory

structure with data blocks on disk, tries to fix inconsistencies.

◮ Recover lost files (or disk) by restoring data from backup

8 / 34

Recovering from System Failure

Before system failure. . .

◮ Backups!

After system failure. . .

◮ Run consistency checker—compares data in directory

structure with data blocks on disk, tries to fix inconsistencies.

◮ Recover lost files (or disk) by restoring data from backup

2013-05-17

CS34 Implementation Issues Failures Recovering from System Failure

slide-9
SLIDE 9

Implementation Issues Disk Scheduling

Disk Scheduling

OS needs to use all I/O devices efficiently:

◮ Minimize access time—composed of

◮ Seek time ◮ Rotational latency

◮ Maximize disk bandwidth

◮ Bandwidth = Total Bytes Transferred / Total Time Taken

◮ Usually, disk requests can be re-ordered ◮ Several algorithms exist to schedule disk I/O requests

◮ Consider, e.g., cylinders 98, 183, 37, 122, 14, 124, 65, 67 and

an initial head position of 53

9 / 34

Disk Scheduling

OS needs to use all I/O devices efficiently:

◮ Minimize access time—composed of ◮ Seek time ◮ Rotational latency ◮ Maximize disk bandwidth ◮ Bandwidth = Total Bytes Transferred / Total Time Taken ◮ Usually, disk requests can be re-ordered ◮ Several algorithms exist to schedule disk I/O requests ◮ Consider, e.g., cylinders 98, 183, 37, 122, 14, 124, 65, 67 and an initial head position of 53

2013-05-17

CS34 Implementation Issues Disk Scheduling Disk Scheduling

slide-10
SLIDE 10

Implementation Issues Disk Scheduling

First-Come First-Served (FCFS)

Handle request queue in order. . . Total head movement of 640 cylinders—Yuck!

10 / 34

First-Come First-Served (FCFS)

Handle request queue in order. . . Total head movement of 640 cylinders—Yuck!

2013-05-17

CS34 Implementation Issues Disk Scheduling First-Come First-Served (FCFS)

slide-11
SLIDE 11

Implementation Issues Disk Scheduling

Shortest Seek Time First (SSTF)

Service request with minimum seek time from current head position Total head movement of 236 cylinders

11 / 34

Shortest Seek Time First (SSTF)

Service request with minimum seek time from current head position Total head movement of 236 cylinders

2013-05-17

CS34 Implementation Issues Disk Scheduling Shortest Seek Time First (SSTF)

SSTF may starve requests (why?).

slide-12
SLIDE 12

Implementation Issues Disk Scheduling

Scan (aka The Elevator Algorithm)

Move head from one end of the disk to the other, servicing requests as we go Total head movement of 208 cylinders

12 / 34

Scan (aka The Elevator Algorithm)

Move head from one end of the disk to the other, servicing requests as we go Total head movement of 208 cylinders

2013-05-17

CS34 Implementation Issues Disk Scheduling Scan (aka The Elevator Algorithm)

slide-13
SLIDE 13

Implementation Issues Disk Scheduling

Look

Like Scan, but only go as far as least/greatest request. . . Total head movement of 180 cylinders

13 / 34

Look

Like Scan, but only go as far as least/greatest request. . . Total head movement of 180 cylinders

2013-05-17

CS34 Implementation Issues Disk Scheduling Look

slide-14
SLIDE 14

Implementation Issues Disk Scheduling

Circular-SCAN (C-Scan)

Like Scan, but only move in one direction. . . Total head movement of 382 cylinders

14 / 34

Circular-SCAN (C-Scan)

Like Scan, but only move in one direction. . . Total head movement of 382 cylinders

2013-05-17

CS34 Implementation Issues Disk Scheduling Circular-SCAN (C-Scan)

Why do this? It wastes head movement. . .

slide-15
SLIDE 15

Implementation Issues Disk Scheduling

C-Look

The circular variant of Look (again, only move in one direction) Total head movement of 322 cylinders

15 / 34

C-Look

The circular variant of Look (again, only move in one direction) Total head movement of 322 cylinders

2013-05-17

CS34 Implementation Issues Disk Scheduling C-Look

slide-16
SLIDE 16

Implementation Issues Disk Scheduling

Fairness

What if we had two processes, each producing the following requests

◮ 0, 5, 10, . . . , 75 ◮ 125, 130, 135, . . . , 200

How fair would these techniques be?

16 / 34

Fairness

What if we had two processes, each producing the following requests

◮ 0, 5, 10, . . . , 75 ◮ 125, 130, 135, . . . , 200

How fair would these techniques be?

2013-05-17

CS34 Implementation Issues Disk Scheduling Fairness

slide-17
SLIDE 17

Implementation Issues Disk Scheduling

Fairness—Look

17 / 34

Fairness—Look

2013-05-17

CS34 Implementation Issues Disk Scheduling Fairness—Look

slide-18
SLIDE 18

Implementation Issues Disk Scheduling

Fairness—FCFS

18 / 34

Fairness—FCFS

2013-05-17

CS34 Implementation Issues Disk Scheduling Fairness—FCFS

slide-19
SLIDE 19

Implementation Issues Disk Scheduling

Fairness—FScan / N-step-Scan

19 / 34

Fairness—FScan / N-step-Scan

2013-05-17

CS34 Implementation Issues Disk Scheduling Fairness—FScan / N-step-Scan

slide-20
SLIDE 20

Implementation Issues Disk Scheduling

Selecting a Disk-Scheduling Algorithm

Comparing algorithms, we find that:

◮ FCFS can be okay if disk controller manages the scheduling

(many do these days)

◮ SSTF is common and has natural appeal ◮ LOOK works well (lowest amount of head moment in our test) ◮ LOOK and C-LOOK perform better for systems under heavy

load

20 / 34

Selecting a Disk-Scheduling Algorithm

Comparing algorithms, we find that:

◮ FCFS can be okay if disk controller manages the scheduling

(many do these days)

◮ SSTF is common and has natural appeal ◮ LOOK works well (lowest amount of head moment in our test) ◮ LOOK and C-LOOK perform better for systems under heavy

load

2013-05-17

CS34 Implementation Issues Disk Scheduling Selecting a Disk-Scheduling Algorithm

slide-21
SLIDE 21

Implementation Issues Disk Scheduling

Modern Disk Geometry

Modern disks maximize utilization by varying the number of sectors per cylinder

◮ Logical block→(Sector, Track/Cylinder)?

◮ Complex or impossible for operating system to calculate!

Sometimes tracks are even laid out in a zig-zag pattern Consequences?

21 / 34

Modern Disk Geometry

Modern disks maximize utilization by varying the number of sectors per cylinder

◮ Logical block→(Sector, Track/Cylinder)? ◮ Complex or impossible for operating system to calculate!

Sometimes tracks are even laid out in a zig-zag pattern Consequences?

2013-05-17

CS34 Implementation Issues Disk Scheduling Modern Disk Geometry

Disk scheduling becomes impossible to perform perfectly. But approximations work reasonably well. Or we can just hand it off to the disk (modern disks are smart). But careful placement of vital data across platters may not work out.

slide-22
SLIDE 22

API What Goes in an API?

File-Access API Class Exercise

What operations should we provide for accessing files? Describe & Develop

◮ Basic requirements for a file access API ◮ A stateful interface that satisfies those requirements ◮ A stateless interface that satisfies them

22 / 34

File-Access API Class Exercise

What operations should we provide for accessing files? Describe & Develop

◮ Basic requirements for a file access API ◮ A stateful interface that satisfies those requirements ◮ A stateless interface that satisfies them

2013-05-17

CS34 API What Goes in an API? File-Access API

This is a lengthy exercise; they should divide into groups and do it on paper.

slide-23
SLIDE 23

API What Goes in an API?

Stateful File Access

OS maintains some “context” for each open file. . .

File access

◮ handle = open(filename, mode) ◮ read(handle, length, buffer) ◮ write(handle, length, buffer) ◮ truncate(handle, length) ◮ seek(handle, position) ◮ close(handle)

File management

◮ info(name, info) ◮ delete(name) ◮ change_directory(dirname) ◮ create_dir(dirname) ◮ move(name, name)

23 / 34

Stateful File Access

OS maintains some “context” for each open file. . . File access

◮ handle = open(filename, mode) ◮ read(handle, length, buffer) ◮ write(handle, length, buffer) ◮ truncate(handle, length) ◮ seek(handle, position) ◮ close(handle)

File management

◮ info(name, info) ◮ delete(name) ◮ change_directory(dirname) ◮ create_dir(dirname) ◮ move(name, name)

2013-05-17

CS34 API What Goes in an API? Stateful File Access

slide-24
SLIDE 24

API What Goes in an API?

Stateless File Access

No (apparent) internal state—each system call fully describes the desired operation

File access

◮ create(filename) ◮ read(filename, pos, length, buffer) ◮ write(filename, pos, length, buffer) ◮ truncate(filename, length)

File management

◮ info(name, info) ◮ delete(name) ◮ create_dir(dirname) ◮ move(name, name)

Class Exercise

Contrast these two approaches. . .

24 / 34

Stateless File Access

No (apparent) internal state—each system call fully describes the desired operation File access

◮ create(filename) ◮ read(filename, pos, length, buffer) ◮ write(filename, pos, length, buffer) ◮ truncate(filename, length)

File management

◮ info(name, info) ◮ delete(name) ◮ create_dir(dirname) ◮ move(name, name)

Class Exercise

Contrast these two approaches. . .

2013-05-17

CS34 API What Goes in an API? Stateless File Access

Completeness: Each method can simulate the other Stateless operation:

  • Simple
  • Works well in a multi-threaded program
  • Basis for NFS

Stateful operation:

  • Assumes file-locality and sequential access is common
  • Provides the operating system with more information about which

files are being used

  • Maps well to other kinds of device besides files (e.g., read/write to a

terminal)

  • May add arbitrary limitations (e.g, maximum open files)
slide-25
SLIDE 25

API Two Strange APIs

Michigan Terminal System (60’s)

◮ Files divided into variable-length “lines”

◮ Even binary files made up of lines ◮ Each line numbered (fixed-point, 6 fractional decimal digits) ◮ Could read by line number or by “next line” ◮ Could write by line number (inserting in middle if appropriate)

  • r (?) just append at end

◮ No user access to devices

◮ E.g., print by creating file, then handing to OS ◮ Slightly problematic when terminals introduced. . . 25 / 34

Michigan Terminal System (60’s)

◮ Files divided into variable-length “lines” ◮ Even binary files made up of lines ◮ Each line numbered (fixed-point, 6 fractional decimal digits) ◮ Could read by line number or by “next line” ◮ Could write by line number (inserting in middle if appropriate)

  • r (?) just append at end

◮ No user access to devices ◮ E.g., print by creating file, then handing to OS ◮ Slightly problematic when terminals introduced. . .

2013-05-17

CS34 API Two Strange APIs Michigan Terminal System (60’s)

slide-26
SLIDE 26

API Two Strange APIs

RSX-11M/VMS (70’s)

◮ “inodes” exposed to application ◮ Directories identified by OS but accessed with file API ◮ OS responsible for allocating blocks upon request ◮ Complex read/write interface (asynchronous, fixed/variable

records, devices treated separately)

◮ Much of file system in library

26 / 34

RSX-11M/VMS (70’s)

◮ “inodes” exposed to application ◮ Directories identified by OS but accessed with file API ◮ OS responsible for allocating blocks upon request ◮ Complex read/write interface (asynchronous, fixed/variable

records, devices treated separately)

◮ Much of file system in library

2013-05-17

CS34 API Two Strange APIs RSX-11M/VMS (70’s)

slide-27
SLIDE 27

API Consistency

Consistency Model

Additional complications:

◮ Multiple processes can access files

—Two (or more) processes could read and write same file

◮ Asynchronous writes + errors = ? ◮ What if file is moved/renamed/deleted while a process is

using it? What should the rules be?

27 / 34

Consistency Model

Additional complications:

◮ Multiple processes can access files

—Two (or more) processes could read and write same file

◮ Asynchronous writes + errors = ? ◮ What if file is moved/renamed/deleted while a process is

using it? What should the rules be?

2013-05-17

CS34 API Consistency Consistency Model

slide-28
SLIDE 28

API Consistency

Class Exercise

Develop and justify a consistency model for file operations.

28 / 34

Class Exercise

Develop and justify a consistency model for file operations.

2013-05-17

CS34 API Consistency Class Exercise

slide-29
SLIDE 29

API Consistency

Class Exercise

Develop and justify a consistency model for file operations. Develop another one.

28 / 34

Class Exercise

Develop and justify a consistency model for file operations. Develop another one.

2013-05-17

CS34 API Consistency Class Exercise

slide-30
SLIDE 30

API Consistency

Unix Consistency Model

Unix uses the following rules:

◮ All file operations are globally atomic ◮ File is only deleted when its link (name) count is zero—an

  • pen counts as a link

◮ write in one process is globally visible immediately

afterwards

◮ writes are asynchronous—I/O errors may not be discovered

until the file is closed

29 / 34

Unix Consistency Model

Unix uses the following rules:

◮ All file operations are globally atomic ◮ File is only deleted when its link (name) count is zero—an

  • pen counts as a link

◮ write in one process is globally visible immediately

afterwards

◮ writes are asynchronous—I/O errors may not be discovered

until the file is closed

2013-05-17

CS34 API Consistency Unix Consistency Model

These rules do not map well onto a stateless I/O interface.

slide-31
SLIDE 31

API Consistency

NFSv2 Consistency Model

Classic NFS uses the following rules:

◮ All file operations are globally atomic and stateless ◮ move/rename/delete can disrupt accesses performed by

  • ther processes

◮ write in one process is globally visible immediately

afterwards

◮ writes are synchronous—I/O errors are discovered

immediately

30 / 34

NFSv2 Consistency Model

Classic NFS uses the following rules:

◮ All file operations are globally atomic and stateless ◮ move/rename/delete can disrupt accesses performed by

  • ther processes

◮ write in one process is globally visible immediately

afterwards

◮ writes are synchronous—I/O errors are discovered

immediately

2013-05-17

CS34 API Consistency NFSv2 Consistency Model

These rules map well onto a stateless I/O interface, but are not entirely consistent with the POSIX file model.

slide-32
SLIDE 32

API Consistency

File Locking

What if we want to lock sections of a file?

31 / 34

File Locking

What if we want to lock sections of a file?

2013-05-17

CS34 API Consistency File Locking

slide-33
SLIDE 33

API Other File Operations

File Access—Leveraging the VM System

Use virtual memory system to provide file access

◮ “Map” file into memory ◮ Page faults retrieve file’s data from disk ◮ Provide copy-on-write or writeback semantics

Add the following system calls:

◮ map_file(handle, size, mode, address) ◮ unmap_file(address)

(This mechanism doesn’t allow extending or truncating the file.)

32 / 34

File Access—Leveraging the VM System

Use virtual memory system to provide file access

◮ “Map” file into memory ◮ Page faults retrieve file’s data from disk ◮ Provide copy-on-write or writeback semantics

Add the following system calls:

◮ map_file(handle, size, mode, address) ◮ unmap_file(address)

(This mechanism doesn’t allow extending or truncating the file.)

2013-05-17

CS34 API Other File Operations File Access—Leveraging the VM System

slide-34
SLIDE 34

API Other File Operations

More File Access

Besides calls for protection, there are other system calls we might want:

◮ Control owner, permissions, etc. ◮ Control file caching ◮ Find (remaining) capacity of the disk ◮ Eject disk ◮ Discover whether any reads/writes have failed ◮ . . .

33 / 34

More File Access

Besides calls for protection, there are other system calls we might want:

◮ Control owner, permissions, etc. ◮ Control file caching ◮ Find (remaining) capacity of the disk ◮ Eject disk ◮ Discover whether any reads/writes have failed ◮ . . .

2013-05-17

CS34 API Other File Operations More File Access

slide-35
SLIDE 35

API Other File Operations

Generic Mechanisms

Two approaches

◮ ioctl(handle, request, buffer) ◮ Pseudo-files and pseudo-filesystems

34 / 34

Generic Mechanisms

Two approaches

◮ ioctl(handle, request, buffer) ◮ Pseudo-files and pseudo-filesystems

2013-05-17

CS34 API Other File Operations Generic Mechanisms

Ioctl is clumsy, hard to use, prone to inconsistency. Pseudo-files aren’t good for adding one new feature (such as locking).