summaryrefslogtreecommitdiff
path: root/Kernel/FileSystem/Inode.cpp
blob: ef4fbe82071befe49cdeb75e8ad898ce3460ca4a (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <AK/StringBuilder.h>
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/Net/LocalSocket.h>
#include <Kernel/VM/VMObject.h>

HashTable<Inode*>& all_inodes()
{
    static HashTable<Inode*>* s_inode_set;
    if (!s_inode_set)
        s_inode_set = new HashTable<Inode*>();
    return *s_inode_set;
}

void Inode::sync()
{
    Vector<Retained<Inode>, 32> inodes;
    {
        InterruptDisabler disabler;
        for (auto* inode : all_inodes()) {
            if (inode->is_metadata_dirty())
                inodes.append(*inode);
        }
    }

    for (auto& inode : inodes) {
        ASSERT(inode->is_metadata_dirty());
        inode->flush_metadata();
    }
}

ByteBuffer Inode::read_entire(FileDescription* descriptor) const
{
    size_t initial_size = metadata().size ? metadata().size : 4096;
    StringBuilder builder(initial_size);

    ssize_t nread;
    byte buffer[4096];
    off_t offset = 0;
    for (;;) {
        nread = read_bytes(offset, sizeof(buffer), buffer, descriptor);
        ASSERT(nread <= (ssize_t)sizeof(buffer));
        if (nread <= 0)
            break;
        builder.append((const char*)buffer, nread);
        offset += nread;
    }
    if (nread < 0) {
        kprintf("Inode::read_entire: ERROR: %d\n", nread);
        return nullptr;
    }

    return builder.to_byte_buffer();
}

unsigned Inode::fsid() const
{
    return m_fs.fsid();
}

Inode::Inode(FS& fs, unsigned index)
    : m_fs(fs)
    , m_index(index)
{
    all_inodes().set(this);
}

Inode::~Inode()
{
    all_inodes().remove(this);
}

void Inode::will_be_destroyed()
{
    if (m_metadata_dirty)
        flush_metadata();
}

void Inode::inode_contents_changed(off_t offset, ssize_t size, const byte* data)
{
    if (m_vmo)
        m_vmo->inode_contents_changed({}, offset, size, data);
}

void Inode::inode_size_changed(size_t old_size, size_t new_size)
{
    if (m_vmo)
        m_vmo->inode_size_changed({}, old_size, new_size);
}

int Inode::set_atime(time_t)
{
    return -ENOTIMPL;
}

int Inode::set_ctime(time_t)
{
    return -ENOTIMPL;
}

int Inode::set_mtime(time_t)
{
    return -ENOTIMPL;
}

int Inode::increment_link_count()
{
    return -ENOTIMPL;
}

int Inode::decrement_link_count()
{
    return -ENOTIMPL;
}

void Inode::set_vmo(VMObject& vmo)
{
    m_vmo = vmo.make_weak_ptr();
}

bool Inode::bind_socket(LocalSocket& socket)
{
    ASSERT(!m_socket);
    m_socket = socket;
    return true;
}

bool Inode::unbind_socket()
{
    ASSERT(m_socket);
    m_socket = nullptr;
    return true;
}