SlideShare a Scribd company logo
1 of 27
A journey of file system
       on JOS
      Name :wayling
     Date: 2012/09/11


                        1
Who am I?

 wayling

• JuluOSDev 星系 001
  – virtual file system (2011-12-20 )
     • http://www.juluos.org/home/files
• 出沒於 TOSSUG & H4
• waylingII@gmail.com
                                          2
Introduction JOS
• MIT 6.828 Operating System Engineering
• JOS 是一個有基本雛形的作業系統,擁有傳統 unix-like
  functiom(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 Morris3
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




                                   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




                                   5
The File System
• File system 是 memory 跟 disk 操作結合
  而成的,意及我們在存取檔案時, memory
  跟 disk 都需要有對應的 struct 來存放表示
  。
• static + dynamic 組成 file system
 – Static : 格式化硬碟成特有檔案系統格式 ex :
   ext2 、 ext3 。
 – Dynamic : runtime 時作業系統保存對應的資
   料結構,藉此操作 disk ,提供檔案操作給使用
   者。
                         6
JOS file system item(1/2)
• Sectors and Blocks
  – Sector :Disk 實體磁區 (512 B) 。
  – Block : file system 存取單位 (4KB) 。
• Superblocks (disk & memory)
  –   保存 block size 、 disk size 、 以及其他
    meta-data 用於尋找到 root directory 。
• Block Bitmap (disk & memory)
  – 管理 free disk blocks 。

                                 7
JOS file system item(2/2)
• File Meta-data
  – 保存”檔案”的必要資訊,檔名、大小、 以及
   block pointer(direct/indirect) 。
• Directories versus Regular Files
  – Directories 主要保存多個 file struct 。
  – Regular Files 保存 data block 。




                                 8
JOS file system struct overview
                                        User access




          File/Directory struct level



              Block struct level




          Disk struct (sector) level

                                            9
Block N -1




                                                           File/directory
                                                            data blocks




                                      t b
                                     siN
     l B
skc oN




                                                           Free Block
                                                             Bitmap
                     : ez s pa m B
                               ti




                                                                            Block 2
: ez s ks dl a o T


                                     h o k c o b hguon E
                        i
              t




                                                           Spuerblock       Block 1
   i    i




                                                           BootSector       Block 0
                                             l




                                                                                      10
                                                                              Block struct level
                                        t
File data blocks
       struct File
                                        (4096 bytes each)
       (256 bytes)
                                              Block 0
Name : “foo”
                                              Block 1
Size : 54321
Direct block pointers:                        Block 2

                                              Block 3
          (10)
                                              Block 4
Indirect block pointer :
                                              Block 5

                                              Block 6
      Indirect block
                                                …..
      (4096 bytes)
                                              Block 9



                                             Block 10

           (1024)                            Block 11

                                             Block 12

                                               11
                           File/Directory struct level
JOS file system
• Disk access
  – user-level file system - IDE disk driver 實做於 user
    space 。
  – polling vs interrupt-driven 。
• The Block Cache
  – 運用虛擬記憶體的特性,當程式存取檔案時轉換為對
   其地址空間做存取 (0x10000000 - 0xD000000 ,
   3GB) 。
• File operations
  – Open/Read/Write ,基本檔案操作。

                                         12
Format a file system (1/2)
#fsformat fs.img 1024 init newmotd motd
                struct File                           struct File

                     /                                    init

                     dir                                  reg




  struct File                    data block
                                                                 data block
                                        struct File
      init
                     struct File              mode
      reg
                         newmode               reg                      ……

                           reg



                                                                  13
=> 請參閱 source code (fsformat.c)
Format a file system (2/2)
  #fs.img (block view)
                                                                struct
             struct File                                        File
                                                           Name : “init”
                                                                               21
          Name : “/”                                       Size : 54321        22
                                                     11
          Size : 54321                               12                        23
          Direct block pointers:                     13          struct File
                                                     14
                                       struct File          Name : “newmode”
                                                            Size : 54321

                                   Name : “init”
                                   Size : 54321
                                                                                35
                                                                                36
                                                      52                        37
                                                      53
使用 block bitmap 紀錄 block                                                        38

                                   。
                                                      54
分配狀況 ( 不一定連續分配 )                                                      14
                                                      55
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 +
    process




                                   15
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 間的橋樑。
                                                        16
Client/Server file system access
• JOS 使用 IPC 機制實作 client/server 架構的 file
  system service 。
• File system Server (FSServ) :
   – 作為 user space porcess ,提供檔案操作服務, 藉由
     IPC 提供其他 process 所需之請求。
• Client :
   – 透過 FSServ 來存取檔案。
• FSServ 特徵 :
   – Process id 1 。
   – Block cache 。
   – 回應 client 的 IPC request 。

                                 17
FSServ Block cache
             FSServ address space

                                    1

0xD000000


                    3GB

                                        2
0x10000000




        1. 申請實體記憶體 page 。
        2. 讀取 block 大小到記憶體中。
        (4096byte = 512 byte *8)
                                            18
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,
    uint32_t value, void *srcva, unsigned perm)


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


                                         19
