0911 juluosdev

27
1 A journey of file system on JOS Name :wayling Date: 2012/09/11

Upload: waylin-ch

Post on 06-May-2015

141 views

Category:

Technology


8 download

TRANSCRIPT

Page 1: 0911 juluosdev

1

A journey of file system on JOS

Name :wayling

Date: 2012/09/11

Page 2: 0911 juluosdev

2

Who am I?

• JuluOSDev 星系 001– virtual file system (2011-12-20 )

• http://www.juluos.org/home/files

• 出沒於 TOSSUG & H4

[email protected]

wayling

Page 3: 0911 juluosdev

3

Introduction JOS

• MIT 6.828 Operating System Engineering• JOS 是一個有基本雛形的作業系統,擁有傳統 unix-like fu

nctiom(fork, exec…) ,但是實作方式是 exokernel style( 實作的 unix-like functiom 大部份皆為 user level 的函式,不實作於 kernel level) 。

• 主要部份 :– Booting– Memory management– User-level environments– Preemptive multitasking– File system and spawn– A shell Lectures:

Frans Kaashoek Robert Morris

Page 4: 0911 juluosdev

4

JOS File System

• Part1 : File system struct – In-memory and On-disk struct

• Part 2: Client/Server file system access– Remote procedure call (RPC) base on IPC +

process

Page 5: 0911 juluosdev

5

JOS File System

• Part1 : File system struct – In-memory and On-disk struct

• Part 2: Client/Server File System Access– remote procedure call (RPC) base on IPC + p

rocess

Page 6: 0911 juluosdev

6

The File System

• File system 是 memory 跟 disk 操作結合而成的,意及我們在存取檔案時, memory跟 disk 都需要有對應的 struct 來存放表示。

• static + dynamic 組成 file system– Static : 格式化硬碟成特有檔案系統格式 ex :

ext2 、 ext3 。– Dynamic : runtime 時作業系統保存對應的資料結構,藉此操作 disk ,提供檔案操作給使用者。

Page 7: 0911 juluosdev

7

JOS file system item(1/2)

• Sectors and Blocks– Sector :Disk 實體磁區 (512 B) 。– Block : file system 存取單位 (4KB) 。

• Superblocks (disk & memory)– 保存 block size 、 disk size 、 以及其他 met

a-data 用於尋找到 root directory 。• Block Bitmap (disk & memory)

– 管理 free disk blocks 。

Page 8: 0911 juluosdev

8

JOS file system item(2/2)

• File Meta-data – 保存”檔案”的必要資訊,檔名、大小、 以及 block pointer(direct/indirect) 。

• Directories versus Regular Files– Directories 主要保存多個 file struct 。– Regular Files 保存 data block 。

Page 9: 0911 juluosdev

9

JOS file system struct overview

File/Directory struct level

Block struct level

Disk struct (sector) level

User access

Page 10: 0911 juluosdev

10

File/directorydata blocks

Free Block Bitmap

Spuerblock

BootSector Block 0

Block 1

Block 2Tot

al d

isk

size

: N

Blo

cks

B

itmap

siz

e:

Eno

ugh

bloc

k to

hol

d N

bits

Block N -1

Block struct level

Page 11: 0911 juluosdev

11

struct File

(256 bytes)

Name : “foo”

Size : 54321

Direct block pointers:

(10)

Indirect block pointer :

Indirect block

(4096 bytes)

(1024)

Block 0

Block 1

Block 2

Block 3

Block 4

Block 5

Block 6

Block 9

Block 10

Block 11

Block 12

…..

File data blocks

(4096 bytes each)

File/Directory struct level

Page 12: 0911 juluosdev

12

JOS file system

• Disk access– user-level file system  - IDE disk driver 實做於 user s

pace 。– polling vs  interrupt-driven 。

• The Block Cache– 運用虛擬記憶體的特性,當程式存取檔案時轉換為對其地址空間做存取 (0x10000000 - 0xD000000 , 3GB) 。

• File operations– Open/Read/Write ,基本檔案操作。

Page 13: 0911 juluosdev

13

Format a file system (1/2)

#fsformat fs.img 1024 init newmotd motd

struct File

struct File

struct File

struct File

struct File

/

init

newmode

mode

init

dir

reg

reg

reg

reg

data blockdata block

……

=> 請參閱 source code (fsformat.c)

Page 14: 0911 juluosdev

14

struct File

Name : “/”

Size : 54321

Direct block pointers:

11

12

13

14

struct File

struct File

Name : “init”

Size : 54321

Name : “newmode”

Size : 54321

struct File

Name : “init”

Size : 54321

21

22

23

35

36

37

38

52

53

54

55

Format a file system (2/2)

#fs.img (block view)

使用 block bitmap 紀錄 block

分配狀況 (不一定連續分配 ) 。

Page 15: 0911 juluosdev

15

JOS file system service

• Part1 : File system struct – In-memory and On-disk struct

• Part 2: Client/Server file system access– remote procedure call (RPC) base on IPC + p

rocess

Page 16: 0911 juluosdev

16

