summaryrefslogtreecommitdiff
path: root/Kernel/VirtualFileSystem.cpp
blob: 1f65d6742a9c50962e86ccf44f6d1d58a31abc7a (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include "VirtualFileSystem.h"
#include "FileDescriptor.h"
#include "FileSystem.h"
#include <AK/FileSystemPath.h>
#include <AK/StringBuilder.h>
#include <AK/kmalloc.h>
#include <AK/kstdio.h>
#include <AK/ktime.h>
#include "CharacterDevice.h"
#include <LibC/errno_numbers.h>
#include <Kernel/Process.h>

//#define VFS_DEBUG

static VFS* s_the;

VFS& VFS::the()
{
    ASSERT(s_the);
    return *s_the;
}

VFS::VFS()
{
#ifdef VFS_DEBUG
    kprintf("VFS: Constructing VFS\n");
#endif
    s_the = this;
}

VFS::~VFS()
{
}

InodeIdentifier VFS::root_inode_id() const
{
    ASSERT(m_root_inode);
    return m_root_inode->identifier();
}

bool VFS::mount(RetainPtr<FS>&& file_system, const String& path)
{
    ASSERT(file_system);
    int error;
    auto inode = old_resolve_path(path, root_inode_id(), error);
    if (!inode.is_valid()) {
        kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
        return false;
    }

    kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", file_system->class_name(), file_system.ptr(), path.characters(), inode.index());
    // FIXME: check that this is not already a mount point
    auto mount = make<Mount>(inode, move(file_system));
    m_mounts.append(move(mount));
    return true;
}

bool VFS::mount_root(RetainPtr<FS>&& file_system)
{
    if (m_root_inode) {
        kprintf("VFS: mount_root can't mount another root\n");
        return false;
    }

    auto mount = make<Mount>(InodeIdentifier(), move(file_system));

    auto root_inode_id = mount->guest().fs()->root_inode();
    auto root_inode = mount->guest().fs()->get_inode(root_inode_id);
    if (!root_inode->is_directory()) {
        kprintf("VFS: root inode (%02u:%08u) for / is not a directory :(\n", root_inode_id.fsid(), root_inode_id.index());
        return false;
    }

    m_root_inode = move(root_inode);

    kprintf("VFS: mounted root on %s{%p}\n",
        m_root_inode->fs().class_name(),
        &m_root_inode->fs());

    m_mounts.append(move(mount));
    return true;
}

auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
{
    for (auto& mount : m_mounts) {
        if (mount->host() == inode)
            return mount.ptr();
    }
    return nullptr;
}

auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
{
    for (auto& mount : m_mounts) {
        if (mount->guest() == inode)
            return mount.ptr();
    }
    return nullptr;
}

bool VFS::is_vfs_root(InodeIdentifier inode) const
{
    return inode == root_inode_id();
}

void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
{
    dir_inode.traverse_as_directory([&] (const FS::DirectoryEntry& entry) {
        InodeIdentifier resolved_inode;
        if (auto mount = find_mount_for_host(entry.inode))
            resolved_inode = mount->guest();
        else
            resolved_inode = entry.inode;

        if (dir_inode.identifier().is_root_inode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
            auto mount = find_mount_for_guest(entry.inode);
            ASSERT(mount);
            resolved_inode = mount->host();
        }
        callback(FS::DirectoryEntry(entry.name, entry.name_length, resolved_inode, entry.file_type));
        return true;
    });
}

KResultOr<Retained<FileDescriptor>> VFS::open(RetainPtr<Device>&& device, int options)
{
    // FIXME: Respect options.
    (void)options;
    return FileDescriptor::create(move(device));
}

KResult VFS::utime(const String& path, Inode& base, time_t atime, time_t mtime)
{
    auto descriptor_or_error = VFS::the().open(move(path), 0, 0, base);
    if (descriptor_or_error.is_error())
        return descriptor_or_error.error();
    auto& inode = *descriptor_or_error.value()->inode();
    if (inode.fs().is_readonly())
        return KResult(-EROFS);
    if (inode.metadata().uid != current->euid())
        return KResult(-EACCES);

    int error = inode.set_atime(atime);
    if (error)
        return KResult(error);
    error = inode.set_mtime(mtime);
    if (error)
        return KResult(error);
    return KSuccess;
}

KResult VFS::stat(const String& path, int options, Inode& base, struct stat& statbuf)
{
    auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options);
    if (inode_or_error.is_error())
        return inode_or_error.error();
    return FileDescriptor::create(inode_or_error.value().ptr())->fstat(statbuf);
}