=> 請參閱 source code (ipc.c & syscall.c)
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)



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


                                         20
=> 請參閱 source code (ipc.c & syscall.c)
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 。

                                                          21
 => 請參閱 source code (file.c & file.h)
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;
};

                                   22
=> 請參閱 source code (serv.c)
2
                                  fd
                                           FSREQ_OPEN
            0xD0000000

                                                       共享 page
                               File
                                                  1
                                           ipc_send
            0x0ffff000



                         FSServ                               Client

                                                              ipc_recv      3
                         ipc_recv
                                       1   ipc_send

1.Client 傳送一個 IPC request 讓 FSServ 接收,      3
                                                                       fd
request 中夾帶路徑,並申請 fd 待, FSServ 回                 0xD0000000
應 fd page ,用以共享 page 。
2.FSServ 依路徑開啟 File struct 。
3. 回應 Client, 並把開啟的 fd address map 到
clinet 的 fd 上。
                                                       23
      => 請參閱 source code (serv.c)
2
                               fd         FSREQ_WRITE
         0xD0000000           File




                                                       1
          0x0ffff000
                                            ipc_send

                           FSServ                               Client


                           ipc_recv                                      ipc_recv
                                      1    ipc_send
                                                      3                             3
1.Client 傳送一個 IPC request 讓 FSServ 接收,                          fd
request 中夾帶 file id 及將寫入的資料 buffer 。
2. 將資料 buffer 寫入檔案的 block 中。
3. 回應 Client ,將 file pointer 位址返回。
                                                           24
     => 請參閱 source code (serv.c)
JOS file system RPC
• RPC base on IPC (ex : call file system server
  read a file)

                 Regular env                  FSServ env
         lib/fd.c                  fs/fs.c

                      read( )                 file_read ( )


         lib/file.c                fs/serv.c                        RPC mechanism

                devfile_read ( )             serve_read ( )

         lib/file.c                fs/serv.c

                      fsipc ( )                serve ( )



                    ipc_send ( )              ipc_recv( )          JOS IPC
                                                              25
Q&A




      26
Reference
• [mit 6.828 lab 5]

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




                                  27

More Related Content

What's hot

Rethinkdb and tokudb research
Rethinkdb and tokudb research Rethinkdb and tokudb research
Rethinkdb and tokudb research mysqlops
 
Virtual file system (VFS)
Virtual file system (VFS)Virtual file system (VFS)
Virtual file system (VFS)Waylin Ch
 
4, files & folders
4, files & folders4, files & folders
4, files & foldersted-xu
 
云存储系统设计
云存储系统设计云存储系统设计
云存储系统设计drewz lin
 
Dbabc.net 利用heartbeat + drbd搭建my sql高可用环境
Dbabc.net 利用heartbeat + drbd搭建my sql高可用环境Dbabc.net 利用heartbeat + drbd搭建my sql高可用环境
Dbabc.net 利用heartbeat + drbd搭建my sql高可用环境dbabc
 
Unixtoolbox zh cn
Unixtoolbox zh cnUnixtoolbox zh cn
Unixtoolbox zh cnxdboy2006
 

What's hot (7)

Rethinkdb and tokudb research
Rethinkdb and tokudb research Rethinkdb and tokudb research
Rethinkdb and tokudb research
 
Virtual file system (VFS)
Virtual file system (VFS)Virtual file system (VFS)
Virtual file system (VFS)
 
4, files & folders
4, files & folders4, files & folders
4, files & folders
 
