summaryrefslogtreecommitdiff
path: root/Kernel/FileSystem/DevTmpFS.cpp
blob: 420ccf4ca347043fdb6a1a00985d29423f0f1535 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/StringView.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/DevTmpFS.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>

namespace Kernel {

ErrorOr<NonnullRefPtr<DevTmpFS>> DevTmpFS::try_create()
{
    return adopt_nonnull_ref_or_enomem(new (nothrow) DevTmpFS);
}

DevTmpFS::DevTmpFS()
{
}

size_t DevTmpFS::allocate_inode_index()
{
    MutexLocker locker(m_lock);
    m_next_inode_index = m_next_inode_index.value() + 1;
    VERIFY(m_next_inode_index > 0);
    return 1 + m_next_inode_index.value();
}

DevTmpFS::~DevTmpFS()
{
}

ErrorOr<void> DevTmpFS::initialize()
{
    m_root_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevTmpFSRootDirectoryInode(*this)));
    return {};
}

Inode& DevTmpFS::root_inode()
{
    return *m_root_inode;
}

DevTmpFSInode::DevTmpFSInode(DevTmpFS& fs)
    : Inode(fs, fs.allocate_inode_index())
{
}

DevTmpFSInode::DevTmpFSInode(DevTmpFS& fs, MajorNumber major_number, MinorNumber minor_number)
    : Inode(fs, fs.allocate_inode_index())
    , m_major_number(major_number)
    , m_minor_number(minor_number)
{
}

ErrorOr<size_t> DevTmpFSInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const
{
    VERIFY_NOT_REACHED();
}

ErrorOr<void> DevTmpFSInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const
{
    VERIFY_NOT_REACHED();
}

ErrorOr<NonnullRefPtr<Inode>> DevTmpFSInode::lookup(StringView)
{
    VERIFY_NOT_REACHED();
}

ErrorOr<void> DevTmpFSInode::flush_metadata()
{
    return {};
}

ErrorOr<size_t> DevTmpFSInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*)
{
    VERIFY_NOT_REACHED();
}

ErrorOr<NonnullRefPtr<Inode>> DevTmpFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID)
{
    VERIFY_NOT_REACHED();
}

ErrorOr<void> DevTmpFSInode::add_child(Inode&, StringView, mode_t)
{
    VERIFY_NOT_REACHED();
}

InodeMetadata DevTmpFSInode::metadata() const
{
    MutexLocker locker(m_inode_lock);
    VERIFY((m_mode & 0777) == m_mode);
    InodeMetadata metadata;
    metadata.uid = m_uid;
    metadata.gid = m_gid;
    metadata.size = 0;
    metadata.mtime = mepoch;
    switch (node_type()) {
    case Type::RootDirectory:
        metadata.inode = { fsid(), 1 };
        metadata.mode = 0040555;
        metadata.uid = 0;
        metadata.gid = 0;
        metadata.size = 0;
        metadata.mtime = mepoch;
        break;
    case Type::Directory:
        metadata.inode = { fsid(), index() };
        metadata.mode = S_IFDIR | m_mode;
        break;
    case Type::BlockDevice:
        metadata.inode = { fsid(), index() };
        metadata.mode = S_IFBLK | m_mode;
        metadata.major_device = m_major_number;
        metadata.minor_device = m_minor_number;
        break;
    case Type::CharacterDevice:
        metadata.inode = { fsid(), index() };
        metadata.mode = S_IFCHR | m_mode;
        metadata.major_device = m_major_number;
        metadata.minor_device = m_minor_number;
        break;
    case Type::Link:
        metadata.inode = { fsid(), index() };
        metadata.mode = S_IFLNK | m_mode;
        break;
    default:
        VERIFY_NOT_REACHED();
    }
    return metadata;
}

ErrorOr<void> DevTmpFSInode::remove_child(StringView)
{
    VERIFY_NOT_REACHED();
}

ErrorOr<void> DevTmpFSInode::chmod(mode_t mode)
{
    MutexLocker locker(m_inode_lock);
    mode &= 0777;
    if (m_mode == mode)
        return {};
    m_mode = mode;
    return {};
}

ErrorOr<void> DevTmpFSInode::chown(UserID uid, GroupID gid)
{
    MutexLocker locker(m_inode_lock);
    m_uid = uid;
    m_gid = gid;
    return {};
}

ErrorOr<void> DevTmpFSInode::truncate(u64)
{
    return EPERM;
}

StringView DevTmpFSLinkInode::name() const
{
    return m_name->view();
}

DevTmpFSLinkInode::~DevTmpFSLinkInode()
{
}

DevTmpFSLinkInode::DevTmpFSLinkInode(DevTmpFS& fs, NonnullOwnPtr<KString> name)
    : DevTmpFSInode(fs)
    , m_name(move(name))
{
}

ErrorOr<size_t> DevTmpFSLinkInode::read_bytes(off_t offset, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{
    MutexLocker locker(m_inode_lock);
    VERIFY(offset == 0);
    VERIFY(m_link);
    TRY(buffer.write(m_link->characters() + offset, m_link->length()));
    return m_link->length();
}

ErrorOr<size_t> DevTmpFSLinkInode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription*)
{
    auto new_string = TRY(buffer.try_copy_into_kstring(count));

    MutexLocker locker(m_inode_lock);
    VERIFY(offset == 0);
    VERIFY(buffer.is_kernel_buffer());
    m_link = move(new_string);
    return count;
}

