31
This lecture… Implementing file system abstraction Comparison among disk management algorithms

COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Embed Size (px)

Citation preview

Page 1: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

This lecture… Implementing file system abstraction Comparison among disk management algorithms

Page 2: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

File System Components

Disk management: how to arrange collection of disk blocks into files

Naming: user gives file name, not track 50, platter 5, etc. Protection: keep information secure Reliability/durability: when system crashes, lose stuff in

memory, but want files to be durable.

Physical Reality File System AbstractionBlock oriented Byte orientedPhysical sector #’s Named filesNo protection Users protected from each otherData might be corrupted Robust to machine failuresif machine crashes

Page 3: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

User vs. System View of a File User’s view:

– Durable data structures – executable com file (static data region, relocation table, code, etc.)

– Memory-mapped files: operations – read/write to mem– Serialization (also pointer swizzling, marshalling)

Systems’ view (system call interface):– Collection of bytes (UNIX)

System’s view (inside OS):– Collection of blocks – a block is a logical transfer unit, while a sector is the

physical transfer unit. – Block size >= sector size; – in UNIX, block size is 4KB.

Page 4: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Translating from user to system view What happens if user says: give me bytes 2 – 12?

– a. Fetch block corresponding to those bytes– b. Return just the correct portion of the block

What about: write bytes 2 – 12?– a. Fetch block– b. Modify portion– c. Write out block

Everything inside file system is in whole size blocks.

For example, getc, putc => buffers 4096 bytes, even if interface is one byte at a time.

From now on, file is collection of blocks.

Page 5: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

File Access Methods1. Sequential access –

– bytes read in order (give me the next X bytes, then give me next)

– Almost all file access are of this flavor– example: editor writes out new file, compiler reads in file, etc.

2. Random access – – read/write element out of middle of array (give me bytes i – j)– Less frequent, but still important. For example, virtual

memory backing file: page of memory stored in file– Want this to be fast – don’t want to have to read all bytes to

get to the middle of the file– examples: data set for demand paging, databases

3. Content-based access – – “find me 100 bytes starting with “TOM” – Example: employee records – once you find the bytes,

increase my salary by a factor of 2– Many file systems don’t provide this; instead, database is

built on top to index content (requires efficient random access)

Page 6: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

File Usage Patterns

1. Most files are small (for example, .login, .c files)2. Large files use up most of the disk space3. Large files account for most of the bytes

transferred to/from disk

Bad news: need everything to be efficient.– Need small files to be efficient, since lots of

them.– Need large files to be efficient, since most of the

disk space, most of the I/O due to them

Page 7: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Disk Management Policies Basic entities on a disk:

– File: user-visible group of blocks arranged sequentially in logical space

– Directory: user-visible index mapping names to files

Access disk as linear array of blocks. Two Options: – Identify blocks as vectors [cylinder, surface,

sector]. » Blocks numbered in cylinder-major order, so that

adjacent numbered blocks can be accessed without seeks or rotational delay.

» Track 0, surface 0, sector 0, 1, ... | surface 1, sector 0, 1 ... | ... | track 1, surface 1, sector 0...

» Not used much anymore.

102

3

7

8 910

Page 8: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Disk Management Policies– Logical Block Addressing (LBA). Every block has

integer address from zero up to max number of cylinders.

– Controller translates from address physical position» First case: OS/BIOS must deal with bad blocks» Second case: hardware shields OS from structure of disk

Need way to track free disk blocks– Link free blocks together too slow today– Use bitmap to represent free space on disk

Need way to structure files: File Header (file descriptor) – which blocks are associated with each file within the

logical file structure– Optimize placement of files disk blocks to match

access and usage patterns

Page 9: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Disk Management Policies Caching: every OS today keeps a cache of

recently used disk blocks in memory, to avoid having to go to disk.

Common to all organizations. For now, assume no cache, and add it later. How do we organize file on disk? Goals:

– Maximize sequential performance– Easy random access to file– Easy management of file (growth, truncation, etc)

Page 10: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Contiguous allocation User says in advance how big file will be (disadvantage) Search bit map (using best fit/first fit) to locate space for

file File header (or file descriptor) contains:

– First sector/LBA in file– File size (# of sectors)

Pros & cons:– + Fast sequential access– + Easy random access– – External fragmentation– – Hard to grow files

Example: IBM OS/360

file a (base=1,len=3) file b (base=5,len=2)

what happens if file c needs 2 sectors???

Page 11: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Linked files Each block, pointer to next on disk (Alto, TOPS-10,

DOS FAT) File header: pointer to first block on disk Pros & Cons:

– + Can grow files dynamically– + Free list managed same as file– – Sequential access: seek between each block– – – – Random access: horrible– – Unreliable (lose block, lose rest of file)

file a (base=1) file b (base=5)

how do you find the last block in a?

Page 12: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Example: DOS FS (simplified) Used linked list; but instead of embedding links in pages, they

used a separate structure called the File Allocation Table (FAT).

The FAT has an entry for each block on the disk and the entries corresponding to the blocks of a particular file are linked up.

free

FAT (16-bit entries)

eof

1 eof

eof

3

4...

Directory (5)

a: 6

b: 2

file a

6 4 3

file b

2 1

0

1

2

3

4

5

6

Page 13: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

FAT discussion Space overhead of FAT is trivial:

– 2 bytes / 512 byte block = ~.4% (Compare to Unix)

Reliability: how to protect against errors? – Create duplicate copies of FAT on disk. – State duplication a very common theme in

reliability Bootstrapping: where is root directory?

– Fixed location on disk:

FAT (opt) FAT root dir …

FAT (MS-DOS)

Page 14: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Indexed files (Nachos, VMS) User declares max file size; system allocates a file header to hold an array of pointers

big enough to point to file size number of blocks.

Pros & Cons:– + Can easily grow up to space allocated for descriptor– + Random access is fast– – Clumsy to grow file bigger than table size– – Still lots of seeks: blocks can be spread all over the

disk, so sequential access is slow.

File header

Null

Disk blocks

Page 15: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Multilevel indexed (UNIX 4.1) Key idea: efficient for small files, but still allow big

files File header contains 13 pointers

– fixed size table, pointers not all equivalent – the header is called an “inode” in UNIX

First 10 are pointers to data blocks. – If file is small enough, some pointers will be NULL.

Page 16: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Multilevel indexed (UNIX 4.1)File header

Disk blocks

123

111213

12

11

256

266

256256

267

256256 256

256

256

Page 17: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Multilevel indexed (UNIX 4.1) What if you allocate 11th block?

– Pointer to an indirect block – a block of pointers to data blocks.

– Gives us 256 blocks, + 10 (from file header) = 1/4 MB

What if you allocate a 267th block?– Pointer to a doubly indirect block – a block of

pointers to indirect blocks (in turn, block of pointers to data blocks).

– Gives us about 64K blocks => 64MB What if want a file bigger than this?

– One last pointer – what should it point to?– Instead, pointer to triply indirect block – block of

pointers to doubly indirect blocks (which are ...)

Page 18: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Multilevel indexed (UNIX 4.1) Thus, file header is:

– First 10 data block pointers – point to one block each, so 10 blocks

– 11 indirect block pointer – points to 256 blocks– 12 doubly indirect block pointer – points to 64K blocks– 13 triply indirect block pointer – points to 16M blocks

1. Bad news: Still an upper limit on file size ~ 16 GB.2. Pointers get filled in dynamically: need to allocate

indirect block only when file grows > 10 blocks. – If small file, no indirection needed.

3. How many disk accesses to reach block #23? (2)

Page 19: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Multilevel indexed (UNIX 4.1) How about block # 5? (1) How about block # 340? (3) UNIX Pros & Cons:

– + Simple (more or less)– + Files can easily expand (up to a point)– + Small files particularly cheap and easy– – Very large files, spend lots of time reading

indirect blocks– – Lots of seeks

Page 20: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

DEMOS OS for Cray-1, mid to late 70’s. File system approach corresponds to

segmentation.

Cray-1 had 12 ns cycle time, so CPU:disk speed ratio about the same as today (a few million instructions = 1 seek).

Idea: reduce disk seeks by using contiguous allocation in normal case, but allow flexibility to have non-contiguous allocation.

Page 21: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

DEMOS File header: table of base & size (10 “block group”

pointers)

Each “block group” – a contiguous region of blocks Are 10 block group pointers enough? – No. If need more than 10 block groups, set flag in

file header: BIGFILE.

Base size

On disk

File header

Page 22: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

DEMOS Each table entry now points to an indirect block group – a

block group of pointers to block groups.

Can get huge files this way: Suppose 1000 blocks in a block group, each block 8K size, each entry 8 byte => (10 ptrs1024 groups/ptr1000 blocks/group)*8K =80 GB max file size

How do you find an available block group? – Use bit map to find block of 0’s

Base size

File header

On disk

Indirect block group

Block group

Page 23: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

DEMOS Pros & cons:

– + Fast sequential access– + Easy to find free block groups– + Free areas merge automatically– – When disk fills up:

» a. No long runs of blocks (fragmentation)» b. High CPU overhead to find free block

Solution:– Don’t let disk get full – keep portion in reserve– Free count = # blocks free in bitmap.– Normally, don’t even try allocate if free count = 0.– Change this to: don’t allocate if free count < reserve

Why do this?– Tradeoff: pay for more disk space, get contiguous

allocation How much of a reserve do you need?

– In practice, 10% seems like enough.

Page 24: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

UNIX BSD 4.2 Exactly the same as BSD 4.1 (same file header

and triply indirect blocks), except incorporated some ideas from DEMOS:– Uses bitmap allocation in place of free list– Attempt to allocate files contiguously– 10% reserved disk space– Skip sector positioning

Problem: when you create a file, don’t know how big it will become (in UNIX, most writes are by appending to the file).

So how much contiguous space do you allocate for a file, when it’s created?

Page 25: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

UNIX BSD 4.2 In Demos, power of 2 growth: once it grows past 1

MB, allocate 2MB, etc. In BSD 4.2, just find some range of free blocks,

put each new file at the front of a different range. When need to expand a file, you first try

successive blocks in bitmap, then choose new range of blocks.

Also, rotational delay can cause a problem, even with sequential files.

Page 26: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Dealing with rotational delay Read one block, do processing, and read next

block. In the meantime, disk has continued turning:

missed next block! Need 1 revolution/block! If have to wait for entire rotation, problem!

– Go from reading at disk bandwidth, to reading one sector every rotation.

Skip Sector

Track Buffer(Holds complete track)

Page 27: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Dealing with rotational delay Solution 1

– Skip sector positioning (“interleaving:)» Place the blocks from one file on every other block of

a track: give time for processing to overlap rotation Solution 2

– Read ahead/disk track buffers – read next block right after first, even if application hasn’t asked for it yet. » This could be done either by OS (read ahead) or by

disk itself (track buffers).» Many disk controllers have internal RAM that allows

them to read a complete track Important Aside: Modern disks+controllers do

many complex things “under the covers”– Track buffers, elevator algorithms, bad block

filtering

Page 28: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

How do we actually access files? All information about a file contained in its file header

– UNIX calls this an “inode”» Inodes are global resources identified by index (“inumber”)

– Once you load the header structure, all the other blocks of the file are locatable

Question: how does the user ask for a particular file?– One option: user specifies an inode by a number (index).

» Imagine: open(“14553344”)

– Better option: specify by textual name» Have to map nameinumber

– Another option: Icon» This is how Apple made its money. Graphical user

interfaces. Point to a file and click.

Page 29: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

How do we actually access files? Naming: The process by which a system

translates from user-visible names to system resources– In the case of files, need to translate from strings

(textual names) or icons to inumbers/inodes– For global file systems, data may be spread over

globeneed to translate from strings or icons to some combination of physical server location and inumber

Page 30: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Directories

Hierarchical name space: Files named by ordered set (e.g.: /programs/p/list)

Page 31: COSC 3407: Operating Systems Lecture 18: Disk Management and File System

Directories Directories: a special type of relation

– Just a table of (file name, inumber) pairs– Question: how is the relation stored?

» Directories often stored just like files» Can store inumber for directories or files in other

directories

– Question: how is the directory structured?» Needs to be quickly searchable!