-
Notifications
You must be signed in to change notification settings - Fork 3
/
cachex_linux.h
104 lines (90 loc) · 2.75 KB
/
cachex_linux.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef CACHEX_LINUX_H
#define CACHEX_LINUX_H
#define _POSIX_C_SOURCE 200112L
#include "result.h"
#include <array>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstring>
#include <time.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>
namespace linux_detail
{
void sg_io_exec(int fd, sg_io_hdr_t &io, CommandResult &rv)
{
auto io_rv = ::ioctl(fd, SG_IO, &io);
rv.Valid = (io_rv == 0);
rv.ScsiStatus = io.status;
rv.Duration = io.duration;
}
template <std::size_t CDBLength>
sg_io_hdr_t sg_io_common(const std::array<std::uint8_t, CDBLength> &cdb)
{
sg_io_hdr_t io;
::memset(&io, 0, sizeof(io));
io.interface_id = 'S';
io.timeout = 60000;
io.cmdp = const_cast<unsigned char *>(cdb.data());
io.cmd_len = CDBLength;
return io;
}
template <std::size_t CDBLength>
sg_io_hdr_t sg_io_for_read(CommandResult &rv,
const std::array<std::uint8_t, CDBLength> &cdb)
{
auto io = sg_io_common(cdb);
io.dxfer_direction = SG_DXFER_FROM_DEV;
io.dxfer_len = static_cast<unsigned int>(rv.Data.size());
io.dxferp = rv.Data.data();
return io;
}
template <std::size_t CDBLength>
sg_io_hdr_t sg_io_for_write(const std::vector<std::uint8_t> &data,
const std::array<std::uint8_t, CDBLength> &cdb)
{
auto io = sg_io_common(cdb);
io.dxfer_direction = SG_DXFER_TO_DEV;
io.dxfer_len = static_cast<unsigned int>(data.size());
io.dxferp = const_cast<std::uint8_t *>(data.data());
return io;
}
} // namespace linux_detail
struct platform_linux
{
using device_handle = int;
static device_handle open_volume(const char *path)
{
return ::open(path, O_RDWR | O_NONBLOCK | O_EXCL);
}
static bool handle_is_valid(device_handle fd) { return fd != -1; }
static void close_handle(device_handle fd) { ::close(fd); }
static std::uint32_t monotonic_clock()
{
struct timespec ts;
::clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<std::uint32_t>((ts.tv_sec * 1000) +
(ts.tv_nsec / 1000000));
}
static void set_critical_priority() {}
static void set_normal_priority() {}
template <std::size_t CDBLength>
static void exec_command(device_handle fd, CommandResult &rv,
const std::array<std::uint8_t, CDBLength> &cdb)
{
auto io = linux_detail::sg_io_for_read(rv, cdb);
linux_detail::sg_io_exec(fd, io, rv);
}
template <std::size_t CDBLength>
static void send_data(device_handle fd, CommandResult &rv,
const std::array<std::uint8_t, CDBLength> &cdb,
const std::vector<std::uint8_t> &data)
{
auto io = linux_detail::sg_io_for_write(data, cdb);
linux_detail::sg_io_exec(fd, io, rv);
}
};
using platform = platform_linux;
#endif