forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_example.c
404 lines (339 loc) · 8.98 KB
/
file_example.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
/*
* Copyright 2014, Red Hat, 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.
*/
/*
* Example code to demonstrate how a TCMU handler might work.
*
* Using the example of backing a device by a file to demonstrate:
*
* 1) Registering with tcmu-runner
* 2) Parsing the handler-specific config string as needed for setup
* 3) Opening resources as needed
* 4) Handling SCSI commands and using the handler API
*/
#define _GNU_SOURCE
#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"
#ifdef ASYNC_FILE_HANDLER
#include <pthread.h>
#include <signal.h>
#include "libtcmu.h"
#define NHANDLERS 2
#define NCOMMANDS 16
struct file_handler {
struct tcmu_device *dev;
int num;
pthread_mutex_t mtx;
pthread_cond_t cond;
pthread_t thr;
int cmd_head;
int cmd_tail;
struct tcmulib_cmd *commands[NCOMMANDS];
};
#endif /* ASYNC_FILE_HANDLER */
struct file_state {
int fd;
uint64_t num_lbas;
uint32_t block_size;
#ifdef ASYNC_FILE_HANDLER
pthread_mutex_t completion_mtx;
int curr_handler;
struct file_handler h[NHANDLERS];
#endif /* ASYNC_FILE_HANDLER */
};
#ifdef ASYNC_FILE_HANDLER
static int file_handle_cmd(
struct tcmu_device *dev,
struct tcmulib_cmd *tcmulib_cmd);
static void *
file_handler_run(void *arg)
{
struct file_handler *h = (struct file_handler *) arg;
struct file_state *state = tcmu_get_dev_private(h->dev);
for (;;) {
int result;
struct tcmulib_cmd *cmd;
/* get next command */
pthread_mutex_lock(&h->mtx);
while (h->cmd_tail == h->cmd_head) {
pthread_cond_wait(&h->cond, &h->mtx);
}
cmd = h->commands[h->cmd_tail];
pthread_mutex_unlock(&h->mtx);
/* process command */
result = file_handle_cmd(h->dev, cmd);
pthread_mutex_lock(&state->completion_mtx);
tcmulib_command_complete(h->dev, cmd, result);
tcmulib_processing_complete(h->dev);
pthread_mutex_unlock(&state->completion_mtx);
/* notify that we can process more commands */
pthread_mutex_lock(&h->mtx);
h->commands[h->cmd_tail] = NULL;
h->cmd_tail = (h->cmd_tail + 1) % NCOMMANDS;
pthread_cond_signal(&h->cond);
pthread_mutex_unlock(&h->mtx);
}
return NULL;
}
static void
file_handler_init(struct file_handler *h, struct tcmu_device *dev, int num)
{
int i;
h->dev = dev;
h->num = num;
pthread_mutex_init(&h->mtx, NULL);
pthread_cond_init(&h->cond, NULL);
pthread_create(&h->thr, NULL, file_handler_run, h);
h->cmd_head = h->cmd_tail = 0;
for (i = 0; i < NCOMMANDS; i++)
h->commands[i] = NULL;
}
static void
file_handler_destroy(struct file_handler *h)
{
if (h->thr) {
pthread_kill(h->thr, SIGINT);
pthread_join(h->thr, NULL);
}
pthread_cond_destroy(&h->cond);
pthread_mutex_destroy(&h->mtx);
}
#endif /* ASYNC_FILE_HANDLER */
static bool file_check_config(const char *cfgstring, char **reason)
{
char *path;
int fd;
path = strchr(cfgstring, '/');
if (!path) {
asprintf(reason, "No path found");
return false;
}
path += 1; /* get past '/' */
if (access(path, W_OK) != -1)
return true; /* File exists and is writable */
/* We also support creating the file, so see if we can create it */
fd = creat(path, S_IRUSR | S_IWUSR);
if (fd == -1) {
asprintf(reason, "Could not create file");
return false;
}
unlink(path);
return true;
}
static int file_open(struct tcmu_device *dev)
{
struct file_state *state;
int64_t size;
char *config;
#ifdef ASYNC_FILE_HANDLER
int i;
#endif /* ASYNC_FILE_HANDLER */
state = calloc(1, sizeof(*state));
if (!state)
return -ENOMEM;
tcmu_set_dev_private(dev, state);
state->block_size = tcmu_get_attribute(dev, "hw_block_size");
if (state->block_size == -1) {
errp("Could not get device block size\n");
goto err;
}
size = tcmu_get_device_size(dev);
if (size == -1) {
errp("Could not get device size\n");
goto err;
}
state->num_lbas = size / state->block_size;
config = strchr(tcmu_get_dev_cfgstring(dev), '/');
if (!config) {
errp("no configuration found in cfgstring\n");
goto err;
}
config += 1; /* get past '/' */
state->fd = open(config, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (state->fd == -1) {
errp("could not open %s: %m\n", config);
goto err;
}
#ifdef ASYNC_FILE_HANDLER
pthread_mutex_init(&state->completion_mtx, NULL);
for (i = 0; i < NHANDLERS; i++)
file_handler_init(&state->h[i], dev, i);
#endif /* ASYNC_FILE_HANDLER */
return 0;
err:
free(state);
return -EINVAL;
}
static void file_close(struct tcmu_device *dev)
{
struct file_state *state = tcmu_get_dev_private(dev);
#ifdef ASYNC_FILE_HANDLER
int i;
for (i = 0; i < NHANDLERS; i++)
file_handler_destroy(&state->h[i]);
pthread_mutex_destroy(&state->completion_mtx);
#endif /* ASYNC_FILE_HANDLER */
close(state->fd);
free(state);
}
static int set_medium_error(uint8_t *sense)
{
return tcmu_set_sense_data(sense, MEDIUM_ERROR, ASC_READ_ERROR, NULL);
}
#ifdef ASYNC_FILE_HANDLER
static int file_handle_cmd_async(
struct tcmu_device *dev,
struct tcmulib_cmd *tcmulib_cmd)
{
struct file_state *state = tcmu_get_dev_private(dev);
struct file_handler *h = &state->h[state->curr_handler];
state->curr_handler = (state->curr_handler + 1) % NHANDLERS;
/* enqueue command */
pthread_mutex_lock(&h->mtx);
while ((h->cmd_head + 1) % NCOMMANDS == h->cmd_tail) {
pthread_cond_wait(&h->cond, &h->mtx);
}
h->commands[h->cmd_head] = tcmulib_cmd;
h->cmd_head = (h->cmd_head + 1) % NCOMMANDS;
pthread_cond_signal(&h->cond);
pthread_mutex_unlock(&h->mtx);
return TCMU_ASYNC_HANDLED;
}
#endif /* ASYNC_FILE_HANDLER */
/*
* Return scsi status or TCMU_NOT_HANDLED
*/
static int file_handle_cmd(
struct tcmu_device *dev,
struct tcmulib_cmd *tcmulib_cmd)
{
uint8_t *cdb = tcmulib_cmd->cdb;
struct iovec *iovec = tcmulib_cmd->iovec;
size_t iov_cnt = tcmulib_cmd->iov_cnt;
uint8_t *sense = tcmulib_cmd->sense_buf;
struct file_state *state = tcmu_get_dev_private(dev);
uint8_t cmd;
int remaining;
size_t ret;
cmd = cdb[0];
switch (cmd) {
case INQUIRY:
return tcmu_emulate_inquiry(dev, cdb, iovec, iov_cnt, sense);
break;
case TEST_UNIT_READY:
return tcmu_emulate_test_unit_ready(cdb, iovec, iov_cnt, sense);
break;
case SERVICE_ACTION_IN_16:
if (cdb[1] == READ_CAPACITY_16)
return tcmu_emulate_read_capacity_16(state->num_lbas,
state->block_size,
cdb, iovec, iov_cnt, sense);
else
return TCMU_NOT_HANDLED;
break;
case MODE_SENSE:
case MODE_SENSE_10:
return tcmu_emulate_mode_sense(cdb, iovec, iov_cnt, sense);
break;
case MODE_SELECT:
case MODE_SELECT_10:
return tcmu_emulate_mode_select(cdb, iovec, iov_cnt, sense);
break;
case READ_6:
case READ_10:
case READ_12:
case READ_16:
{
void *buf;
uint64_t offset = state->block_size * tcmu_get_lba(cdb);
int length = tcmu_get_xfer_length(cdb) * state->block_size;
/* Using this buf DTRT even if seek is beyond EOF */
buf = malloc(length);
if (!buf)
return set_medium_error(sense);
memset(buf, 0, length);
ret = pread(state->fd, buf, length, offset);
if (ret == -1) {
errp("read failed: %m\n");
free(buf);
return set_medium_error(sense);
}
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, length);
free(buf);
return SAM_STAT_GOOD;
}
break;
case WRITE_6:
case WRITE_10:
case WRITE_12:
case WRITE_16:
{
uint64_t offset = state->block_size * tcmu_get_lba(cdb);
int length = be16toh(*((uint16_t *)&cdb[7])) * state->block_size;
remaining = length;
while (remaining) {
unsigned int to_copy;
to_copy = (remaining > iovec->iov_len) ? iovec->iov_len : remaining;
ret = pwrite(state->fd, iovec->iov_base, to_copy, offset);
if (ret == -1) {
errp("Could not write: %m\n");
return set_medium_error(sense);
}
remaining -= to_copy;
offset += to_copy;
iovec++;
}
return SAM_STAT_GOOD;
}
break;
default:
errp("unknown command %x\n", cdb[0]);
return TCMU_NOT_HANDLED;
}
}
static const char file_cfg_desc[] =
"The path to the file to use as a backstore.";
static struct tcmur_handler file_handler = {
.cfg_desc = file_cfg_desc,
.check_config = file_check_config,
.open = file_open,
.close = file_close,
#ifdef ASYNC_FILE_HANDLER
.name = "File-backed Handler (example async code)",
.subtype = "file_async",
.handle_cmd = file_handle_cmd_async,
#else
.name = "File-backed Handler (example code)",
.subtype = "file",
.handle_cmd = file_handle_cmd,
#endif
};
/* Entry point must be named "handler_init". */
void handler_init(void)
{
tcmur_register_handler(&file_handler);
}