리λ μ€ μ»€λ μλΈμμ€ν - κ°μ νμΌ μμ€ν (Virtual File System)
Parent Content: 리λ μ€ OSμ μλΈμμ€ν (λ Όλ¬Έ: Conceptual Architecture of the Linux Kernel)
κ°μ νμΌ μμ€ν (VFS-Virtual File System)μ 리λ μ€ μ»€λμ 5κ°μ§ μ£Όμ μλΈμμ€ν μ€ νλλ‘, λͺ¨λ λλ°μ΄μ€μ κ³΅ν΅ νμΌ μΈν°νμ΄μ€(open, stat, read, write, chmod λ±)λ₯Ό μ 곡νμ¬ λ€μν νλμ¨μ΄ λλ°μ΄μ€μ μΈλΆ μ 보λ₯Ό μΆμννλ©° ν΄λΌμ΄μΈνΈ μμ© νλ‘κ·Έλ¨μ΄ μ¬λ¬ νμΌ μμ€ν μ κ°μ λ°©λ²μΌλ‘ μ κ·Όν μ μκ² ν΄ μ£Όλ μννΈμ¨μ΄ λ μ΄μ΄μ λλ€: [1]
μλ₯Ό λ€μ΄ νμΌμ μ¬λ open() ν¨μκ° μ€μ νμΌ μμ€ν μ ν(μ:ext2, ext4...)μ μκ΄μμ΄ λμΌνκ² μλν μ μλ μ΄μ λ μμ²μ΄ VFS λ μ΄μ΄λ₯Ό κ±°μ³ μ€μ νμΌ μμ€ν μ μ λ¬λκΈ° λλ¬Έμ λλ€. VFSλ κ° νμΌ μμ€ν μ νμ λν ν¨μ ν¬μΈν° ν μ΄λΈμ κ°μ§κ³ μμΌλ©°, μ΄ ν μ΄λΈμ ν΄λΉ νμΌ μμ€ν μ λμμ μ μνλ ν¨μλ₯Ό κ°λ¦¬ν€λ ν¬μΈν°λ‘ ꡬμ±λμ΄ μμ΅λλ€.
μλ₯Ό λ€μ΄ ext4 νμΌ μμ€ν μ΄ VFSμ λ±λ‘λ λ, ν΄λΉ νμΌ μμ€ν μ νμ λν open() ν¨μμ κ°μ λμμ μννλ ν¨μ ν¬μΈν°κ° VFS ν μ΄λΈμ μΆκ°λ©λλ€. λ°λΌμ ν΄λΉ ν¨μλ€μ κ°κ°μ νμΌ μμ€ν μ νλ³λ‘ νμΌ μμ€ν λͺ¨λ λ΄μ ν¨μ λμμ΄ ν¬ν¨λ κ΅¬μ‘°μ²΄κ° κ΅¬νλμ΄ μμ΄μΌ ν©λλ€. open() ν¨μκ° νΈμΆλ λ, VFSλ ν΄λΉ νμΌ μμ€ν μ ν¨μ ν¬μΈν°λ₯Ό μ¬μ©νμ¬ μ€μ λμμ μνν©λλ€.
Figure 1.1: [4]
μμ½νλ©΄, κ° νμΌ μμ€ν λ΄μλ VFSμ μνΈμμ© νκΈ° μν ν¨μμ κ΅¬μ‘°μ²΄κ° κ΅¬νλμ΄ μμΌλ©°, μ΄λ¬ν ν¨μλ€μ VFS ν μ΄λΈμ λ±λ‘λμ΄ VFSκ° νμΌ μμ€ν μ νμ μΈμνκ³ ν΄λΉ ν¨μλ₯Ό νΈμΆν μ μκ² λ©λλ€. VFSλ μ΄λ° λ°©μμΌλ‘ λ€μν νμΌ μμ€ν μ νμ μ§μνκ³ , κ° νμΌ μμ€ν μ ν΄λΉ λμμ μ²λ¦¬νκΈ° μν ν¨μλ₯Ό μ체 νμΌ μμ€ν λͺ¨λμ ꡬνν©λλ€. μ΄λ‘ μΈν΄ νμΌ μμ€ν μ μ²λ¦¬λ₯Ό μμ© νλ‘κ·Έλ¨μ λ ΈμΆμν€μ§ μμ μ μμΌλ©°, μ΄λ 리λ μ€ μμ€ν μ λ³΄λ€ μ μ°νκ² λ§λ€μ΄μ£Όκ³ νμ₯μ±μ μ 곡ν©λλ€.
λ μ½μ΄λ³΄κΈ°: VFS Operations - File Open
- Given a file path, an open mode and a file permissions mask
Entry function: sys_open, in fs/open.c.
Underlying open function: filp_open, in fs/open.c.
- Allocate a free file descriptor.
- Try to open the file.
- On success, put the new 'struct file' in the fd table of the process.
- On error, free the allocated file descriptor.
Actually Opening The File
Entry function: open_namei, in fs/namei.c.
Case (1): Create modeκ° μλ μνμμ νμΌ open (file already exists, no O_CREAT flag given)
If not opening in create mode (no O_CREAT flag given):
lookup the file via the dentry cache (path_lookup -> path_walk -> link_path_walk).
Case (2): Create mode μνμμ νμΌ open(open with create)
Otherwise (it is an open in create mode):
Lookup the parent directory (path_lookup again). If it does not exist, or is not a directory - fail the operation.
Lookup the file in the parent directory. If it does not exist, create it (see vfs_create later on).
Handle special cases (flags mismatch, file is a directory, file is a link...).
Case (1),(2) 곡ν΅μ¬ν:
Perform sanity checks.
Check permissions.
Check various mode limitations (e.g. file is read-only and trying to open for write, trying to open a device file on a 'no_dev' file-system mount, etc.).
Handle truncation.
References:
[1] https://ko.wikipedia.org/wiki/%EA%B0%80%EC%83%81_%ED%8C%8C%EC%9D%BC_%EC%8B%9C%EC%8A%A4%ED%85%9C
[3] https://www.kernel.org/doc/html/latest/filesystems/vfs.html
[4] http://haifux.org/lectures/119/linux-2.4-vfs/linux-2.4-vfs.html