Lab7&8: VFS & File System

1
2
Q: 找不到好用的 VFS 範例
A: 要有檔案 建回檔案系統 就要看救援軟體(testdisk)

不愧是大佬,直接看大型專案,對我而言太 hardcore,還是請示了一下 AI:

1
2
3
Here are a few examples of Virtual File System (VFS) implementations available on GitHub:

1. **[simplefs](https://github.com/sysprog21/simplefs)**: This project is a simple file system implementation in C. It supports regular files with operations like create, remove, read/write, and rename. It also supports hard/symbolic links with similar operations. The project provides a partition layout and is licensed under the BSD 2 clause license.

沒想到首推 simplefs,也找到共筆


Question 1:Is it possible that a file exists in a file system, but there is no vnode object of it?

  • Phind

    I am writing a filesystem implementing VFS. What are possible reasons to keep me from creating a vnode while the file is really exist?

  • Bard

    I am writing a filesystem implementing VFS. What are possible reasons to keep me from creating a vnode while the file is really exist?
    https://g.co/bard/share/5d50f541af5f


File descriptor table

File descriptor 無法直接從 VFS 傳給 user task,一來是 kernel space 和 user space 彼此分開,二來是讓 user task 有修改 file descriptor 存取其他 task 檔案的機會。需滿足以下條件:

  • For each opened file, a VFS should pass a token to the user task. The user task accesses the file by the token.
  • A VFS should be able to identify the token belongs to which task.
  • A user task should not be able to fake a token to read another task’s opened file.

簡易的實作方式是在 struct task 實作 file descriptor table,file descriptor 存在內,而傳遞 index of file descriptor 給 user space。


參考文件系统(六)—文件系统mount过程。第一次掛載檔案系統時會先建立 superblock 以及建立 root directory 的 dentry 實體,下次掛載同樣的檔案系統時,直接拿該 dentry 進行操作。


參考 osdev wiki 實作 FAT32 + long filename support 時忍不住向友人抱怨:

FAT32 example 有夠難找

這東西不是 dosfstools 格一格就好?

但 dosfstools 功能有限,想知道 free space, total size, dirty date 等更多資訊,fatcat 更加合適。

dosfstools

以 gdbserver + vscode 進行 trace

1
2
3
4
5
$ fdisk -lu test.img
Device Boot Start End Sectors Size Id Type
test.img1 2048 131071 129024 63M b W95 FAT32
$ sudo losetup -o $((2048*512)) --find --show ./test.img
$ sudo gdbserver :1234 src/fsck.fat -v /dev/loop42

launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/src/fsck.fat",
"miDebuggerServerAddress": "localhost:1234",
"cwd": "${workspaceRoot}",
"externalConsole": true,
"linux": {
"MIMode": "gdb"
},
}
]
}

Reference: Mount single partition from image of entire disk (device)

fatcat

https://github.com/Gregwar/fatcat

Reading image

1
2
3
4
5
6
7
8
9
10
11
$ fdisk -l file.img
Device Boot Start End Sectors Size Id Type
file.img1 2048 131071 129024 63M b W95 FAT32
$ ./fatcat file.img -O $((512*2048)) -l /
Listing path /
Directory cluster: 2
f 2/11/2023 18:36:00 gawrgura.gz (GAWRGURA.GZ) c=4 s=67 (67B)
f 2/11/2023 18:35:28 shirakamifubuki.tar (SHIRAK~1.TAR) c=0 s=0 (0B)
d 2/11/2023 18:36:38 hololive/ (HOLOLIVE) c=3
f 2/11/2023 18:35:44 komica.gz (KOMICA.GZ) c=0 s=0 (0B)

Debug with vscode

1
2
3
mkdir debug && cd debug
cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build .

launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug/fatcat",
"args": ["${workspaceFolder}/debug/file.img", "-O", "1048576", "-l", "/"],
"cwd": "${workspaceFolder}"
}
]
}

MBR&FAT32解析

只有解析到 partition boot record


Requirement 1

In this requirement, you need to mount the FAT32 file system in the SD card. You could set the FAT32 file system as the root file system if you didn’t implement mount in lab7.

Required 1-1 Get the FAT32 partition

You should know the location of the FAT32 file system in the SD card first before mounting it.

The SD card should already be formatted by MBR. You can parse it to get each partition’s type, size, and block index.

不使用 sfn_nctuos.img 進行測試,而是使用 dd 建立檔案再用 garpted 劃分出 fat32 partition + MBR。

在測試前會先掛載創立若干文字檔,因為希望裏面有些 Long filename 檔案。

https://wiki.osdev.org/FAT

FAT 32 was introduced to us by Windows95-B and Windows98. FAT32 solved some of FAT’s problems. No more 64K max clusters! Although FAT32 uses 32 bits per FAT entry, only the bottom 28 bits are actually used to address clusters on the disk (top 4 bits are reserved).

1
2
3
4
#/bin/bash
dd if=/dev/zero of=file.img bs=1024k count=64
sudo mount -t vfat -o loop,offset=1048576,sizelimit=66060288 file.img /mnt/tmp
sudo umount /mnt/tmp