KResultOr<Retained<FileDescriptor>> VFS::open(const String& path, int options, mode_t mode, Inode& base)
{
    auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options);
    if (options & O_CREAT) {
        if (inode_or_error.is_error())
            return create(path, options, mode, base);
        if (options & O_EXCL)
            return KResult(-EEXIST);
    }
    if (inode_or_error.is_error())
        return inode_or_error.error();

    auto metadata = inode_or_error.value()->metadata();

    // NOTE: Read permission is a bit weird, since O_RDONLY == 0,
    //       so we check if (NOT write_only OR read_and_write)
    if (!(options & O_WRONLY) || (options & O_RDWR)) {
        if (!metadata.may_read(*current))
            return KResult(-EACCES);
    }
    if ((options & O_WRONLY) || (options & O_RDWR)) {
        if (!metadata.may_write(*current))
            return KResult(-EACCES);
        if (metadata.is_directory())
            return KResult(-EISDIR);
    }

    if (metadata.is_device()) {
        auto it = m_devices.find(encoded_device(metadata.major_device, metadata.minor_device));
        if (it == m_devices.end()) {
            kprintf("VFS::open: no such device %u,%u\n", metadata.major_device, metadata.minor_device);
            return KResult(-ENODEV);
        }
        auto descriptor_or_error = (*it).value->open(options);
        if (descriptor_or_error.is_error())
            return descriptor_or_error.error();
        descriptor_or_error.value()->set_original_inode(Badge<VFS>(), *inode_or_error.value());
        return descriptor_or_error;
    }
    return FileDescriptor::create(*inode_or_error.value());
}

KResultOr<Retained<FileDescriptor>> VFS::create(const String& path, int options, mode_t mode, Inode& base)
{
    (void)options;

    if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
        // Turn it into a regular file. (This feels rather hackish.)
        mode |= 0100000;
    }

    RetainPtr<Inode> parent_inode;
    auto existing_file_or_error = resolve_path_to_inode(path, base, &parent_inode);
    if (!existing_file_or_error.is_error())
        return KResult(-EEXIST);
    if (!parent_inode)
        return KResult(-ENOENT);
    if (existing_file_or_error.error() != -ENOENT)
        return existing_file_or_error.error();
    if (!parent_inode->metadata().may_write(*current))
        return KResult(-EACCES);

    FileSystemPath p(path);
    dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index());
    int error;
    auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), mode, 0, error);
    if (!new_file)
        return KResult(error);

    return FileDescriptor::create(move(new_file));
}

KResult VFS::mkdir(const String& path, mode_t mode, Inode& base)
{
    RetainPtr<Inode> parent_inode;
    auto result = resolve_path_to_inode(path, base, &parent_inode);
    if (!result.is_error())
        return KResult(-EEXIST);
    if (!parent_inode)
        return KResult(-ENOENT);
    if (result.error() != -ENOENT)
        return result.error();

    if (!parent_inode->metadata().may_write(*current))
        return KResult(-EACCES);

    FileSystemPath p(path);
    dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index());
    int error;
    auto new_dir = parent_inode->fs().create_directory(parent_inode->identifier(), p.basename(), mode, error);
    if (new_dir)
        return KSuccess;
    return KResult(error);
}

KResult VFS::access(const String& path, int mode, Inode& base)
{
    auto inode_or_error = resolve_path_to_inode(path, base);
    if (inode_or_error.is_error())
        return inode_or_error.error();
    auto inode = inode_or_error.value();
    auto metadata = inode->metadata();
    if (mode & R_OK) {
        if (!metadata.may_read(*current))
            return KResult(-EACCES);
    }
    if (mode & W_OK) {
        if (!metadata.may_write(*current))
            return KResult(-EACCES);
    }
    if (mode & X_OK) {
        if (!metadata.may_execute(*current))
            return KResult(-EACCES);
    }
    return KSuccess;
}

