summaryrefslogtreecommitdiff
path: root/Kernel/FileSystem/VirtualFileSystem.h
blob: 548d860fa3c09b86762e446f57748108c0998036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once

#include <AK/AKString.h>
#include <AK/Badge.h>
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/RetainPtr.h>
#include <AK/Vector.h>
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/InodeIdentifier.h>
#include <Kernel/FileSystem/InodeMetadata.h>
#include <Kernel/KResult.h>

#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 0100
#define O_EXCL 0200
#define O_NOCTTY 0400
#define O_TRUNC 01000
#define O_APPEND 02000
#define O_NONBLOCK 04000
#define O_DIRECTORY 00200000
#define O_NOFOLLOW 00400000
#define O_CLOEXEC 02000000
#define O_NOFOLLOW_NOERROR 0x4000000

class Custody;
class Device;
class FileDescriptor;

class VFS {
    AK_MAKE_ETERNAL
public:
    class Mount {
    public:
        Mount(RetainPtr<Custody>&&, Retained<FS>&&);

        InodeIdentifier host() const;
        InodeIdentifier guest() const { return m_guest; }

        const FS& guest_fs() const { return *m_guest_fs; }

        String absolute_path() const;

    private:
        InodeIdentifier m_host;
        InodeIdentifier m_guest;
        Retained<FS> m_guest_fs;
        RetainPtr<Custody> m_host_custody;
    };

    [[gnu::pure]] static VFS& the();

    VFS();
    ~VFS();

    bool mount_root(Retained<FS>&&);
    bool mount(Retained<FS>&&, StringView path);

    KResultOr<Retained<FileDescriptor>> open(RetainPtr<Device>&&, int options);
    KResultOr<Retained<FileDescriptor>> open(StringView path, int options, mode_t mode, Custody& base);
    KResultOr<Retained<FileDescriptor>> create(StringView path, int options, mode_t mode, Custody& base);
    KResult mkdir(StringView path, mode_t mode, Custody& base);
    KResult link(StringView old_path, StringView new_path, Custody& base);
    KResult unlink(StringView path, Custody& base);
    KResult symlink(StringView target, StringView linkpath, Custody& base);
    KResult rmdir(StringView path, Custody& base);
    KResult chmod(StringView path, mode_t, Custody& base);
    KResult fchmod(Inode&, mode_t);
    KResult chown(StringView path, uid_t, gid_t, Custody& base);
    KResult chown(Inode&, uid_t, gid_t);
    KResult access(StringView path, int mode, Custody& base);
    KResult stat(StringView path, int options, Custody& base, struct stat&);
    KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
    KResult rename(StringView oldpath, StringView newpath, Custody& base);
    KResult mknod(StringView path, mode_t, dev_t, Custody& base);
    KResultOr<Retained<Custody>> open_directory(StringView path, Custody& base);

    void register_device(Badge<Device>, Device&);
    void unregister_device(Badge<Device>, Device&);

    size_t mount_count() const { return m_mounts.size(); }
    void for_each_mount(Function<void(const Mount&)>) const;

    InodeIdentifier root_inode_id() const;

    void sync();

    Device* get_device(unsigned major, unsigned minor);

    Custody& root_custody();
    KResultOr<Retained<Custody>> resolve_path(StringView path, Custody& base, RetainPtr<Custody>* parent = nullptr, int options = 0);

private:
    friend class FileDescriptor;

    RetainPtr<Inode> get_inode(InodeIdentifier);

    bool is_vfs_root(InodeIdentifier) const;

    void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);

    Mount* find_mount_for_host(InodeIdentifier);
    Mount* find_mount_for_guest(InodeIdentifier);

    RetainPtr<Inode> m_root_inode;
    Vector<OwnPtr<Mount>> m_mounts;
    HashMap<dword, Device*> m_devices;

    RetainPtr<Custody> m_root_custody;
};