blob: bfa240d9158ec49a16d6603d4d104d795921d8af [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 */
23 Zero(std::string_view inDevPath) : Erase(inDevPath)
24 {}
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080025 /** @brief writes zero to the drive
26 * and throws errors accordingly.
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070027 * @param[in] driveSize - the size of the block device in bytes
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070028 * @param[in] fd - the stdplus file descriptor
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080029 */
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070030 void writeZero(uint64_t driveSize, Fd& fd);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080031
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070032 /** @brief writes zero to the drive using default parameters,
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070033 * and throws errors accordingly.
34 */
35 void writeZero()
36 {
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070037 stdplus::fd::Fd&& fd =
38 stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
39 writeZero(util::findSizeOfBlockDevice(devPath), fd);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070040 }
41
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070042 /** @brief verifies the drive has only zeros on it,
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080043 * and throws errors accordingly.
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070044 * @param[in] driveSize - the size of the block device in bytes
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070045 * @param[in] fd - the stdplus file descriptor
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080046 */
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070047 void verifyZero(uint64_t driveSize, Fd& fd);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080048
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070049 /** @brief verifies the drive has only zeros on it,
50 * using the default parameters. It also throws errors accordingly.
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070051 */
52 void verifyZero()
53 {
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070054 stdplus::fd::Fd&& fd =
55 stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
56 verifyZero(util::findSizeOfBlockDevice(devPath), fd);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070057 }
58
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080059 private:
60 /* @brief the size of the blocks in bytes used for write and verify.
61 * 32768 was also tested. It had almost identical performance.
62 */
63 static constexpr size_t blockSize = 4096;
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070064 static constexpr size_t maxRetry = 32;
65 static constexpr std::chrono::duration delay = std::chrono::milliseconds(1);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080066};
67
68} // namespace estoraged