KResultOr<Retained<Inode>> VFS::open_directory(const String& path, Inode& base)
{
    auto inode_or_error = resolve_path_to_inode(path, base);
    if (inode_or_error.is_error())
        return inode_or_error.error();
    auto inode = inode_or_error.value();
    if (!inode->is_directory())
        return KResult(-ENOTDIR);
    if (!inode->metadata().may_execute(*current))
        return KResult(-EACCES);
    return Retained<Inode>(*inode);
}

KResult VFS::chmod(Inode& inode, mode_t mode)
{
    if (inode.fs().is_readonly())
        return KResult(-EROFS);

    if (current->euid() != inode.metadata().uid && !current->is_superuser())
        return KResult(-EPERM);

    // Only change the permission bits.
    mode = (inode.mode() & ~04777u) | (mode & 04777u);
    return inode.chmod(mode);
}

KResult VFS::chmod(const String& path, mode_t mode, Inode& base)
{
    auto inode_or_error = resolve_path_to_inode(path, base);
    if (inode_or_error.is_error())
        return inode_or_error.error();
    auto inode = inode_or_error.value();
    return chmod(*inode, mode);
}

KResult VFS::chown(const String& path, uid_t a_uid, gid_t a_gid, Inode& base)
{
    auto inode_or_error = resolve_path_to_inode(path, base);
    if (inode_or_error.is_error())
        return inode_or_error.error();
    auto inode = inode_or_error.value();

    if (inode->fs().is_readonly())
        return KResult(-EROFS);

    if (current->euid() != inode->metadata().uid && !current->is_superuser())
        return KResult(-EPERM);

    uid_t new_uid = inode->metadata().uid;
    gid_t new_gid = inode->metadata().gid;

    if (a_uid != (uid_t)-1) {
        if (current->euid() != a_uid && !current->is_superuser())
            return KResult(-EPERM);
        new_uid = a_uid;
    }
    if (a_gid != (gid_t)-1) {
        if (!current->in_group(a_gid) && !current->is_superuser())
            return KResult(-EPERM);
        new_gid = a_gid;
    }

    dbgprintf("VFS::chown(): inode %u:%u <- uid:%d, gid:%d\n", inode->fsid(), inode->index(), new_uid, new_gid);
    return inode->chown(new_uid, new_gid);
}

KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(const String& path, Inode& base, RetainPtr<Inode>* parent_inode, int options)
{
    // FIXME: This won't work nicely across mount boundaries.
    FileSystemPath p(path);
    if (!p.is_valid())
        return KResult(-EINVAL);
    InodeIdentifier parent_id;
    auto result = resolve_path(path, base.identifier(), options, &parent_id);
    if (parent_inode && parent_id.is_valid())
        *parent_inode = get_inode(parent_id);
    if (result.is_error())
        return result.error();
    return Retained<Inode>(*get_inode(result.value()));
}

KResult VFS::link(const String& old_path, const String& new_path, Inode& base)
{
    auto old_inode_or_error = resolve_path_to_inode(old_path, base);
    if (old_inode_or_error.is_error())
        return old_inode_or_error.error();
    auto old_inode = old_inode_or_error.value();

    RetainPtr<Inode> parent_inode;
    auto new_inode_or_error = resolve_path_to_inode(new_path, base, &parent_inode);
    if (!new_inode_or_error.is_error())
        return KResult(-EEXIST);

    if (!parent_inode)
        return KResult(-ENOENT);

    if (parent_inode->fsid() != old_inode->fsid())
        return KResult(-EXDEV);

    if (parent_inode->fs().is_readonly())
        return KResult(-EROFS);

    if (!parent_inode->metadata().may_write(*current))
        return KResult(-EACCES);

    return parent_inode->add_child(old_inode->identifier(), FileSystemPath(new_path).basename(), 0);
}