DevTmpFSDirectoryInode::DevTmpFSDirectoryInode(DevTmpFS& fs)
    : DevTmpFSInode(fs)
{
}
DevTmpFSDirectoryInode::DevTmpFSDirectoryInode(DevTmpFS& fs, NonnullOwnPtr<KString> name)
    : DevTmpFSInode(fs)
    , m_name(move(name))
{
}
DevTmpFSDirectoryInode::~DevTmpFSDirectoryInode()
{
}

ErrorOr<void> DevTmpFSDirectoryInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
{
    MutexLocker locker(m_inode_lock);
    TRY(callback({ ".", identifier(), 0 }));
    TRY(callback({ "..", identifier(), 0 }));
    for (auto& node : m_nodes) {
        InodeIdentifier identifier = { fsid(), node.index() };
        TRY(callback({ node.name(), identifier, 0 }));
    }
    return {};
}

ErrorOr<NonnullRefPtr<Inode>> DevTmpFSDirectoryInode::lookup(StringView name)
{
    MutexLocker locker(m_inode_lock);
    for (auto& node : m_nodes) {
        if (node.name() == name) {
            return node;
        }
    }
    return Error::from_errno(ENOENT);
}

ErrorOr<void> DevTmpFSDirectoryInode::remove_child(StringView name)
{
    MutexLocker locker(m_inode_lock);
    for (auto& node : m_nodes) {
        if (node.name() == name) {
            m_nodes.remove(node);
            return {};
        }
    }
    return Error::from_errno(ENOENT);
}

ErrorOr<NonnullRefPtr<Inode>> DevTmpFSDirectoryInode::create_child(StringView name, mode_t mode, dev_t device_mode, UserID, GroupID)
{
    MutexLocker locker(m_inode_lock);
    for (auto& node : m_nodes) {
        if (node.name() == name)
            return Error::from_errno(EEXIST);
    }

    InodeMetadata metadata;
    metadata.mode = mode;
    if (metadata.is_directory()) {
        auto name_kstring = TRY(KString::try_create(name));
        auto new_directory_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevTmpFSDirectoryInode(fs(), move(name_kstring))));
        m_nodes.append(*new_directory_inode);
        return new_directory_inode;
    }
    if (metadata.is_device()) {
        auto name_kstring = TRY(KString::try_create(name));
        auto major = major_from_encoded_device(device_mode);
        auto minor = minor_from_encoded_device(device_mode);
        auto new_device_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevTmpFSDeviceInode(fs(), major, minor, is_block_device(mode), move(name_kstring))));
        TRY(new_device_inode->chmod(mode));
        m_nodes.append(*new_device_inode);
        return new_device_inode;
    }
    if (metadata.is_symlink()) {
        auto name_kstring = TRY(KString::try_create(name));
        auto new_link_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevTmpFSLinkInode(fs(), move(name_kstring))));
        TRY(new_link_inode->chmod(mode));
        m_nodes.append(*new_link_inode);
        return new_link_inode;
    }
    return EROFS;
}

DevTmpFSRootDirectoryInode::DevTmpFSRootDirectoryInode(DevTmpFS& fs)
    : DevTmpFSDirectoryInode(fs)
{
    m_mode = 0555;
}
DevTmpFSRootDirectoryInode::~DevTmpFSRootDirectoryInode()
{
}
ErrorOr<void> DevTmpFSRootDirectoryInode::chmod(mode_t)
{
    return EPERM;
}

ErrorOr<void> DevTmpFSRootDirectoryInode::chown(UserID, GroupID)
{
    return EPERM;
}

DevTmpFSDeviceInode::DevTmpFSDeviceInode(DevTmpFS& fs, MajorNumber major_number, MinorNumber minor_number, bool block_device, NonnullOwnPtr<KString> name)
    : DevTmpFSInode(fs, major_number, minor_number)
    , m_name(move(name))
    , m_block_device(block_device)
{
}

DevTmpFSDeviceInode::~DevTmpFSDeviceInode()
{
}

StringView DevTmpFSDeviceInode::name() const
{
    return m_name->view();
}

ErrorOr<size_t> DevTmpFSDeviceInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
{
    MutexLocker locker(m_inode_lock);
    VERIFY(!!description);
    RefPtr<Device> device = DeviceManagement::the().get_device(m_major_number, m_minor_number);
    if (!device)
        return Error::from_errno(ENODEV);
    if (!device->can_read(*description, offset))
        return Error::from_errno(ENOTIMPL);
    auto result = const_cast<Device&>(*device).read(*description, offset, buffer, count);
    if (result.is_error())
        return result;
    return result.value();
}

ErrorOr<size_t> DevTmpFSDeviceInode::write_bytes(off_t offset, size_t count, const UserOrKernelBuffer& buffer, OpenFileDescription* description)
{
    MutexLocker locker(m_inode_lock);
    VERIFY(!!description);
    RefPtr<Device> device = DeviceManagement::the().get_device(m_major_number, m_minor_number);
    if (!device)
        return Error::from_errno(ENODEV);
    if (!device->can_write(*description, offset))
        return Error::from_errno(ENOTIMPL);
    auto result = const_cast<Device&>(*device).write(*description, offset, buffer, count);
    if (result.is_error())
        return result;
    return result.value();
}

}