Ubuntu
UbuntuUbuntu
Ubuntu
 
云存储系统设计
云存储系统设计云存储系统设计
云存储系统设计
 
Dbabc.net 利用heartbeat + drbd搭建my sql高可用环境
Dbabc.net 利用heartbeat + drbd搭建my sql高可用环境Dbabc.net 利用heartbeat + drbd搭建my sql高可用环境
Dbabc.net 利用heartbeat + drbd搭建my sql高可用环境
 
Unixtoolbox zh cn
Unixtoolbox zh cnUnixtoolbox zh cn
Unixtoolbox zh cn
 

Viewers also liked

Jason nam world music
Jason nam world musicJason nam world music
Jason nam world musicEllen So
 
Karolina poland
Karolina  polandKarolina  poland
Karolina polandEllen So
 
San Pablo Tanguillo
San Pablo TanguilloSan Pablo Tanguillo
San Pablo TanguilloJav2011
 
What Really Is The Greatest Race?
What Really Is The Greatest Race?What Really Is The Greatest Race?
What Really Is The Greatest Race?Paul Trudeau
 
prezentacja Ani Minasyan SP7 w Szczecinku, kl. VIc
prezentacja Ani Minasyan SP7 w Szczecinku, kl. VIcprezentacja Ani Minasyan SP7 w Szczecinku, kl. VIc
prezentacja Ani Minasyan SP7 w Szczecinku, kl. VIctemat
 
Black History Presentation
Black  History  PresentationBlack  History  Presentation
Black History PresentationPaul Trudeau
 

Viewers also liked (7)

Jason nam world music
Jason nam world musicJason nam world music
Jason nam world music
 
Karolina poland
Karolina  polandKarolina  poland
Karolina poland
 
Over Mijn Werk
Over Mijn WerkOver Mijn Werk
Over Mijn Werk
 
San Pablo Tanguillo
San Pablo TanguilloSan Pablo Tanguillo
San Pablo Tanguillo
 
What Really Is The Greatest Race?
What Really Is The Greatest Race?What Really Is The Greatest Race?
What Really Is The Greatest Race?
 
prezentacja Ani Minasyan SP7 w Szczecinku, kl. VIc
prezentacja Ani Minasyan SP7 w Szczecinku, kl. VIcprezentacja Ani Minasyan SP7 w Szczecinku, kl. VIc
prezentacja Ani Minasyan SP7 w Szczecinku, kl. VIc
 
Black History Presentation
Black  History  PresentationBlack  History  Presentation
Black History Presentation
 