KResult VFS::unlink(const String& path, Inode& base)
{
    RetainPtr<Inode> parent_inode;
    auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode);
    if (inode_or_error.is_error())
        return inode_or_error.error();
    auto inode = inode_or_error.value();

    if (inode->is_directory())
        return KResult(-EISDIR);

    if (!parent_inode->metadata().may_write(*current))
        return KResult(-EACCES);

    return parent_inode->remove_child(FileSystemPath(path).basename());
}

KResult VFS::symlink(const String& target, const String& linkpath, Inode& base)
{
    RetainPtr<Inode> parent_inode;
    auto existing_file_or_error = resolve_path_to_inode(linkpath, base, &parent_inode);
    if (!existing_file_or_error.is_error())
        return KResult(-EEXIST);
    if (!parent_inode)
        return KResult(-ENOENT);
    if (existing_file_or_error.error() != -ENOENT)
        return existing_file_or_error.error();
    if (!parent_inode->metadata().may_write(*current))
        return KResult(-EACCES);

    FileSystemPath p(linkpath);
    dbgprintf("VFS::symlink: '%s' (-> '%s') in %u:%u\n", p.basename().characters(), target.characters(), parent_inode->fsid(), parent_inode->index());
    int error;
    auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), 0120644, 0, error);
    if (!new_file)
        return KResult(error);
    ssize_t nwritten = new_file->write_bytes(0, target.length(), (const byte*)target.characters(), nullptr);
    if (nwritten < 0)
        return KResult(nwritten);
    return KSuccess;
}

KResult VFS::rmdir(const String& path, Inode& base)
{
    RetainPtr<Inode> parent_inode;
    auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode);
    if (inode_or_error.is_error())
        return KResult(inode_or_error.error());

    auto inode = inode_or_error.value();
    if (inode->fs().is_readonly())
        return KResult(-EROFS);

    // FIXME: We should return EINVAL if the last component of the path is "."
    // FIXME: We should return ENOTEMPTY if the last component of the path is ".."

    if (!inode->is_directory())
        return KResult(-ENOTDIR);

    if (!parent_inode->metadata().may_write(*current))
        return KResult(-EACCES);

    if (inode->directory_entry_count() != 2)
        return KResult(-ENOTEMPTY);

    auto result = inode->remove_child(".");
    if (result.is_error())
        return result;

    result = inode->remove_child("..");
    if (result.is_error())
        return result;

    // FIXME: The reverse_lookup here can definitely be avoided.
    return parent_inode->remove_child(parent_inode->reverse_lookup(inode->identifier()));
}

KResultOr<InodeIdentifier> VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode)
{
    auto symlink_contents = symlink_inode.read_entire();
    if (!symlink_contents)
        return KResult(-ENOENT);
    auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.size());
#ifdef VFS_DEBUG
    kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
#endif
    return resolve_path(linkee, base);
}

RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
{
    if (!inode_id.is_valid())
        return nullptr;
    return inode_id.fs()->get_inode(inode_id);
}

KResultOr<String> VFS::absolute_path(InodeIdentifier inode_id)
{
    auto inode = get_inode(inode_id);
    if (!inode)
        return KResult(-EIO);
    return absolute_path(*inode);
}

KResultOr<String> VFS::absolute_path(Inode& core_inode)
{
    Vector<InodeIdentifier> lineage;
    RetainPtr<Inode> inode = &core_inode;
    while (inode->identifier() != root_inode_id()) {
        if (auto* mount = find_mount_for_guest(inode->identifier()))
            lineage.append(mount->host());
        else
            lineage.append(inode->identifier());

        InodeIdentifier parent_id;
        if (inode->is_directory()) {
            auto result = resolve_path("..", inode->identifier());
            if (result.is_error())
                return result.error();
            parent_id = result.value();
        } else {
            parent_id = inode->parent()->identifier();
        }
        if (!parent_id.is_valid())
            return KResult(-EIO);
        inode = get_inode(parent_id);
    }
    if (lineage.is_empty())
        return "/";
    lineage.append(root_inode_id());
    StringBuilder builder;
    for (size_t i = lineage.size() - 1; i >= 1; --i) {
        auto& child = lineage[i - 1];
        auto parent = lineage[i];
        if (auto* mount = find_mount_for_host(parent))
            parent = mount->guest();
        builder.append('/');
        auto parent_inode = get_inode(parent);
        builder.append(parent_inode->reverse_lookup(child));
    }
    return builder.to_string();
}

