2018-10-10 11:53:07 +02:00
|
|
|
#include "VirtualFileSystem.h"
|
2018-11-07 11:37:54 +01:00
|
|
|
#include "FileDescriptor.h"
|
2018-10-10 11:53:07 +02:00
|
|
|
#include "FileSystem.h"
|
2018-11-18 14:57:41 +01:00
|
|
|
#include <AK/FileSystemPath.h>
|
2018-10-28 12:20:25 +01:00
|
|
|
#include <AK/StringBuilder.h>
|
2018-10-13 00:44:54 +02:00
|
|
|
#include <AK/kmalloc.h>
|
2018-10-17 10:55:43 +02:00
|
|
|
#include <AK/kstdio.h>
|
|
|
|
#include <AK/ktime.h>
|
2018-10-30 13:59:29 +01:00
|
|
|
#include "CharacterDevice.h"
|
2018-11-07 21:19:47 +01:00
|
|
|
#include <LibC/errno_numbers.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
//#define VFS_DEBUG
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
static VFS* s_the;
|
2018-10-18 10:27:07 +02:00
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
VFS& VFS::the()
|
2018-10-18 10:27:07 +02:00
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:10:12 +01:00
|
|
|
void VFS::initialize_globals()
|
2018-10-22 12:54:19 +02:00
|
|
|
{
|
|
|
|
s_the = nullptr;
|
2018-11-15 17:13:10 +01:00
|
|
|
FS::initializeGlobals();
|
2018-10-22 12:54:19 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
VFS::VFS()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-10-22 11:15:16 +02:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("VFS: Constructing VFS\n");
|
2018-10-22 11:15:16 +02:00
|
|
|
#endif
|
2018-10-18 10:27:07 +02:00
|
|
|
s_the = this;
|
2018-11-15 15:10:12 +01:00
|
|
|
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());
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-11-15 15:10:12 +01:00
|
|
|
for (unsigned i = 0; i < m_max_vnode_count; ++i)
|
|
|
|
m_vnode_freelist.append(&m_nodes[i]);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
VFS::~VFS()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-15 15:10:12 +01:00
|
|
|
kprintf("VFS: ~VirtualFileSystem with %u nodes allocated\n", allocated_vnode_count());
|
2018-10-26 17:42:12 +02:00
|
|
|
// FIXME: m_nodes is never freed. Does it matter though?
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
auto VFS::makeNode(InodeIdentifier inode) -> RetainPtr<Vnode>
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
auto metadata = inode.metadata();
|
|
|
|
if (!metadata.isValid())
|
|
|
|
return nullptr;
|
|
|
|
|
2018-11-13 13:02:39 +01:00
|
|
|
auto core_inode = inode.fileSystem()->get_inode(inode);
|
2018-11-13 23:44:54 +01:00
|
|
|
if (core_inode)
|
|
|
|
core_inode->m_metadata = metadata;
|
2018-11-13 13:02:39 +01:00
|
|
|
|
2018-10-31 21:31:56 +01:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
|
2018-10-14 02:59:22 +02:00
|
|
|
CharacterDevice* characterDevice = nullptr;
|
|
|
|
if (metadata.isCharacterDevice()) {
|
2018-11-15 15:10:12 +01:00
|
|
|
auto it = m_character_devices.find(encodedDevice(metadata.majorDevice, metadata.minorDevice));
|
|
|
|
if (it != m_character_devices.end()) {
|
2018-10-14 02:59:22 +02:00
|
|
|
characterDevice = (*it).value;
|
|
|
|
} else {
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("VFS: makeNode() no such character device %u,%u\n", metadata.majorDevice, metadata.minorDevice);
|
2018-10-14 02:59:22 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
auto vnode = allocateNode();
|
|
|
|
ASSERT(vnode);
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
FS* fileSystem = inode.fileSystem();
|
2018-10-10 11:53:07 +02:00
|
|
|
fileSystem->retain();
|
|
|
|
|
|
|
|
vnode->inode = inode;
|
2018-11-13 13:02:39 +01:00
|
|
|
vnode->m_core_inode = move(core_inode);
|
2018-11-13 13:32:16 +01:00
|
|
|
vnode->m_cachedMetadata = metadata;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("makeNode: inode=%u, size=%u, mode=%o, uid=%u, gid=%u\n", inode.index(), metadata.size, metadata.mode, metadata.uid, metadata.gid);
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
m_inode2vnode.set(inode, vnode.ptr());
|
2018-10-14 02:59:22 +02:00
|
|
|
vnode->m_characterDevice = characterDevice;
|
2018-10-24 12:43:52 +02:00
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
return vnode;
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
auto VFS::makeNode(CharacterDevice& device) -> RetainPtr<Vnode>
|
2018-10-30 15:33:37 +01:00
|
|
|
{
|
2018-10-31 21:31:56 +01:00
|
|
|
InterruptDisabler disabler;
|
2018-10-30 15:33:37 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:35 +01:00
|
|
|
auto VFS::get_or_create_node(InodeIdentifier inode) -> RetainPtr<Vnode>
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-10-31 21:31:56 +01:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto it = m_inode2vnode.find(inode);
|
|
|
|
if (it != m_inode2vnode.end())
|
|
|
|
return (*it).value;
|
|
|
|
}
|
2018-10-10 11:53:07 +02:00
|
|
|
return makeNode(inode);
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:35 +01:00
|
|
|
auto VFS::get_or_create_node(CharacterDevice& device) -> RetainPtr<Vnode>
|
2018-10-30 15:33:37 +01:00
|
|
|
{
|
2018-10-31 21:31:56 +01:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto it = m_device2vnode.find(encodedDevice(device.major(), device.minor()));
|
|
|
|
if (it != m_device2vnode.end())
|
|
|
|
return (*it).value;
|
|
|
|
}
|
2018-10-30 15:33:37 +01:00
|
|
|
return makeNode(device);
|
|
|
|
}
|
|
|
|
|
2018-11-18 23:28:43 +01:00
|
|
|
InodeIdentifier VFS::root_inode_id() const
|
|
|
|
{
|
|
|
|
return m_root_vnode->inode;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
bool VFS::mount(RetainPtr<FS>&& fileSystem, const String& path)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
ASSERT(fileSystem);
|
2018-10-28 14:11:51 +01:00
|
|
|
int error;
|
2018-11-18 23:28:43 +01:00
|
|
|
auto inode = resolve_path(path, root_inode_id(), error);
|
2018-10-10 11:53:07 +02:00
|
|
|
if (!inode.isValid()) {
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:35 +01:00
|
|
|
kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", fileSystem->class_name(), fileSystem.ptr(), path.characters(), inode.index());
|
2018-10-10 11:53:07 +02:00
|
|
|
// FIXME: check that this is not already a mount point
|
2018-10-17 11:40:58 +02:00
|
|
|
auto mount = make<Mount>(inode, move(fileSystem));
|
|
|
|
m_mounts.append(move(mount));
|
2018-10-10 11:53:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
bool VFS::mount_root(RetainPtr<FS>&& fileSystem)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-15 15:10:12 +01:00
|
|
|
if (m_root_vnode) {
|
|
|
|
kprintf("VFS: mount_root can't mount another root\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-17 11:40:58 +02:00
|
|
|
auto mount = make<Mount>(InodeIdentifier(), move(fileSystem));
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
auto node = makeNode(mount->guest());
|
|
|
|
if (!node->inUse()) {
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("VFS: root inode for / is not in use :(\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-13 13:32:16 +01:00
|
|
|
if (!node->metadata().isDirectory()) {
|
2018-10-31 21:31:56 +01:00
|
|
|
kprintf("VFS: root inode for / is not a directory :(\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:10:12 +01:00
|
|
|
m_root_vnode = move(node);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("VFS: mounted root on %s{%p}\n",
|
2018-11-15 15:36:35 +01:00
|
|
|
m_root_vnode->fileSystem()->class_name(),
|
2018-11-15 15:10:12 +01:00
|
|
|
m_root_vnode->fileSystem());
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-17 11:40:58 +02:00
|
|
|
m_mounts.append(move(mount));
|
2018-10-10 11:53:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
auto VFS::allocateNode() -> RetainPtr<Vnode>
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-15 15:10:12 +01:00
|
|
|
if (m_vnode_freelist.isEmpty()) {
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("VFS: allocateNode has no nodes left\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-15 15:10:12 +01:00
|
|
|
auto* node = m_vnode_freelist.takeLast();
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(node->retainCount == 0);
|
|
|
|
node->retainCount = 1;
|
2018-10-24 12:43:52 +02:00
|
|
|
node->m_vfs = this;
|
2018-11-08 15:39:26 +01:00
|
|
|
node->m_vmo = nullptr;
|
2018-10-10 11:53:07 +02:00
|
|
|
return adopt(*node);
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
void VFS::freeNode(Vnode* node)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-10-31 21:31:56 +01:00
|
|
|
InterruptDisabler disabler;
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(node);
|
|
|
|
ASSERT(node->inUse());
|
2018-10-30 15:33:37 +01:00
|
|
|
if (node->inode.isValid()) {
|
|
|
|
m_inode2vnode.remove(node->inode);
|
|
|
|
node->inode.fileSystem()->release();
|
|
|
|
node->inode = InodeIdentifier();
|
|
|
|
}
|
|
|
|
if (node->m_characterDevice) {
|
|
|
|
m_device2vnode.remove(encodedDevice(node->m_characterDevice->major(), node->m_characterDevice->minor()));
|
|
|
|
node->m_characterDevice = nullptr;
|
|
|
|
}
|
2018-11-08 15:39:26 +01:00
|
|
|
node->m_vfs = nullptr;
|
|
|
|
node->m_vmo = nullptr;
|
2018-11-15 15:10:12 +01:00
|
|
|
m_vnode_freelist.append(move(node));
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:10:12 +01:00
|
|
|
auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
|
|
|
if (mount->host() == inode)
|
|
|
|
return mount.ptr();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:10:12 +01:00
|
|
|
auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
|
|
|
if (mount->guest() == inode)
|
|
|
|
return mount.ptr();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
bool VFS::is_vfs_root(InodeIdentifier inode) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-15 15:10:12 +01:00
|
|
|
return inode == m_root_vnode->inode;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
void VFS::traverse_directory_inode(CoreInode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-15 17:13:10 +01:00
|
|
|
dir_inode.traverse_as_directory([&] (const FS::DirectoryEntry& entry) {
|
2018-10-10 11:53:07 +02:00
|
|
|
InodeIdentifier resolvedInode;
|
2018-11-15 15:10:12 +01:00
|
|
|
if (auto mount = find_mount_for_host(entry.inode))
|
2018-10-10 11:53:07 +02:00
|
|
|
resolvedInode = mount->guest();
|
|
|
|
else
|
|
|
|
resolvedInode = entry.inode;
|
|
|
|
|
2018-11-15 15:36:35 +01:00
|
|
|
if (dir_inode.identifier().isRootInode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
|
2018-11-15 15:10:12 +01:00
|
|
|
auto mount = find_mount_for_guest(entry.inode);
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(mount);
|
|
|
|
resolvedInode = mount->host();
|
|
|
|
}
|
2018-11-15 17:13:10 +01:00
|
|
|
callback(FS::DirectoryEntry(entry.name, entry.name_length, resolvedInode, entry.fileType));
|
2018-10-10 11:53:07 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
bool VFS::touch(const String& path)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-10-28 14:11:51 +01:00
|
|
|
int error;
|
2018-11-18 23:28:43 +01:00
|
|
|
auto inode = resolve_path(path, root_inode_id(), error);
|
2018-10-10 11:53:07 +02:00
|
|
|
if (!inode.isValid())
|
|
|
|
return false;
|
2018-11-15 15:36:35 +01:00
|
|
|
return inode.fileSystem()->set_mtime(inode, ktime(nullptr));
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
RetainPtr<FileDescriptor> VFS::open(CharacterDevice& device, int options)
|
2018-10-30 15:33:37 +01:00
|
|
|
{
|
2018-11-09 10:03:21 +01:00
|
|
|
// FIXME: Respect options.
|
|
|
|
(void) options;
|
2018-11-15 15:36:35 +01:00
|
|
|
auto vnode = get_or_create_node(device);
|
2018-10-30 15:33:37 +01:00
|
|
|
if (!vnode)
|
|
|
|
return nullptr;
|
2018-11-07 11:37:54 +01:00
|
|
|
return FileDescriptor::create(move(vnode));
|
2018-10-30 15:33:37 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
RetainPtr<FileDescriptor> VFS::open(const String& path, int& error, int options, InodeIdentifier base)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-18 23:28:43 +01:00
|
|
|
auto inode = resolve_path(path, base, error, options);
|
2018-10-10 11:53:07 +02:00
|
|
|
if (!inode.isValid())
|
|
|
|
return nullptr;
|
2018-11-15 15:36:35 +01:00
|
|
|
auto vnode = get_or_create_node(inode);
|
2018-10-10 11:53:07 +02:00
|
|
|
if (!vnode)
|
|
|
|
return nullptr;
|
2018-11-07 11:37:54 +01:00
|
|
|
return FileDescriptor::create(move(vnode));
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
RetainPtr<FileDescriptor> VFS::create(const String& path, InodeIdentifier base, int& error)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
// FIXME: Do the real thing, not just this fake thing!
|
2018-10-17 16:48:16 +02:00
|
|
|
(void) path;
|
2018-11-09 10:03:21 +01:00
|
|
|
(void) base;
|
2018-11-18 14:57:41 +01:00
|
|
|
m_root_vnode->fileSystem()->create_inode(m_root_vnode->fileSystem()->rootInode(), "empty", 0100644, 0, error);
|
2018-10-16 00:35:03 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
bool VFS::mkdir(const String& path, mode_t mode, InodeIdentifier base, int& error)
|
2018-10-16 00:35:03 +02:00
|
|
|
{
|
2018-11-18 14:57:41 +01:00
|
|
|
error = EWHYTHO;
|
|
|
|
// FIXME: This won't work nicely across mount boundaries.
|
|
|
|
FileSystemPath p(path);
|
|
|
|
if (!p.isValid()) {
|
|
|
|
error = -EINVAL;
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-18 23:28:43 +01:00
|
|
|
|
|
|
|
InodeIdentifier parent_dir;
|
|
|
|
auto existing_dir = resolve_path(path, base, error, 0, &parent_dir);
|
|
|
|
if (existing_dir.isValid()) {
|
|
|
|
error = -EEXIST;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!parent_dir.isValid()) {
|
|
|
|
error = -ENOENT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (error != -ENOENT) {
|
2018-11-18 14:57:41 +01:00
|
|
|
return false;
|
2018-11-18 23:28:43 +01:00
|
|
|
}
|
|
|
|
dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_dir.fsid(), parent_dir.index());
|
2018-11-18 14:57:41 +01:00
|
|
|
auto new_dir = base.fileSystem()->create_directory(parent_dir, p.basename(), mode, error);
|
|
|
|
if (new_dir.isValid()) {
|
|
|
|
error = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
InodeIdentifier VFS::resolveSymbolicLink(InodeIdentifier base, InodeIdentifier symlinkInode, int& error)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-15 15:36:35 +01:00
|
|
|
auto symlinkContents = symlinkInode.readEntireFile();
|
|
|
|
if (!symlinkContents)
|
2018-10-10 11:53:07 +02:00
|
|
|
return { };
|
2018-11-15 15:36:35 +01:00
|
|
|
auto linkee = String((const char*)symlinkContents.pointer(), symlinkContents.size());
|
2018-10-29 21:54:11 +01:00
|
|
|
#ifdef VFS_DEBUG
|
2018-11-15 15:10:12 +01:00
|
|
|
kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
|
2018-10-29 21:54:11 +01:00
|
|
|
#endif
|
2018-11-18 23:28:43 +01:00
|
|
|
return resolve_path(linkee, base, error);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
RetainPtr<CoreInode> VFS::get_inode(InodeIdentifier inode_id)
|
2018-10-28 12:20:25 +01:00
|
|
|
{
|
2018-11-13 23:44:54 +01:00
|
|
|
if (!inode_id.isValid())
|
|
|
|
return nullptr;
|
|
|
|
return inode_id.fileSystem()->get_inode(inode_id);
|
|
|
|
}
|
2018-10-28 12:20:25 +01:00
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
String VFS::absolute_path(CoreInode& core_inode)
|
2018-11-13 23:44:54 +01:00
|
|
|
{
|
2018-10-28 14:11:51 +01:00
|
|
|
int error;
|
2018-10-28 12:20:25 +01:00
|
|
|
Vector<InodeIdentifier> lineage;
|
2018-11-13 23:44:54 +01:00
|
|
|
RetainPtr<CoreInode> inode = &core_inode;
|
2018-11-15 15:10:12 +01:00
|
|
|
while (inode->identifier() != m_root_vnode->inode) {
|
|
|
|
if (auto* mount = find_mount_for_guest(inode->identifier()))
|
2018-10-28 12:20:25 +01:00
|
|
|
lineage.append(mount->host());
|
|
|
|
else
|
2018-11-13 23:44:54 +01:00
|
|
|
lineage.append(inode->identifier());
|
|
|
|
|
|
|
|
InodeIdentifier parent_id;
|
|
|
|
if (inode->is_directory()) {
|
2018-11-18 23:28:43 +01:00
|
|
|
parent_id = resolve_path("..", inode->identifier(), error);
|
2018-11-13 23:44:54 +01:00
|
|
|
} else {
|
2018-11-15 15:10:12 +01:00
|
|
|
parent_id = inode->fs().find_parent_of_inode(inode->identifier());
|
2018-11-13 23:44:54 +01:00
|
|
|
}
|
|
|
|
ASSERT(parent_id.isValid());
|
|
|
|
inode = get_inode(parent_id);
|
2018-10-28 12:20:25 +01:00
|
|
|
}
|
|
|
|
if (lineage.isEmpty())
|
|
|
|
return "/";
|
2018-11-15 15:10:12 +01:00
|
|
|
lineage.append(m_root_vnode->inode);
|
2018-10-28 12:20:25 +01:00
|
|
|
StringBuilder builder;
|
|
|
|
for (size_t i = lineage.size() - 1; i >= 1; --i) {
|
|
|
|
auto& child = lineage[i - 1];
|
|
|
|
auto parent = lineage[i];
|
2018-11-15 15:10:12 +01:00
|
|
|
if (auto* mount = find_mount_for_host(parent))
|
2018-10-28 12:20:25 +01:00
|
|
|
parent = mount->guest();
|
|
|
|
builder.append('/');
|
2018-11-15 17:04:55 +01:00
|
|
|
auto parent_inode = get_inode(parent);
|
|
|
|
builder.append(parent_inode->reverse_lookup(child));
|
2018-10-28 12:20:25 +01:00
|
|
|
}
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2018-11-18 23:28:43 +01:00
|
|
|
InodeIdentifier VFS::resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* deepest_dir)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-18 23:28:43 +01:00
|
|
|
if (path.isEmpty()) {
|
|
|
|
error = -EINVAL;
|
2018-10-26 14:24:11 +02:00
|
|
|
return { };
|
2018-11-18 23:28:43 +01:00
|
|
|
}
|
2018-10-26 14:24:11 +02:00
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
auto parts = path.split('/');
|
2018-11-15 15:36:35 +01:00
|
|
|
InodeIdentifier crumb_id;
|
2018-10-26 14:24:11 +02:00
|
|
|
|
|
|
|
if (path[0] == '/')
|
2018-11-15 15:36:35 +01:00
|
|
|
crumb_id = m_root_vnode->inode;
|
2018-10-26 14:24:11 +02:00
|
|
|
else
|
2018-11-15 15:36:35 +01:00
|
|
|
crumb_id = base.isValid() ? base : m_root_vnode->inode;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-11-18 23:28:43 +01:00
|
|
|
if (deepest_dir)
|
|
|
|
*deepest_dir = crumb_id;
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
for (unsigned i = 0; i < parts.size(); ++i) {
|
2018-11-15 15:36:35 +01:00
|
|
|
bool inode_was_root_at_head_of_loop = crumb_id.isRootInode();
|
2018-10-10 11:53:07 +02:00
|
|
|
auto& part = parts[i];
|
2018-11-10 15:40:24 +01:00
|
|
|
if (part.isEmpty())
|
|
|
|
break;
|
2018-11-15 15:36:35 +01:00
|
|
|
auto metadata = crumb_id.metadata();
|
2018-10-10 11:53:07 +02:00
|
|
|
if (!metadata.isValid()) {
|
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("invalid metadata\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
2018-10-28 14:11:51 +01:00
|
|
|
error = -EIO;
|
2018-10-26 18:19:31 +02:00
|
|
|
return { };
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
if (!metadata.isDirectory()) {
|
|
|
|
#ifdef VFS_DEBUG
|
2018-11-15 15:10:12 +01:00
|
|
|
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);
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
2018-11-18 23:28:43 +01:00
|
|
|
error = -ENOTDIR;
|
2018-10-26 18:19:31 +02:00
|
|
|
return { };
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
2018-11-15 15:36:35 +01:00
|
|
|
auto parent = crumb_id;
|
2018-11-15 16:34:36 +01:00
|
|
|
auto dir_inode = get_inode(crumb_id);
|
|
|
|
crumb_id = dir_inode->lookup(part);
|
2018-11-15 15:36:35 +01:00
|
|
|
if (!crumb_id.isValid()) {
|
2018-10-10 11:53:07 +02:00
|
|
|
#ifdef VFS_DEBUG
|
2018-11-15 15:10:12 +01:00
|
|
|
kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
2018-10-28 14:25:51 +01:00
|
|
|
error = -ENOENT;
|
2018-10-26 18:19:31 +02:00
|
|
|
return { };
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
#ifdef VFS_DEBUG
|
2018-11-15 15:10:12 +01:00
|
|
|
kprintf("<%s> %u:%u\n", part.characters(), inode.fsid(), inode.index());
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
2018-11-15 15:36:35 +01:00
|
|
|
if (auto mount = find_mount_for_host(crumb_id)) {
|
2018-10-10 11:53:07 +02:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf(" -- is host\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
2018-11-15 15:36:35 +01:00
|
|
|
crumb_id = mount->guest();
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
2018-11-15 15:36:35 +01:00
|
|
|
if (inode_was_root_at_head_of_loop && crumb_id.isRootInode() && !is_vfs_root(crumb_id) && part == "..") {
|
2018-10-10 11:53:07 +02:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf(" -- is guest\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
2018-11-15 15:36:35 +01:00
|
|
|
auto mount = find_mount_for_guest(crumb_id);
|
2018-11-15 16:34:36 +01:00
|
|
|
auto dir_inode = get_inode(mount->host());
|
|
|
|
crumb_id = dir_inode->lookup("..");
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
2018-11-15 15:36:35 +01:00
|
|
|
metadata = crumb_id.metadata();
|
2018-11-18 23:28:43 +01:00
|
|
|
if (metadata.isDirectory()) {
|
|
|
|
if (deepest_dir)
|
|
|
|
*deepest_dir = crumb_id;
|
|
|
|
}
|
2018-10-10 11:53:07 +02:00
|
|
|
if (metadata.isSymbolicLink()) {
|
2018-10-28 14:11:51 +01:00
|
|
|
if (i == parts.size() - 1) {
|
|
|
|
if (options & O_NOFOLLOW) {
|
|
|
|
error = -ELOOP;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
if (options & O_NOFOLLOW_NOERROR)
|
2018-11-15 15:36:35 +01:00
|
|
|
return crumb_id;
|
2018-10-28 14:11:51 +01:00
|
|
|
}
|
2018-11-15 15:36:35 +01:00
|
|
|
crumb_id = resolveSymbolicLink(parent, crumb_id, error);
|
|
|
|
if (!crumb_id.isValid()) {
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("Symbolic link resolution failed :(\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:35 +01:00
|
|
|
return crumb_id;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
void Vnode::retain()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-01 01:04:02 +01:00
|
|
|
InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
|
2018-10-10 11:53:07 +02:00
|
|
|
++retainCount;
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
void Vnode::release()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-01 01:04:02 +01:00
|
|
|
InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(retainCount);
|
|
|
|
if (--retainCount == 0) {
|
2018-10-24 12:43:52 +02:00
|
|
|
m_vfs->freeNode(this);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
const InodeMetadata& Vnode::metadata() const
|
2018-10-24 12:43:52 +02:00
|
|
|
{
|
2018-11-13 13:32:16 +01:00
|
|
|
if (m_core_inode)
|
|
|
|
return m_core_inode->metadata();
|
2018-10-24 12:43:52 +02:00
|
|
|
if (!m_cachedMetadata.isValid())
|
|
|
|
m_cachedMetadata = inode.metadata();
|
|
|
|
return m_cachedMetadata;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
|
2018-10-10 11:53:07 +02:00
|
|
|
: m_host(host)
|
2018-11-15 15:36:35 +01:00
|
|
|
, m_guest(guest_fs->rootInode())
|
|
|
|
, m_guest_fs(move(guest_fs))
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:10:12 +01:00
|
|
|
void VFS::register_character_device(CharacterDevice& device)
|
2018-10-14 02:59:22 +02:00
|
|
|
{
|
2018-11-15 15:10:12 +01:00
|
|
|
m_character_devices.set(encodedDevice(device.major(), device.minor()), &device);
|
2018-10-14 02:59:22 +02:00
|
|
|
}
|
2018-10-26 18:43:25 +02:00
|
|
|
|
2018-11-15 15:10:12 +01:00
|
|
|
void VFS::for_each_mount(Function<void(const Mount&)> callback) const
|
2018-10-26 18:43:25 +02:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
|
|
|
callback(*mount);
|
|
|
|
}
|
|
|
|
}
|