summaryrefslogtreecommitdiff
path: root/include/hilbert/kernel/storage.hpp
blob: ea40d86b563bb419c1475f3a6d4ca506cc41927c (plain) (blame)
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
#ifndef HILBERT_KERNEL_STORAGE_HPP
#define HILBERT_KERNEL_STORAGE_HPP

#include <hilbert/kernel/utility.hpp>
#include <cstdint>

namespace hilbert::kernel::storage {

  typedef uint64_t node_id_t;
  typedef uint64_t directory_iter_t;

  enum class file_type {
    regular_file,
    directory,
    symlink
  };

  enum class bd_result {
    success,
    out_of_bounds,
    device_error
  };

  enum class fs_result {
    success,
    device_error,
    fs_corrupt
  };

  struct dir_entry {
    utility::string name;
    node_id_t node;
    file_type type;
    //only used if type is regular_file.
    uint64_t length;
    //only used if type is symlink.
    utility::string target;
  };

  class file_system_instance {

  public:
    virtual ~file_system_instance() {}

    virtual fs_result get_root_node(node_id_t &out) = 0;

    //it is assumed that this is a directory. sets iter_out
    //to a value that can be passed to subsequent calls
    //to get_next_child with the same argument for node.
    virtual fs_result get_first_child(node_id_t node, std::optional<dir_entry> &out, directory_iter_t &iter_out) = 0;

    //it is assumed that this is a directory. sets iter_out
    //to a value that can be passed to subsequent calls
    //to get_next_child with the same argument for node.
    virtual fs_result get_next_child(node_id_t node, std::optional<dir_entry> &out, directory_iter_t &iter) = 0;

    //it is assumed that this is a regular file and that
    //the region requested is within the bounds of the file.
    virtual fs_result read_bytes_from_file(node_id_t node, uint64_t start, uint64_t count, void *into) = 0;

  };

  class block_device {

  private:
    uint8_t *block_cache;
    uint64_t block_cache_i;

    //it is assumed that this block is within the bounds of the device.
    bd_result load_cache_block(uint64_t i);

  protected:
    //implemented in driver. it is assumed that the
    //blocks are within the bounds of the device.
    virtual bd_result read_blocks_no_cache(uint64_t start, uint64_t count, void *into) = 0;

    //it is assumed that this is only called once, by the driver, after
    //block_size and block_count have been set, and before read_bytes or
    //write_bytes can be called (e.g. in the derived class's constructor).
    void allocate_block_cache();

  public:
    //set by storage component
    file_system_instance *mounted_as;
    //set by storage component
    utility::uuid id;

    //set by driver
    uint64_t block_size;
    //set by driver
    uint64_t block_count;

    virtual ~block_device() {
      if (block_cache)
        delete block_cache;
    }

    bd_result read_bytes(uint64_t start, uint64_t count, void *into);

  };

}

#endif