← Semua pelajaran
Cara kerja file system
Simpan sebuah file
Aplikasi
Direktori
Inode
Disk
save.c
int fd = open("/home/ana/draft.txt", O_CREAT | O_WRONLY, 0644);write(fd, text, len);close(fd); fd = open("/home/ana/draft.txt", O_RDONLY);read(fd, buf, sizeof buf);fs/write.c
// open() dengan O_CREAT: namanya belum adastruct inode *dir = namei("/home/ana");if (!dir_find(dir, "draft.txt")) { ino = alloc_inode(); // 43 dir_add(dir, "draft.txt", ino->num);} // write(): tiap 16 byte masuk ke satu blok kosongfor (size_t i = 0; i < len; i += BLOCK) { int b = alloc_block(); disk_write(b, text + i, BLOCK); ino->blocks[ino->nblocks++] = b;}ino->size = len;Direktori itu tabel berisi nama dan nomor inode
Diawali / berarti dari root, selain itu dari /home/ana
Atau coba salah satu
Lookup
Inode tujuannya
Nama file tinggal di direktori, bukan di inode. Dua baris bisa menunjuk ke satu inode: itulah hard link
fs/namei.c
struct inode *namei(const char *path) { // path absolut mulai dari /, sisanya dari working directory struct inode *ino = path[0] == '/' ? root : cwd; const char *name; while ((name = next_part(&path))) { if (!S_ISDIR(ino->mode)) return ERR(-ENOTDIR); int num = dir_find(ino, name); // satu lookup tabel per nama if (!num) return ERR(-ENOENT); ino = iget(num); } return ino;}Fragmentasi saat ruang kosongnya penuh lubang
Disk 32 blok
kosong
deret terpanjang
File
SSD tidak punya head yang harus bergerak, jadi potongan jauh lebih murah baginya dibanding hard disk
alloc.js
function allocate(disk, n) { // first fit: satu deret yang memuat seluruh file const run = freeRuns(disk).find((r) => r.length >= n); if (run) return run.slice(0, n); if (freeCount(disk) < n) throw new Error("ENOSPC"); // tidak ada yang muat, ambil blok kosong terendah return freeBlocks(disk).slice(0, n);} function remove(disk, file) { // datanya tetap di sana, cuma ditandai kosong for (const b of file.blocks) disk[b] = null;} function defragment(disk, files) { let next = 0; // tulis ulang tiap file jadi satu deret for (const f of byFirstBlock(files)) { f.blocks = move(disk, f.blocks, next); next += f.blocks.length; }}Topik
Berikutnya: Cara kerja cache CPU
Buka pelajaran