blob: 25bc04ef0ff13fb896b2575fafaa2bfbdfdfb3f3 [file] [log] [blame]
John Edward Broadbent4bc8a102021-12-30 16:11:49 -08001#pragma once
2
3#include "erase.hpp"
John Edward Broadbenta6e3b992022-03-17 14:33:15 -07004#include "util.hpp"
John Edward Broadbent4bc8a102021-12-30 16:11:49 -08005
6#include <stdplus/fd/create.hpp>
7#include <stdplus/fd/managed.hpp>
8
John Edward Broadbentd6071fc2022-03-31 19:33:21 -07009#include <chrono>
10
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080011namespace estoraged
12{
13
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070014using stdplus::fd::Fd;
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080015
16class Zero : public Erase
17{
18 public:
19 /** @brief Creates a zero erase object.
20 *
21 * @param[in] inDevPath - the linux device path for the block device.
22 */
Patrick Williams04c28fa2023-05-10 07:51:24 -050023 Zero(std::string_view inDevPath) : Erase(inDevPath) {}
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080024 /** @brief writes zero to the drive
25 * and throws errors accordingly.
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070026 * @param[in] driveSize - the size of the block device in bytes
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070027 * @param[in] fd - the stdplus file descriptor
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080028 */
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070029 void writeZero(uint64_t driveSize, Fd& fd);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080030
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070031 /** @brief writes zero to the drive using default parameters,
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070032 * and throws errors accordingly.
33 */
34 void writeZero()
35 {
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070036 stdplus::fd::Fd&& fd =
37 stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
38 writeZero(util::findSizeOfBlockDevice(devPath), fd);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070039 }
40
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070041 /** @brief verifies the drive has only zeros on it,
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080042 * and throws errors accordingly.
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070043 * @param[in] driveSize - the size of the block device in bytes
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070044 * @param[in] fd - the stdplus file descriptor
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080045 */
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070046 void verifyZero(uint64_t driveSize, Fd& fd);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080047
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070048 /** @brief verifies the drive has only zeros on it,
49 * using the default parameters. It also throws errors accordingly.
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070050 */
51 void verifyZero()
52 {
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070053 stdplus::fd::Fd&& fd =
54 stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
55 verifyZero(util::findSizeOfBlockDevice(devPath), fd);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070056 }
57
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080058 private:
59 /* @brief the size of the blocks in bytes used for write and verify.
60 * 32768 was also tested. It had almost identical performance.
61 */
62 static constexpr size_t blockSize = 4096;
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070063 static constexpr size_t maxRetry = 32;
64 static constexpr std::chrono::duration delay = std::chrono::milliseconds(1);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080065};
66
67} // namespace estoraged