diff options
Diffstat (limited to 'kernel/kernel/vfs/inode.hpp')
| -rw-r--r-- | kernel/kernel/vfs/inode.hpp | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/kernel/kernel/vfs/inode.hpp b/kernel/kernel/vfs/inode.hpp new file mode 100644 index 00000000..80657b92 --- /dev/null +++ b/kernel/kernel/vfs/inode.hpp @@ -0,0 +1,142 @@ +#ifndef TEACHOS_KERNEL_VFS_INODE_HPP +#define TEACHOS_KERNEL_VFS_INODE_HPP + +#include <kernel/vfs/directory_listing_cursor.hpp> +#include <kernel/vfs/directory_listing_entry.hpp> + +#include <kapi/filesystem.hpp> + +#include <kstd/memory.hpp> +#include <kstd/result.hpp> +#include <kstd/units.hpp> + +#include <cstddef> +#include <optional> +#include <span> +#include <utility> + +namespace kernel::vfs +{ + struct mount; + + //! An inode in the filesystem. + //! + //! Specific filesystems, or other subsystems, may provide their own implementation of this abstract base class. For + //! example, the ext2 filesystem provides its own inode implementation derived from this class, performing ext2 + //! specific operations to fulfill the contract of this class. + struct inode + { + //! Create an inode. + inode() = default; + + //! Virtual destructor for the inode. + virtual ~inode() = default; + + //! @name I/O Operations + //! @{ + + //! Read from this inode into a buffer + //! + //! This function reads data from this inode into a given buffer. Implementations must guarantee, that if buffer is + //! larger than the entity represented by this inode, the buffer bytes beyond the range of the read bytes are left + //! untouched. + //! + //! @param buffer Destination buffer. + //! @param offset Read offset in bytes. + //! @return The number of bytes read on success, an error otherwise. + [[nodiscard]] virtual auto read(std::span<std::byte> buffer, kstd::bytes offset) const + -> kstd::result<kstd::bytes> = 0; + + //! Writes data from a buffer into this inode. + //! + //! This function writes data from a buffer into this inode. Implementations must handle buffer sizes larger than + //! inode entity sizes gracefully. Specifically, all bytes in the buffer that do not fit into the entity represented + //! by this inode must be ignored. + //! + //! @param buffer Source buffer. + //! @param offset Write offset in bytes. + //! @return The number of bytes written on success, an error otherwise. + virtual auto write(std::span<std::byte const> buffer, kstd::bytes offset) -> kstd::result<kstd::bytes> = 0; + + //! @} + + //! @name Property Checking + //! @{ + + //! Check if this inode describes a block device node. + //! + //! @return @c true iff. this inode describes a block device node, @c false otherwise. + [[nodiscard]] virtual auto is_block_device() const -> bool; + + //! Check if this inode describes a character device node. + //! + //! @return @c true iff. this inode describes a character device node, @c false otherwise. + [[nodiscard]] virtual auto is_character_device() const -> bool; + + //! Check if this inode describes a device. + //! + //! @return @c true iff. this inode is either a block or character device node, @c false otherwise. + [[nodiscard]] auto is_device() const -> bool; + + //! Check if this inode describes a directory. + //! + //! @return @c true iff. this inode describes a directory, @c false otherwise. + [[nodiscard]] virtual auto is_directory() const -> bool; + + //! Check if this inode describes a regular file. + //! + //! @return @c true iff. this inode describes a regular file, @c false otherwise. + [[nodiscard]] virtual auto is_regular() const -> bool; + + //! Check if this inode describes a symbolic link. + //! + //! @return @c true iff. this inode describes a symbolic link, @c false otherwise. + [[nodiscard]] virtual auto is_symbolic_link() const -> bool; + + //! @} + + //! @name POSIX Information Access + //! @{ + + //! Get POSIX status information for this inode. + //! + //! @return a populated status information object, or an error or failure. + [[nodiscard]] virtual auto status() const -> kstd::result<kapi::filesystem::file_status>; + + //! Get the raw device, if any, this inode refers to. + //! + //! @return a device number if this inode refers to a device, an empty optional otherwise. + [[nodiscard]] virtual auto raw_device() const -> std::optional<kapi::filesystem::device_number>; + + //! List the contents of a directory. + //! + //! @param position The current directory listing position. + //! @param entries An output span to store directory entries. + //! @return A pair of the number or read entries and the next listing position on success, an error otherwise. + [[nodiscard]] virtual auto read_directory(directory_listing_cursor position, + std::span<directory_listing_entry> entries) const + -> kstd::result<std::pair<std::size_t, directory_listing_cursor>>; + + //! @} + + //! @name Mount Resolution + //! @{ + + //! Get the mount this inode belongs to. + //! + //! @return The mount this inode belongs to. + [[nodiscard]] auto owning_mount() const noexcept -> kstd::weak_ptr<mount>; + + //! Define which mount this inode belongs to. + //! + //! @param source The mount this inode belongs to. + auto set_owning_mount(kstd::shared_ptr<mount> const & source) noexcept -> void; + + //! @} + + private: + kstd::weak_ptr<mount> m_owning_mount{}; + }; +} // namespace kernel::vfs + +#endif |
