2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/RetainPtr.h>
|
2018-12-04 02:27:16 +03:00
|
|
|
#include <AK/AKString.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
#include <AK/Vector.h>
|
2018-10-24 13:43:52 +03:00
|
|
|
#include <AK/Function.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
#include "InodeIdentifier.h"
|
2018-10-24 13:43:52 +03:00
|
|
|
#include "InodeMetadata.h"
|
2018-10-14 22:19:27 +03:00
|
|
|
#include "Limits.h"
|
2018-10-24 13:43:52 +03:00
|
|
|
#include "FileSystem.h"
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-10-28 16:11:51 +03:00
|
|
|
#define O_RDONLY 0
|
|
|
|
#define O_WRONLY 1
|
|
|
|
#define O_RDWR 2
|
2018-11-13 03:36:31 +03:00
|
|
|
#define O_CREAT 0100
|
|
|
|
#define O_EXCL 0200
|
|
|
|
#define O_NOCTTY 0400
|
|
|
|
#define O_TRUNC 01000
|
|
|
|
#define O_APPEND 02000
|
|
|
|
#define O_NONBLOCK 04000
|
2018-10-28 16:11:51 +03:00
|
|
|
#define O_DIRECTORY 00200000
|
|
|
|
#define O_NOFOLLOW 00400000
|
2018-11-13 03:36:31 +03:00
|
|
|
#define O_CLOEXEC 02000000
|
2018-10-28 16:11:51 +03:00
|
|
|
#define O_NOFOLLOW_NOERROR 0x4000000
|
|
|
|
|
2018-10-14 03:59:22 +03:00
|
|
|
class CharacterDevice;
|
2018-11-07 13:37:54 +03:00
|
|
|
class FileDescriptor;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-10-30 15:59:29 +03:00
|
|
|
inline constexpr dword encodedDevice(unsigned major, unsigned minor)
|
|
|
|
{
|
|
|
|
return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:43:10 +03:00
|
|
|
class VFS;
|
|
|
|
|
|
|
|
class Vnode {
|
|
|
|
public:
|
|
|
|
InodeIdentifier inode;
|
|
|
|
const InodeMetadata& metadata() const;
|
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
bool inUse() const { return inode.is_valid() || m_characterDevice; }
|
2018-11-15 16:43:10 +03:00
|
|
|
|
|
|
|
bool isCharacterDevice() const { return m_characterDevice; }
|
|
|
|
CharacterDevice* characterDevice() { return m_characterDevice; }
|
|
|
|
const CharacterDevice* characterDevice() const { return m_characterDevice; }
|
|
|
|
|
|
|
|
void retain();
|
|
|
|
void release();
|
|
|
|
|
2018-12-19 23:56:45 +03:00
|
|
|
FS* fs() { return inode.fs(); }
|
|
|
|
const FS* fs() const { return inode.fs(); }
|
2018-11-15 16:43:10 +03:00
|
|
|
|
|
|
|
VFS* vfs() { return m_vfs; }
|
|
|
|
const VFS* vfs() const { return m_vfs; }
|
|
|
|
|
|
|
|
void* vmo() { return m_vmo; }
|
|
|
|
void set_vmo(void* vmo) { m_vmo = vmo; }
|
|
|
|
|
|
|
|
unsigned retain_count() const { return retainCount; }
|
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
Inode* core_inode() { return m_core_inode.ptr(); }
|
2018-11-15 16:43:10 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class VFS;
|
|
|
|
VFS* m_vfs { nullptr };
|
|
|
|
unsigned retainCount { 0 };
|
|
|
|
CharacterDevice* m_characterDevice { nullptr };
|
|
|
|
mutable InodeMetadata m_cachedMetadata;
|
|
|
|
void* m_vmo { nullptr };
|
2018-12-19 23:18:28 +03:00
|
|
|
RetainPtr<Inode> m_core_inode;
|
2018-11-15 16:43:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class VFS {
|
2018-11-01 01:19:15 +03:00
|
|
|
AK_MAKE_ETERNAL
|
2018-11-09 19:46:55 +03:00
|
|
|
friend ByteBuffer procfs$vnodes();
|
2018-10-10 12:53:07 +03:00
|
|
|
public:
|
2018-11-15 17:10:12 +03:00
|
|
|
static void initialize_globals();
|
2018-10-19 12:20:49 +03:00
|
|
|
|
2018-10-26 19:43:25 +03:00
|
|
|
class Mount {
|
|
|
|
public:
|
2018-11-15 19:13:10 +03:00
|
|
|
Mount(InodeIdentifier host, RetainPtr<FS>&&);
|
2018-10-26 19:43:25 +03:00
|
|
|
|
|
|
|
InodeIdentifier host() const { return m_host; }
|
|
|
|
InodeIdentifier guest() const { return m_guest; }
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
const FS& guest_fs() const { return *m_guest_fs; }
|
2018-10-26 19:43:25 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
InodeIdentifier m_host;
|
|
|
|
InodeIdentifier m_guest;
|
2018-11-15 19:13:10 +03:00
|
|
|
RetainPtr<FS> m_guest_fs;
|
2018-10-26 19:43:25 +03:00
|
|
|
};
|
|
|
|
|
2018-11-15 16:43:10 +03:00
|
|
|
static VFS& the() PURE;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 16:43:10 +03:00
|
|
|
VFS();
|
|
|
|
~VFS();
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
unsigned max_vnode_count() const { return m_max_vnode_count; }
|
|
|
|
unsigned allocated_vnode_count() const { return m_max_vnode_count - m_vnode_freelist.size(); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
Vnode* root() { return m_root_vnode.ptr(); }
|
|
|
|
const Vnode* root() const { return m_root_vnode.ptr(); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
bool mount_root(RetainPtr<FS>&&);
|
|
|
|
bool mount(RetainPtr<FS>&&, const String& path);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-07 13:37:54 +03:00
|
|
|
RetainPtr<FileDescriptor> open(CharacterDevice&, int options);
|
|
|
|
RetainPtr<FileDescriptor> open(const String& path, int& error, int options = 0, InodeIdentifier base = InodeIdentifier());
|
2018-11-18 16:57:41 +03:00
|
|
|
RetainPtr<FileDescriptor> create(const String& path, InodeIdentifier base, int& error);
|
|
|
|
bool mkdir(const String& path, mode_t mode, InodeIdentifier base, int& error);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
bool touch(const String&path);
|
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
void register_character_device(CharacterDevice&);
|
2018-10-14 03:59:22 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
size_t mount_count() const { return m_mounts.size(); }
|
|
|
|
void for_each_mount(Function<void(const Mount&)>) const;
|
2018-10-26 19:43:25 +03:00
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
String absolute_path(Inode&);
|
2018-10-28 14:20:25 +03:00
|
|
|
|
2018-11-19 01:28:43 +03:00
|
|
|
InodeIdentifier root_inode_id() const;
|
|
|
|
|
2018-12-20 02:39:29 +03:00
|
|
|
void sync();
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
private:
|
2018-11-07 13:37:54 +03:00
|
|
|
friend class FileDescriptor;
|
2018-11-15 16:43:10 +03:00
|
|
|
friend class Vnode;
|
2018-10-24 13:43:52 +03:00
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
RetainPtr<Inode> get_inode(InodeIdentifier);
|
2018-11-14 01:44:54 +03:00
|
|
|
|
2018-11-15 16:43:10 +03:00
|
|
|
bool is_vfs_root(InodeIdentifier) const;
|
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
|
2018-11-19 01:28:43 +03:00
|
|
|
InodeIdentifier resolve_path(const String& path, InodeIdentifier base, int& error, int options = 0, InodeIdentifier* deepest_dir = nullptr);
|
2018-10-30 00:43:39 +03:00
|
|
|
InodeIdentifier resolveSymbolicLink(InodeIdentifier base, InodeIdentifier symlinkInode, int& error);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 16:43:10 +03:00
|
|
|
RetainPtr<Vnode> allocateNode();
|
|
|
|
void freeNode(Vnode*);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 16:43:10 +03:00
|
|
|
RetainPtr<Vnode> makeNode(InodeIdentifier);
|
|
|
|
RetainPtr<Vnode> makeNode(CharacterDevice&);
|
2018-11-15 17:36:35 +03:00
|
|
|
RetainPtr<Vnode> get_or_create_node(InodeIdentifier);
|
|
|
|
RetainPtr<Vnode> get_or_create_node(CharacterDevice&);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
Mount* find_mount_for_host(InodeIdentifier);
|
|
|
|
Mount* find_mount_for_guest(InodeIdentifier);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 16:43:10 +03:00
|
|
|
HashMap<InodeIdentifier, Vnode*> m_inode2vnode;
|
|
|
|
HashMap<dword, Vnode*> m_device2vnode;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
Vector<OwnPtr<Mount>> m_mounts;
|
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
unsigned m_max_vnode_count { 0 };
|
2018-11-15 16:43:10 +03:00
|
|
|
Vnode* m_nodes { nullptr };
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
Vector<Vnode*> m_vnode_freelist;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
RetainPtr<Vnode> m_root_vnode;
|
2018-10-14 03:59:22 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
HashMap<dword, CharacterDevice*> m_character_devices;
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|