forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.c
382 lines (320 loc) · 7.27 KB
/
api.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
/*
* Copyright (c) 2014 Red Hat, Inc.
*
* This file is licensed to you under your choice of the GNU Lesser
* General Public License, version 2.1 or any later version (LGPLv2.1 or
* later), or the Apache License 2.0.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <sys/uio.h>
#include <string.h>
#include <scsi/scsi.h>
#include <endian.h>
#include <errno.h>
#include <assert.h>
#include "libtcmu_log.h"
#include "libtcmu_common.h"
#include "libtcmu_priv.h"
#include "be_byteshift.h"
int tcmu_cdb_get_length(uint8_t *cdb)
{
uint8_t group_code = cdb[0] >> 5;
/* See spc-4 4.2.5.1 operation code */
switch (group_code) {
case 0: /*000b for 6 bytes commands */
return 6;
case 1: /*001b for 10 bytes commands */
case 2: /*010b for 10 bytes commands */
return 10;
case 3: /*011b Reserved ? */
if (cdb[0] == 0x7f)
return 8 + cdb[7];
goto cdb_not_supp;
case 4: /*100b for 16 bytes commands */
return 16;
case 5: /*101b for 12 bytes commands */
return 12;
case 6: /*110b Vendor Specific */
case 7: /*111b Vendor Specific */
default:
/* TODO: */
goto cdb_not_supp;
}
cdb_not_supp:
tcmu_err("CDB %x0x not supported.\n", cdb[0]);
return -EINVAL;
}
uint64_t tcmu_cdb_get_lba(uint8_t *cdb)
{
uint16_t val;
switch (tcmu_cdb_get_length(cdb)) {
case 6:
val = be16toh(*((uint16_t *)&cdb[2]));
return ((cdb[1] & 0x1f) << 16) | val;
case 10:
return be32toh(*((u_int32_t *)&cdb[2]));
case 12:
return be32toh(*((u_int32_t *)&cdb[2]));
case 16:
return be64toh(*((u_int64_t *)&cdb[2]));
default:
assert_perror(EINVAL);
return 0; /* not reached */
}
}
uint32_t tcmu_cdb_get_xfer_length(uint8_t *cdb)
{
switch (tcmu_cdb_get_length(cdb)) {
case 6:
return cdb[4];
case 10:
return be16toh(*((uint16_t *)&cdb[7]));
case 12:
return be32toh(*((u_int32_t *)&cdb[6]));
case 16:
return be32toh(*((u_int32_t *)&cdb[10]));
default:
assert_perror(EINVAL);
return 0; /* not reached */
}
}
/*
* Returns location of first mismatch between bytes in mem and the iovec.
* If they are the same, return -1.
*/
off_t tcmu_iovec_compare(void *mem, struct iovec *iovec, size_t size)
{
off_t mem_off;
int ret;
mem_off = 0;
while (size) {
size_t part = min(size, iovec->iov_len);
ret = memcmp(mem + mem_off, iovec->iov_base, part);
if (ret) {
size_t pos;
char *spos = mem + mem_off;
char *dpos = iovec->iov_base;
/*
* Data differed, this is assumed to be 'rare'
* so use a much more expensive byte-by-byte
* comparison to find out at which offset the
* data differs.
*/
for (pos = 0; pos < part && *spos++ == *dpos++;
pos++)
;
return pos + mem_off;
}
size -= part;
mem_off += part;
iovec++;
}
return -1;
}
/*
* Consume an iovec. Count must not exceed the total iovec[] size.
*/
size_t tcmu_iovec_seek(struct iovec *iovec, size_t count)
{
size_t consumed = 0;
while (count) {
if (count >= iovec->iov_len) {
count -= iovec->iov_len;
iovec->iov_len = 0;
iovec++;
consumed++;
} else {
iovec->iov_base += count;
iovec->iov_len -= count;
count = 0;
}
}
return consumed;
}
/*
* Consume an iovec. Count must not exceed the total iovec[] size.
* iove count should be updated.
*/
void tcmu_cmd_seek(struct tcmulib_cmd *cmd, size_t count)
{
cmd->iov_cnt -= tcmu_iovec_seek(cmd->iovec, count);
}
size_t tcmu_iovec_length(struct iovec *iovec, size_t iov_cnt)
{
size_t length = 0;
while (iov_cnt) {
length += iovec->iov_len;
iovec++;
iov_cnt--;
}
return length;
}
void __tcmu_sense_set_data(uint8_t *sense_buf, uint8_t key, uint16_t asc_ascq)
{
sense_buf[0] |= 0x70; /* fixed, current */
sense_buf[2] = key;
sense_buf[7] = 0xa;
sense_buf[12] = (asc_ascq >> 8) & 0xff;
sense_buf[13] = asc_ascq & 0xff;
}
int tcmu_sense_set_data(uint8_t *sense_buf, uint8_t key, uint16_t asc_ascq)
{
memset(sense_buf, 0, SENSE_BUFFERSIZE);
__tcmu_sense_set_data(sense_buf, key, asc_ascq);
return TCMU_STS_PASSTHROUGH_ERR;
}
void tcmu_sense_set_key_specific_info(uint8_t *sense_buf, uint16_t info)
{
memset(sense_buf, 0, 18);
put_unaligned_be16(info, &sense_buf[16]);
/* Set SKSV bit */
sense_buf[15] |= 0x80;
}
void tcmu_sense_set_info(uint8_t *sense_buf, uint32_t info)
{
memset(sense_buf, 0, 18);
put_unaligned_be32(info, &sense_buf[3]);
/* Set VALID bit */
sense_buf[0] |= 0x80;
}
/*
* Zero iovec.
*/
void tcmu_iovec_zero(struct iovec *iovec, size_t iov_cnt)
{
while (iov_cnt) {
bzero(iovec->iov_base, iovec->iov_len);
iovec++;
iov_cnt--;
}
}
static inline bool tcmu_zeroed_mem(const char *buf, size_t size)
{
int i;
for (i = 0; i < size; i++) {
if (buf[i])
return false;
}
return true;
}
bool tcmu_iovec_zeroed(struct iovec *iovec, size_t iov_cnt)
{
int i;
for (i = 0; i < iov_cnt; i++) {
if (!tcmu_zeroed_mem(iovec[i].iov_base, iovec[i].iov_len))
return false;
}
return true;
}
/*
* Copy data into an iovec, and consume the space in the iovec.
*
* Will truncate instead of overrunning the iovec.
*/
size_t tcmu_memcpy_into_iovec(
struct iovec *iovec,
size_t iov_cnt,
void *src,
size_t len)
{
size_t copied = 0;
while (len && iov_cnt) {
size_t to_copy = min(iovec->iov_len, len);
if (to_copy) {
memcpy(iovec->iov_base, src + copied, to_copy);
len -= to_copy;
copied += to_copy;
iovec->iov_base += to_copy;
iovec->iov_len -= to_copy;
}
iovec++;
iov_cnt--;
}
return copied;
}
/*
* Copy data from an iovec, and consume the space in the iovec.
*/
size_t tcmu_memcpy_from_iovec(
void *dest,
size_t len,
struct iovec *iovec,
size_t iov_cnt)
{
size_t copied = 0;
while (len && iov_cnt) {
size_t to_copy = min(iovec->iov_len, len);
if (to_copy) {
memcpy(dest + copied, iovec->iov_base, to_copy);
len -= to_copy;
copied += to_copy;
iovec->iov_base += to_copy;
iovec->iov_len -= to_copy;
}
iovec++;
iov_cnt--;
}
return copied;
}
#define CDB_TO_BUF_SIZE(bytes) ((bytes) * 3 + 2)
#define CDB_FIX_BYTES 64 /* 64 bytes for default */
#define CDB_FIX_SIZE CDB_TO_BUF_SIZE(CDB_FIX_BYTES)
void tcmu_cdb_print_info(struct tcmu_device *dev,
const struct tcmulib_cmd *cmd,
const char *info)
{
int i, n, bytes, info_len = 0;
char fix[CDB_FIX_SIZE], *buf;
buf = fix;
bytes = tcmu_cdb_get_length(cmd->cdb);
if (bytes < 0)
return;
if (info)
info_len = strlen(info);
if (CDB_TO_BUF_SIZE(bytes) + info_len > CDB_FIX_SIZE) {
buf = malloc(CDB_TO_BUF_SIZE(bytes) + info_len);
if (!buf) {
tcmu_dev_err(dev, "out of memory\n");
return;
}
}
for (i = 0, n = 0; i < bytes; i++) {
n += sprintf(buf + n, "%x ", cmd->cdb[i]);
}
if (info)
n += sprintf(buf + n, "%s", info);
sprintf(buf + n, "\n");
if (info) {
tcmu_dev_dbg(dev, "%s", buf);
} else {
tcmu_dev_dbg_scsi_cmd(dev, "%s", buf);
}
if (buf != fix)
free(buf);
}
void tcmu_thread_cancel(pthread_t thread)
{
void *join_retval;
int ret;
ret = pthread_cancel(thread);
if (ret) {
tcmu_err("pthread_cancel failed with value %d\n", ret);
return;
}
ret = pthread_join(thread, &join_retval);
if (ret) {
tcmu_err("pthread_join failed with value %d\n", ret);
return;
}
if (join_retval != PTHREAD_CANCELED)
tcmu_err("unexpected join retval: %p\n", join_retval);
}