Line data Source code
1 : /*
2 : Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain
3 : Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany
4 :
5 : This software was partially supported by the
6 : EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
7 :
8 : This software was partially supported by the
9 : ADA-FS project under the SPPEXA project funded by the DFG.
10 :
11 : This file is part of GekkoFS.
12 :
13 : GekkoFS is free software: you can redistribute it and/or modify
14 : it under the terms of the GNU General Public License as published by
15 : the Free Software Foundation, either version 3 of the License, or
16 : (at your option) any later version.
17 :
18 : GekkoFS is distributed in the hope that it will be useful,
19 : but WITHOUT ANY WARRANTY; without even the implied warranty of
20 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 : GNU General Public License for more details.
22 :
23 : You should have received a copy of the GNU General Public License
24 : along with GekkoFS. If not, see <https://www.gnu.org/licenses/>.
25 :
26 : SPDX-License-Identifier: GPL-3.0-or-later
27 : */
28 : /**
29 : * @brief Chunk storage declarations handles all interactions with the
30 : * node-local storage system.
31 : */
32 :
33 : #ifndef GEKKOFS_CHUNK_STORAGE_HPP
34 : #define GEKKOFS_CHUNK_STORAGE_HPP
35 :
36 : #include <common/common_defs.hpp>
37 :
38 : #include <limits>
39 : #include <string>
40 : #include <memory>
41 : #include <system_error>
42 :
43 : /* Forward declarations */
44 : namespace spdlog {
45 : class logger;
46 : }
47 :
48 : namespace gkfs::data {
49 :
50 : struct ChunkStat {
51 : unsigned long chunk_size;
52 : unsigned long chunk_total;
53 : unsigned long chunk_free;
54 : }; //!< Struct for attaining current usage of storage backend
55 :
56 : /**
57 : * @brief Generic exception for ChunkStorage
58 : */
59 : class ChunkStorageException : public std::system_error {
60 : public:
61 14 : ChunkStorageException(const int err_code, const std::string& s)
62 14 : : std::system_error(err_code, std::generic_category(), s){};
63 : };
64 :
65 : /**
66 : * @brief ChunkStorage class handles _all_ interaction with node-local storage
67 : * system and is run as a single instance within the GekkoFS daemon.
68 : */
69 : class ChunkStorage {
70 : private:
71 : std::shared_ptr<spdlog::logger> log_; //!< Class logger
72 :
73 : std::string root_path_; //!< Path to GekkoFS root directory
74 : size_t chunksize_; //!< File system chunksize. TODO Why does that exist?
75 :
76 : /**
77 : * @brief Converts an internal gkfs path under the root dir to the absolute
78 : * path of the system.
79 : * @param internal_path E.g., /foo/bar
80 : * @return Absolute path, e.g., /tmp/rootdir/<pid>/data/chunks/foo:bar
81 : */
82 : [[nodiscard]] inline std::string
83 : absolute(const std::string& internal_path) const;
84 :
85 : /**
86 : * @brief Returns the chunk dir directory for a given path which is expected
87 : * to be absolute.
88 : * @param file_path
89 : * @return Chunk dir path
90 : */
91 : static inline std::string
92 : get_chunks_dir(const std::string& file_path);
93 :
94 : /**
95 : * @brief Returns the backend chunk file path for a given internal path.
96 : * @param file_path Internal file path, e.g., /foo/bar
97 : * @param chunk_id Number of chunk id
98 : * @return Chunk file path, e.g., /foo/bar
99 : * /tmp/rootdir/<pid>>/data/chunks/foo:bar/0
100 : */
101 : static inline std::string
102 : get_chunk_path(const std::string& file_path, gkfs::rpc::chnk_id_t chunk_id);
103 :
104 : /**
105 : * @brief Initializes the chunk space for a GekkoFS file, creating its
106 : * directory on the local file system.
107 : * @param file_path Chunk file path, e.g., /foo/bar
108 : */
109 : void
110 : init_chunk_space(const std::string& file_path) const;
111 :
112 : public:
113 : /**
114 : * @brief Initializes the ChunkStorage object on daemon launch.
115 : * @param path Root directory where all data is placed on the local FS.
116 : * @param chunksize Used chunksize in this GekkoFS instance.
117 : * @throws ChunkStorageException on launch failure
118 : */
119 : ChunkStorage(std::string& path, size_t chunksize);
120 :
121 : /**
122 : * @brief Removes chunk directory with all its files which is a recursive
123 : * remove operation on the chunk directory.
124 : * @param file_path Chunk file path, e.g., /foo/bar
125 : * @throws ChunkStorageException
126 : */
127 : void
128 : destroy_chunk_space(const std::string& file_path) const;
129 :
130 : /**
131 : * @brief Writes a single chunk file and is usually called by an Argobots
132 : * tasklet.
133 : * @param file_path Chunk file path, e.g., /foo/bar
134 : * @param chunk_id Number of chunk id
135 : * @param buf Buffer to write to chunk
136 : * @param size Amount of bytes to write to the chunk file
137 : * @param offset Offset where to write to the chunk file
138 : * @return The amount of bytes written
139 : * @throws ChunkStorageException with its error code
140 : */
141 : ssize_t
142 : write_chunk(const std::string& file_path, gkfs::rpc::chnk_id_t chunk_id,
143 : const char* buf, size_t size, off64_t offset) const;
144 :
145 : /**
146 : * @brief Reads a single chunk file and is usually called by an Argobots
147 : * tasklet.
148 : * @param file_path Chunk file path, e.g., /foo/bar
149 : * @param chunk_id Number of chunk id
150 : * @param buf Buffer to read to from chunk
151 : * @param size Amount of bytes to read to the chunk file
152 : * @param offset Offset where to read from the chunk file
153 : * @return The amount of bytes read
154 : * @throws ChunkStorageException with its error code
155 : */
156 : ssize_t
157 : read_chunk(const std::string& file_path, gkfs::rpc::chnk_id_t chunk_id,
158 : char* buf, size_t size, off64_t offset) const;
159 :
160 : /**
161 : * @brief Delete all chunks starting with chunk a chunk id.
162 : * @param file_path Chunk file path, e.g., /foo/bar
163 : * @param chunk_start Number of chunk id
164 : * @throws ChunkStorageException with its error code
165 : */
166 : void
167 : trim_chunk_space(const std::string& file_path,
168 : gkfs::rpc::chnk_id_t chunk_start);
169 :
170 : /**
171 : * @brief Truncates a single chunk file to a given byte length.
172 : * @param file_path Chunk file path, e.g., /foo/bar
173 : * @param chunk_id Number of chunk id
174 : * @param length Length of bytes to truncate the chunk to
175 : * @throws ChunkStorageException
176 : */
177 : void
178 : truncate_chunk_file(const std::string& file_path,
179 : gkfs::rpc::chnk_id_t chunk_id, off_t length);
180 :
181 : /**
182 : * @brief Calls statfs on the chunk directory to get statistic on its used
183 : * storage space.
184 : * @return ChunkStat struct
185 : * @throws ChunkStorageException
186 : */
187 : [[nodiscard]] ChunkStat
188 : chunk_stat() const;
189 : };
190 :
191 : } // namespace gkfs::data
192 :
193 : #endif // GEKKOFS_CHUNK_STORAGE_HPP
|