1fef66a95SDhruvaraj Subhashchandran #include "config.h"
2fef66a95SDhruvaraj Subhashchandran
3fef66a95SDhruvaraj Subhashchandran #include "dump_manager_bmc.hpp"
4fef66a95SDhruvaraj Subhashchandran
5fef66a95SDhruvaraj Subhashchandran #include "bmc_dump_entry.hpp"
63604710dSDhruvaraj Subhashchandran #include "dump_types.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>
15d1f670feSDhruvaraj 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>
20fef66a95SDhruvaraj Subhashchandran
21fef66a95SDhruvaraj Subhashchandran namespace phosphor
22fef66a95SDhruvaraj Subhashchandran {
23fef66a95SDhruvaraj Subhashchandran namespace dump
24fef66a95SDhruvaraj Subhashchandran {
25fef66a95SDhruvaraj Subhashchandran namespace bmc
26fef66a95SDhruvaraj Subhashchandran {
27fef66a95SDhruvaraj Subhashchandran
28fef66a95SDhruvaraj Subhashchandran using namespace sdbusplus::xyz::openbmc_project::Common::Error;
29fef66a95SDhruvaraj Subhashchandran using namespace phosphor::logging;
30fef66a95SDhruvaraj Subhashchandran
3173953b8fSMarri Devender Rao bool Manager::fUserDumpInProgress = false;
323604710dSDhruvaraj Subhashchandran constexpr auto BMC_DUMP = "BMC_DUMP";
3373953b8fSMarri Devender Rao
34969f9a59SDhruvaraj Subhashchandran sdbusplus::message::object_path
createDump(phosphor::dump::DumpCreateParams params)35ddc3366eSDhruvaraj Subhashchandran Manager::createDump(phosphor::dump::DumpCreateParams params)
36fef66a95SDhruvaraj Subhashchandran {
3774a1f39cSAsmitha Karunanithi if (params.size() > CREATE_DUMP_MAX_PARAMS)
38969f9a59SDhruvaraj Subhashchandran {
39d1f670feSDhruvaraj Subhashchandran lg2::warning("BMC dump accepts not more than 2 additional parameters");
40969f9a59SDhruvaraj Subhashchandran }
4174a1f39cSAsmitha Karunanithi
4274a1f39cSAsmitha Karunanithi // Get the originator id and type from params
4374a1f39cSAsmitha Karunanithi std::string originatorId;
4474a1f39cSAsmitha Karunanithi originatorTypes originatorType;
4574a1f39cSAsmitha Karunanithi
4674a1f39cSAsmitha Karunanithi phosphor::dump::extractOriginatorProperties(params, originatorId,
4774a1f39cSAsmitha Karunanithi originatorType);
483604710dSDhruvaraj Subhashchandran using CreateParameters =
493604710dSDhruvaraj Subhashchandran sdbusplus::common::xyz::openbmc_project::dump::Create::CreateParameters;
503604710dSDhruvaraj Subhashchandran
513604710dSDhruvaraj Subhashchandran DumpTypes dumpType = DumpTypes::USER;
523604710dSDhruvaraj Subhashchandran std::string type = extractParameter<std::string>(
533604710dSDhruvaraj Subhashchandran convertCreateParametersToString(CreateParameters::DumpType), params);
543604710dSDhruvaraj Subhashchandran if (!type.empty())
553604710dSDhruvaraj Subhashchandran {
563604710dSDhruvaraj Subhashchandran dumpType = validateDumpType(type, BMC_DUMP);
573604710dSDhruvaraj Subhashchandran }
58e4350f93SDhruvaraj Subhashchandran
59e4350f93SDhruvaraj Subhashchandran if (dumpType == DumpTypes::ELOG)
60e4350f93SDhruvaraj Subhashchandran {
61e4350f93SDhruvaraj Subhashchandran dumpType = getErrorDumpType(params);
62e4350f93SDhruvaraj Subhashchandran }
633604710dSDhruvaraj Subhashchandran std::string path = extractParameter<std::string>(
643604710dSDhruvaraj Subhashchandran convertCreateParametersToString(CreateParameters::FilePath), params);
653604710dSDhruvaraj Subhashchandran
663604710dSDhruvaraj Subhashchandran if ((Manager::fUserDumpInProgress == true) && (dumpType == DumpTypes::USER))
673604710dSDhruvaraj Subhashchandran {
683604710dSDhruvaraj Subhashchandran lg2::info("Another user initiated dump in progress");
693604710dSDhruvaraj Subhashchandran elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
703604710dSDhruvaraj Subhashchandran }
713604710dSDhruvaraj Subhashchandran
723604710dSDhruvaraj Subhashchandran lg2::info("Initiating new BMC dump with type: {TYPE} path: {PATH}", "TYPE",
73*418d460aSJayanth Othayoth dumpTypeToString(dumpType).value_or("unknown").c_str(), "PATH",
74*418d460aSJayanth Othayoth path);
753604710dSDhruvaraj Subhashchandran
763604710dSDhruvaraj Subhashchandran auto id = captureDump(dumpType, path);
776ccb50e1SDhruvaraj Subhashchandran
786ccb50e1SDhruvaraj Subhashchandran // Entry Object path.
793fc6df48SJayanth Othayoth auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
806ccb50e1SDhruvaraj Subhashchandran
816ccb50e1SDhruvaraj Subhashchandran try
826ccb50e1SDhruvaraj Subhashchandran {
83c0ab9d43SClaire Weinan uint64_t timeStamp =
84c0ab9d43SClaire Weinan std::chrono::duration_cast<std::chrono::microseconds>(
85c0ab9d43SClaire Weinan std::chrono::system_clock::now().time_since_epoch())
86c0ab9d43SClaire Weinan .count();
87c0ab9d43SClaire Weinan
886ccb50e1SDhruvaraj Subhashchandran entries.insert(std::make_pair(
89a6ab806dSDhruvaraj Subhashchandran id, std::make_unique<bmc::Entry>(
90a6ab806dSDhruvaraj Subhashchandran bus, objPath.c_str(), id, timeStamp, 0, std::string(),
9174a1f39cSAsmitha Karunanithi phosphor::dump::OperationStatus::InProgress, originatorId,
9274a1f39cSAsmitha Karunanithi originatorType, *this)));
936ccb50e1SDhruvaraj Subhashchandran }
946ccb50e1SDhruvaraj Subhashchandran catch (const std::invalid_argument& e)
956ccb50e1SDhruvaraj Subhashchandran {
96d1f670feSDhruvaraj Subhashchandran lg2::error("Error in creating dump entry, errormsg: {ERROR}, "
97d1f670feSDhruvaraj Subhashchandran "OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
98d1f670feSDhruvaraj Subhashchandran "ERROR", e, "OBJECT_PATH", objPath, "ID", id);
996ccb50e1SDhruvaraj Subhashchandran elog<InternalFailure>();
1006ccb50e1SDhruvaraj Subhashchandran }
1016ccb50e1SDhruvaraj Subhashchandran
1023604710dSDhruvaraj Subhashchandran if (dumpType == DumpTypes::USER)
1033604710dSDhruvaraj Subhashchandran {
10473953b8fSMarri Devender Rao Manager::fUserDumpInProgress = true;
1053604710dSDhruvaraj Subhashchandran }
1066ccb50e1SDhruvaraj Subhashchandran return objPath.string();
107fef66a95SDhruvaraj Subhashchandran }
108fef66a95SDhruvaraj Subhashchandran
captureDump(DumpTypes type,const std::string & path)1093604710dSDhruvaraj Subhashchandran uint32_t Manager::captureDump(DumpTypes type, const std::string& path)
1103604710dSDhruvaraj Subhashchandran {
111fef66a95SDhruvaraj Subhashchandran // Get Dump size.
112fef66a95SDhruvaraj Subhashchandran auto size = getAllowedSize();
113fef66a95SDhruvaraj Subhashchandran
114fef66a95SDhruvaraj Subhashchandran pid_t pid = fork();
115fef66a95SDhruvaraj Subhashchandran
116fef66a95SDhruvaraj Subhashchandran if (pid == 0)
117fef66a95SDhruvaraj Subhashchandran {
1183fc6df48SJayanth Othayoth std::filesystem::path dumpPath(dumpDir);
119fef66a95SDhruvaraj Subhashchandran auto id = std::to_string(lastEntryId + 1);
120fef66a95SDhruvaraj Subhashchandran dumpPath /= id;
121fef66a95SDhruvaraj Subhashchandran
122*418d460aSJayanth Othayoth auto strType = dumpTypeToString(type).value_or("unknown");
123fef66a95SDhruvaraj Subhashchandran execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
124fef66a95SDhruvaraj Subhashchandran id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
1253604710dSDhruvaraj Subhashchandran path.empty() ? "" : path.c_str(), "-t", strType.c_str(), nullptr);
126fef66a95SDhruvaraj Subhashchandran
127fef66a95SDhruvaraj Subhashchandran // dreport script execution is failed.
128fef66a95SDhruvaraj Subhashchandran auto error = errno;
129d1f670feSDhruvaraj Subhashchandran lg2::error("Error occurred during dreport function execution, "
130d1f670feSDhruvaraj Subhashchandran "errno: {ERRNO}",
131d1f670feSDhruvaraj Subhashchandran "ERRNO", error);
132fef66a95SDhruvaraj Subhashchandran elog<InternalFailure>();
133fef66a95SDhruvaraj Subhashchandran }
134fef66a95SDhruvaraj Subhashchandran else if (pid > 0)
135fef66a95SDhruvaraj Subhashchandran {
1363ed02c32SMarri Devender Rao Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
1373604710dSDhruvaraj Subhashchandran if (type == DumpTypes::USER)
1383ed02c32SMarri Devender Rao {
139d1f670feSDhruvaraj Subhashchandran lg2::info("User initiated dump completed, resetting flag");
1403ed02c32SMarri Devender Rao Manager::fUserDumpInProgress = false;
1413ed02c32SMarri Devender Rao }
1423ed02c32SMarri Devender Rao this->childPtrMap.erase(pid);
1433ed02c32SMarri Devender Rao };
1443ed02c32SMarri Devender Rao try
1453ed02c32SMarri Devender Rao {
1463ed02c32SMarri Devender Rao childPtrMap.emplace(pid,
1473ed02c32SMarri Devender Rao std::make_unique<Child>(eventLoop.get(), pid,
1483ed02c32SMarri Devender Rao WEXITED | WSTOPPED,
1493ed02c32SMarri Devender Rao std::move(callback)));
1503ed02c32SMarri Devender Rao }
1513ed02c32SMarri Devender Rao catch (const sdeventplus::SdEventError& ex)
152fef66a95SDhruvaraj Subhashchandran {
153fef66a95SDhruvaraj Subhashchandran // Failed to add to event loop
154d1f670feSDhruvaraj Subhashchandran lg2::error(
155d1f670feSDhruvaraj Subhashchandran "Error occurred during the sdeventplus::source::Child creation "
156d1f670feSDhruvaraj Subhashchandran "ex: {ERROR}",
157d1f670feSDhruvaraj Subhashchandran "ERROR", ex);
158fef66a95SDhruvaraj Subhashchandran elog<InternalFailure>();
159fef66a95SDhruvaraj Subhashchandran }
160fef66a95SDhruvaraj Subhashchandran }
161fef66a95SDhruvaraj Subhashchandran else
162fef66a95SDhruvaraj Subhashchandran {
163fef66a95SDhruvaraj Subhashchandran auto error = errno;
164d1f670feSDhruvaraj Subhashchandran lg2::error("Error occurred during fork, errno: {ERRNO}", "ERRNO",
165d1f670feSDhruvaraj Subhashchandran error);
166fef66a95SDhruvaraj Subhashchandran elog<InternalFailure>();
167fef66a95SDhruvaraj Subhashchandran }
168fef66a95SDhruvaraj Subhashchandran return ++lastEntryId;
169fef66a95SDhruvaraj Subhashchandran }
170fef66a95SDhruvaraj Subhashchandran
createEntry(const std::filesystem::path & file)1713fc6df48SJayanth Othayoth void Manager::createEntry(const std::filesystem::path& file)
172fef66a95SDhruvaraj Subhashchandran {
17393f06410SDhruvaraj Subhashchandran auto dumpDetails = extractDumpDetails(file);
17493f06410SDhruvaraj Subhashchandran if (!dumpDetails)
175fef66a95SDhruvaraj Subhashchandran {
17693f06410SDhruvaraj Subhashchandran lg2::error("Failed to extract dump details from file name: {PATH}",
17793f06410SDhruvaraj Subhashchandran "PATH", file);
178fef66a95SDhruvaraj Subhashchandran return;
179fef66a95SDhruvaraj Subhashchandran }
180fef66a95SDhruvaraj Subhashchandran
18193f06410SDhruvaraj Subhashchandran auto [id, timestamp, size] = *dumpDetails;
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 {
208d1f670feSDhruvaraj Subhashchandran lg2::error(
209d1f670feSDhruvaraj Subhashchandran "Error in creating dump entry, errormsg: {ERROR}, "
210d1f670feSDhruvaraj Subhashchandran "OBJECTPATH: {OBJECT_PATH}, ID: {ID}, TIMESTAMP: {TIMESTAMP}, "
211d1f670feSDhruvaraj Subhashchandran "SIZE: {SIZE}, FILENAME: {FILENAME}",
212d1f670feSDhruvaraj Subhashchandran "ERROR", e, "OBJECT_PATH", objPath, "ID", id, "TIMESTAMP",
213d1f670feSDhruvaraj Subhashchandran timestamp, "SIZE", std::filesystem::file_size(file), "FILENAME",
214d1f670feSDhruvaraj Subhashchandran file);
215fef66a95SDhruvaraj Subhashchandran }
216fef66a95SDhruvaraj Subhashchandran }
217fef66a95SDhruvaraj Subhashchandran
watchCallback(const UserMap & fileInfo)218fef66a95SDhruvaraj Subhashchandran void Manager::watchCallback(const UserMap& fileInfo)
219fef66a95SDhruvaraj Subhashchandran {
220fef66a95SDhruvaraj Subhashchandran for (const auto& i : fileInfo)
221fef66a95SDhruvaraj Subhashchandran {
222fef66a95SDhruvaraj Subhashchandran // For any new dump file create dump entry object
223fef66a95SDhruvaraj Subhashchandran // and associated inotify watch.
224fef66a95SDhruvaraj Subhashchandran if (IN_CLOSE_WRITE == i.second)
225fef66a95SDhruvaraj Subhashchandran {
2264cb0799fSChirag Sharma if (!std::filesystem::is_directory(i.first))
2274cb0799fSChirag Sharma {
2284cb0799fSChirag Sharma // Don't require filename to be passed, as the path
2294cb0799fSChirag Sharma // of dump directory is stored in the childWatchMap
2304cb0799fSChirag Sharma removeWatch(i.first.parent_path());
231fef66a95SDhruvaraj Subhashchandran
2324cb0799fSChirag Sharma // dump file is written now create D-Bus entry
233fef66a95SDhruvaraj Subhashchandran createEntry(i.first);
234fef66a95SDhruvaraj Subhashchandran }
2354cb0799fSChirag Sharma else
2364cb0799fSChirag Sharma {
2374cb0799fSChirag Sharma removeWatch(i.first);
2384cb0799fSChirag Sharma }
2394cb0799fSChirag Sharma }
240fef66a95SDhruvaraj Subhashchandran // Start inotify watch on newly created directory.
2413fc6df48SJayanth Othayoth else if ((IN_CREATE == i.second) &&
2423fc6df48SJayanth Othayoth std::filesystem::is_directory(i.first))
243fef66a95SDhruvaraj Subhashchandran {
244fef66a95SDhruvaraj Subhashchandran auto watchObj = std::make_unique<Watch>(
245fef66a95SDhruvaraj Subhashchandran eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
246fef66a95SDhruvaraj Subhashchandran std::bind(
247fef66a95SDhruvaraj Subhashchandran std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
248fef66a95SDhruvaraj Subhashchandran this, std::placeholders::_1));
249fef66a95SDhruvaraj Subhashchandran
250fef66a95SDhruvaraj Subhashchandran childWatchMap.emplace(i.first, std::move(watchObj));
251fef66a95SDhruvaraj Subhashchandran }
252fef66a95SDhruvaraj Subhashchandran }
253fef66a95SDhruvaraj Subhashchandran }
254fef66a95SDhruvaraj Subhashchandran
removeWatch(const std::filesystem::path & path)2553fc6df48SJayanth Othayoth void Manager::removeWatch(const std::filesystem::path& path)
256fef66a95SDhruvaraj Subhashchandran {
257fef66a95SDhruvaraj Subhashchandran // Delete Watch entry from map.
258fef66a95SDhruvaraj Subhashchandran childWatchMap.erase(path);
259fef66a95SDhruvaraj Subhashchandran }
260fef66a95SDhruvaraj Subhashchandran
restore()261fef66a95SDhruvaraj Subhashchandran void Manager::restore()
262fef66a95SDhruvaraj Subhashchandran {
2633fc6df48SJayanth Othayoth std::filesystem::path dir(dumpDir);
2643fc6df48SJayanth Othayoth if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
265fef66a95SDhruvaraj Subhashchandran {
266fef66a95SDhruvaraj Subhashchandran return;
267fef66a95SDhruvaraj Subhashchandran }
268fef66a95SDhruvaraj Subhashchandran
269fef66a95SDhruvaraj Subhashchandran // Dump file path: <DUMP_PATH>/<id>/<filename>
2703fc6df48SJayanth Othayoth for (const auto& p : std::filesystem::directory_iterator(dir))
271fef66a95SDhruvaraj Subhashchandran {
272fef66a95SDhruvaraj Subhashchandran auto idStr = p.path().filename().string();
273fef66a95SDhruvaraj Subhashchandran
27493f06410SDhruvaraj Subhashchandran // Consider only directories with dump id as name.
275fef66a95SDhruvaraj Subhashchandran // Note: As per design one file per directory.
2763fc6df48SJayanth Othayoth if ((std::filesystem::is_directory(p.path())) &&
277fef66a95SDhruvaraj Subhashchandran std::all_of(idStr.begin(), idStr.end(), ::isdigit))
278fef66a95SDhruvaraj Subhashchandran {
279973b291eSPatrick Williams lastEntryId =
280973b291eSPatrick Williams std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
28193f06410SDhruvaraj Subhashchandran for (const auto& file :
28293f06410SDhruvaraj Subhashchandran std::filesystem::directory_iterator(p.path()))
283fef66a95SDhruvaraj Subhashchandran {
28493f06410SDhruvaraj Subhashchandran // Skip .preserve directory
28593f06410SDhruvaraj Subhashchandran if (file.path().filename() == PRESERVE)
28693f06410SDhruvaraj Subhashchandran {
28793f06410SDhruvaraj Subhashchandran continue;
28893f06410SDhruvaraj Subhashchandran }
28993f06410SDhruvaraj Subhashchandran
29093f06410SDhruvaraj Subhashchandran // Entry Object path.
29193f06410SDhruvaraj Subhashchandran auto objPath = std::filesystem::path(baseEntryPath) / idStr;
292973b291eSPatrick Williams auto entry = Entry::deserializeEntry(
293973b291eSPatrick Williams bus, std::stoul(idStr), objPath.string(), file.path(),
294973b291eSPatrick Williams *this);
29593f06410SDhruvaraj Subhashchandran
29693f06410SDhruvaraj Subhashchandran if (entry != nullptr)
29793f06410SDhruvaraj Subhashchandran {
29893f06410SDhruvaraj Subhashchandran entries.insert(
29993f06410SDhruvaraj Subhashchandran std::make_pair(entry->getDumpId(), std::move(entry)));
30093f06410SDhruvaraj Subhashchandran }
301fef66a95SDhruvaraj Subhashchandran }
302fef66a95SDhruvaraj Subhashchandran }
303fef66a95SDhruvaraj Subhashchandran }
304fef66a95SDhruvaraj Subhashchandran }
305fef66a95SDhruvaraj Subhashchandran
getDirectorySize(const std::string dir)306fc69f35cSXie Ning size_t getDirectorySize(const std::string dir)
307fef66a95SDhruvaraj Subhashchandran {
308fef66a95SDhruvaraj Subhashchandran auto size = 0;
309fc69f35cSXie Ning for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
310fef66a95SDhruvaraj Subhashchandran {
3113fc6df48SJayanth Othayoth if (!std::filesystem::is_directory(p))
312fef66a95SDhruvaraj Subhashchandran {
31317ba876cSJayanth Othayoth std::uintmax_t fileSize = std::filesystem::file_size(p);
31417ba876cSJayanth Othayoth size += std::ceil(static_cast<double>(fileSize) / 1024.0);
315fef66a95SDhruvaraj Subhashchandran }
316fef66a95SDhruvaraj Subhashchandran }
317fc69f35cSXie Ning return size;
318fc69f35cSXie Ning }
319fc69f35cSXie Ning
getAllowedSize()320fc69f35cSXie Ning size_t Manager::getAllowedSize()
321fc69f35cSXie Ning {
322fc69f35cSXie Ning // Get current size of the dump directory.
323fc69f35cSXie Ning auto size = getDirectorySize(dumpDir);
324fef66a95SDhruvaraj Subhashchandran
325fef66a95SDhruvaraj Subhashchandran // Set the Dump size to Maximum if the free space is greater than
326fef66a95SDhruvaraj Subhashchandran // Dump max size otherwise return the available size.
327fef66a95SDhruvaraj Subhashchandran
328fef66a95SDhruvaraj Subhashchandran size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
329fef66a95SDhruvaraj Subhashchandran
330fc69f35cSXie Ning #ifdef BMC_DUMP_ROTATE_CONFIG
331fc69f35cSXie Ning // Delete the first existing file until the space is enough
332fc69f35cSXie Ning while (size < BMC_DUMP_MIN_SPACE_REQD)
333fc69f35cSXie Ning {
334e70edac3SPatrick Williams auto delEntry = min_element(
335e70edac3SPatrick Williams entries.begin(), entries.end(),
336e70edac3SPatrick Williams [](const auto& l, const auto& r) { return l.first < r.first; });
33778e88402SPatrick Williams auto delPath = std::filesystem::path(dumpDir) /
33878e88402SPatrick Williams std::to_string(delEntry->first);
339fc69f35cSXie Ning
340fc69f35cSXie Ning size += getDirectorySize(delPath);
341fc69f35cSXie Ning
342fc69f35cSXie Ning delEntry->second->delete_();
343fc69f35cSXie Ning }
344fc69f35cSXie Ning #else
345fc69f35cSXie Ning using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
346fc69f35cSXie Ning using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
347fc69f35cSXie Ning
348fef66a95SDhruvaraj Subhashchandran if (size < BMC_DUMP_MIN_SPACE_REQD)
349fef66a95SDhruvaraj Subhashchandran {
350fef66a95SDhruvaraj Subhashchandran // Reached to maximum limit
351fef66a95SDhruvaraj Subhashchandran elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
352fef66a95SDhruvaraj Subhashchandran }
353fc69f35cSXie Ning #endif
354fc69f35cSXie Ning
355fef66a95SDhruvaraj Subhashchandran if (size > BMC_DUMP_MAX_SIZE)
356fef66a95SDhruvaraj Subhashchandran {
357fef66a95SDhruvaraj Subhashchandran size = BMC_DUMP_MAX_SIZE;
358fef66a95SDhruvaraj Subhashchandran }
359fef66a95SDhruvaraj Subhashchandran
360fef66a95SDhruvaraj Subhashchandran return size;
361fef66a95SDhruvaraj Subhashchandran }
362fef66a95SDhruvaraj Subhashchandran
363fef66a95SDhruvaraj Subhashchandran } // namespace bmc
364fef66a95SDhruvaraj Subhashchandran } // namespace dump
365fef66a95SDhruvaraj Subhashchandran } // namespace phosphor
366