forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbd.c
405 lines (340 loc) · 8.97 KB
/
rbd.c
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
396
397
398
399
400
401
402
403
404
405
/*
* Copyright 2016, China Mobile, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#define _GNU_SOURCE
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <endian.h>
#include <scsi/scsi.h>
#include <errno.h>
#include "tcmu-runner.h"
#include "libtcmu.h"
#include <rbd/librbd.h>
struct tcmu_rbd_state {
rados_t cluster;
rados_ioctx_t io_ctx;
rbd_image_t image;
};
struct rbd_aio_cb {
struct tcmu_device *dev;
struct tcmulib_cmd *tcmulib_cmd;
int64_t length;
char *bounce_buffer;
};
static int tcmu_rbd_open(struct tcmu_device *dev)
{
char *pool, *name;
char *config;
struct tcmu_rbd_state *state;
uint64_t size, rbd_size;
int ret, block_size;
state = calloc(1, sizeof(*state));
if (!state)
return -ENOMEM;
tcmu_set_dev_private(dev, state);
config = strchr(tcmu_get_dev_cfgstring(dev), '/');
if (!config) {
tcmu_err("no configuration found in cfgstring\n");
ret = -EINVAL;
goto free_state;
}
config += 1; /* get past '/' */
block_size = tcmu_get_attribute(dev, "hw_block_size");
if (block_size < 0) {
tcmu_err("Could not get hw_block_size\n");
ret = -EINVAL;
goto free_state;
}
tcmu_set_dev_block_size(dev, block_size);
size = tcmu_get_device_size(dev);
if (size < 0) {
tcmu_err("Could not get device size\n");
goto free_state;
}
tcmu_set_dev_num_lbas(dev, size / block_size);
pool = strtok(config, "/");
if (!pool) {
ret = -EINVAL;
goto free_state;
}
name = strtok(NULL, "/");
if (!name) {
ret = -EINVAL;
goto free_state;
}
ret = rados_create(&state->cluster, NULL);
if (ret < 0) {
tcmu_err("error initializing\n");
goto free_state;
}
/* Fow now, we will only read /etc/ceph/ceph.conf */
rados_conf_read_file(state->cluster, NULL);
rados_conf_set(state->cluster, "rbd_cache", "false");
ret = rados_connect(state->cluster);
if (ret < 0) {
tcmu_err("error connecting\n");
goto rados_shutdown;
}
ret = rados_ioctx_create(state->cluster, pool, &state->io_ctx);
if (ret < 0) {
tcmu_err("error opening pool %s\n", pool);
goto rados_destroy;
}
ret = rbd_open(state->io_ctx, name, &state->image, NULL);
if (ret < 0) {
tcmu_err("error reading header from %s\n", name);
goto rados_destroy;
}
ret = rbd_get_size(state->image, &rbd_size);
if(ret < 0) {
tcmu_err("error get rbd_size %s\n", name);
goto rados_destroy;
}
if(size != rbd_size) {
tcmu_err("device size and backing size disagree: "
"device %lld backing %lld\n",
size,
rbd_size);
ret = -EIO;
goto rbd_close;
}
tcmu_dbg("config %s, size %lld\n", tcmu_get_dev_cfgstring(dev),
rbd_size);
return 0;
rbd_close:
rbd_close(state->image);
rados_destroy:
rados_ioctx_destroy(state->io_ctx);
rados_shutdown:
rados_shutdown(state->cluster);
free_state:
free(state);
return ret;
}
static void tcmu_rbd_close(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
rbd_close(state->image);
rados_ioctx_destroy(state->io_ctx);
rados_shutdown(state->cluster);
free(state);
}
/*
* NOTE: RBD async APIs almost always return 0 (success), except
* when allocation (via new) fails - which is not caught. So,
* the only errno we've to bother about as of now are memory
* allocation errors.
*/
static void rbd_finish_aio_read(rbd_completion_t completion,
struct rbd_aio_cb *aio_cb)
{
struct tcmu_device *dev = aio_cb->dev;
struct tcmulib_cmd *tcmulib_cmd = aio_cb->tcmulib_cmd;
struct iovec *iovec = tcmulib_cmd->iovec;
size_t iov_cnt = tcmulib_cmd->iov_cnt;
int64_t ret;
int tcmu_r;
ret = rbd_aio_get_return_value(completion);
rbd_aio_release(completion);
if (ret < 0) {
tcmu_r = tcmu_set_sense_data(tcmulib_cmd->sense_buf,
MEDIUM_ERROR, ASC_READ_ERROR, NULL);
} else {
tcmu_r = SAM_STAT_GOOD;
tcmu_memcpy_into_iovec(iovec, iov_cnt,
aio_cb->bounce_buffer, aio_cb->length);
}
tcmulib_cmd->done(dev, tcmulib_cmd, tcmu_r);
free(aio_cb->bounce_buffer);
free(aio_cb);
}
static int tcmu_rbd_read(struct tcmu_device *dev, struct tcmulib_cmd *cmd,
struct iovec *iov, size_t iov_cnt, size_t length,
off_t offset)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
struct rbd_aio_cb *aio_cb;
rbd_completion_t completion;
ssize_t ret;
aio_cb = calloc(1, sizeof(*aio_cb));
if (!aio_cb) {
tcmu_err("could not allocated aio_cb\n");
goto out;
}
aio_cb->dev = dev;
aio_cb->length = length;
aio_cb->tcmulib_cmd = cmd;
aio_cb->bounce_buffer = malloc(length);
if (!aio_cb->bounce_buffer) {
tcmu_err("could not allocate bounce buffer\n");
goto out_free_aio_cb;
}
ret = rbd_aio_create_completion
(aio_cb, (rbd_callback_t) rbd_finish_aio_read, &completion);
if (ret < 0) {
goto out_free_bounce_buffer;
}
ret = rbd_aio_read(state->image, offset, length, aio_cb->bounce_buffer,
completion);
if (ret < 0) {
goto out_remove_tracked_aio;
}
return 0;
out_remove_tracked_aio:
rbd_aio_release(completion);
out_free_bounce_buffer:
free(aio_cb->bounce_buffer);
out_free_aio_cb:
free(aio_cb);
out:
return SAM_STAT_TASK_SET_FULL;
}
static void rbd_finish_aio_generic(rbd_completion_t completion,
struct rbd_aio_cb *aio_cb)
{
struct tcmu_device *dev = aio_cb->dev;
struct tcmulib_cmd *tcmulib_cmd = aio_cb->tcmulib_cmd;
int64_t ret;
int tcmu_r;
ret = rbd_aio_get_return_value(completion);
rbd_aio_release(completion);
if (ret < 0) {
tcmu_r = tcmu_set_sense_data(tcmulib_cmd->sense_buf,
MEDIUM_ERROR, ASC_WRITE_ERROR,
NULL);
} else {
tcmu_r = SAM_STAT_GOOD;
}
tcmulib_cmd->done(dev, tcmulib_cmd, tcmu_r);
if (aio_cb->bounce_buffer) {
free(aio_cb->bounce_buffer);
}
free(aio_cb);
}
static int tcmu_rbd_write(struct tcmu_device *dev, struct tcmulib_cmd *cmd,
struct iovec *iov, size_t iov_cnt, size_t length,
off_t offset)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
struct rbd_aio_cb *aio_cb;
rbd_completion_t completion;
ssize_t ret;
aio_cb = calloc(1, sizeof(*aio_cb));
if (!aio_cb) {
tcmu_err("could not allocated aio_cb\n");
goto out;
}
aio_cb->dev = dev;
aio_cb->length = length;
aio_cb->tcmulib_cmd = cmd;
aio_cb->bounce_buffer = malloc(length);
if (!aio_cb->bounce_buffer) {
tcmu_err("failed to allocate bounce buffer\n");
goto out_free_aio_cb;
}
tcmu_memcpy_from_iovec(aio_cb->bounce_buffer, length, iov, iov_cnt);
ret = rbd_aio_create_completion
(aio_cb, (rbd_callback_t) rbd_finish_aio_generic, &completion);
if (ret < 0) {
goto out_free_bounce_buffer;
}
ret = rbd_aio_write(state->image, offset,
length, aio_cb->bounce_buffer, completion);
if (ret < 0) {
goto out_remove_tracked_aio;
}
return 0;
out_remove_tracked_aio:
rbd_aio_release(completion);
out_free_bounce_buffer:
free(aio_cb->bounce_buffer);
out_free_aio_cb:
free(aio_cb);
out:
return SAM_STAT_TASK_SET_FULL;
}
static int tcmu_rbd_flush(struct tcmu_device *dev, struct tcmulib_cmd *cmd)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
struct rbd_aio_cb *aio_cb;
rbd_completion_t completion;
ssize_t ret = -ENOMEM;
aio_cb = calloc(1, sizeof(*aio_cb));
if (!aio_cb) {
tcmu_err("could not allocated aio_cb\n");
goto out;
}
aio_cb->dev = dev;
aio_cb->tcmulib_cmd = cmd;
aio_cb->bounce_buffer = NULL;
ret = rbd_aio_create_completion
(aio_cb, (rbd_callback_t) rbd_finish_aio_generic, &completion);
if (ret < 0) {
goto out_free_aio_cb;
}
ret = rbd_aio_flush(state->image, completion);
if (ret < 0) {
goto out_remove_tracked_aio;
}
return 0;
out_remove_tracked_aio:
rbd_aio_release(completion);
out_free_aio_cb:
free(aio_cb);
out:
return SAM_STAT_TASK_SET_FULL;
}
/*
* For backstore creation
*
* Specify poolname/devicename, e.g,
*
* $ targetcli create /backstores/user:rbd/test 2G rbd/test
*
* poolname must be the name of an existing rados pool.
*
* devicename is the name of the rbd image.
*/
static const char tcmu_rbd_cfg_desc[] =
"RBD config string is of the form:\n"
"poolname/devicename\n"
"where:\n"
"poolname: Existing RADOS pool\n"
"devicename: Name of the RBD image\n";
struct tcmur_handler tcmu_rbd_handler = {
.name = "Ceph RBD handler",
.subtype = "rbd",
.cfg_desc = tcmu_rbd_cfg_desc,
.open = tcmu_rbd_open,
.close = tcmu_rbd_close,
.read = tcmu_rbd_read,
.write = tcmu_rbd_write,
#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
.flush = tcmu_rbd_flush,
#endif
};
int handler_init(void)
{
return tcmur_register_handler(&tcmu_rbd_handler);
}