Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "dump_manager_bmc.hpp" |
| 4 | |
| 5 | #include "bmc_dump_entry.hpp" |
| 6 | #include "dump_internal.hpp" |
| 7 | #include "xyz/openbmc_project/Common/error.hpp" |
| 8 | #include "xyz/openbmc_project/Dump/Create/error.hpp" |
| 9 | |
| 10 | #include <sys/inotify.h> |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | #include <phosphor-logging/elog-errors.hpp> |
| 14 | #include <phosphor-logging/elog.hpp> |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 15 | #include <phosphor-logging/lg2.hpp> |
Marri Devender Rao | 3ed02c3 | 2022-06-28 23:12:14 -0500 | [diff] [blame] | 16 | #include <sdeventplus/exception.hpp> |
| 17 | #include <sdeventplus/source/base.hpp> |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 18 | |
Tim Lee | bb9366d | 2021-06-24 14:00:07 +0800 | [diff] [blame] | 19 | #include <cmath> |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 20 | #include <ctime> |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 21 | #include <regex> |
| 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace dump |
| 26 | { |
| 27 | namespace bmc |
| 28 | { |
| 29 | |
| 30 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 31 | using namespace phosphor::logging; |
| 32 | |
Marri Devender Rao | 73953b8 | 2022-02-15 09:15:42 -0600 | [diff] [blame] | 33 | bool Manager::fUserDumpInProgress = false; |
| 34 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 35 | namespace internal |
| 36 | { |
| 37 | |
| 38 | void Manager::create(Type type, std::vector<std::string> fullPaths) |
| 39 | { |
| 40 | dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths); |
| 41 | } |
| 42 | |
| 43 | } // namespace internal |
| 44 | |
Dhruvaraj Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 45 | sdbusplus::message::object_path |
Dhruvaraj Subhashchandran | ddc3366 | 2021-07-19 09:28:42 -0500 | [diff] [blame] | 46 | Manager::createDump(phosphor::dump::DumpCreateParams params) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 47 | { |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 48 | if (params.size() > CREATE_DUMP_MAX_PARAMS) |
Dhruvaraj Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 49 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 50 | lg2::warning("BMC dump accepts not more than 2 additional parameters"); |
Dhruvaraj Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 51 | } |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 52 | |
Marri Devender Rao | 73953b8 | 2022-02-15 09:15:42 -0600 | [diff] [blame] | 53 | if (Manager::fUserDumpInProgress == true) |
| 54 | { |
| 55 | elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>(); |
| 56 | } |
| 57 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 58 | // Get the originator id and type from params |
| 59 | std::string originatorId; |
| 60 | originatorTypes originatorType; |
| 61 | |
| 62 | phosphor::dump::extractOriginatorProperties(params, originatorId, |
| 63 | originatorType); |
| 64 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 65 | std::vector<std::string> paths; |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 66 | auto id = captureDump(Type::UserRequested, paths); |
| 67 | |
| 68 | // Entry Object path. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 69 | auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 70 | |
| 71 | try |
| 72 | { |
Claire Weinan | c0ab9d4 | 2022-08-17 23:01:07 -0700 | [diff] [blame] | 73 | uint64_t timeStamp = |
| 74 | std::chrono::duration_cast<std::chrono::microseconds>( |
| 75 | std::chrono::system_clock::now().time_since_epoch()) |
| 76 | .count(); |
| 77 | |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 78 | entries.insert(std::make_pair( |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 79 | id, std::make_unique<bmc::Entry>( |
| 80 | bus, objPath.c_str(), id, timeStamp, 0, std::string(), |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 81 | phosphor::dump::OperationStatus::InProgress, originatorId, |
| 82 | originatorType, *this))); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 83 | } |
| 84 | catch (const std::invalid_argument& e) |
| 85 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 86 | lg2::error("Error in creating dump entry, errormsg: {ERROR}, " |
| 87 | "OBJECTPATH: {OBJECT_PATH}, ID: {ID}", |
| 88 | "ERROR", e, "OBJECT_PATH", objPath, "ID", id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 89 | elog<InternalFailure>(); |
| 90 | } |
| 91 | |
Marri Devender Rao | 73953b8 | 2022-02-15 09:15:42 -0600 | [diff] [blame] | 92 | Manager::fUserDumpInProgress = true; |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 93 | return objPath.string(); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | uint32_t Manager::captureDump(Type type, |
| 97 | const std::vector<std::string>& fullPaths) |
| 98 | { |
| 99 | // Get Dump size. |
| 100 | auto size = getAllowedSize(); |
| 101 | |
| 102 | pid_t pid = fork(); |
| 103 | |
| 104 | if (pid == 0) |
| 105 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 106 | std::filesystem::path dumpPath(dumpDir); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 107 | auto id = std::to_string(lastEntryId + 1); |
| 108 | dumpPath /= id; |
| 109 | |
| 110 | // get dreport type map entry |
| 111 | auto tempType = TypeMap.find(type); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 112 | execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i", |
| 113 | id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p", |
| 114 | fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t", |
| 115 | tempType->second.c_str(), nullptr); |
| 116 | |
| 117 | // dreport script execution is failed. |
| 118 | auto error = errno; |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 119 | lg2::error("Error occurred during dreport function execution, " |
| 120 | "errno: {ERRNO}", |
| 121 | "ERRNO", error); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 122 | elog<InternalFailure>(); |
| 123 | } |
| 124 | else if (pid > 0) |
| 125 | { |
Marri Devender Rao | 3ed02c3 | 2022-06-28 23:12:14 -0500 | [diff] [blame] | 126 | Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) { |
| 127 | if (type == Type::UserRequested) |
| 128 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 129 | lg2::info("User initiated dump completed, resetting flag"); |
Marri Devender Rao | 3ed02c3 | 2022-06-28 23:12:14 -0500 | [diff] [blame] | 130 | Manager::fUserDumpInProgress = false; |
| 131 | } |
| 132 | this->childPtrMap.erase(pid); |
| 133 | }; |
| 134 | try |
| 135 | { |
| 136 | childPtrMap.emplace(pid, |
| 137 | std::make_unique<Child>(eventLoop.get(), pid, |
| 138 | WEXITED | WSTOPPED, |
| 139 | std::move(callback))); |
| 140 | } |
| 141 | catch (const sdeventplus::SdEventError& ex) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 142 | { |
| 143 | // Failed to add to event loop |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 144 | lg2::error( |
| 145 | "Error occurred during the sdeventplus::source::Child creation " |
| 146 | "ex: {ERROR}", |
| 147 | "ERROR", ex); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 148 | elog<InternalFailure>(); |
| 149 | } |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | auto error = errno; |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 154 | lg2::error("Error occurred during fork, errno: {ERRNO}", "ERRNO", |
| 155 | error); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 156 | elog<InternalFailure>(); |
| 157 | } |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 158 | return ++lastEntryId; |
| 159 | } |
| 160 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 161 | void Manager::createEntry(const std::filesystem::path& file) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 162 | { |
| 163 | // Dump File Name format obmcdump_ID_EPOCHTIME.EXT |
| 164 | static constexpr auto ID_POS = 1; |
| 165 | static constexpr auto EPOCHTIME_POS = 2; |
| 166 | std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)"); |
| 167 | |
| 168 | std::smatch match; |
| 169 | std::string name = file.filename(); |
| 170 | |
| 171 | if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0))) |
| 172 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 173 | lg2::error("Invalid Dump file name, FILENAME: {FILENAME}", "FILENAME", |
| 174 | file); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | auto idString = match[ID_POS]; |
Xie Ning | 56bd797 | 2022-02-25 15:20:02 +0800 | [diff] [blame] | 179 | uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000; |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 180 | |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 181 | auto id = stoul(idString); |
| 182 | |
| 183 | // If there is an existing entry update it and return. |
| 184 | auto dumpEntry = entries.find(id); |
| 185 | if (dumpEntry != entries.end()) |
| 186 | { |
| 187 | dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get()) |
Xie Ning | 56bd797 | 2022-02-25 15:20:02 +0800 | [diff] [blame] | 188 | ->update(timestamp, std::filesystem::file_size(file), file); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | |
| 192 | // Entry Object path. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 193 | auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 194 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 195 | // TODO: Get the persisted originator id & type |
| 196 | // For now, replacing it with null |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 197 | try |
| 198 | { |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 199 | entries.insert(std::make_pair( |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 200 | id, std::make_unique<bmc::Entry>( |
Xie Ning | 56bd797 | 2022-02-25 15:20:02 +0800 | [diff] [blame] | 201 | bus, objPath.c_str(), id, timestamp, |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 202 | std::filesystem::file_size(file), file, |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 203 | phosphor::dump::OperationStatus::Completed, std::string(), |
| 204 | originatorTypes::Internal, *this))); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 205 | } |
| 206 | catch (const std::invalid_argument& e) |
| 207 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 208 | lg2::error( |
| 209 | "Error in creating dump entry, errormsg: {ERROR}, " |
| 210 | "OBJECTPATH: {OBJECT_PATH}, ID: {ID}, TIMESTAMP: {TIMESTAMP}, " |
| 211 | "SIZE: {SIZE}, FILENAME: {FILENAME}", |
| 212 | "ERROR", e, "OBJECT_PATH", objPath, "ID", id, "TIMESTAMP", |
| 213 | timestamp, "SIZE", std::filesystem::file_size(file), "FILENAME", |
| 214 | file); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void Manager::watchCallback(const UserMap& fileInfo) |
| 220 | { |
| 221 | for (const auto& i : fileInfo) |
| 222 | { |
| 223 | // For any new dump file create dump entry object |
| 224 | // and associated inotify watch. |
| 225 | if (IN_CLOSE_WRITE == i.second) |
| 226 | { |
Chirag Sharma | 4cb0799 | 2022-05-09 04:37:22 -0500 | [diff] [blame] | 227 | if (!std::filesystem::is_directory(i.first)) |
| 228 | { |
| 229 | // Don't require filename to be passed, as the path |
| 230 | // of dump directory is stored in the childWatchMap |
| 231 | removeWatch(i.first.parent_path()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 232 | |
Chirag Sharma | 4cb0799 | 2022-05-09 04:37:22 -0500 | [diff] [blame] | 233 | // dump file is written now create D-Bus entry |
| 234 | createEntry(i.first); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | removeWatch(i.first); |
| 239 | } |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 240 | } |
| 241 | // Start inotify watch on newly created directory. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 242 | else if ((IN_CREATE == i.second) && |
| 243 | std::filesystem::is_directory(i.first)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 244 | { |
| 245 | auto watchObj = std::make_unique<Watch>( |
| 246 | eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first, |
| 247 | std::bind( |
| 248 | std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback), |
| 249 | this, std::placeholders::_1)); |
| 250 | |
| 251 | childWatchMap.emplace(i.first, std::move(watchObj)); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 256 | void Manager::removeWatch(const std::filesystem::path& path) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 257 | { |
| 258 | // Delete Watch entry from map. |
| 259 | childWatchMap.erase(path); |
| 260 | } |
| 261 | |
| 262 | void Manager::restore() |
| 263 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 264 | std::filesystem::path dir(dumpDir); |
| 265 | if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 266 | { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | // Dump file path: <DUMP_PATH>/<id>/<filename> |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 271 | for (const auto& p : std::filesystem::directory_iterator(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 272 | { |
| 273 | auto idStr = p.path().filename().string(); |
| 274 | |
| 275 | // Consider only directory's with dump id as name. |
| 276 | // Note: As per design one file per directory. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 277 | if ((std::filesystem::is_directory(p.path())) && |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 278 | std::all_of(idStr.begin(), idStr.end(), ::isdigit)) |
| 279 | { |
Patrick Williams | 78e8840 | 2023-05-10 07:50:48 -0500 | [diff] [blame] | 280 | lastEntryId = std::max(lastEntryId, |
| 281 | static_cast<uint32_t>(std::stoul(idStr))); |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 282 | auto fileIt = std::filesystem::directory_iterator(p.path()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 283 | // Create dump entry d-bus object. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 284 | if (fileIt != std::filesystem::end(fileIt)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 285 | { |
| 286 | createEntry(fileIt->path()); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 292 | size_t getDirectorySize(const std::string dir) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 293 | { |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 294 | auto size = 0; |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 295 | for (const auto& p : std::filesystem::recursive_directory_iterator(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 296 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 297 | if (!std::filesystem::is_directory(p)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 298 | { |
Tim Lee | bb9366d | 2021-06-24 14:00:07 +0800 | [diff] [blame] | 299 | size += std::ceil(std::filesystem::file_size(p) / 1024.0); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 300 | } |
| 301 | } |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 302 | return size; |
| 303 | } |
| 304 | |
| 305 | size_t Manager::getAllowedSize() |
| 306 | { |
| 307 | // Get current size of the dump directory. |
| 308 | auto size = getDirectorySize(dumpDir); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 309 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 310 | // Set the Dump size to Maximum if the free space is greater than |
| 311 | // Dump max size otherwise return the available size. |
| 312 | |
| 313 | size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size); |
| 314 | |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 315 | #ifdef BMC_DUMP_ROTATE_CONFIG |
| 316 | // Delete the first existing file until the space is enough |
| 317 | while (size < BMC_DUMP_MIN_SPACE_REQD) |
| 318 | { |
Patrick Williams | 78e8840 | 2023-05-10 07:50:48 -0500 | [diff] [blame] | 319 | auto delEntry = min_element(entries.begin(), entries.end(), |
| 320 | [](const auto& l, const auto& r) { |
| 321 | return l.first < r.first; |
| 322 | }); |
| 323 | auto delPath = std::filesystem::path(dumpDir) / |
| 324 | std::to_string(delEntry->first); |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 325 | |
| 326 | size += getDirectorySize(delPath); |
| 327 | |
| 328 | delEntry->second->delete_(); |
| 329 | } |
| 330 | #else |
| 331 | using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error; |
| 332 | using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON; |
| 333 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 334 | if (size < BMC_DUMP_MIN_SPACE_REQD) |
| 335 | { |
| 336 | // Reached to maximum limit |
| 337 | elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps")); |
| 338 | } |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 339 | #endif |
| 340 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 341 | if (size > BMC_DUMP_MAX_SIZE) |
| 342 | { |
| 343 | size = BMC_DUMP_MAX_SIZE; |
| 344 | } |
| 345 | |
| 346 | return size; |
| 347 | } |
| 348 | |
| 349 | } // namespace bmc |
| 350 | } // namespace dump |
| 351 | } // namespace phosphor |