-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcls_rbd.cc
396 lines (315 loc) · 10.5 KB
/
cls_rbd.cc
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "include/types.h"
#include "objclass/objclass.h"
#include "include/rbd_types.h"
CLS_VER(1,3)
CLS_NAME(rbd)
cls_handle_t h_class;
cls_method_handle_t h_snapshots_list;
cls_method_handle_t h_snapshot_add;
cls_method_handle_t h_snapshot_remove;
cls_method_handle_t h_snapshot_revert;
cls_method_handle_t h_assign_bid;
cls_method_handle_t h_test_exec;
static int snap_read_header(cls_method_context_t hctx, bufferlist& bl)
{
unsigned snap_count = 0;
uint64_t snap_names_len = 0;
int rc;
struct rbd_obj_header_ondisk *header;
cls_log("snapshots_list");
while (1) {
int len = sizeof(*header) +
snap_count * sizeof(struct rbd_obj_snap_ondisk) +
snap_names_len;
rc = cls_cxx_read(hctx, 0, len, &bl);
if (rc < 0)
return rc;
header = (struct rbd_obj_header_ondisk *)bl.c_str();
if ((snap_count != header->snap_count) ||
(snap_names_len != header->snap_names_len)) {
snap_count = header->snap_count;
snap_names_len = header->snap_names_len;
bl.clear();
continue;
}
break;
}
return 0;
}
int snapshots_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
bufferlist bl;
struct rbd_obj_header_ondisk *header;
int rc = snap_read_header(hctx, bl);
if (rc < 0)
return rc;
header = (struct rbd_obj_header_ondisk *)bl.c_str();
bufferptr p(header->snap_names_len);
char *buf = (char *)header;
char *name = buf + sizeof(*header) + header->snap_count * sizeof(struct rbd_obj_snap_ondisk);
char *end = name + header->snap_names_len;
memcpy(p.c_str(),
buf + sizeof(*header) + header->snap_count * sizeof(struct rbd_obj_snap_ondisk),
header->snap_names_len);
::encode(header->snap_seq, *out);
::encode(header->snap_count, *out);
for (unsigned i = 0; i < header->snap_count; i++) {
string s = name;
::encode(header->snaps[i].id, *out);
::encode(header->snaps[i].image_size, *out);
::encode(s, *out);
name += strlen(name) + 1;
if (name > end)
return -EIO;
}
return 0;
}
int snapshot_add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
bufferlist bl;
struct rbd_obj_header_ondisk *header;
bufferlist newbl;
bufferptr header_bp(sizeof(*header));
struct rbd_obj_snap_ondisk *new_snaps;
int rc = snap_read_header(hctx, bl);
if (rc < 0)
return rc;
header = (struct rbd_obj_header_ondisk *)bl.c_str();
int snaps_id_ofs = sizeof(*header);
int len = snaps_id_ofs;
int names_ofs = snaps_id_ofs + sizeof(*new_snaps) * header->snap_count;
const char *snap_name;
const char *snap_names = ((char *)header) + names_ofs;
const char *end = snap_names + header->snap_names_len;
bufferlist::iterator iter = in->begin();
string s;
uint64_t snap_id;
try {
::decode(s, iter);
::decode(snap_id, iter);
} catch (const buffer::error &err) {
return -EINVAL;
}
snap_name = s.c_str();
const char *cur_snap_name;
for (cur_snap_name = snap_names; cur_snap_name < end; cur_snap_name += strlen(cur_snap_name) + 1) {
if (strncmp(cur_snap_name, snap_name, end - cur_snap_name) == 0)
return -EEXIST;
}
if (cur_snap_name > end)
return -EIO;
int snap_name_len = strlen(snap_name);
bufferptr new_names_bp(header->snap_names_len + snap_name_len + 1);
bufferptr new_snaps_bp(sizeof(*new_snaps) * (header->snap_count + 1));
/* copy snap names and append to new snap name */
char *new_snap_names = new_names_bp.c_str();
strcpy(new_snap_names, snap_name);
memcpy(new_snap_names + snap_name_len + 1, snap_names, header->snap_names_len);
/* append new snap id */
new_snaps = (struct rbd_obj_snap_ondisk *)new_snaps_bp.c_str();
memcpy(new_snaps + 1, header->snaps, sizeof(*new_snaps) * header->snap_count);
header->snap_count = header->snap_count + 1;
header->snap_names_len = header->snap_names_len + snap_name_len + 1;
header->snap_seq = snap_id;
new_snaps[0].id = snap_id;
new_snaps[0].image_size = header->image_size;
len += sizeof(*new_snaps) * header->snap_count + header->snap_names_len;
memcpy(header_bp.c_str(), header, sizeof(*header));
newbl.push_back(header_bp);
newbl.push_back(new_snaps_bp);
newbl.push_back(new_names_bp);
rc = cls_cxx_write_full(hctx, &newbl);
if (rc < 0)
return rc;
return 0;
}
int snapshot_revert(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
bufferlist bl;
struct rbd_obj_header_ondisk *header;
bufferlist newbl;
bufferptr header_bp(sizeof(*header));
struct rbd_obj_snap_ondisk *new_snaps;
int rc = snap_read_header(hctx, bl);
if (rc < 0)
return rc;
header = (struct rbd_obj_header_ondisk *)bl.c_str();
int snaps_id_ofs = sizeof(*header);
int names_ofs = snaps_id_ofs + sizeof(*new_snaps) * header->snap_count;
const char *snap_name;
const char *snap_names = ((char *)header) + names_ofs;
const char *end = snap_names + header->snap_names_len;
bufferlist::iterator iter = in->begin();
string s;
int i;
bool found = false;
struct rbd_obj_snap_ondisk snap;
try {
::decode(s, iter);
} catch (const buffer::error &err) {
return -EINVAL;
}
snap_name = s.c_str();
for (i = 0; snap_names < end; i++) {
if (strcmp(snap_names, snap_name) == 0) {
snap = header->snaps[i];
found = true;
break;
}
snap_names += strlen(snap_names) + 1;
}
if (!found) {
CLS_LOG("couldn't find snap %s\n",snap_name);
return -ENOENT;
}
header->image_size = snap.image_size;
header->snap_seq = header->snap_seq + 1;
snap_names += strlen(snap_names) + 1;
i++;
header->snap_count = header->snap_count - i;
bufferptr new_names_bp(end - snap_names);
bufferptr new_snaps_bp(sizeof(header->snaps[0]) * header->snap_count);
memcpy(header_bp.c_str(), header, sizeof(*header));
newbl.push_back(header_bp);
if (header->snap_count) {
memcpy(new_snaps_bp.c_str(), header->snaps + i, sizeof(header->snaps[0]) * header->snap_count);
memcpy(new_names_bp.c_str(), snap_names, end - snap_names);
newbl.push_back(new_snaps_bp);
newbl.push_back(new_names_bp);
}
rc = cls_cxx_write_full(hctx, &newbl);
if (rc < 0)
return rc;
::encode(snap.id, *out);
return out->length();
}
int snapshot_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
bufferlist bl;
struct rbd_obj_header_ondisk *header;
bufferlist newbl;
bufferptr header_bp(sizeof(*header));
struct rbd_obj_snap_ondisk *new_snaps;
int rc = snap_read_header(hctx, bl);
if (rc < 0)
return rc;
header = (struct rbd_obj_header_ondisk *)bl.c_str();
int snaps_id_ofs = sizeof(*header);
int names_ofs = snaps_id_ofs + sizeof(*new_snaps) * header->snap_count;
const char *snap_name;
const char *snap_names = ((char *)header) + names_ofs;
const char *orig_names = snap_names;
const char *end = snap_names + header->snap_names_len;
bufferlist::iterator iter = in->begin();
string s;
unsigned i;
bool found = false;
struct rbd_obj_snap_ondisk snap;
try {
::decode(s, iter);
} catch (const buffer::error &err) {
return -EINVAL;
}
snap_name = s.c_str();
for (i = 0; snap_names < end; i++) {
if (strcmp(snap_names, snap_name) == 0) {
snap = header->snaps[i];
found = true;
break;
}
snap_names += strlen(snap_names) + 1;
}
if (!found) {
CLS_LOG("couldn't find snap %s\n",snap_name);
return -ENOENT;
}
header->snap_names_len = header->snap_names_len - (s.length() + 1);
header->snap_count = header->snap_count - 1;
bufferptr new_names_bp(header->snap_names_len);
bufferptr new_snaps_bp(sizeof(header->snaps[0]) * header->snap_count);
memcpy(header_bp.c_str(), header, sizeof(*header));
newbl.push_back(header_bp);
if (header->snap_count) {
int snaps_len = 0;
int names_len = 0;
CLS_LOG("i=%d\n", i);
if (i > 0) {
snaps_len = sizeof(header->snaps[0]) * i;
names_len = snap_names - orig_names;
memcpy(new_snaps_bp.c_str(), header->snaps, snaps_len);
memcpy(new_names_bp.c_str(), orig_names, names_len);
}
snap_names += s.length() + 1;
if (i < header->snap_count) {
memcpy(new_snaps_bp.c_str() + snaps_len,
header->snaps + i + 1,
sizeof(header->snaps[0]) * (header->snap_count - i));
memcpy(new_names_bp.c_str() + names_len, snap_names , end - snap_names);
}
newbl.push_back(new_snaps_bp);
newbl.push_back(new_names_bp);
}
rc = cls_cxx_write_full(hctx, &newbl);
if (rc < 0)
return rc;
return 0;
}
/* assign block id. This method should be called on the rbd_info object */
int rbd_assign_bid(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
struct rbd_info info;
int rc;
bufferlist bl;
rc = cls_cxx_read(hctx, 0, sizeof(info), &bl);
if (rc < 0 && rc != -EEXIST)
return rc;
if (rc && rc < (int)sizeof(info)) {
CLS_LOG("bad rbd_info object, read %d bytes, expected %d", rc, sizeof(info));
return -EIO;
}
uint64_t max_id;
if (rc) {
memcpy(&info, bl.c_str(), sizeof(info));
max_id = info.max_id + 1;
info.max_id = max_id;
} else {
memset(&info, 0, sizeof(info));
max_id = 0;
}
bufferlist newbl;
bufferptr bp(sizeof(info));
memcpy(bp.c_str(), &info, sizeof(info));
newbl.push_back(bp);
rc = cls_cxx_write_full(hctx, &newbl);
if (rc < 0) {
CLS_LOG("error writing rbd_info, got rc=%d", rc);
return rc;
}
::encode(max_id, *out);
return out->length();
}
/* Used for testing rados_exec */
static int test_exec(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
bufferlist bl;
std::string testing123("testing123");
::encode(testing123, *out);
return out->length();
}
void __cls_init()
{
CLS_LOG("Loaded rbd class!");
cls_register("rbd", &h_class);
cls_register_cxx_method(h_class, "snap_list", CLS_METHOD_RD | CLS_METHOD_PUBLIC, snapshots_list, &h_snapshots_list);
cls_register_cxx_method(h_class, "snap_add", CLS_METHOD_RD | CLS_METHOD_WR | CLS_METHOD_PUBLIC, snapshot_add, &h_snapshot_add);
cls_register_cxx_method(h_class, "snap_remove", CLS_METHOD_RD | CLS_METHOD_WR | CLS_METHOD_PUBLIC, snapshot_remove, &h_snapshot_remove);
cls_register_cxx_method(h_class, "snap_revert", CLS_METHOD_RD | CLS_METHOD_WR | CLS_METHOD_PUBLIC, snapshot_revert, &h_snapshot_revert);
/* assign a unique block id for rbd blocks */
cls_register_cxx_method(h_class, "assign_bid", CLS_METHOD_RD | CLS_METHOD_WR | CLS_METHOD_PUBLIC, rbd_assign_bid, &h_assign_bid);
cls_register_cxx_method(h_class, "test_exec", CLS_METHOD_RD | CLS_METHOD_PUBLIC, test_exec, &h_test_exec);
return;
}