2018-10-10 09:53:07 +00:00
|
|
|
#include "VirtualFileSystem.h"
|
2018-11-07 10:37:54 +00:00
|
|
|
#include "FileDescriptor.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
#include "FileSystem.h"
|
2018-10-28 11:20:25 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2018-10-12 22:44:54 +00:00
|
|
|
#include <AK/kmalloc.h>
|
2018-10-17 08:55:43 +00:00
|
|
|
#include <AK/kstdio.h>
|
|
|
|
#include <AK/ktime.h>
|
2018-10-30 12:59:29 +00:00
|
|
|
#include "CharacterDevice.h"
|
2018-11-07 20:19:47 +00:00
|
|
|
#include <LibC/errno_numbers.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
//#define VFS_DEBUG
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
static VFS* s_the;
|
2018-10-18 08:27:07 +00:00
|
|
|
|
2018-11-07 11:05:51 +00:00
|
|
|
#ifndef SERENITY
|
|
|
|
typedef int InterruptDisabler;
|
|
|
|
#endif
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS& VFS::the()
|
2018-10-18 08:27:07 +00:00
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
void VFS::initializeGlobals()
|
2018-10-22 10:54:19 +00:00
|
|
|
{
|
|
|
|
s_the = nullptr;
|
|
|
|
FileSystem::initializeGlobals();
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS::VFS()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-22 09:15:16 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: Constructing VFS\n");
|
2018-10-22 09:15:16 +00:00
|
|
|
#endif
|
2018-10-18 08:27:07 +00:00
|
|
|
s_the = this;
|
2018-10-10 09:53:07 +00:00
|
|
|
m_maxNodeCount = 16;
|
2018-11-15 13:43:10 +00:00
|
|
|
m_nodes = reinterpret_cast<Vnode*>(kmalloc(sizeof(Vnode) * maxNodeCount()));
|
|
|
|
memset(m_nodes, 0, sizeof(Vnode) * maxNodeCount());
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < m_maxNodeCount; ++i)
|
|
|
|
m_nodeFreeList.append(&m_nodes[i]);
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS::~VFS()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: ~VirtualFileSystem with %u nodes allocated\n", allocatedNodeCount());
|
2018-10-26 15:42:12 +00:00
|
|
|
// FIXME: m_nodes is never freed. Does it matter though?
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
auto VFS::makeNode(InodeIdentifier inode) -> RetainPtr<Vnode>
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
auto metadata = inode.metadata();
|
|
|
|
if (!metadata.isValid())
|
|
|
|
return nullptr;
|
|
|
|
|
2018-11-13 12:02:39 +00:00
|
|
|
auto core_inode = inode.fileSystem()->get_inode(inode);
|
2018-11-13 22:44:54 +00:00
|
|
|
if (core_inode)
|
|
|
|
core_inode->m_metadata = metadata;
|
2018-11-13 12:02:39 +00:00
|
|
|
|
2018-10-31 20:31:56 +00:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
|
2018-10-14 00:59:22 +00:00
|
|
|
CharacterDevice* characterDevice = nullptr;
|
|
|
|
if (metadata.isCharacterDevice()) {
|
|
|
|
auto it = m_characterDevices.find(encodedDevice(metadata.majorDevice, metadata.minorDevice));
|
|
|
|
if (it != m_characterDevices.end()) {
|
|
|
|
characterDevice = (*it).value;
|
|
|
|
} else {
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: makeNode() no such character device %u,%u\n", metadata.majorDevice, metadata.minorDevice);
|
2018-10-14 00:59:22 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
auto vnode = allocateNode();
|
|
|
|
ASSERT(vnode);
|
|
|
|
|
|
|
|
FileSystem* fileSystem = inode.fileSystem();
|
|
|
|
fileSystem->retain();
|
|
|
|
|
|
|
|
vnode->inode = inode;
|
2018-11-13 12:02:39 +00:00
|
|
|
vnode->m_core_inode = move(core_inode);
|
2018-11-13 12:32:16 +00:00
|
|
|
vnode->m_cachedMetadata = metadata;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 08:55:43 +00: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 09:53:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
m_inode2vnode.set(inode, vnode.ptr());
|
2018-10-14 00:59:22 +00:00
|
|
|
vnode->m_characterDevice = characterDevice;
|
2018-10-24 10:43:52 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
return vnode;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
auto VFS::makeNode(CharacterDevice& device) -> RetainPtr<Vnode>
|
2018-10-30 14:33:37 +00:00
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
InterruptDisabler disabler;
|
2018-10-30 14:33:37 +00: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 13:43:10 +00:00
|
|
|
auto VFS::getOrCreateNode(InodeIdentifier inode) -> RetainPtr<Vnode>
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto it = m_inode2vnode.find(inode);
|
|
|
|
if (it != m_inode2vnode.end())
|
|
|
|
return (*it).value;
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
return makeNode(inode);
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
auto VFS::getOrCreateNode(CharacterDevice& device) -> RetainPtr<Vnode>
|
2018-10-30 14:33:37 +00:00
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto it = m_device2vnode.find(encodedDevice(device.major(), device.minor()));
|
|
|
|
if (it != m_device2vnode.end())
|
|
|
|
return (*it).value;
|
|
|
|
}
|
2018-10-30 14:33:37 +00:00
|
|
|
return makeNode(device);
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
bool VFS::mount(RetainPtr<FileSystem>&& fileSystem, const String& path)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
ASSERT(fileSystem);
|
2018-10-28 13:11:51 +00:00
|
|
|
int error;
|
2018-10-29 21:43:39 +00:00
|
|
|
auto inode = resolvePath(path, error);
|
2018-10-10 09:53:07 +00:00
|
|
|
if (!inode.isValid()) {
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", fileSystem->className(), fileSystem.ptr(), path.characters(), inode.index());
|
2018-10-10 09:53:07 +00:00
|
|
|
// FIXME: check that this is not already a mount point
|
2018-10-17 09:40:58 +00:00
|
|
|
auto mount = make<Mount>(inode, move(fileSystem));
|
|
|
|
m_mounts.append(move(mount));
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
bool VFS::mountRoot(RetainPtr<FileSystem>&& fileSystem)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
if (m_rootNode) {
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: mountRoot can't mount another root\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-17 09:40:58 +00:00
|
|
|
auto mount = make<Mount>(InodeIdentifier(), move(fileSystem));
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
auto node = makeNode(mount->guest());
|
|
|
|
if (!node->inUse()) {
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: root inode for / is not in use :(\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-13 12:32:16 +00:00
|
|
|
if (!node->metadata().isDirectory()) {
|
2018-10-31 20:31:56 +00:00
|
|
|
kprintf("VFS: root inode for / is not a directory :(\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-17 09:40:58 +00:00
|
|
|
m_rootNode = move(node);
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: mounted root on %s{%p}\n",
|
2018-10-10 09:53:07 +00:00
|
|
|
m_rootNode->fileSystem()->className(),
|
|
|
|
m_rootNode->fileSystem());
|
|
|
|
|
2018-10-17 09:40:58 +00:00
|
|
|
m_mounts.append(move(mount));
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
auto VFS::allocateNode() -> RetainPtr<Vnode>
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
if (m_nodeFreeList.isEmpty()) {
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: allocateNode has no nodes left\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto* node = m_nodeFreeList.takeLast();
|
|
|
|
ASSERT(node->retainCount == 0);
|
|
|
|
node->retainCount = 1;
|
2018-10-24 10:43:52 +00:00
|
|
|
node->m_vfs = this;
|
2018-11-08 14:39:26 +00:00
|
|
|
node->m_vmo = nullptr;
|
2018-10-10 09:53:07 +00:00
|
|
|
return adopt(*node);
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
void VFS::freeNode(Vnode* node)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
InterruptDisabler disabler;
|
2018-10-10 09:53:07 +00:00
|
|
|
ASSERT(node);
|
|
|
|
ASSERT(node->inUse());
|
2018-10-30 14:33:37 +00: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 14:39:26 +00:00
|
|
|
node->m_vfs = nullptr;
|
|
|
|
node->m_vmo = nullptr;
|
2018-10-17 09:40:58 +00:00
|
|
|
m_nodeFreeList.append(move(node));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 20:54:11 +00:00
|
|
|
#ifndef SERENITY
|
2018-11-15 13:43:10 +00:00
|
|
|
bool VFS::isDirectory(const String& path, InodeIdentifier base)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-28 13:11:51 +00:00
|
|
|
int error;
|
|
|
|
auto inode = resolvePath(path, error, base);
|
2018-10-10 09:53:07 +00:00
|
|
|
if (!inode.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return inode.metadata().isDirectory();
|
|
|
|
}
|
2018-10-29 20:54:11 +00:00
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
auto VFS::findMountForHost(InodeIdentifier inode) -> Mount*
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
|
|
|
if (mount->host() == inode)
|
|
|
|
return mount.ptr();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
auto VFS::findMountForGuest(InodeIdentifier inode) -> Mount*
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
|
|
|
if (mount->guest() == inode)
|
|
|
|
return mount.ptr();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
bool VFS::is_vfs_root(InodeIdentifier inode) const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
return inode == m_rootNode->inode;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
void VFS::enumerateDirectoryInode(InodeIdentifier directoryInode, Function<bool(const FileSystem::DirectoryEntry&)> callback)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
if (!directoryInode.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
directoryInode.fileSystem()->enumerateDirectoryInode(directoryInode, [&] (const FileSystem::DirectoryEntry& entry) {
|
|
|
|
InodeIdentifier resolvedInode;
|
|
|
|
if (auto mount = findMountForHost(entry.inode))
|
|
|
|
resolvedInode = mount->guest();
|
|
|
|
else
|
|
|
|
resolvedInode = entry.inode;
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
if (directoryInode.isRootInode() && !is_vfs_root(directoryInode) && !strcmp(entry.name, "..")) {
|
2018-10-10 09:53:07 +00:00
|
|
|
auto mount = findMountForGuest(entry.inode);
|
|
|
|
ASSERT(mount);
|
|
|
|
resolvedInode = mount->host();
|
|
|
|
}
|
2018-11-12 23:17:30 +00:00
|
|
|
callback(FileSystem::DirectoryEntry(entry.name, entry.name_length, resolvedInode, entry.fileType));
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-29 20:54:11 +00:00
|
|
|
#ifndef SERENITY
|
2018-11-15 13:43:10 +00:00
|
|
|
void VFS::listDirectory(const String& path, InodeIdentifier base)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-28 13:11:51 +00:00
|
|
|
int error;
|
2018-11-07 14:51:39 +00:00
|
|
|
auto directoryInode = resolvePath(path, error, base);
|
2018-10-10 09:53:07 +00:00
|
|
|
if (!directoryInode.isValid())
|
|
|
|
return;
|
|
|
|
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: ls %s -> %s %02u:%08u\n", path.characters(), directoryInode.fileSystem()->className(), directoryInode.fileSystemID(), directoryInode.index());
|
2018-10-10 09:53:07 +00:00
|
|
|
enumerateDirectoryInode(directoryInode, [&] (const FileSystem::DirectoryEntry& entry) {
|
|
|
|
const char* nameColorBegin = "";
|
|
|
|
const char* nameColorEnd = "";
|
|
|
|
auto metadata = entry.inode.metadata();
|
|
|
|
ASSERT(metadata.isValid());
|
|
|
|
if (metadata.isDirectory()) {
|
|
|
|
nameColorBegin = "\033[34;1m";
|
|
|
|
nameColorEnd = "\033[0m";
|
|
|
|
} else if (metadata.isSymbolicLink()) {
|
|
|
|
nameColorBegin = "\033[36;1m";
|
|
|
|
nameColorEnd = "\033[0m";
|
|
|
|
}
|
|
|
|
if (metadata.isSticky()) {
|
|
|
|
nameColorBegin = "\033[42;30m";
|
|
|
|
nameColorEnd = "\033[0m";
|
|
|
|
}
|
2018-10-14 00:24:12 +00:00
|
|
|
if (metadata.isCharacterDevice() || metadata.isBlockDevice()) {
|
|
|
|
nameColorBegin = "\033[33;1m";
|
|
|
|
nameColorEnd = "\033[0m";
|
|
|
|
}
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("%02u:%08u ",
|
2018-10-10 09:53:07 +00:00
|
|
|
metadata.inode.fileSystemID(),
|
|
|
|
metadata.inode.index());
|
|
|
|
|
|
|
|
if (metadata.isDirectory())
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("d");
|
2018-10-10 09:53:07 +00:00
|
|
|
else if (metadata.isSymbolicLink())
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("l");
|
2018-10-10 09:53:07 +00:00
|
|
|
else if (metadata.isBlockDevice())
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("b");
|
2018-10-10 09:53:07 +00:00
|
|
|
else if (metadata.isCharacterDevice())
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("c");
|
2018-10-10 09:53:07 +00:00
|
|
|
else if (metadata.isSocket())
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("s");
|
2018-10-10 09:53:07 +00:00
|
|
|
else if (metadata.isFIFO())
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("f");
|
2018-10-10 09:53:07 +00:00
|
|
|
else if (metadata.isRegularFile())
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("-");
|
2018-10-10 09:53:07 +00:00
|
|
|
else
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("?");
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("%c%c%c%c%c%c%c%c",
|
2018-10-10 09:53:07 +00:00
|
|
|
metadata.mode & 00400 ? 'r' : '-',
|
|
|
|
metadata.mode & 00200 ? 'w' : '-',
|
|
|
|
metadata.mode & 00100 ? 'x' : '-',
|
|
|
|
metadata.mode & 00040 ? 'r' : '-',
|
|
|
|
metadata.mode & 00020 ? 'w' : '-',
|
|
|
|
metadata.mode & 00010 ? 'x' : '-',
|
|
|
|
metadata.mode & 00004 ? 'r' : '-',
|
|
|
|
metadata.mode & 00002 ? 'w' : '-'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (metadata.isSticky())
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("t");
|
2018-10-10 09:53:07 +00:00
|
|
|
else
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("%c", metadata.mode & 00001 ? 'x' : '-');
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-14 00:24:12 +00:00
|
|
|
if (metadata.isCharacterDevice() || metadata.isBlockDevice()) {
|
|
|
|
char buf[16];
|
2018-10-17 09:40:58 +00:00
|
|
|
ksprintf(buf, "%u, %u", metadata.majorDevice, metadata.minorDevice);
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("%12s ", buf);
|
2018-10-14 00:24:12 +00:00
|
|
|
} else {
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("%12lld ", metadata.size);
|
2018-10-14 00:24:12 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("\033[30;1m");
|
2018-10-17 10:23:19 +00:00
|
|
|
time_t mtime = metadata.mtime;
|
|
|
|
auto tm = *klocaltime(&mtime);
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("%04u-%02u-%02u %02u:%02u:%02u ",
|
2018-10-10 09:53:07 +00:00
|
|
|
tm.tm_year + 1900,
|
|
|
|
tm.tm_mon + 1,
|
|
|
|
tm.tm_mday,
|
|
|
|
tm.tm_hour,
|
|
|
|
tm.tm_min,
|
|
|
|
tm.tm_sec);
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("\033[0m");
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("%s%s%s",
|
2018-10-10 09:53:07 +00:00
|
|
|
nameColorBegin,
|
2018-11-12 23:17:30 +00:00
|
|
|
entry.name,
|
2018-10-10 09:53:07 +00:00
|
|
|
nameColorEnd);
|
|
|
|
|
|
|
|
if (metadata.isDirectory()) {
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("/");
|
2018-10-10 09:53:07 +00:00
|
|
|
} else if (metadata.isSymbolicLink()) {
|
2018-10-14 22:16:14 +00:00
|
|
|
auto symlinkContents = directoryInode.fileSystem()->readEntireInode(metadata.inode);
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf(" -> %s", String((const char*)symlinkContents.pointer(), symlinkContents.size()).characters());
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
void VFS::listDirectoryRecursively(const String& path, InodeIdentifier base)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-28 13:11:51 +00:00
|
|
|
int error;
|
2018-11-07 14:51:39 +00:00
|
|
|
auto directory = resolvePath(path, error, base);
|
2018-10-10 09:53:07 +00:00
|
|
|
if (!directory.isValid())
|
|
|
|
return;
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("%s\n", path.characters());
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
enumerateDirectoryInode(directory, [&] (const FileSystem::DirectoryEntry& entry) {
|
|
|
|
auto metadata = entry.inode.metadata();
|
|
|
|
if (metadata.isDirectory()) {
|
|
|
|
if (entry.name != "." && entry.name != "..") {
|
|
|
|
char buf[4096];
|
2018-11-12 23:17:30 +00:00
|
|
|
ksprintf(buf, "%s/%s", path.characters(), entry.name);
|
2018-11-07 14:51:39 +00:00
|
|
|
listDirectoryRecursively(buf, base);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-12 23:17:30 +00:00
|
|
|
kprintf("%s/%s\n", path.characters(), entry.name);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2018-10-24 10:43:52 +00:00
|
|
|
return true;
|
2018-10-10 09:53:07 +00:00
|
|
|
});
|
|
|
|
}
|
2018-10-29 20:54:11 +00:00
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
bool VFS::touch(const String& path)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-28 13:11:51 +00:00
|
|
|
int error;
|
2018-10-29 21:43:39 +00:00
|
|
|
auto inode = resolvePath(path, error);
|
2018-10-10 09:53:07 +00:00
|
|
|
if (!inode.isValid())
|
|
|
|
return false;
|
2018-10-17 09:40:58 +00:00
|
|
|
return inode.fileSystem()->setModificationTime(inode, ktime(nullptr));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
RetainPtr<FileDescriptor> VFS::open(CharacterDevice& device, int options)
|
2018-10-30 14:33:37 +00:00
|
|
|
{
|
2018-11-09 09:03:21 +00:00
|
|
|
// FIXME: Respect options.
|
|
|
|
(void) options;
|
2018-10-30 14:33:37 +00:00
|
|
|
auto vnode = getOrCreateNode(device);
|
|
|
|
if (!vnode)
|
|
|
|
return nullptr;
|
2018-11-07 10:37:54 +00:00
|
|
|
return FileDescriptor::create(move(vnode));
|
2018-10-30 14:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
RetainPtr<FileDescriptor> VFS::open(const String& path, int& error, int options, InodeIdentifier base)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-29 21:43:39 +00:00
|
|
|
auto inode = resolvePath(path, error, base, options);
|
2018-10-10 09:53:07 +00:00
|
|
|
if (!inode.isValid())
|
|
|
|
return nullptr;
|
|
|
|
auto vnode = getOrCreateNode(inode);
|
|
|
|
if (!vnode)
|
|
|
|
return nullptr;
|
2018-11-07 10:37:54 +00:00
|
|
|
return FileDescriptor::create(move(vnode));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
RetainPtr<FileDescriptor> VFS::create(const String& path, InodeIdentifier base)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
// FIXME: Do the real thing, not just this fake thing!
|
2018-10-17 14:48:16 +00:00
|
|
|
(void) path;
|
2018-11-09 09:03:21 +00:00
|
|
|
(void) base;
|
2018-10-15 22:35:03 +00:00
|
|
|
m_rootNode->fileSystem()->createInode(m_rootNode->fileSystem()->rootInode(), "empty", 0100644, 0);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
RetainPtr<FileDescriptor> VFS::mkdir(const String& path, InodeIdentifier base)
|
2018-10-15 22:35:03 +00:00
|
|
|
{
|
|
|
|
// FIXME: Do the real thing, not just this fake thing!
|
2018-10-17 14:48:16 +00:00
|
|
|
(void) path;
|
2018-11-09 09:03:21 +00:00
|
|
|
(void) base;
|
2018-10-15 22:35:03 +00:00
|
|
|
m_rootNode->fileSystem()->makeDirectory(m_rootNode->fileSystem()->rootInode(), "mydir", 0400755);
|
2018-10-10 09:53:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
InodeIdentifier VFS::resolveSymbolicLink(InodeIdentifier base, InodeIdentifier symlinkInode, int& error)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
auto symlinkContents = symlinkInode.readEntireFile();
|
|
|
|
if (!symlinkContents)
|
|
|
|
return { };
|
2018-10-29 20:54:11 +00:00
|
|
|
auto linkee = String((const char*)symlinkContents.pointer(), symlinkContents.size());
|
|
|
|
#ifdef VFS_DEBUG
|
|
|
|
kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fileSystemID(), base.index());
|
|
|
|
#endif
|
2018-10-29 21:43:39 +00:00
|
|
|
return resolvePath(linkee, error, base);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
RetainPtr<CoreInode> VFS::get_inode(InodeIdentifier inode_id)
|
2018-10-28 11:20:25 +00:00
|
|
|
{
|
2018-11-13 22:44:54 +00:00
|
|
|
if (!inode_id.isValid())
|
|
|
|
return nullptr;
|
|
|
|
return inode_id.fileSystem()->get_inode(inode_id);
|
|
|
|
}
|
2018-10-28 11:20:25 +00:00
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
String VFS::absolute_path(CoreInode& core_inode)
|
2018-11-13 22:44:54 +00:00
|
|
|
{
|
2018-10-28 13:11:51 +00:00
|
|
|
int error;
|
2018-10-28 11:20:25 +00:00
|
|
|
Vector<InodeIdentifier> lineage;
|
2018-11-13 22:44:54 +00:00
|
|
|
RetainPtr<CoreInode> inode = &core_inode;
|
|
|
|
while (inode->identifier() != m_rootNode->inode) {
|
|
|
|
if (auto* mount = findMountForGuest(inode->identifier()))
|
2018-10-28 11:20:25 +00:00
|
|
|
lineage.append(mount->host());
|
|
|
|
else
|
2018-11-13 22:44:54 +00:00
|
|
|
lineage.append(inode->identifier());
|
|
|
|
|
|
|
|
InodeIdentifier parent_id;
|
|
|
|
if (inode->is_directory()) {
|
|
|
|
parent_id = resolvePath("..", error, inode->identifier());
|
|
|
|
} else {
|
|
|
|
parent_id = inode->fs().findParentOfInode(inode->identifier());
|
|
|
|
}
|
|
|
|
ASSERT(parent_id.isValid());
|
|
|
|
inode = get_inode(parent_id);
|
2018-10-28 11:20:25 +00:00
|
|
|
}
|
|
|
|
if (lineage.isEmpty())
|
|
|
|
return "/";
|
|
|
|
lineage.append(m_rootNode->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 = findMountForHost(parent))
|
|
|
|
parent = mount->guest();
|
|
|
|
builder.append('/');
|
|
|
|
builder.append(parent.fileSystem()->nameOfChildInDirectory(parent, child));
|
|
|
|
}
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
InodeIdentifier VFS::resolvePath(const String& path, int& error, InodeIdentifier base, int options)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-26 12:24:11 +00:00
|
|
|
if (path.isEmpty())
|
|
|
|
return { };
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
auto parts = path.split('/');
|
2018-10-26 12:24:11 +00:00
|
|
|
InodeIdentifier inode;
|
|
|
|
|
|
|
|
if (path[0] == '/')
|
|
|
|
inode = m_rootNode->inode;
|
|
|
|
else
|
2018-10-28 11:20:25 +00:00
|
|
|
inode = base.isValid() ? base : m_rootNode->inode;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < parts.size(); ++i) {
|
2018-10-26 16:19:31 +00:00
|
|
|
bool wasRootInodeAtHeadOfLoop = inode.isRootInode();
|
2018-10-10 09:53:07 +00:00
|
|
|
auto& part = parts[i];
|
2018-11-10 14:40:24 +00:00
|
|
|
if (part.isEmpty())
|
|
|
|
break;
|
2018-10-10 09:53:07 +00:00
|
|
|
auto metadata = inode.metadata();
|
|
|
|
if (!metadata.isValid()) {
|
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("invalid metadata\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
2018-10-28 13:11:51 +00:00
|
|
|
error = -EIO;
|
2018-10-26 16:19:31 +00:00
|
|
|
return { };
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
if (!metadata.isDirectory()) {
|
|
|
|
#ifdef VFS_DEBUG
|
2018-10-29 20:54:11 +00:00
|
|
|
kprintf("parent of <%s> not directory, it's inode %u:%u / %u:%u, mode: %u, size: %u\n", part.characters(), inode.fileSystemID(), inode.index(), metadata.inode.fileSystemID(), metadata.inode.index(), metadata.mode, metadata.size);
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
2018-10-28 13:11:51 +00:00
|
|
|
error = -EIO;
|
2018-10-26 16:19:31 +00:00
|
|
|
return { };
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2018-10-28 13:25:51 +00:00
|
|
|
auto parent = inode;
|
2018-10-10 09:53:07 +00:00
|
|
|
inode = inode.fileSystem()->childOfDirectoryInodeWithName(inode, part);
|
|
|
|
if (!inode.isValid()) {
|
|
|
|
#ifdef VFS_DEBUG
|
2018-10-29 20:54:11 +00:00
|
|
|
kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fileSystemID(), parent.index());
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
2018-10-28 13:25:51 +00:00
|
|
|
error = -ENOENT;
|
2018-10-26 16:19:31 +00:00
|
|
|
return { };
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
#ifdef VFS_DEBUG
|
2018-10-26 16:19:31 +00:00
|
|
|
kprintf("<%s> %u:%u\n", part.characters(), inode.fileSystemID(), inode.index());
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
|
|
|
if (auto mount = findMountForHost(inode)) {
|
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf(" -- is host\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
|
|
|
inode = mount->guest();
|
|
|
|
}
|
2018-11-15 13:43:10 +00:00
|
|
|
if (wasRootInodeAtHeadOfLoop && inode.isRootInode() && !is_vfs_root(inode) && part == "..") {
|
2018-10-10 09:53:07 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf(" -- is guest\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
|
|
|
auto mount = findMountForGuest(inode);
|
|
|
|
inode = mount->host();
|
|
|
|
inode = inode.fileSystem()->childOfDirectoryInodeWithName(inode, "..");
|
|
|
|
}
|
|
|
|
metadata = inode.metadata();
|
|
|
|
if (metadata.isSymbolicLink()) {
|
2018-10-28 13:11:51 +00:00
|
|
|
if (i == parts.size() - 1) {
|
|
|
|
if (options & O_NOFOLLOW) {
|
|
|
|
error = -ELOOP;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
if (options & O_NOFOLLOW_NOERROR)
|
|
|
|
return inode;
|
|
|
|
}
|
2018-10-29 21:43:39 +00:00
|
|
|
inode = resolveSymbolicLink(parent, inode, error);
|
2018-10-10 09:53:07 +00:00
|
|
|
if (!inode.isValid()) {
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("Symbolic link resolution failed :(\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
void Vnode::retain()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-11-01 00:04:02 +00:00
|
|
|
InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
|
2018-10-10 09:53:07 +00:00
|
|
|
++retainCount;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
void Vnode::release()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-11-01 00:04:02 +00:00
|
|
|
InterruptDisabler disabler; // FIXME: Make a Retainable with atomic retain count instead.
|
2018-10-10 09:53:07 +00:00
|
|
|
ASSERT(retainCount);
|
|
|
|
if (--retainCount == 0) {
|
2018-10-24 10:43:52 +00:00
|
|
|
m_vfs->freeNode(this);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
const InodeMetadata& Vnode::metadata() const
|
2018-10-24 10:43:52 +00:00
|
|
|
{
|
2018-11-13 12:32:16 +00:00
|
|
|
if (m_core_inode)
|
|
|
|
return m_core_inode->metadata();
|
2018-10-24 10:43:52 +00:00
|
|
|
if (!m_cachedMetadata.isValid())
|
|
|
|
m_cachedMetadata = inode.metadata();
|
|
|
|
return m_cachedMetadata;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FileSystem>&& guestFileSystem)
|
2018-10-10 09:53:07 +00:00
|
|
|
: m_host(host)
|
|
|
|
, m_guest(guestFileSystem->rootInode())
|
2018-10-17 09:40:58 +00:00
|
|
|
, m_fileSystem(move(guestFileSystem))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
void VFS::registerCharacterDevice(CharacterDevice& device)
|
2018-10-14 00:59:22 +00:00
|
|
|
{
|
2018-10-30 12:59:29 +00:00
|
|
|
m_characterDevices.set(encodedDevice(device.major(), device.minor()), &device);
|
2018-10-14 00:59:22 +00:00
|
|
|
}
|
2018-10-26 16:43:25 +00:00
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
void VFS::forEachMount(Function<void(const Mount&)> callback) const
|
2018-10-26 16:43:25 +00:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
|
|
|
callback(*mount);
|
|
|
|
}
|
|
|
|
}
|