File-System Operations
File-system operations 是用 on-disk metadata 與 in-memory cache 實作 create、open、read、write、close。
On-Disk Structures
- boot control block:volume boot information;不可 boot 可為空。
- volume control block:block size/count、free-block count、free-FCB pointer 等;UFS 類似 superblock。
- directory structure:file name → metadata identifier,例如 inode number。
- per-file FCB:單一 file 的 permission、owner、size、data-block pointers;見 D-OS-Ch14ba-File_Control_Block。
In-Memory Structures
- mount table:mounted volumes。
- directory cache:加速 pathname lookup。
- system-wide open-file table:共享 open file entry,含 FCB copy、open count、dirty state。
- per-process open-file table:process-local fd/handle,保存 current offset、access mode。
- buffer / page cache:保存 file-system blocks 或 file pages。
Create Flow
- 搜尋目標 directory,確認無同名 entry。
- 配置新的 FCB / inode。
- 讀入並修改 directory block。
- 建立 name → FCB identifier mapping。
- 將 directory / FCB / free-space metadata 寫回,或先交給 journal / cache。
Open / Read / Close Flow
open(path) 先查 system-wide open-file table;若已開啟,只替 process 建立 per-process entry。若未開啟,kernel 做 directory lookup、載入 FCB、建立 shared entry,再回傳 process-local descriptor / handle。
read(fd) / write(fd) 之後不再用 file name,而是 fd → per-process entry → system-wide entry → FCB / block mapping。close(fd) 移除 per-process entry、遞減 open count;count 歸零時才移除 shared entry,並寫回 dirty metadata。
Metadata update 不是 atomic,所以 crash consistency 需要 D-OS-Ch14g-File_System_Recovery。