aboutsummaryrefslogtreecommitdiff
path: root/kernel/kernel/vfs/filesystem.hpp
blob: 0c33b2b4191b530907f341ba4a7763eb19987bfc (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
#ifndef TEACHOS_KERNEL_VFS_FILESYSTEM_HPP
#define TEACHOS_KERNEL_VFS_FILESYSTEM_HPP

#include <kernel/vfs/driver_state.hpp>
#include <kernel/vfs/inode.hpp>

#include <kapi/filesystem.hpp>

#include <kstd/memory.hpp>
#include <kstd/result.hpp>
#include <kstd/system_error.hpp>
#include <kstd/vector.hpp>

#include <cstdint>
#include <optional>
#include <string_view>
#include <utility>

namespace kernel::vfs
{
  //! The base class for all filesystems.
  //!
  //! This class provides a common interface for managing files and directories within the virtual filesystem.
  //! Filesystem implementations must derive from this class.
  struct filesystem
  {
    using filesystem_ptr = kstd::shared_ptr<filesystem>;
    using driver_data_ptr = kstd::shared_ptr<driver_state>;
    using inode_ptr = kstd::shared_ptr<inode>;
    using mount_result = std::pair<inode_ptr, driver_data_ptr>;

    //! Virtual destructor enabling polymorphic destruction.
    virtual ~filesystem() = default;

    //! Check if the data represented by the given inode is recognized as this filesystem.
    //!
    //! @param inode The inode to probe, if any.
    //! @return the priority (higher is better) of the match on success, an error otherwise.
    [[nodiscard]] virtual auto probe(inode_ptr const & inode) const -> kstd::result<std::uint32_t> = 0;

    //! Initializes the filesystem with the given inode.
    //!
    //! Typically, the backing inode is the inode representing the block device or another inode which contains the
    //! filesystem data (e.g. a file on an already mounted filesystem).
    //!
    //! @param backing_inode The inode to use as the backing inode for the filesystem.
    //! @return The root inode of the mounted filesystem on success, and error otherwise.
    virtual auto mount(inode_ptr const & backing_inode) -> kstd::result<mount_result> = 0;

    //! Find a child inode below the given parent inode with the specified name.
    //!
    //! This method must be implemented by concrete filesystem subclasses to provide the logic for traversing the
    //! filesystem structure and finding the requested inode.
    //!
    //! @param parent The parent inode.
    //! @param name The name of the child inode to look up.
    //! @param driver_data The per-mount data used by the driver.
    //! @return A pointer to the requested child inode on success, an error otherwise.
    [[nodiscard]] virtual auto lookup(inode_ptr const & parent, std::string_view name,
                                      driver_data_ptr driver_data) const -> kstd::result<inode_ptr> = 0;

    //! Create a new inode with the given name below a given parent inode.
    //!
    //! @param parent The parent inode.
    //! @param name The name of the inode to create.
    //! @param type The file type of inode to be created.
    //! @param driver_data The per-mount data used by the driver.
    //! @param raw_device The device number the new inode should represent, if any.
    //! @return A pointer to the created inode on success, an error otherwise.
    [[nodiscard]] virtual auto create_inode(inode_ptr const & parent, std::string_view name,
                                            kapi::filesystem::file_type type, driver_data_ptr driver_data,
                                            std::optional<kapi::filesystem::device_number> raw_device = std::nullopt)
        -> kstd::result<kstd::shared_ptr<inode>> = 0;
  };

}  // namespace kernel::vfs

#endif