xref: /openbmc/phosphor-debug-collector/dump_manager_bmc.cpp (revision d1f670fe7ea219c643f8b630e8c2d7b333f16e35)
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 
10fef66a95SDhruvaraj Subhashchandran #include <sys/inotify.h>
11fef66a95SDhruvaraj Subhashchandran #include <unistd.h>
12fef66a95SDhruvaraj Subhashchandran 
13fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog-errors.hpp>
14fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog.hpp>
15*d1f670feSDhruvaraj Subhashchandran #include <phosphor-logging/lg2.hpp>
163ed02c32SMarri Devender Rao #include <sdeventplus/exception.hpp>
173ed02c32SMarri 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     {
50*d1f670feSDhruvaraj Subhashchandran         lg2::warning("BMC dump accepts not more than 2 additional parameters");
51969f9a59SDhruvaraj Subhashchandran     }
5274a1f39cSAsmitha Karunanithi 
5373953b8fSMarri Devender Rao     if (Manager::fUserDumpInProgress == true)
5473953b8fSMarri Devender Rao     {
5573953b8fSMarri Devender Rao         elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
5673953b8fSMarri Devender Rao     }
5773953b8fSMarri Devender Rao 
5874a1f39cSAsmitha Karunanithi     // Get the originator id and type from params
5974a1f39cSAsmitha Karunanithi     std::string originatorId;
6074a1f39cSAsmitha Karunanithi     originatorTypes originatorType;
6174a1f39cSAsmitha Karunanithi 
6274a1f39cSAsmitha Karunanithi     phosphor::dump::extractOriginatorProperties(params, originatorId,
6374a1f39cSAsmitha Karunanithi                                                 originatorType);
6474a1f39cSAsmitha Karunanithi 
65fef66a95SDhruvaraj Subhashchandran     std::vector<std::string> paths;
666ccb50e1SDhruvaraj Subhashchandran     auto id = captureDump(Type::UserRequested, paths);
676ccb50e1SDhruvaraj Subhashchandran 
686ccb50e1SDhruvaraj Subhashchandran     // Entry Object path.
693fc6df48SJayanth Othayoth     auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
706ccb50e1SDhruvaraj Subhashchandran 
716ccb50e1SDhruvaraj Subhashchandran     try
726ccb50e1SDhruvaraj Subhashchandran     {
73c0ab9d43SClaire Weinan         uint64_t timeStamp =
74c0ab9d43SClaire Weinan             std::chrono::duration_cast<std::chrono::microseconds>(
75c0ab9d43SClaire Weinan                 std::chrono::system_clock::now().time_since_epoch())
76c0ab9d43SClaire Weinan                 .count();
77c0ab9d43SClaire Weinan 
786ccb50e1SDhruvaraj Subhashchandran         entries.insert(std::make_pair(
79a6ab806dSDhruvaraj Subhashchandran             id, std::make_unique<bmc::Entry>(
80a6ab806dSDhruvaraj Subhashchandran                     bus, objPath.c_str(), id, timeStamp, 0, std::string(),
8174a1f39cSAsmitha Karunanithi                     phosphor::dump::OperationStatus::InProgress, originatorId,
8274a1f39cSAsmitha Karunanithi                     originatorType, *this)));
836ccb50e1SDhruvaraj Subhashchandran     }
846ccb50e1SDhruvaraj Subhashchandran     catch (const std::invalid_argument& e)
856ccb50e1SDhruvaraj Subhashchandran     {
86*d1f670feSDhruvaraj Subhashchandran         lg2::error("Error in creating dump entry, errormsg: {ERROR}, "
87*d1f670feSDhruvaraj Subhashchandran                    "OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
88*d1f670feSDhruvaraj Subhashchandran                    "ERROR", e, "OBJECT_PATH", objPath, "ID", id);
896ccb50e1SDhruvaraj Subhashchandran         elog<InternalFailure>();
906ccb50e1SDhruvaraj Subhashchandran     }
916ccb50e1SDhruvaraj Subhashchandran 
9273953b8fSMarri Devender Rao     Manager::fUserDumpInProgress = true;
936ccb50e1SDhruvaraj Subhashchandran     return objPath.string();
94fef66a95SDhruvaraj Subhashchandran }
95fef66a95SDhruvaraj Subhashchandran 
96fef66a95SDhruvaraj Subhashchandran uint32_t Manager::captureDump(Type type,
97fef66a95SDhruvaraj Subhashchandran                               const std::vector<std::string>& fullPaths)
98fef66a95SDhruvaraj Subhashchandran {
99fef66a95SDhruvaraj Subhashchandran     // Get Dump size.
100fef66a95SDhruvaraj Subhashchandran     auto size = getAllowedSize();
101fef66a95SDhruvaraj Subhashchandran 
102fef66a95SDhruvaraj Subhashchandran     pid_t pid = fork();
103fef66a95SDhruvaraj Subhashchandran 
104fef66a95SDhruvaraj Subhashchandran     if (pid == 0)
105fef66a95SDhruvaraj Subhashchandran     {
1063fc6df48SJayanth Othayoth         std::filesystem::path dumpPath(dumpDir);
107fef66a95SDhruvaraj Subhashchandran         auto id = std::to_string(lastEntryId + 1);
108fef66a95SDhruvaraj Subhashchandran         dumpPath /= id;
109fef66a95SDhruvaraj Subhashchandran 
110fef66a95SDhruvaraj Subhashchandran         // get dreport type map entry
111fef66a95SDhruvaraj Subhashchandran         auto tempType = TypeMap.find(type);
112fef66a95SDhruvaraj Subhashchandran         execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
113fef66a95SDhruvaraj Subhashchandran               id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
114fef66a95SDhruvaraj Subhashchandran               fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
115fef66a95SDhruvaraj Subhashchandran               tempType->second.c_str(), nullptr);
116fef66a95SDhruvaraj Subhashchandran 
117fef66a95SDhruvaraj Subhashchandran         // dreport script execution is failed.
118fef66a95SDhruvaraj Subhashchandran         auto error = errno;
119*d1f670feSDhruvaraj Subhashchandran         lg2::error("Error occurred during dreport function execution, "
120*d1f670feSDhruvaraj Subhashchandran                    "errno: {ERRNO}",
121*d1f670feSDhruvaraj Subhashchandran                    "ERRNO", error);
122fef66a95SDhruvaraj Subhashchandran         elog<InternalFailure>();
123fef66a95SDhruvaraj Subhashchandran     }
124fef66a95SDhruvaraj Subhashchandran     else if (pid > 0)
125fef66a95SDhruvaraj Subhashchandran     {
1263ed02c32SMarri Devender Rao         Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
1273ed02c32SMarri Devender Rao             if (type == Type::UserRequested)
1283ed02c32SMarri Devender Rao             {
129*d1f670feSDhruvaraj Subhashchandran                 lg2::info("User initiated dump completed, resetting flag");
1303ed02c32SMarri Devender Rao                 Manager::fUserDumpInProgress = false;
1313ed02c32SMarri Devender Rao             }
1323ed02c32SMarri Devender Rao             this->childPtrMap.erase(pid);
1333ed02c32SMarri Devender Rao         };
1343ed02c32SMarri Devender Rao         try
1353ed02c32SMarri Devender Rao         {
1363ed02c32SMarri Devender Rao             childPtrMap.emplace(pid,
1373ed02c32SMarri Devender Rao                                 std::make_unique<Child>(eventLoop.get(), pid,
1383ed02c32SMarri Devender Rao                                                         WEXITED | WSTOPPED,
1393ed02c32SMarri Devender Rao                                                         std::move(callback)));
1403ed02c32SMarri Devender Rao         }
1413ed02c32SMarri Devender Rao         catch (const sdeventplus::SdEventError& ex)
142fef66a95SDhruvaraj Subhashchandran         {
143fef66a95SDhruvaraj Subhashchandran             // Failed to add to event loop
144*d1f670feSDhruvaraj Subhashchandran             lg2::error(
145*d1f670feSDhruvaraj Subhashchandran                 "Error occurred during the sdeventplus::source::Child creation "
146*d1f670feSDhruvaraj Subhashchandran                 "ex: {ERROR}",
147*d1f670feSDhruvaraj Subhashchandran                 "ERROR", ex);
148fef66a95SDhruvaraj Subhashchandran             elog<InternalFailure>();
149fef66a95SDhruvaraj Subhashchandran         }
150fef66a95SDhruvaraj Subhashchandran     }
151fef66a95SDhruvaraj Subhashchandran     else
152fef66a95SDhruvaraj Subhashchandran     {
153fef66a95SDhruvaraj Subhashchandran         auto error = errno;
154*d1f670feSDhruvaraj Subhashchandran         lg2::error("Error occurred during fork, errno: {ERRNO}", "ERRNO",
155*d1f670feSDhruvaraj Subhashchandran                    error);
156fef66a95SDhruvaraj Subhashchandran         elog<InternalFailure>();
157fef66a95SDhruvaraj Subhashchandran     }
158fef66a95SDhruvaraj Subhashchandran     return ++lastEntryId;
159fef66a95SDhruvaraj Subhashchandran }
160fef66a95SDhruvaraj Subhashchandran 
1613fc6df48SJayanth Othayoth void Manager::createEntry(const std::filesystem::path& file)
162fef66a95SDhruvaraj Subhashchandran {
163fef66a95SDhruvaraj Subhashchandran     // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
164fef66a95SDhruvaraj Subhashchandran     static constexpr auto ID_POS = 1;
165fef66a95SDhruvaraj Subhashchandran     static constexpr auto EPOCHTIME_POS = 2;
166fef66a95SDhruvaraj Subhashchandran     std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
167fef66a95SDhruvaraj Subhashchandran 
168fef66a95SDhruvaraj Subhashchandran     std::smatch match;
169fef66a95SDhruvaraj Subhashchandran     std::string name = file.filename();
170fef66a95SDhruvaraj Subhashchandran 
171fef66a95SDhruvaraj Subhashchandran     if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
172fef66a95SDhruvaraj Subhashchandran     {
173*d1f670feSDhruvaraj Subhashchandran         lg2::error("Invalid Dump file name, FILENAME: {FILENAME}", "FILENAME",
174*d1f670feSDhruvaraj Subhashchandran                    file);
175fef66a95SDhruvaraj Subhashchandran         return;
176fef66a95SDhruvaraj Subhashchandran     }
177fef66a95SDhruvaraj Subhashchandran 
178fef66a95SDhruvaraj Subhashchandran     auto idString = match[ID_POS];
17956bd797eSXie Ning     uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
180fef66a95SDhruvaraj Subhashchandran 
181fef66a95SDhruvaraj Subhashchandran     auto id = stoul(idString);
1826ccb50e1SDhruvaraj Subhashchandran 
1836ccb50e1SDhruvaraj Subhashchandran     // If there is an existing entry update it and return.
1846ccb50e1SDhruvaraj Subhashchandran     auto dumpEntry = entries.find(id);
1856ccb50e1SDhruvaraj Subhashchandran     if (dumpEntry != entries.end())
1866ccb50e1SDhruvaraj Subhashchandran     {
1876ccb50e1SDhruvaraj Subhashchandran         dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
18856bd797eSXie Ning             ->update(timestamp, std::filesystem::file_size(file), file);
1896ccb50e1SDhruvaraj Subhashchandran         return;
1906ccb50e1SDhruvaraj Subhashchandran     }
1916ccb50e1SDhruvaraj Subhashchandran 
192fef66a95SDhruvaraj Subhashchandran     // Entry Object path.
1933fc6df48SJayanth Othayoth     auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
194fef66a95SDhruvaraj Subhashchandran 
19574a1f39cSAsmitha Karunanithi     // TODO: Get the persisted originator id & type
19674a1f39cSAsmitha Karunanithi     // For now, replacing it with null
1976ccb50e1SDhruvaraj Subhashchandran     try
1986ccb50e1SDhruvaraj Subhashchandran     {
199a6ab806dSDhruvaraj Subhashchandran         entries.insert(std::make_pair(
2003fc6df48SJayanth Othayoth             id, std::make_unique<bmc::Entry>(
20156bd797eSXie Ning                     bus, objPath.c_str(), id, timestamp,
2023fc6df48SJayanth Othayoth                     std::filesystem::file_size(file), file,
20374a1f39cSAsmitha Karunanithi                     phosphor::dump::OperationStatus::Completed, std::string(),
20474a1f39cSAsmitha Karunanithi                     originatorTypes::Internal, *this)));
205fef66a95SDhruvaraj Subhashchandran     }
206fef66a95SDhruvaraj Subhashchandran     catch (const std::invalid_argument& e)
207fef66a95SDhruvaraj Subhashchandran     {
208*d1f670feSDhruvaraj Subhashchandran         lg2::error(
209*d1f670feSDhruvaraj Subhashchandran             "Error in creating dump entry, errormsg: {ERROR}, "
210*d1f670feSDhruvaraj Subhashchandran             "OBJECTPATH: {OBJECT_PATH}, ID: {ID}, TIMESTAMP: {TIMESTAMP}, "
211*d1f670feSDhruvaraj Subhashchandran             "SIZE: {SIZE}, FILENAME: {FILENAME}",
212*d1f670feSDhruvaraj Subhashchandran             "ERROR", e, "OBJECT_PATH", objPath, "ID", id, "TIMESTAMP",
213*d1f670feSDhruvaraj Subhashchandran             timestamp, "SIZE", std::filesystem::file_size(file), "FILENAME",
214*d1f670feSDhruvaraj Subhashchandran             file);
215fef66a95SDhruvaraj Subhashchandran         return;
216fef66a95SDhruvaraj Subhashchandran     }
217fef66a95SDhruvaraj Subhashchandran }
218fef66a95SDhruvaraj Subhashchandran 
219fef66a95SDhruvaraj Subhashchandran void Manager::watchCallback(const UserMap& fileInfo)
220fef66a95SDhruvaraj Subhashchandran {
221fef66a95SDhruvaraj Subhashchandran     for (const auto& i : fileInfo)
222fef66a95SDhruvaraj Subhashchandran     {
223fef66a95SDhruvaraj Subhashchandran         // For any new dump file create dump entry object
224fef66a95SDhruvaraj Subhashchandran         // and associated inotify watch.
225fef66a95SDhruvaraj Subhashchandran         if (IN_CLOSE_WRITE == i.second)
226fef66a95SDhruvaraj Subhashchandran         {
2274cb0799fSChirag Sharma             if (!std::filesystem::is_directory(i.first))
2284cb0799fSChirag Sharma             {
2294cb0799fSChirag Sharma                 // Don't require filename to be passed, as the path
2304cb0799fSChirag Sharma                 // of dump directory is stored in the childWatchMap
2314cb0799fSChirag Sharma                 removeWatch(i.first.parent_path());
232fef66a95SDhruvaraj Subhashchandran 
2334cb0799fSChirag Sharma                 // dump file is written now create D-Bus entry
234fef66a95SDhruvaraj Subhashchandran                 createEntry(i.first);
235fef66a95SDhruvaraj Subhashchandran             }
2364cb0799fSChirag Sharma             else
2374cb0799fSChirag Sharma             {
2384cb0799fSChirag Sharma                 removeWatch(i.first);
2394cb0799fSChirag Sharma             }
2404cb0799fSChirag Sharma         }
241fef66a95SDhruvaraj Subhashchandran         // Start inotify watch on newly created directory.
2423fc6df48SJayanth Othayoth         else if ((IN_CREATE == i.second) &&
2433fc6df48SJayanth Othayoth                  std::filesystem::is_directory(i.first))
244fef66a95SDhruvaraj Subhashchandran         {
245fef66a95SDhruvaraj Subhashchandran             auto watchObj = std::make_unique<Watch>(
246fef66a95SDhruvaraj Subhashchandran                 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
247fef66a95SDhruvaraj Subhashchandran                 std::bind(
248fef66a95SDhruvaraj Subhashchandran                     std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
249fef66a95SDhruvaraj Subhashchandran                     this, std::placeholders::_1));
250fef66a95SDhruvaraj Subhashchandran 
251fef66a95SDhruvaraj Subhashchandran             childWatchMap.emplace(i.first, std::move(watchObj));
252fef66a95SDhruvaraj Subhashchandran         }
253fef66a95SDhruvaraj Subhashchandran     }
254fef66a95SDhruvaraj Subhashchandran }
255fef66a95SDhruvaraj Subhashchandran 
2563fc6df48SJayanth Othayoth void Manager::removeWatch(const std::filesystem::path& path)
257fef66a95SDhruvaraj Subhashchandran {
258fef66a95SDhruvaraj Subhashchandran     // Delete Watch entry from map.
259fef66a95SDhruvaraj Subhashchandran     childWatchMap.erase(path);
260fef66a95SDhruvaraj Subhashchandran }
261fef66a95SDhruvaraj Subhashchandran 
262fef66a95SDhruvaraj Subhashchandran void Manager::restore()
263fef66a95SDhruvaraj Subhashchandran {
2643fc6df48SJayanth Othayoth     std::filesystem::path dir(dumpDir);
2653fc6df48SJayanth Othayoth     if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
266fef66a95SDhruvaraj Subhashchandran     {
267fef66a95SDhruvaraj Subhashchandran         return;
268fef66a95SDhruvaraj Subhashchandran     }
269fef66a95SDhruvaraj Subhashchandran 
270fef66a95SDhruvaraj Subhashchandran     // Dump file path: <DUMP_PATH>/<id>/<filename>
2713fc6df48SJayanth Othayoth     for (const auto& p : std::filesystem::directory_iterator(dir))
272fef66a95SDhruvaraj Subhashchandran     {
273fef66a95SDhruvaraj Subhashchandran         auto idStr = p.path().filename().string();
274fef66a95SDhruvaraj Subhashchandran 
275fef66a95SDhruvaraj Subhashchandran         // Consider only directory's with dump id as name.
276fef66a95SDhruvaraj Subhashchandran         // Note: As per design one file per directory.
2773fc6df48SJayanth Othayoth         if ((std::filesystem::is_directory(p.path())) &&
278fef66a95SDhruvaraj Subhashchandran             std::all_of(idStr.begin(), idStr.end(), ::isdigit))
279fef66a95SDhruvaraj Subhashchandran         {
28078e88402SPatrick Williams             lastEntryId = std::max(lastEntryId,
28178e88402SPatrick Williams                                    static_cast<uint32_t>(std::stoul(idStr)));
2823fc6df48SJayanth Othayoth             auto fileIt = std::filesystem::directory_iterator(p.path());
283fef66a95SDhruvaraj Subhashchandran             // Create dump entry d-bus object.
2843fc6df48SJayanth Othayoth             if (fileIt != std::filesystem::end(fileIt))
285fef66a95SDhruvaraj Subhashchandran             {
286fef66a95SDhruvaraj Subhashchandran                 createEntry(fileIt->path());
287fef66a95SDhruvaraj Subhashchandran             }
288fef66a95SDhruvaraj Subhashchandran         }
289fef66a95SDhruvaraj Subhashchandran     }
290fef66a95SDhruvaraj Subhashchandran }
291fef66a95SDhruvaraj Subhashchandran 
292fc69f35cSXie Ning size_t getDirectorySize(const std::string dir)
293fef66a95SDhruvaraj Subhashchandran {
294fef66a95SDhruvaraj Subhashchandran     auto size = 0;
295fc69f35cSXie Ning     for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
296fef66a95SDhruvaraj Subhashchandran     {
2973fc6df48SJayanth Othayoth         if (!std::filesystem::is_directory(p))
298fef66a95SDhruvaraj Subhashchandran         {
299bb9366d0STim Lee             size += std::ceil(std::filesystem::file_size(p) / 1024.0);
300fef66a95SDhruvaraj Subhashchandran         }
301fef66a95SDhruvaraj Subhashchandran     }
302fc69f35cSXie Ning     return size;
303fc69f35cSXie Ning }
304fc69f35cSXie Ning 
305fc69f35cSXie Ning size_t Manager::getAllowedSize()
306fc69f35cSXie Ning {
307fc69f35cSXie Ning     // Get current size of the dump directory.
308fc69f35cSXie Ning     auto size = getDirectorySize(dumpDir);
309fef66a95SDhruvaraj Subhashchandran 
310fef66a95SDhruvaraj Subhashchandran     // Set the Dump size to Maximum  if the free space is greater than
311fef66a95SDhruvaraj Subhashchandran     // Dump max size otherwise return the available size.
312fef66a95SDhruvaraj Subhashchandran 
313fef66a95SDhruvaraj Subhashchandran     size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
314fef66a95SDhruvaraj Subhashchandran 
315fc69f35cSXie Ning #ifdef BMC_DUMP_ROTATE_CONFIG
316fc69f35cSXie Ning     // Delete the first existing file until the space is enough
317fc69f35cSXie Ning     while (size < BMC_DUMP_MIN_SPACE_REQD)
318fc69f35cSXie Ning     {
31978e88402SPatrick Williams         auto delEntry = min_element(entries.begin(), entries.end(),
32078e88402SPatrick Williams                                     [](const auto& l, const auto& r) {
32178e88402SPatrick Williams             return l.first < r.first;
32278e88402SPatrick Williams         });
32378e88402SPatrick Williams         auto delPath = std::filesystem::path(dumpDir) /
32478e88402SPatrick Williams                        std::to_string(delEntry->first);
325fc69f35cSXie Ning 
326fc69f35cSXie Ning         size += getDirectorySize(delPath);
327fc69f35cSXie Ning 
328fc69f35cSXie Ning         delEntry->second->delete_();
329fc69f35cSXie Ning     }
330fc69f35cSXie Ning #else
331fc69f35cSXie Ning     using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
332fc69f35cSXie Ning     using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
333fc69f35cSXie Ning 
334fef66a95SDhruvaraj Subhashchandran     if (size < BMC_DUMP_MIN_SPACE_REQD)
335fef66a95SDhruvaraj Subhashchandran     {
336fef66a95SDhruvaraj Subhashchandran         // Reached to maximum limit
337fef66a95SDhruvaraj Subhashchandran         elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
338fef66a95SDhruvaraj Subhashchandran     }
339fc69f35cSXie Ning #endif
340fc69f35cSXie Ning 
341fef66a95SDhruvaraj Subhashchandran     if (size > BMC_DUMP_MAX_SIZE)
342fef66a95SDhruvaraj Subhashchandran     {
343fef66a95SDhruvaraj Subhashchandran         size = BMC_DUMP_MAX_SIZE;
344fef66a95SDhruvaraj Subhashchandran     }
345fef66a95SDhruvaraj Subhashchandran 
346fef66a95SDhruvaraj Subhashchandran     return size;
347fef66a95SDhruvaraj Subhashchandran }
348fef66a95SDhruvaraj Subhashchandran 
349fef66a95SDhruvaraj Subhashchandran } // namespace bmc
350fef66a95SDhruvaraj Subhashchandran } // namespace dump
351fef66a95SDhruvaraj Subhashchandran } // namespace phosphor
352