123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- #include "VirtualFileSystem.h"
- #include "FileDescriptor.h"
- #include "FileSystem.h"
- #include <AK/FileSystemPath.h>
- #include <AK/StringBuilder.h>
- #include <AK/kmalloc.h>
- #include <AK/kstdio.h>
- #include <AK/ktime.h>
- #include "CharacterDevice.h"
- #include <LibC/errno_numbers.h>
- //#define VFS_DEBUG
- static VFS* s_the;
- VFS& VFS::the()
- {
- ASSERT(s_the);
- return *s_the;
- }
- void VFS::initialize_globals()
- {
- s_the = nullptr;
- FS::initialize_globals();
- }
- VFS::VFS()
- {
- #ifdef VFS_DEBUG
- kprintf("VFS: Constructing VFS\n");
- #endif
- s_the = this;
- m_max_vnode_count = 16;
- m_nodes = reinterpret_cast<Vnode*>(kmalloc(sizeof(Vnode) * max_vnode_count()));
- memset(m_nodes, 0, sizeof(Vnode) * max_vnode_count());
- for (unsigned i = 0; i < m_max_vnode_count; ++i)
- m_vnode_freelist.append(&m_nodes[i]);
- }
- VFS::~VFS()
- {
- kprintf("VFS: ~VirtualFileSystem with %u nodes allocated\n", allocated_vnode_count());
- // FIXME: m_nodes is never freed. Does it matter though?
- }
- auto VFS::makeNode(InodeIdentifier inode) -> RetainPtr<Vnode>
- {
- auto core_inode = inode.fs()->get_inode(inode);
- if (!core_inode)
- return nullptr;
- auto metadata = core_inode->metadata();
- InterruptDisabler disabler;
- CharacterDevice* characterDevice = nullptr;
- if (metadata.isCharacterDevice()) {
- auto it = m_character_devices.find(encodedDevice(metadata.majorDevice, metadata.minorDevice));
- if (it != m_character_devices.end()) {
- characterDevice = (*it).value;
- } else {
- kprintf("VFS: makeNode() no such character device %u,%u\n", metadata.majorDevice, metadata.minorDevice);
- return nullptr;
- }
- }
- auto vnode = allocateNode();
- ASSERT(vnode);
- FS* fileSystem = inode.fs();
- fileSystem->retain();
- vnode->inode = inode;
- vnode->m_core_inode = move(core_inode);
- #ifdef VFS_DEBUG
- kprintf("makeNode: inode=%u, size=%u, mode=%o, uid=%u, gid=%u\n", inode.index(), metadata.size, metadata.mode, metadata.uid, metadata.gid);
- #endif
- m_inode2vnode.set(inode, vnode.ptr());
- vnode->m_characterDevice = characterDevice;
- return vnode;
- }
- auto VFS::makeNode(CharacterDevice& device) -> RetainPtr<Vnode>
- {
- InterruptDisabler disabler;
- auto vnode = allocateNode();
- ASSERT(vnode);
- #ifdef VFS_DEBUG
- kprintf("makeNode: device=%p (%u,%u)\n", &device, device.major(), device.minor());
- #endif
- m_device2vnode.set(encodedDevice(device.major(), device.minor()), vnode.ptr());
- vnode->m_characterDevice = &device;
- return vnode;
- }
- auto VFS::get_or_create_node(InodeIdentifier inode) -> RetainPtr<Vnode>
- {
- {
- InterruptDisabler disabler;
- auto it = m_inode2vnode.find(inode);
- if (it != m_inode2vnode.end())
- return (*it).value;
- }
- return makeNode(inode);
- }
- auto VFS::get_or_create_node(CharacterDevice& device) -> RetainPtr<Vnode>
- {
- {
- InterruptDisabler disabler;
- auto it = m_device2vnode.find(encodedDevice(device.major(), device.minor()));
- if (it != m_device2vnode.end())
- return (*it).value;
- }
- return makeNode(device);
- }
- InodeIdentifier VFS::root_inode_id() const
- {
- return m_root_vnode->inode;
- }
- bool VFS::mount(RetainPtr<FS>&& fileSystem, const String& path)
- {
- ASSERT(fileSystem);
- int error;
- auto inode = resolve_path(path, root_inode_id(), error);
- if (!inode.is_valid()) {
- kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
- return false;
- }
- kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", fileSystem->class_name(), fileSystem.ptr(), path.characters(), inode.index());
- // FIXME: check that this is not already a mount point
- auto mount = make<Mount>(inode, move(fileSystem));
- m_mounts.append(move(mount));
- return true;
- }
- bool VFS::mount_root(RetainPtr<FS>&& fileSystem)
- {
- if (m_root_vnode) {
- kprintf("VFS: mount_root can't mount another root\n");
- return false;
- }
- auto mount = make<Mount>(InodeIdentifier(), move(fileSystem));
- auto node = makeNode(mount->guest());
- if (!node->inUse()) {
- kprintf("VFS: root inode for / is not in use :(\n");
- return false;
- }
- if (!node->metadata().isDirectory()) {
- kprintf("VFS: root inode for / is not a directory :(\n");
- return false;
- }
- m_root_vnode = move(node);
- kprintf("VFS: mounted root on %s{%p}\n",
- m_root_vnode->fs()->class_name(),
- m_root_vnode->fs());
- m_mounts.append(move(mount));
- return true;
- }
- auto VFS::allocateNode() -> RetainPtr<Vnode>
- {
- if (m_vnode_freelist.is_empty()) {
- kprintf("VFS: allocateNode has no nodes left\n");
- return nullptr;
- }
- auto* node = m_vnode_freelist.takeLast();
- ASSERT(node->retainCount == 0);
- node->retainCount = 1;
- node->m_vfs = this;
- node->m_vmo = nullptr;
- return adopt(*node);
- }
- void VFS::freeNode(Vnode* node)
- {
- InterruptDisabler disabler;
- ASSERT(node);
- ASSERT(node->inUse());
- if (node->inode.is_valid()) {
- m_inode2vnode.remove(node->inode);
- node->inode.fs()->release();
- node->inode = InodeIdentifier();
- node->m_core_inode = nullptr;
- }
- if (node->m_characterDevice) {
- m_device2vnode.remove(encodedDevice(node->m_characterDevice->major(), node->m_characterDevice->minor()));
- node->m_characterDevice = nullptr;
- }
- node->m_vfs = nullptr;
- node->m_vmo = nullptr;
- m_vnode_freelist.append(move(node));
- }
- auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
- {
- for (auto& mount : m_mounts) {
- if (mount->host() == inode)
- return mount.ptr();
- }
- return nullptr;
- }
- auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
- {
- for (auto& mount : m_mounts) {
- if (mount->guest() == inode)
- return mount.ptr();
- }
- return nullptr;
- }
- bool VFS::is_vfs_root(InodeIdentifier inode) const
- {
- return inode == m_root_vnode->inode;
- }
- void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
- {
- dir_inode.traverse_as_directory([&] (const FS::DirectoryEntry& entry) {
- InodeIdentifier resolvedInode;
- if (auto mount = find_mount_for_host(entry.inode))
- resolvedInode = mount->guest();
- else
- resolvedInode = entry.inode;
- if (dir_inode.identifier().is_root_inode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
- auto mount = find_mount_for_guest(entry.inode);
- ASSERT(mount);
- resolvedInode = mount->host();
- }
- callback(FS::DirectoryEntry(entry.name, entry.name_length, resolvedInode, entry.fileType));
- return true;
- });
- }
- RetainPtr<FileDescriptor> VFS::open(CharacterDevice& device, int options)
- {
- // FIXME: Respect options.
- (void) options;
- auto vnode = get_or_create_node(device);
- if (!vnode)
- return nullptr;
- return FileDescriptor::create(move(vnode));
- }
- RetainPtr<FileDescriptor> VFS::open(const String& path, int& error, int options, InodeIdentifier base)
- {
- auto inode = resolve_path(path, base, error, options);
- if (!inode.is_valid())
- return nullptr;
- auto vnode = get_or_create_node(inode);
- if (!vnode)
- return nullptr;
- return FileDescriptor::create(move(vnode));
- }
- RetainPtr<FileDescriptor> VFS::create(const String& path, InodeIdentifier base, int& error)
- {
- // FIXME: Do the real thing, not just this fake thing!
- (void) path;
- (void) base;
- m_root_vnode->fs()->create_inode(m_root_vnode->fs()->root_inode(), "empty", 0100644, 0, error);
- return nullptr;
- }
- bool VFS::mkdir(const String& path, mode_t mode, InodeIdentifier base, int& error)
- {
- error = -EWHYTHO;
- // FIXME: This won't work nicely across mount boundaries.
- FileSystemPath p(path);
- if (!p.is_valid()) {
- error = -EINVAL;
- return false;
- }
- InodeIdentifier parent_dir;
- auto existing_dir = resolve_path(path, base, error, 0, &parent_dir);
- if (existing_dir.is_valid()) {
- error = -EEXIST;
- return false;
- }
- if (!parent_dir.is_valid()) {
- error = -ENOENT;
- return false;
- }
- if (error != -ENOENT) {
- return false;
- }
- dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
- auto new_dir = base.fs()->create_directory(parent_dir, p.basename(), mode, error);
- if (new_dir) {
- error = 0;
- return true;
- }
- return false;
- }
- InodeIdentifier VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode, int& error)
- {
- auto symlink_contents = symlink_inode.read_entire();
- if (!symlink_contents)
- return { };
- auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.size());
- #ifdef VFS_DEBUG
- kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
- #endif
- return resolve_path(linkee, base, error);
- }
- RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
- {
- if (!inode_id.is_valid())
- return nullptr;
- return inode_id.fs()->get_inode(inode_id);
- }
- String VFS::absolute_path(Inode& core_inode)
- {
- int error;
- Vector<InodeIdentifier> lineage;
- RetainPtr<Inode> inode = &core_inode;
- while (inode->identifier() != m_root_vnode->inode) {
- if (auto* mount = find_mount_for_guest(inode->identifier()))
- lineage.append(mount->host());
- else
- lineage.append(inode->identifier());
- InodeIdentifier parent_id;
- if (inode->is_directory()) {
- parent_id = resolve_path("..", inode->identifier(), error);
- } else {
- parent_id = inode->fs().find_parent_of_inode(inode->identifier());
- }
- ASSERT(parent_id.is_valid());
- inode = get_inode(parent_id);
- }
- if (lineage.is_empty())
- return "/";
- lineage.append(m_root_vnode->inode);
- StringBuilder builder;
- for (size_t i = lineage.size() - 1; i >= 1; --i) {
- auto& child = lineage[i - 1];
- auto parent = lineage[i];
- if (auto* mount = find_mount_for_host(parent))
- parent = mount->guest();
- builder.append('/');
- auto parent_inode = get_inode(parent);
- builder.append(parent_inode->reverse_lookup(child));
- }
- return builder.build();
- }
- InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* deepest_dir)
- {
- if (path.is_empty()) {
- error = -EINVAL;
- return { };
- }
- auto parts = path.split('/');
- InodeIdentifier crumb_id;
- if (path[0] == '/')
- crumb_id = m_root_vnode->inode;
- else
- crumb_id = base.is_valid() ? base : m_root_vnode->inode;
- if (deepest_dir)
- *deepest_dir = crumb_id;
- for (unsigned i = 0; i < parts.size(); ++i) {
- bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
- auto& part = parts[i];
- if (part.is_empty())
- break;
- auto crumb_inode = get_inode(crumb_id);
- if (!crumb_inode) {
- #ifdef VFS_DEBUG
- kprintf("invalid metadata\n");
- #endif
- error = -EIO;
- return { };
- }
- auto metadata = crumb_inode->metadata();
- if (!metadata.isDirectory()) {
- #ifdef VFS_DEBUG
- kprintf("parent of <%s> not directory, it's inode %u:%u / %u:%u, mode: %u, size: %u\n", part.characters(), inode.fsid(), inode.index(), metadata.inode.fsid(), metadata.inode.index(), metadata.mode, metadata.size);
- #endif
- error = -ENOTDIR;
- return { };
- }
- auto parent = crumb_id;
- crumb_id = crumb_inode->lookup(part);
- if (!crumb_id.is_valid()) {
- #ifdef VFS_DEBUG
- kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
- #endif
- error = -ENOENT;
- return { };
- }
- #ifdef VFS_DEBUG
- kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
- #endif
- if (auto mount = find_mount_for_host(crumb_id)) {
- #ifdef VFS_DEBUG
- kprintf(" -- is host\n");
- #endif
- crumb_id = mount->guest();
- }
- if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
- #ifdef VFS_DEBUG
- kprintf(" -- is guest\n");
- #endif
- auto mount = find_mount_for_guest(crumb_id);
- auto dir_inode = get_inode(mount->host());
- crumb_id = dir_inode->lookup("..");
- }
- crumb_inode = get_inode(crumb_id);
- metadata = crumb_inode->metadata();
- if (metadata.isDirectory()) {
- if (deepest_dir)
- *deepest_dir = crumb_id;
- }
- if (metadata.isSymbolicLink()) {
- if (i == parts.size() - 1) {
- if (options & O_NOFOLLOW) {
- error = -ELOOP;
- return { };
- }
- if (options & O_NOFOLLOW_NOERROR)
- return crumb_id;
- }
- crumb_id = resolve_symbolic_link(parent, *crumb_inode, error);
- if (!crumb_id.is_valid()) {
- kprintf("Symbolic link resolution failed :(\n");
- return { };
- }
- }
- }
- return crumb_id;
- }
- void Vnode::retain()
- {
- InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
- ++retainCount;
- }
- void Vnode::release()
- {
- InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
- ASSERT(retainCount);
- if (--retainCount == 0) {
- m_vfs->freeNode(this);
- }
- }
- InodeMetadata Vnode::metadata() const
- {
- if (m_core_inode)
- return m_core_inode->metadata();
- ASSERT_NOT_REACHED();
- return { };
- }
- VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
- : m_host(host)
- , m_guest(guest_fs->root_inode())
- , m_guest_fs(move(guest_fs))
- {
- }
- void VFS::register_character_device(CharacterDevice& device)
- {
- m_character_devices.set(encodedDevice(device.major(), device.minor()), &device);
- }
- void VFS::for_each_mount(Function<void(const Mount&)> callback) const
- {
- for (auto& mount : m_mounts) {
- callback(*mount);
- }
- }
- void VFS::sync()
- {
- FS::sync();
- }
|