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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <stdlib.h>
#include "common/debug.h"
#include "common/errno.h"
#include "Driver.h"
#include "Request.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "rbd::ggate::Driver: " << this \
<< " " << __func__ << ": "
namespace rbd {
namespace ggate {
int Driver::load() {
return ggate_drv_load();
}
int Driver::kill(const std::string &devname) {
int r = ggate_drv_kill(devname.c_str());
return r;
}
int Driver::list(std::map<std::string, DevInfo> *devices) {
size_t size = 1024;
ggate_drv_info *devs = nullptr;
int r;
while (size <= 1024 * 1024) {
devs = static_cast<ggate_drv_info *>(
realloc(static_cast<void *>(devs), size * sizeof(*devs)));
r = ggate_drv_list(devs, &size);
if (r != -ERANGE) {
break;
}
}
if (r < 0) {
goto free;
}
devices->clear();
for (size_t i = 0; i < size; i++) {
auto &dev = devs[i];
(*devices)[dev.id] = {dev.name, dev.info};
}
free:
free(devs);
return r;
}
Driver::Driver(const std::string &devname, size_t sectorsize, size_t mediasize,
bool readonly, const std::string &info)
: m_devname(devname), m_sectorsize(sectorsize), m_mediasize(mediasize),
m_readonly(readonly), m_info(info) {
}
int Driver::init() {
dout(20) << dendl;
char name[PATH_MAX];
size_t namelen;
if (m_devname.empty()) {
name[0] = '\0';
namelen = PATH_MAX;
} else {
namelen = m_devname.size();
if (namelen >= PATH_MAX) {
return -ENAMETOOLONG;
}
strncpy(name, m_devname.c_str(), namelen + 1);
}
int r = ggate_drv_create(name, namelen, m_sectorsize, m_mediasize, m_readonly,
m_info.c_str(), &m_drv);
if (r < 0) {
return r;
}
if (m_devname.empty()) {
m_devname = name;
}
return 0;
}
std::string Driver::get_devname() const {
dout(30) << m_devname << dendl;
return m_devname;
}
void Driver::shut_down() {
dout(20) << dendl;
ggate_drv_destroy(m_drv);
}
int Driver::resize(size_t newsize) {
dout(20) << "newsize=" << newsize << dendl;
int r = ggate_drv_resize(m_drv, newsize);
if (r < 0) {
return r;
}
m_mediasize = newsize;
return 0;
}
int Driver::recv(Request **req) {
dout(20) << dendl;
ggate_drv_req_t req_;
int r = ggate_drv_recv(m_drv, &req_);
if (r < 0) {
return r;
}
*req = new Request(req_);
dout(20) << "req=" << *req << dendl;
if (ggate_drv_req_cmd(req_) == GGATE_DRV_CMD_WRITE) {
bufferptr ptr(buffer::claim_malloc(
ggate_drv_req_length(req_),
static_cast<char *>(ggate_drv_req_release_buf(req_))));
(*req)->bl.push_back(ptr);
}
return 0;
}
int Driver::send(Request *req) {
dout(20) << "req=" << req << dendl;
if (ggate_drv_req_cmd(req->req) == GGATE_DRV_CMD_READ &&
ggate_drv_req_error(req->req) == 0) {
ceph_assert(req->bl.length() == ggate_drv_req_length(req->req));
// TODO: avoid copying?
req->bl.begin().copy(ggate_drv_req_length(req->req),
static_cast<char *>(ggate_drv_req_buf(req->req)));
dout(20) << "copied resulting " << req->bl.length() << " bytes to "
<< ggate_drv_req_buf(req->req) << dendl;
}
int r = ggate_drv_send(m_drv, req->req);
delete req;
return r;
}
} // namespace ggate
} // namespace rbd
|