0911 juluosdev

  • 1. A journey of file system on JOS Name :wayling Date: 2012/09/11 1
  • 2. Who am I? wayling • JuluOSDev 星系 001 – virtual file system (2011-12-20 ) • http://www.juluos.org/home/files • 出沒於 TOSSUG & H4 • waylingII@gmail.com 2
  • 3. Introduction JOS • MIT 6.828 Operating System Engineering • JOS 是一個有基本雛形的作業系統,擁有傳統 unix-like functiom(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 Morris3
  • 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 4
  • 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 + process 5
  • 6. The File System • File system 是 memory 跟 disk 操作結合 而成的,意及我們在存取檔案時, memory 跟 disk 都需要有對應的 struct 來存放表示 。 • static + dynamic 組成 file system – Static : 格式化硬碟成特有檔案系統格式 ex : ext2 、 ext3 。 – Dynamic : runtime 時作業系統保存對應的資 料結構,藉此操作 disk ,提供檔案操作給使用 者。 6
  • 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 、 以及其他 meta-data 用於尋找到 root directory 。 • Block Bitmap (disk & memory) – 管理 free disk blocks 。 7
  • 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 。 8
  • 9. JOS file system struct overview User access File/Directory struct level Block struct level Disk struct (sector) level 9
  • 10. Block N -1 File/directory data blocks t b siN l B skc oN Free Block Bitmap : ez s pa m B ti Block 2 : ez s ks dl a o T h o k c o b hguon E i t Spuerblock Block 1 i i BootSector Block 0 l 10 Block struct level t
  • 11. File data blocks struct File (4096 bytes each) (256 bytes) Block 0 Name : “foo” Block 1 Size : 54321 Direct block pointers: Block 2 Block 3 (10) Block 4 Indirect block pointer : Block 5 Block 6 Indirect block ….. (4096 bytes) Block 9 Block 10 (1024) Block 11 Block 12 11 File/Directory struct level
  • 12. JOS file system • Disk access – user-level file system - IDE disk driver 實做於 user space 。 – polling vs interrupt-driven 。 • The Block Cache – 運用虛擬記憶體的特性,當程式存取檔案時轉換為對 其地址空間做存取 (0x10000000 - 0xD000000 , 3GB) 。 • File operations – Open/Read/Write ,基本檔案操作。 12
  • 13. Format a file system (1/2) #fsformat fs.img 1024 init newmotd motd struct File struct File / init dir reg struct File data block data block struct File init struct File mode reg newmode reg …… reg 13 => 請參閱 source code (fsformat.c)
  • 14. Format a file system (2/2) #fs.img (block view) struct struct File File Name : “init” 21 Name : “/” Size : 54321 22 11 Size : 54321 12 23 Direct block pointers: 13 struct File 14 struct File Name : “newmode” Size : 54321 Name : “init” Size : 54321 35 36 52 37 53 使用 block bitmap 紀錄 block 38 。 54 分配狀況 ( 不一定連續分配 ) 14 55
  • 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 + process 15
  • 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 間的橋樑。 16
  • 17. Client/Server file system access • JOS 使用 IPC 機制實作 client/server 架構的 file system service 。 • File system Server (FSServ) : – 作為 user space porcess ,提供檔案操作服務, 藉由 IPC 提供其他 process 所需之請求。 • Client : – 透過 FSServ 來存取檔案。 • FSServ 特徵 : – Process id 1 。 – Block cache 。 – 回應 client 的 IPC request 。 17
  • 18. FSServ Block cache FSServ address space 1 0xD000000 3GB 2 0x10000000 1. 申請實體記憶體 page 。 2. 讀取 block 大小到記憶體中。 (4096byte = 512 byte *8) 18
  • 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, uint32_t value, void *srcva, unsigned perm) 對某個 process 傳送資料 (32bit) ,也 可指定一個地址 ( 小於 UTOP) ,跟 對方 process 共享該 page 。 19 => 請參閱 source code (ipc.c & syscall.c)
  • 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) Blocking 自己等待對方把資料傳送完 畢。可指定地址 ( 小於 UTOP) ,決定 要跟傳送者共享 page 的位置。 20 => 請參閱 source code (ipc.c & syscall.c)
  • 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 。 21 => 請參閱 source code (file.c & file.h)
  • 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; }; 22 => 請參閱 source code (serv.c)
  • 23. 2 fd FSREQ_OPEN 0xD0000000 共享 page File 1 ipc_send 0x0ffff000 FSServ Client ipc_recv 3 ipc_recv 1 ipc_send 1.Client 傳送一個 IPC request 讓 FSServ 接收, 3 fd request 中夾帶路徑,並申請 fd 待, FSServ 回 0xD0000000 應 fd page ,用以共享 page 。 2.FSServ 依路徑開啟 File struct 。 3. 回應 Client, 並把開啟的 fd address map 到 clinet 的 fd 上。 23 => 請參閱 source code (serv.c)
  • 24. 2 fd FSREQ_WRITE 0xD0000000 File 1 0x0ffff000 ipc_send FSServ Client ipc_recv ipc_recv 1 ipc_send 3 3 1.Client 傳送一個 IPC request 讓 FSServ 接收, fd request 中夾帶 file id 及將寫入的資料 buffer 。 2. 將資料 buffer 寫入檔案的 block 中。 3. 回應 Client ,將 file pointer 位址返回。 24 => 請參閱 source code (serv.c)
  • 25. JOS file system RPC • RPC base on IPC (ex : call file system server read a file) Regular env FSServ env lib/fd.c fs/fs.c read( ) file_read ( ) lib/file.c fs/serv.c RPC mechanism devfile_read ( ) serve_read ( ) lib/file.c fs/serv.c fsipc ( ) serve ( ) ipc_send ( ) ipc_recv( ) JOS IPC 25
  • 26. Q&A 26
  • 27. Reference • [mit 6.828 lab 5] http://pdos.csail.mit.edu/6.828/2011/labs/lab5/ 27