/
ld_iscsi.c
439 lines (344 loc) · 10.6 KB
/
ld_iscsi.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
Copyright (C) 2011, 2012 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <asm/fcntl.h>
#include "iscsi.h"
#include "iscsi-private.h"
#include "scsi-lowlevel.h"
#include <sys/syscall.h>
#include <dlfcn.h>
static const char *initiator = "iqn.2007-10.com.github:sahlberg:libiscsi:ld-iscsi";
#define ISCSI_MAX_FD 255
struct iscsi_fd_list {
int is_iscsi;
int dup2fd;
int in_flight;
struct iscsi_context *iscsi;
int lun;
uint32_t block_size;
uint64_t num_blocks;
off_t offset;
};
static struct iscsi_fd_list iscsi_fd_list[ISCSI_MAX_FD];
int (*real_open)(__const char *path, int flags, mode_t mode);
int open(const char *path, int flags, mode_t mode)
{
int fd;
if (!strncmp(path, "iscsi:", 6)) {
struct iscsi_context *iscsi;
struct iscsi_url *iscsi_url;
struct scsi_task *task;
struct scsi_readcapacity10 *rc10;
iscsi = iscsi_create_context(initiator);
if (iscsi == NULL) {
fprintf(stderr, "ld-iscsi: Failed to create context\n");
errno = ENOMEM;
return -1;
}
iscsi_url = iscsi_parse_full_url(iscsi, path);
if (iscsi_url == NULL) {
fprintf(stderr, "ld-iscsi: Failed to parse URL: %s\n",
iscsi_get_error(iscsi));
iscsi_destroy_context(iscsi);
errno = EINVAL;
return -1;
}
iscsi_set_targetname(iscsi, iscsi_url->target);
iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);
iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
if (iscsi_url->user != NULL) {
if (iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, iscsi_url->passwd) != 0) {
fprintf(stderr, "Failed to set initiator username and password\n");
iscsi_destroy_context(iscsi);
errno = ENOMEM;
return -1;
}
}
if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
fprintf(stderr, "ld-iscsi: Login Failed. %s\n", iscsi_get_error(iscsi));
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
errno = EIO;
return -1;
}
task = iscsi_readcapacity10_sync(iscsi, iscsi_url->lun, 0, 0);
if (task == NULL || task->status != SCSI_STATUS_GOOD) {
fprintf(stderr, "ld-iscsi: failed to send readcapacity command\n");
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
errno = EIO;
return -1;
}
rc10 = scsi_datain_unmarshall(task);
if (rc10 == NULL) {
fprintf(stderr, "ld-iscsi: failed to unmarshall readcapacity10 data\n");
scsi_free_scsi_task(task);
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
errno = EIO;
return -1;
}
fd = iscsi_get_fd(iscsi);
if (fd >= ISCSI_MAX_FD) {
fprintf(stderr, "ld-iscsi: Too many files open\n");
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
errno = ENFILE;
return -1;
}
iscsi_fd_list[fd].is_iscsi = 1;
iscsi_fd_list[fd].dup2fd = -1;
iscsi_fd_list[fd].iscsi = iscsi;
iscsi_fd_list[fd].block_size = rc10->block_size;
iscsi_fd_list[fd].num_blocks = rc10->lba + 1;
iscsi_fd_list[fd].offset = 0;
iscsi_fd_list[fd].lun = iscsi_url->lun;
scsi_free_scsi_task(task);
iscsi_destroy_url(iscsi_url);
return fd;
}
return real_open(path, flags, mode);
}
int open64(const char *path, int flags, mode_t mode)
{
return open(path, flags | O_LARGEFILE, mode);
}
int (*real_close)(int fd);
int close(int fd)
{
if (iscsi_fd_list[fd].is_iscsi == 1) {
int i;
if (iscsi_fd_list[fd].dup2fd >= 0) {
iscsi_fd_list[fd].is_iscsi = 0;
iscsi_fd_list[fd].dup2fd = -1;
real_close(fd);
return 0;
}
/* are there any FDs dup2ed onto this ? */
for(i = 0; i < ISCSI_MAX_FD; i++) {
if (iscsi_fd_list[i].dup2fd == fd) {
break;
}
}
if (i < ISCSI_MAX_FD) {
int j;
/* yes there are DUPs onto fd, make i the new real device and repoint all other
* duplicates
*/
memcpy(&iscsi_fd_list[i], &iscsi_fd_list[fd], sizeof(struct iscsi_fd_list));
iscsi_fd_list[i].dup2fd = -1;
memset(&iscsi_fd_list[fd], 0, sizeof(struct iscsi_fd_list));
iscsi_fd_list[fd].dup2fd = -1;
iscsi_fd_list[i].iscsi->fd = i;
real_close(fd);
for(j = 0; j < ISCSI_MAX_FD; j++) {
if (j != i && iscsi_fd_list[j].dup2fd == fd) {
iscsi_fd_list[j].dup2fd = i;
}
}
return 0;
}
iscsi_fd_list[fd].is_iscsi = 0;
iscsi_fd_list[fd].dup2fd = -1;
iscsi_destroy_context(iscsi_fd_list[fd].iscsi);
iscsi_fd_list[fd].iscsi = NULL;
return 0;
}
return real_close(fd);
}
int (*real_fxstat)(int ver, int fd, struct stat *buf);
int __fxstat(int ver, int fd, struct stat *buf)
{
if (iscsi_fd_list[fd].is_iscsi == 1) {
if (iscsi_fd_list[fd].dup2fd >= 0) {
return __fxstat(ver, iscsi_fd_list[fd].dup2fd, buf);
}
memset(buf, 0, sizeof(struct stat));
buf->st_mode = S_IRUSR | S_IRGRP | S_IROTH | S_IFREG;
buf->st_size = iscsi_fd_list[fd].num_blocks * iscsi_fd_list[fd].block_size;
return 0;
}
return real_fxstat(ver, fd, buf);
}
int (*real_lxstat)(int ver, __const char *path, struct stat *buf);
int __lxstat(int ver, const char *path, struct stat *buf)
{
if (!strncmp(path, "iscsi:", 6)) {
int fd, ret;
fd = open(path, 0, 0);
if (fd == -1) {
return fd;
}
ret = __fxstat(ver, fd, buf);
close(fd);
return ret;
}
return real_lxstat(ver, path, buf);
}
int (*real_xstat)(int ver, __const char *path, struct stat *buf);
int __xstat(int ver, const char *path, struct stat *buf)
{
return __lxstat(ver, path, buf);
}
ssize_t (*real_read)(int fd, void *buf, size_t count);
ssize_t read(int fd, void *buf, size_t count)
{
if ((iscsi_fd_list[fd].is_iscsi == 1) && (iscsi_fd_list[fd].in_flight == 0)) {
uint64_t offset;
uint32_t num_blocks, lba;
struct scsi_task *task;
if (iscsi_fd_list[fd].dup2fd >= 0) {
return read(iscsi_fd_list[fd].dup2fd, buf, count);
}
offset = iscsi_fd_list[fd].offset / iscsi_fd_list[fd].block_size * iscsi_fd_list[fd].block_size;
num_blocks = (iscsi_fd_list[fd].offset - offset + count + iscsi_fd_list[fd].block_size - 1) / iscsi_fd_list[fd].block_size;
lba = offset / iscsi_fd_list[fd].block_size;
/* Don't try to read beyond the last LBA */
if (lba >= iscsi_fd_list[fd].num_blocks) {
return 0;
}
/* Trim num_blocks requested to last lba */
if ((lba + num_blocks) > iscsi_fd_list[fd].num_blocks) {
num_blocks = iscsi_fd_list[fd].num_blocks - lba;
count = num_blocks * iscsi_fd_list[fd].block_size;
}
iscsi_fd_list[fd].in_flight = 1;
task = iscsi_read10_sync(iscsi_fd_list[fd].iscsi, iscsi_fd_list[fd].lun, lba, num_blocks * iscsi_fd_list[fd].block_size, iscsi_fd_list[fd].block_size, 0, 0, 0, 0, 0);
iscsi_fd_list[fd].in_flight = 0;
if (task == NULL || task->status != SCSI_STATUS_GOOD) {
fprintf(stderr, "ld-iscsi: failed to send read10 command\n");
errno = EIO;
return -1;
}
memcpy(buf, &task->datain.data[iscsi_fd_list[fd].offset - offset], count);
iscsi_fd_list[fd].offset += count;
scsi_free_scsi_task(task);
return count;
}
return real_read(fd, buf, count);
}
int (*real_dup2)(int oldfd, int newfd);
int dup2(int oldfd, int newfd)
{
close(newfd);
if (iscsi_fd_list[oldfd].is_iscsi == 1) {
int ret;
if (iscsi_fd_list[oldfd].dup2fd >= 0) {
return dup2(iscsi_fd_list[oldfd].dup2fd, newfd);
}
ret = real_dup2(oldfd, newfd);
if (ret < 0) {
return ret;
}
iscsi_fd_list[newfd].is_iscsi = 1;
iscsi_fd_list[newfd].dup2fd = oldfd;
return newfd;
}
return real_dup2(oldfd, newfd);
}
int (*real_fxstat64)(int ver, int fd, struct stat64 *buf);
int __fxstat64(int ver, int fd, struct stat64 *buf)
{
if (iscsi_fd_list[fd].is_iscsi == 1) {
if (iscsi_fd_list[fd].dup2fd >= 0) {
return __fxstat64(ver, iscsi_fd_list[fd].dup2fd, buf);
}
memset(buf, 0, sizeof(struct stat64));
buf->st_mode = S_IRUSR | S_IRGRP | S_IROTH | S_IFREG;
buf->st_size = iscsi_fd_list[fd].num_blocks * iscsi_fd_list[fd].block_size;
return 0;
}
return real_fxstat64(ver, fd, buf);
}
int (*real_lxstat64)(int ver, __const char *path, struct stat64 *buf);
int __lxstat64(int ver, const char *path, struct stat64 *buf)
{
if (!strncmp(path, "iscsi:", 6)) {
int fd, ret;
fd = open64(path, 0, 0);
if (fd == -1) {
return fd;
}
ret = __fxstat64(ver, fd, buf);
close(fd);
return ret;
}
return real_lxstat64(ver, path, buf);
}
int (*real_xstat64)(int ver, __const char *path, struct stat64 *buf);
int __xstat64(int ver, const char *path, struct stat64 *buf)
{
return __lxstat64(ver, path, buf);
}
static void __attribute__((constructor)) _init(void)
{
int i;
for(i = 0; i < ISCSI_MAX_FD; i++) {
iscsi_fd_list[i].dup2fd = -1;
}
real_open = dlsym(RTLD_NEXT, "open");
if (real_open == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(open)\n");
exit(10);
}
real_close = dlsym(RTLD_NEXT, "close");
if (real_close == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(close)\n");
exit(10);
}
real_fxstat = dlsym(RTLD_NEXT, "__fxstat");
if (real_fxstat == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(__fxstat)\n");
exit(10);
}
real_lxstat = dlsym(RTLD_NEXT, "__lxstat");
if (real_lxstat == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(__lxstat)\n");
exit(10);
}
real_xstat = dlsym(RTLD_NEXT, "__xstat");
if (real_xstat == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(__xstat)\n");
exit(10);
}
real_read = dlsym(RTLD_NEXT, "read");
if (real_read == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(read)\n");
exit(10);
}
real_dup2 = dlsym(RTLD_NEXT, "dup2");
if (real_dup2 == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(dup2)\n");
exit(10);
}
real_fxstat64 = dlsym(RTLD_NEXT, "__fxstat64");
if (real_fxstat64 == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(__fxstat64)\n");
}
real_lxstat64 = dlsym(RTLD_NEXT, "__lxstat64");
if (real_lxstat64 == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(_lxstat64)\n");
}
real_xstat64 = dlsym(RTLD_NEXT, "__xstat64");
if (real_xstat64 == NULL) {
fprintf(stderr, "ld_iscsi: Failed to dlsym(__xstat64)\n");
}
}