xref: /openbmc/phosphor-debug-collector/dump_manager_bmc.cpp (revision 3ed02c32ccef3bc438a46bb3c48a7dbb131072a8)
1fef66a95SDhruvaraj Subhashchandran #include "config.h"
2fef66a95SDhruvaraj Subhashchandran 
3fef66a95SDhruvaraj Subhashchandran #include "dump_manager_bmc.hpp"
4fef66a95SDhruvaraj Subhashchandran 
5fef66a95SDhruvaraj Subhashchandran #include "bmc_dump_entry.hpp"
6fef66a95SDhruvaraj Subhashchandran #include "dump_internal.hpp"
7fef66a95SDhruvaraj Subhashchandran #include "xyz/openbmc_project/Common/error.hpp"
8fef66a95SDhruvaraj Subhashchandran #include "xyz/openbmc_project/Dump/Create/error.hpp"
9fef66a95SDhruvaraj Subhashchandran 
10858fbb2eSGeorge Liu #include <fmt/core.h>
11fef66a95SDhruvaraj Subhashchandran #include <sys/inotify.h>
12fef66a95SDhruvaraj Subhashchandran #include <unistd.h>
13fef66a95SDhruvaraj Subhashchandran 
14fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog-errors.hpp>
15fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog.hpp>
16*3ed02c32SMarri Devender Rao #include <sdeventplus/exception.hpp>
17*3ed02c32SMarri Devender Rao #include <sdeventplus/source/base.hpp>
180af74a5eSJayanth Othayoth 
19bb9366d0STim Lee #include <cmath>
200af74a5eSJayanth Othayoth #include <ctime>
21fef66a95SDhruvaraj Subhashchandran #include <regex>
22fef66a95SDhruvaraj Subhashchandran 
23fef66a95SDhruvaraj Subhashchandran namespace phosphor
24fef66a95SDhruvaraj Subhashchandran {
25fef66a95SDhruvaraj Subhashchandran namespace dump
26fef66a95SDhruvaraj Subhashchandran {
27fef66a95SDhruvaraj Subhashchandran namespace bmc
28fef66a95SDhruvaraj Subhashchandran {
29fef66a95SDhruvaraj Subhashchandran 
30fef66a95SDhruvaraj Subhashchandran using namespace sdbusplus::xyz::openbmc_project::Common::Error;
31fef66a95SDhruvaraj Subhashchandran using namespace phosphor::logging;
32fef66a95SDhruvaraj Subhashchandran 
3373953b8fSMarri Devender Rao bool Manager::fUserDumpInProgress = false;
3473953b8fSMarri Devender Rao 
35fef66a95SDhruvaraj Subhashchandran namespace internal
36fef66a95SDhruvaraj Subhashchandran {
37fef66a95SDhruvaraj Subhashchandran 
38fef66a95SDhruvaraj Subhashchandran void Manager::create(Type type, std::vector<std::string> fullPaths)
39fef66a95SDhruvaraj Subhashchandran {
40fef66a95SDhruvaraj Subhashchandran     dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths);
41fef66a95SDhruvaraj Subhashchandran }
42fef66a95SDhruvaraj Subhashchandran 
43fef66a95SDhruvaraj Subhashchandran } // namespace internal
44fef66a95SDhruvaraj Subhashchandran 
45969f9a59SDhruvaraj Subhashchandran sdbusplus::message::object_path
46ddc3366eSDhruvaraj Subhashchandran     Manager::createDump(phosphor::dump::DumpCreateParams params)
47fef66a95SDhruvaraj Subhashchandran {
4874a1f39cSAsmitha Karunanithi     if (params.size() > CREATE_DUMP_MAX_PARAMS)
49969f9a59SDhruvaraj Subhashchandran     {
5074a1f39cSAsmitha Karunanithi         log<level::WARNING>(
5174a1f39cSAsmitha Karunanithi             "BMC dump accepts not more than 2 additional parameters");
52969f9a59SDhruvaraj Subhashchandran     }
5374a1f39cSAsmitha Karunanithi 
5473953b8fSMarri Devender Rao     if (Manager::fUserDumpInProgress == true)
5573953b8fSMarri Devender Rao     {
5673953b8fSMarri Devender Rao         elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
5773953b8fSMarri Devender Rao     }
5873953b8fSMarri Devender Rao 
5974a1f39cSAsmitha Karunanithi     // Get the originator id and type from params
6074a1f39cSAsmitha Karunanithi     std::string originatorId;
6174a1f39cSAsmitha Karunanithi     originatorTypes originatorType;
6274a1f39cSAsmitha Karunanithi 
6374a1f39cSAsmitha Karunanithi     phosphor::dump::extractOriginatorProperties(params, originatorId,
6474a1f39cSAsmitha Karunanithi                                                 originatorType);
6574a1f39cSAsmitha Karunanithi 
66fef66a95SDhruvaraj Subhashchandran     std::vector<std::string> paths;
676ccb50e1SDhruvaraj Subhashchandran     auto id = captureDump(Type::UserRequested, paths);
686ccb50e1SDhruvaraj Subhashchandran 
696ccb50e1SDhruvaraj Subhashchandran     // Entry Object path.
703fc6df48SJayanth Othayoth     auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
716ccb50e1SDhruvaraj Subhashchandran 
726ccb50e1SDhruvaraj Subhashchandran     try
736ccb50e1SDhruvaraj Subhashchandran     {
74c0ab9d43SClaire Weinan         uint64_t timeStamp =
75c0ab9d43SClaire Weinan             std::chrono::duration_cast<std::chrono::microseconds>(
76c0ab9d43SClaire Weinan                 std::chrono::system_clock::now().time_since_epoch())
77c0ab9d43SClaire Weinan                 .count();
78c0ab9d43SClaire Weinan 
796ccb50e1SDhruvaraj Subhashchandran         entries.insert(std::make_pair(
80a6ab806dSDhruvaraj Subhashchandran             id, std::make_unique<bmc::Entry>(
81a6ab806dSDhruvaraj Subhashchandran                     bus, objPath.c_str(), id, timeStamp, 0, std::string(),
8274a1f39cSAsmitha Karunanithi                     phosphor::dump::OperationStatus::InProgress, originatorId,
8374a1f39cSAsmitha Karunanithi                     originatorType, *this)));
846ccb50e1SDhruvaraj Subhashchandran     }
856ccb50e1SDhruvaraj Subhashchandran     catch (const std::invalid_argument& e)
866ccb50e1SDhruvaraj Subhashchandran     {
87858fbb2eSGeorge Liu         log<level::ERR>(fmt::format("Error in creating dump entry, "
88858fbb2eSGeorge Liu                                     "errormsg({}), OBJECTPATH({}), ID({})",
89858fbb2eSGeorge Liu                                     e.what(), objPath.c_str(), id)
90858fbb2eSGeorge Liu                             .c_str());
916ccb50e1SDhruvaraj Subhashchandran         elog<InternalFailure>();
926ccb50e1SDhruvaraj Subhashchandran     }
936ccb50e1SDhruvaraj Subhashchandran 
9473953b8fSMarri Devender Rao     Manager::fUserDumpInProgress = true;
956ccb50e1SDhruvaraj Subhashchandran     return objPath.string();
96fef66a95SDhruvaraj Subhashchandran }
97fef66a95SDhruvaraj Subhashchandran 
98fef66a95SDhruvaraj Subhashchandran uint32_t Manager::captureDump(Type type,
99fef66a95SDhruvaraj Subhashchandran                               const std::vector<std::string>& fullPaths)
100fef66a95SDhruvaraj Subhashchandran {
101fef66a95SDhruvaraj Subhashchandran     // Get Dump size.
102fef66a95SDhruvaraj Subhashchandran     auto size = getAllowedSize();
103fef66a95SDhruvaraj Subhashchandran 
104fef66a95SDhruvaraj Subhashchandran     pid_t pid = fork();
105fef66a95SDhruvaraj Subhashchandran 
106fef66a95SDhruvaraj Subhashchandran     if (pid == 0)
107fef66a95SDhruvaraj Subhashchandran     {
1083fc6df48SJayanth Othayoth         std::filesystem::path dumpPath(dumpDir);
109fef66a95SDhruvaraj Subhashchandran         auto id = std::to_string(lastEntryId + 1);
110fef66a95SDhruvaraj Subhashchandran         dumpPath /= id;
111fef66a95SDhruvaraj Subhashchandran 
112fef66a95SDhruvaraj Subhashchandran         // get dreport type map entry
113fef66a95SDhruvaraj Subhashchandran         auto tempType = TypeMap.find(type);
114fef66a95SDhruvaraj Subhashchandran         execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
115fef66a95SDhruvaraj Subhashchandran               id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
116fef66a95SDhruvaraj Subhashchandran               fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
117fef66a95SDhruvaraj Subhashchandran               tempType->second.c_str(), nullptr);
118fef66a95SDhruvaraj Subhashchandran 
119fef66a95SDhruvaraj Subhashchandran         // dreport script execution is failed.
120fef66a95SDhruvaraj Subhashchandran         auto error = errno;
12173953b8fSMarri Devender Rao         log<level::ERR>(fmt::format("Error occurred during dreport "
12273953b8fSMarri Devender Rao                                     "function execution, errno({})",
123858fbb2eSGeorge Liu                                     error)
124858fbb2eSGeorge Liu                             .c_str());
125fef66a95SDhruvaraj Subhashchandran         elog<InternalFailure>();
126fef66a95SDhruvaraj Subhashchandran     }
127fef66a95SDhruvaraj Subhashchandran     else if (pid > 0)
128fef66a95SDhruvaraj Subhashchandran     {
129*3ed02c32SMarri Devender Rao         Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
130*3ed02c32SMarri Devender Rao             if (type == Type::UserRequested)
131*3ed02c32SMarri Devender Rao             {
132*3ed02c32SMarri Devender Rao                 log<level::INFO>(
133*3ed02c32SMarri Devender Rao                     "User initiated dump completed, resetting flag");
134*3ed02c32SMarri Devender Rao                 Manager::fUserDumpInProgress = false;
135*3ed02c32SMarri Devender Rao             }
136*3ed02c32SMarri Devender Rao             this->childPtrMap.erase(pid);
137*3ed02c32SMarri Devender Rao         };
138*3ed02c32SMarri Devender Rao         try
139*3ed02c32SMarri Devender Rao         {
140*3ed02c32SMarri Devender Rao             childPtrMap.emplace(pid,
141*3ed02c32SMarri Devender Rao                                 std::make_unique<Child>(eventLoop.get(), pid,
142*3ed02c32SMarri Devender Rao                                                         WEXITED | WSTOPPED,
143*3ed02c32SMarri Devender Rao                                                         std::move(callback)));
144*3ed02c32SMarri Devender Rao         }
145*3ed02c32SMarri Devender Rao         catch (const sdeventplus::SdEventError& ex)
146fef66a95SDhruvaraj Subhashchandran         {
147fef66a95SDhruvaraj Subhashchandran             // Failed to add to event loop
148*3ed02c32SMarri Devender Rao             log<level::ERR>(
149*3ed02c32SMarri Devender Rao                 fmt::format(
150*3ed02c32SMarri Devender Rao                     "Error occurred during the sdeventplus::source::Child "
151*3ed02c32SMarri Devender Rao                     "creation ex({})",
152*3ed02c32SMarri Devender Rao                     ex.what())
153858fbb2eSGeorge Liu                     .c_str());
154fef66a95SDhruvaraj Subhashchandran             elog<InternalFailure>();
155fef66a95SDhruvaraj Subhashchandran         }
156fef66a95SDhruvaraj Subhashchandran     }
157fef66a95SDhruvaraj Subhashchandran     else
158fef66a95SDhruvaraj Subhashchandran     {
159fef66a95SDhruvaraj Subhashchandran         auto error = errno;
160858fbb2eSGeorge Liu         log<level::ERR>(
161858fbb2eSGeorge Liu             fmt::format("Error occurred during fork, errno({})", error)
162858fbb2eSGeorge Liu                 .c_str());
163fef66a95SDhruvaraj Subhashchandran         elog<InternalFailure>();
164fef66a95SDhruvaraj Subhashchandran     }
165fef66a95SDhruvaraj Subhashchandran     return ++lastEntryId;
166fef66a95SDhruvaraj Subhashchandran }
167fef66a95SDhruvaraj Subhashchandran 
1683fc6df48SJayanth Othayoth void Manager::createEntry(const std::filesystem::path& file)
169fef66a95SDhruvaraj Subhashchandran {
170fef66a95SDhruvaraj Subhashchandran     // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
171fef66a95SDhruvaraj Subhashchandran     static constexpr auto ID_POS = 1;
172fef66a95SDhruvaraj Subhashchandran     static constexpr auto EPOCHTIME_POS = 2;
173fef66a95SDhruvaraj Subhashchandran     std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
174fef66a95SDhruvaraj Subhashchandran 
175fef66a95SDhruvaraj Subhashchandran     std::smatch match;
176fef66a95SDhruvaraj Subhashchandran     std::string name = file.filename();
177fef66a95SDhruvaraj Subhashchandran 
178fef66a95SDhruvaraj Subhashchandran     if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
179fef66a95SDhruvaraj Subhashchandran     {
180858fbb2eSGeorge Liu         log<level::ERR>(fmt::format("Invalid Dump file name, FILENAME({})",
181858fbb2eSGeorge Liu                                     file.filename().c_str())
182858fbb2eSGeorge Liu                             .c_str());
183fef66a95SDhruvaraj Subhashchandran         return;
184fef66a95SDhruvaraj Subhashchandran     }
185fef66a95SDhruvaraj Subhashchandran 
186fef66a95SDhruvaraj Subhashchandran     auto idString = match[ID_POS];
18756bd797eSXie Ning     uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
188fef66a95SDhruvaraj Subhashchandran 
189fef66a95SDhruvaraj Subhashchandran     auto id = stoul(idString);
1906ccb50e1SDhruvaraj Subhashchandran 
1916ccb50e1SDhruvaraj Subhashchandran     // If there is an existing entry update it and return.
1926ccb50e1SDhruvaraj Subhashchandran     auto dumpEntry = entries.find(id);
1936ccb50e1SDhruvaraj Subhashchandran     if (dumpEntry != entries.end())
1946ccb50e1SDhruvaraj Subhashchandran     {
1956ccb50e1SDhruvaraj Subhashchandran         dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
19656bd797eSXie Ning             ->update(timestamp, std::filesystem::file_size(file), file);
1976ccb50e1SDhruvaraj Subhashchandran         return;
1986ccb50e1SDhruvaraj Subhashchandran     }
1996ccb50e1SDhruvaraj Subhashchandran 
200fef66a95SDhruvaraj Subhashchandran     // Entry Object path.
2013fc6df48SJayanth Othayoth     auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
202fef66a95SDhruvaraj Subhashchandran 
20374a1f39cSAsmitha Karunanithi     // TODO: Get the persisted originator id & type
20474a1f39cSAsmitha Karunanithi     // For now, replacing it with null
2056ccb50e1SDhruvaraj Subhashchandran     try
2066ccb50e1SDhruvaraj Subhashchandran     {
207a6ab806dSDhruvaraj Subhashchandran         entries.insert(std::make_pair(
2083fc6df48SJayanth Othayoth             id, std::make_unique<bmc::Entry>(
20956bd797eSXie Ning                     bus, objPath.c_str(), id, timestamp,
2103fc6df48SJayanth Othayoth                     std::filesystem::file_size(file), file,
21174a1f39cSAsmitha Karunanithi                     phosphor::dump::OperationStatus::Completed, std::string(),
21274a1f39cSAsmitha Karunanithi                     originatorTypes::Internal, *this)));
213fef66a95SDhruvaraj Subhashchandran     }
214fef66a95SDhruvaraj Subhashchandran     catch (const std::invalid_argument& e)
215fef66a95SDhruvaraj Subhashchandran     {
216858fbb2eSGeorge Liu         log<level::ERR>(
21773953b8fSMarri Devender Rao             fmt::format("Error in creating dump entry, errormsg({}), "
21873953b8fSMarri Devender Rao                         "OBJECTPATH({}), "
219858fbb2eSGeorge Liu                         "ID({}), TIMESTAMP({}), SIZE({}), FILENAME({})",
22056bd797eSXie Ning                         e.what(), objPath.c_str(), id, timestamp,
22173953b8fSMarri Devender Rao                         std::filesystem::file_size(file),
22273953b8fSMarri Devender Rao                         file.filename().c_str())
223858fbb2eSGeorge Liu                 .c_str());
224fef66a95SDhruvaraj Subhashchandran         return;
225fef66a95SDhruvaraj Subhashchandran     }
226fef66a95SDhruvaraj Subhashchandran }
227fef66a95SDhruvaraj Subhashchandran 
228fef66a95SDhruvaraj Subhashchandran void Manager::watchCallback(const UserMap& fileInfo)
229fef66a95SDhruvaraj Subhashchandran {
230fef66a95SDhruvaraj Subhashchandran     for (const auto& i : fileInfo)
231fef66a95SDhruvaraj Subhashchandran     {
232fef66a95SDhruvaraj Subhashchandran         // For any new dump file create dump entry object
233fef66a95SDhruvaraj Subhashchandran         // and associated inotify watch.
234fef66a95SDhruvaraj Subhashchandran         if (IN_CLOSE_WRITE == i.second)
235fef66a95SDhruvaraj Subhashchandran         {
2364cb0799fSChirag Sharma             if (!std::filesystem::is_directory(i.first))
2374cb0799fSChirag Sharma             {
2384cb0799fSChirag Sharma                 // Don't require filename to be passed, as the path
2394cb0799fSChirag Sharma                 // of dump directory is stored in the childWatchMap
2404cb0799fSChirag Sharma                 removeWatch(i.first.parent_path());
241fef66a95SDhruvaraj Subhashchandran 
2424cb0799fSChirag Sharma                 // dump file is written now create D-Bus entry
243fef66a95SDhruvaraj Subhashchandran                 createEntry(i.first);
244fef66a95SDhruvaraj Subhashchandran             }
2454cb0799fSChirag Sharma             else
2464cb0799fSChirag Sharma             {
2474cb0799fSChirag Sharma                 removeWatch(i.first);
2484cb0799fSChirag Sharma             }
2494cb0799fSChirag Sharma         }
250fef66a95SDhruvaraj Subhashchandran         // Start inotify watch on newly created directory.
2513fc6df48SJayanth Othayoth         else if ((IN_CREATE == i.second) &&
2523fc6df48SJayanth Othayoth                  std::filesystem::is_directory(i.first))
253fef66a95SDhruvaraj Subhashchandran         {
254fef66a95SDhruvaraj Subhashchandran             auto watchObj = std::make_unique<Watch>(
255fef66a95SDhruvaraj Subhashchandran                 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
256fef66a95SDhruvaraj Subhashchandran                 std::bind(
257fef66a95SDhruvaraj Subhashchandran                     std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
258fef66a95SDhruvaraj Subhashchandran                     this, std::placeholders::_1));
259fef66a95SDhruvaraj Subhashchandran 
260fef66a95SDhruvaraj Subhashchandran             childWatchMap.emplace(i.first, std::move(watchObj));
261fef66a95SDhruvaraj Subhashchandran         }
262fef66a95SDhruvaraj Subhashchandran     }
263fef66a95SDhruvaraj Subhashchandran }
264fef66a95SDhruvaraj Subhashchandran 
2653fc6df48SJayanth Othayoth void Manager::removeWatch(const std::filesystem::path& path)
266fef66a95SDhruvaraj Subhashchandran {
267fef66a95SDhruvaraj Subhashchandran     // Delete Watch entry from map.
268fef66a95SDhruvaraj Subhashchandran     childWatchMap.erase(path);
269fef66a95SDhruvaraj Subhashchandran }
270fef66a95SDhruvaraj Subhashchandran 
271fef66a95SDhruvaraj Subhashchandran void Manager::restore()
272fef66a95SDhruvaraj Subhashchandran {
2733fc6df48SJayanth Othayoth     std::filesystem::path dir(dumpDir);
2743fc6df48SJayanth Othayoth     if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
275fef66a95SDhruvaraj Subhashchandran     {
276fef66a95SDhruvaraj Subhashchandran         return;
277fef66a95SDhruvaraj Subhashchandran     }
278fef66a95SDhruvaraj Subhashchandran 
279fef66a95SDhruvaraj Subhashchandran     // Dump file path: <DUMP_PATH>/<id>/<filename>
2803fc6df48SJayanth Othayoth     for (const auto& p : std::filesystem::directory_iterator(dir))
281fef66a95SDhruvaraj Subhashchandran     {
282fef66a95SDhruvaraj Subhashchandran         auto idStr = p.path().filename().string();
283fef66a95SDhruvaraj Subhashchandran 
284fef66a95SDhruvaraj Subhashchandran         // Consider only directory's with dump id as name.
285fef66a95SDhruvaraj Subhashchandran         // Note: As per design one file per directory.
2863fc6df48SJayanth Othayoth         if ((std::filesystem::is_directory(p.path())) &&
287fef66a95SDhruvaraj Subhashchandran             std::all_of(idStr.begin(), idStr.end(), ::isdigit))
288fef66a95SDhruvaraj Subhashchandran         {
289fef66a95SDhruvaraj Subhashchandran             lastEntryId =
290fef66a95SDhruvaraj Subhashchandran                 std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
2913fc6df48SJayanth Othayoth             auto fileIt = std::filesystem::directory_iterator(p.path());
292fef66a95SDhruvaraj Subhashchandran             // Create dump entry d-bus object.
2933fc6df48SJayanth Othayoth             if (fileIt != std::filesystem::end(fileIt))
294fef66a95SDhruvaraj Subhashchandran             {
295fef66a95SDhruvaraj Subhashchandran                 createEntry(fileIt->path());
296fef66a95SDhruvaraj Subhashchandran             }
297fef66a95SDhruvaraj Subhashchandran         }
298fef66a95SDhruvaraj Subhashchandran     }
299fef66a95SDhruvaraj Subhashchandran }
300fef66a95SDhruvaraj Subhashchandran 
301fc69f35cSXie Ning size_t getDirectorySize(const std::string dir)
302fef66a95SDhruvaraj Subhashchandran {
303fef66a95SDhruvaraj Subhashchandran     auto size = 0;
304fc69f35cSXie Ning     for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
305fef66a95SDhruvaraj Subhashchandran     {
3063fc6df48SJayanth Othayoth         if (!std::filesystem::is_directory(p))
307fef66a95SDhruvaraj Subhashchandran         {
308bb9366d0STim Lee             size += std::ceil(std::filesystem::file_size(p) / 1024.0);
309fef66a95SDhruvaraj Subhashchandran         }
310fef66a95SDhruvaraj Subhashchandran     }
311fc69f35cSXie Ning     return size;
312fc69f35cSXie Ning }
313fc69f35cSXie Ning 
314fc69f35cSXie Ning size_t Manager::getAllowedSize()
315fc69f35cSXie Ning {
316fc69f35cSXie Ning     // Get current size of the dump directory.
317fc69f35cSXie Ning     auto size = getDirectorySize(dumpDir);
318fef66a95SDhruvaraj Subhashchandran 
319fef66a95SDhruvaraj Subhashchandran     // Set the Dump size to Maximum  if the free space is greater than
320fef66a95SDhruvaraj Subhashchandran     // Dump max size otherwise return the available size.
321fef66a95SDhruvaraj Subhashchandran 
322fef66a95SDhruvaraj Subhashchandran     size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
323fef66a95SDhruvaraj Subhashchandran 
324fc69f35cSXie Ning #ifdef BMC_DUMP_ROTATE_CONFIG
325fc69f35cSXie Ning     // Delete the first existing file until the space is enough
326fc69f35cSXie Ning     while (size < BMC_DUMP_MIN_SPACE_REQD)
327fc69f35cSXie Ning     {
328fc69f35cSXie Ning         auto delEntry = min_element(
329fc69f35cSXie Ning             entries.begin(), entries.end(),
330fc69f35cSXie Ning             [](const auto& l, const auto& r) { return l.first < r.first; });
331fc69f35cSXie Ning         auto delPath =
332fc69f35cSXie Ning             std::filesystem::path(dumpDir) / std::to_string(delEntry->first);
333fc69f35cSXie Ning 
334fc69f35cSXie Ning         size += getDirectorySize(delPath);
335fc69f35cSXie Ning 
336fc69f35cSXie Ning         delEntry->second->delete_();
337fc69f35cSXie Ning     }
338fc69f35cSXie Ning #else
339fc69f35cSXie Ning     using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
340fc69f35cSXie Ning     using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
341fc69f35cSXie Ning 
342fef66a95SDhruvaraj Subhashchandran     if (size < BMC_DUMP_MIN_SPACE_REQD)
343fef66a95SDhruvaraj Subhashchandran     {
344fef66a95SDhruvaraj Subhashchandran         // Reached to maximum limit
345fef66a95SDhruvaraj Subhashchandran         elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
346fef66a95SDhruvaraj Subhashchandran     }
347fc69f35cSXie Ning #endif
348fc69f35cSXie Ning 
349fef66a95SDhruvaraj Subhashchandran     if (size > BMC_DUMP_MAX_SIZE)
350fef66a95SDhruvaraj Subhashchandran     {
351fef66a95SDhruvaraj Subhashchandran         size = BMC_DUMP_MAX_SIZE;
352fef66a95SDhruvaraj Subhashchandran     }
353fef66a95SDhruvaraj Subhashchandran 
354fef66a95SDhruvaraj Subhashchandran     return size;
355fef66a95SDhruvaraj Subhashchandran }
356fef66a95SDhruvaraj Subhashchandran 
357fef66a95SDhruvaraj Subhashchandran } // namespace bmc
358fef66a95SDhruvaraj Subhashchandran } // namespace dump
359fef66a95SDhruvaraj Subhashchandran } // namespace phosphor
360