JOS’s file overview

• Server– server.c

• Server 端主體,處理 clinet 請求。 (JOS 定義八種檔案操作請求 )– FSREQ_OPEN, 、 FSREQ_SET_SIZE 、 FSREQ_READ– FSREQ_WRITE 、 FSREQ_STAT 、 FSREQ_FLUSH– FSREQ_REMOVE 、 FSREQ_SYNC

– fs.c• File system 主體,處理 block 、 File 操作。

– bc.c• Block cache ,類似 copy on write 的機制。

• Client– file.c

• Client 端主要函式,對 server 發起 IPC 操作。– fd.c

• file description (fd) 是 server 跟 client 間的橋樑。

Page 17: 0911 juluosdev

17

Client/Server file system access

• JOS 使用 IPC 機制實作 client/server 架構的 file system service 。

• File system Server (FSServ) :– 作為 user space porcess ,提供檔案操作服務, 藉由 I

PC 提供其他 process 所需之請求。• Client :

– 透過 FSServ 來存取檔案。• FSServ 特徵 :

– Process id 1 。– Block cache 。– 回應 client 的 IPC request 。

Page 18: 0911 juluosdev

18

FSServ Block cache

3GB

0x10000000

0xD000000

FSServ address space

1

2

1. 申請實體記憶體 page 。2. 讀取 block 大小到記憶體中。(4096byte = 512 byte *8)

Page 19: 0911 juluosdev

19

JOS IPC (Inter-process communication ) (1/2)

• void ipc_send(envid_t to_env, uint32_t val, void *pg, int perm)static int sys_ipc_try_send(envid_t envid, uint

32_t value, void *srcva, unsigned perm)

對某個 process 傳送資料 (32bit) ,也可指定一個地址 (小於 UTOP) ,跟對方 process 共享該 page 。

=> 請參閱 source code (ipc.c & syscall.c)

Page 20: 0911 juluosdev

20

JOS IPC (Inter-process communication ) (2/2)

• int32_t ipc_recv(envid_t *from_env_store, void *pg, int *perm_store)static int sys_ipc_recv(void *dstva)

=> 請參閱 source code (ipc.c & syscall.c)

Blocking 自己等待對方把資料傳送完畢。可指定地址 ( 小於 UTOP) ,決定要跟傳送者共享 page 的位置。

Page 21: 0911 juluosdev

21

Client (fs.c)

// Send an inter-environment request to the file server, and wait for// a reply. static int fsipc(unsigned type, void *dstva){

if (debug){

…….}ipc_send(envs[1].env_id, type, &fsipcbuf, PTE_P | PTE_W | PTE_U);return ipc_recv(NULL, dstva, NULL);

}

封裝 IPC 成一個 fsipc 給 client 使用。IPC 訊息格式定義於 fs.h 。

=> 請參閱 source code (file.c & file.h)

Page 22: 0911 juluosdev

22

FSServ (serv.c) 初始 OpenFile table 。 // 用於管理 FSServ 所開啟的檔

案。 • struct OpenFile opentab[MAXOPEN] = {

{ 0, 0, 1, 0 }};• struct OpenFile { uint32_t o_fileid; struct File *o_file;

int o_mode; struct Fd *o_fd;};

=> 請參閱 source code (serv.c)

Page 23: 0911 juluosdev

23

FSServ Client

fd

File

fd

FSREQ_OPEN

ipc_send

ipc_sendipc_recv ipc_recv

1

1

2

3

3

0xD0000000

0xD0000000

0x0ffff000

1.Client 傳送一個 IPC request 讓 FSServ 接收,request 中夾帶路徑,並申請 fd 待, FSServ 回應 fd page ,用以共享 page 。2.FSServ 依路徑開啟 File struct 。3. 回應 Client, 並把開啟的 fd address map 到clinet 的 fd 上。

共享 page

=> 請參閱 source code (serv.c)

Page 24: 0911 juluosdev

24

FSServ Client

fd

File

FSREQ_WRITE

ipc_send

ipc_sendipc_recv ipc_recv

1

1

2

3 3

0xD0000000

0x0ffff000

1.Client 傳送一個 IPC request 讓 FSServ 接收,request 中夾帶 file id 及將寫入的資料 buffer 。2. 將資料 buffer 寫入檔案的 block 中。3. 回應 Client ,將 file pointer 位址返回。

=> 請參閱 source code (serv.c)

fd

Page 25: 0911 juluosdev

25

JOS file system RPC

• RPC base on IPC (ex : call file system server read a file)

read( )

lib/fd.c

devfile_read ( )

lib/file.c

fsipc ( )

lib/file.c

ipc_send ( )

file_read ( )

fs/fs.c

serve_read ( )

fs/serv.c

serve ( )

fs/serv.c

ipc_recv( )

RPC mechanism

JOS IPC

Regular env FSServ env

Page 26: 0911 juluosdev

26

Q&A

Page 27: 0911 juluosdev

27

Reference

• [mit 6.828 lab 5]

http://pdos.csail.mit.edu/6.828/2011/labs/lab5/