Serenity Operating System
at master 70 lines 4.7 kB view raw view rendered
1# `RAMFS` filesystem and its purposes 2 3`RAMFS` is a RAM-backed filesystem. It is used to hold files and directories in the `/tmp` directory and 4device nodes in the `/dev` directory. 5 6## What are the `RAMFS` filesystem characteristics? 7 8`RAMFS` is a pure RAM-backed filesystem, which means all files and directories 9actually live in memory, each in its own `RAMFS` instance in the kernel. 10 11The `RAMFS` in its current design is very conservative about allocating virtual memory ranges 12for itself, and instead it uses the `AnonymousVMObject` object to hold physical pages containing 13data for its inodes. When doing actual IO, the `RAMFS` code temporarily allocates a small virtual memory 14`Memory::Region` to perform the task, which works quite well although it puts a strain on the virtual memory 15mapping code. The current design also ensures that fabricated huge files can be easily created in the filesystem 16with very small overhead until actual IO is performed. 17 18### The `/tmp` directory and its purposes 19 20Currently, the `/tmp` directory is the **place** for facilitating the inter-process 21communication layer, with many Unix sockets nodes being present in the directory. 22 23Many test suites in the project leverage the `/tmp` for placing their test files 24when trying to check the correctness of many system-related functionality. 25Other programs rely on `/tmp` for placing their temporary files to properly function. 26 27### Why does the `RAMFS` work well for the `/dev` directory? 28 29To understand why `RAMFS` works reliably when mounted on `/dev`, we must understand 30first what we did in the past and how `RAMFS` solves many of the issues with the previous design. 31 32At first, we didn't have any special filesystem mounted in `/dev` as the image build 33script generated all the required device nodes in `/dev`. This was quite sufficient in 34the early days of the project, where hardware support was extremely limited and of course 35hotplugging any kind of hardware was not even a consideration. 36 37As the project grew larger and more hardware support was introduced, it became obvious 38that this "solution" was not future-proof. For example, if one user has two SATA drives 39connected to his computer, and another user has just one old IDE drive being used, 40then how should we support both cases? The answer was that each user could simply invoke 41the `mknod` utility to create device nodes. This solution meant that user interaction as well 42as a deep understanding of kernel internals was required to achieve a proper setup. 43 44When it became apparent that another solution was needed, the `DevFS` filesystem was 45invented. The idea was plain simple - the `DevFS` is a read-only filesystem that only 46lists all present char and block devices. Permissions were hardcoded at known value, 47and modifying the filesystem (including adding subdirectories) was strictly prohibited. 48This solution was efficient in the sense of ensuring minimal user interaction for using 49device nodes in `/dev`. The shortcomings were strictly immutable filesystem layout and hardcoded 50permissions. Also, the filesystem implementation was specific to `/dev`, because no other 51mount in the system used this special filesystem, which meant it needed special test cases, etc. 52 53The `DevFS` solution was short-lived, and was quickly replaced by the `DevTmpFS` solution. 54That new shiny filesystem was again specific to `/dev`, but it solved many of the issues 55`DevFS` suffered from - no more hardcoded permissions and now the design has flexible filesystem 56layout in its mindset. 57This was achieved by implementing from scratch a filesystem that resembles the `RAMFS` 58filesystem, but was different in one major aspect - only device nodes and directories are allowed 59to be in `/dev`. This strict requirement has been mandated to ensure the user doesn't 60accidentally put unrelated files in `/dev`. When the `DevTmpFS` was invented, it clearly 61needed userspace cooperation to create device nodes in `/dev`, so `SystemServer` was modified 62to create those during boot. The process of how `SystemServer` does that is not discussed 63in this document, but ultimately evolved to be flexible enough to work quite well. 64 65Everything worked quite well, but there was still a prominent problem with `DevTmpFS` - 66it was an entire filesystem solution just for `/dev` and nobody else used it. 67Testing the filesystem was quite clunky and truthfully lacking from the beginning until its removal. 68To solve this problem, it was decided to stop using it, and instead just use `RAMFS`. 69To ensure the current behavior of disallowing regular files in `/dev`, a new mount flag called 70`MS_NOREGULAR` was invented, so it could be mounted with it.