KResultOr<InodeIdentifier> VFS::resolve_path(const String& path, InodeIdentifier base, int options, InodeIdentifier* parent_id)
{
    if (path.is_empty())
        return KResult(-EINVAL);

    auto parts = path.split('/');
    InodeIdentifier crumb_id;

    if (path[0] == '/')
        crumb_id = root_inode_id();
    else
        crumb_id = base.is_valid() ? base : root_inode_id();

    if (parent_id)
        *parent_id = crumb_id;

    for (int i = 0; i < parts.size(); ++i) {
        bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
        auto& part = parts[i];
        if (part.is_empty())
            break;
        auto crumb_inode = get_inode(crumb_id);
        if (!crumb_inode) {
#ifdef VFS_DEBUG
            kprintf("invalid metadata\n");
#endif
            return KResult(-EIO);
        }
        auto metadata = crumb_inode->metadata();
        if (!metadata.is_directory()) {
#ifdef VFS_DEBUG
            kprintf("parent of <%s> not directory, it's inode %u:%u / %u:%u, mode: %u, size: %u\n", part.characters(), crumb_id.fsid(), crumb_id.index(), metadata.inode.fsid(), metadata.inode.index(), metadata.mode, metadata.size);
#endif
            return KResult(-ENOTDIR);
        }
        if (!metadata.may_execute(*current))
            return KResult(-EACCES);
        auto parent = crumb_id;
        crumb_id = crumb_inode->lookup(part);
        if (!crumb_id.is_valid()) {
#ifdef VFS_DEBUG
            kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
#endif
            return KResult(-ENOENT);
        }
#ifdef VFS_DEBUG
        kprintf("<%s> %u:%u\n", part.characters(), crumb_id.fsid(), crumb_id.index());
#endif
        if (auto mount = find_mount_for_host(crumb_id)) {
#ifdef VFS_DEBUG
            kprintf("  -- is host\n");
#endif
            crumb_id = mount->guest();
        }
        if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
#ifdef VFS_DEBUG
            kprintf("  -- is guest\n");
#endif
            auto mount = find_mount_for_guest(crumb_id);
            auto dir_inode = get_inode(mount->host());
            ASSERT(dir_inode);
            crumb_id = dir_inode->lookup("..");
        }
        crumb_inode = get_inode(crumb_id);
        ASSERT(crumb_inode);
        metadata = crumb_inode->metadata();
        if (metadata.is_directory()) {
            if (i != parts.size() - 1) {
                if (parent_id)
                    *parent_id = crumb_id;
            }
        }
        if (metadata.is_symlink()) {
            if (i == parts.size() - 1) {
                if (options & O_NOFOLLOW)
                    return KResult(-ELOOP);
                if (options & O_NOFOLLOW_NOERROR)
                    return crumb_id;
            }
            auto result = resolve_symbolic_link(parent, *crumb_inode);
            if (result.is_error())
                return KResult(-ENOENT);
            crumb_id = result.value();
            ASSERT(crumb_id.is_valid());
        }
    }

    return crumb_id;
}

InodeIdentifier VFS::old_resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* parent_id)
{
    auto result = resolve_path(path, base, options, parent_id);
    if (result.is_error()) {
        error = result.error();
        return { };
    }
    return result.value();
}

VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
    : m_host(host)
    , m_guest(guest_fs->root_inode())
    , m_guest_fs(move(guest_fs))
{
}

void VFS::register_device(Device& device)
{
    m_devices.set(encoded_device(device.major(), device.minor()), &device);
}

void VFS::unregister_device(Device& device)
{
    m_devices.remove(encoded_device(device.major(), device.minor()));
}

Device* VFS::get_device(unsigned major, unsigned minor)
{
    auto it = m_devices.find(encoded_device(major, minor));
    if (it == m_devices.end())
        return nullptr;
    return (*it).value;
}

void VFS::for_each_mount(Function<void(const Mount&)> callback) const
{
    for (auto& mount : m_mounts) {
        callback(*mount);
    }
}

void VFS::sync()
{
    FS::sync();
}