forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_example.c
235 lines (199 loc) · 5.16 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
/*
* 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 <errno.h>
#include <scsi/scsi.h>
#include "scsi_defs.h"
#include "tcmu-runner.h"
struct file_state {
int fd;
};
static bool file_check_config(const char *cfgstring, char **reason)
{
char *path;
int fd;
path = strchr(cfgstring, '/');
if (!path) {
if (asprintf(reason, "No path found") == -1)
*reason = NULL;
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) {
if (asprintf(reason, "Could not create file") == -1)
*reason = NULL;
return false;
}
unlink(path);
return true;
}
static int file_open(struct tcmu_device *dev)
{
struct file_state *state;
int64_t size;
char *config;
int block_size;
state = calloc(1, sizeof(*state));
if (!state)
return -ENOMEM;
tcmu_set_dev_private(dev, state);
block_size = tcmu_get_attribute(dev, "hw_block_size");
if (block_size < 0) {
tcmu_err("Could not get device block size\n");
goto err;
}
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 err;
}
tcmu_set_dev_num_lbas(dev, size / block_size);
config = strchr(tcmu_get_dev_cfgstring(dev), '/');
if (!config) {
tcmu_err("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) {
tcmu_err("could not open %s: %m\n", config);
goto err;
}
tcmu_dbg("config %s, size %lld\n", tcmu_get_dev_cfgstring(dev), size);
return 0;
err:
free(state);
return -EINVAL;
}
static void file_close(struct tcmu_device *dev)
{
struct file_state *state = tcmu_get_dev_private(dev);
close(state->fd);
free(state);
}
static int file_read(struct tcmu_device *dev, struct tcmulib_cmd *cmd,
struct iovec *iov, size_t iov_cnt, size_t length,
off_t offset)
{
struct file_state *state = tcmu_get_dev_private(dev);
size_t remaining = length;
ssize_t ret;
while (remaining) {
ret = preadv(state->fd, iov, iov_cnt, offset);
if (ret < 0) {
tcmu_err("read failed: %m\n");
ret = tcmu_set_sense_data(cmd->sense_buf, MEDIUM_ERROR,
ASC_READ_ERROR, NULL);
goto done;
}
if (ret == 0) {
/* EOF, then zeros the iovecs left */
tcmu_zero_iovec(iov, iov_cnt);
break;
}
tcmu_seek_in_iovec(iov, ret);
offset += ret;
remaining -= ret;
}
ret = SAM_STAT_GOOD;
done:
cmd->done(dev, cmd, ret);
return 0;
}
static int file_write(struct tcmu_device *dev, struct tcmulib_cmd *cmd,
struct iovec *iov, size_t iov_cnt, size_t length,
off_t offset)
{
struct file_state *state = tcmu_get_dev_private(dev);
size_t remaining = length;
ssize_t ret;
while (remaining) {
ret = pwritev(state->fd, iov, iov_cnt, offset);
if (ret < 0) {
tcmu_err("write failed: %m\n");
ret = tcmu_set_sense_data(cmd->sense_buf, MEDIUM_ERROR,
ASC_WRITE_ERROR, NULL);
goto done;
}
tcmu_seek_in_iovec(iov, ret);
offset += ret;
remaining -= ret;
}
ret = SAM_STAT_GOOD;
done:
cmd->done(dev, cmd, ret);
return 0;
}
static int file_flush(struct tcmu_device *dev, struct tcmulib_cmd *cmd)
{
struct file_state *state = tcmu_get_dev_private(dev);
int ret;
if (fsync(state->fd)) {
tcmu_err("sync failed\n");
ret = tcmu_set_sense_data(cmd->sense_buf, MEDIUM_ERROR,
ASC_WRITE_ERROR, NULL);
goto done;
}
ret = SAM_STAT_GOOD;
done:
cmd->done(dev, cmd, ret);
return 0;
}
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,
.read = file_read,
.write = file_write,
.flush = file_flush,
.name = "File-backed Handler (example code)",
.subtype = "file",
.nr_threads = 2,
};
/* Entry point must be named "handler_init". */
int handler_init(void)
{
return tcmur_register_handler(&file_handler);
}