forked from ublk-org/ublksrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgt_loop.c
279 lines (236 loc) · 6.43 KB
/
tgt_loop.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
// SPDX-License-Identifier: MIT or GPL-2.0-only
#include "ublksrv_tgt.h"
static const char *loop_tgt_backfile(struct ublksrv_tgt_info *tgt)
{
return (const char *)tgt->tgt_data;
}
static bool backing_supports_discard(char *name)
{
int fd;
char buf[512];
int len;
len = snprintf(buf, 512, "/sys/block/%s/queue/discard_max_hw_bytes",
basename(name));
buf[len] = 0;
fd = open(buf, O_RDONLY);
if (fd > 0) {
char val[128];
int ret = pread(fd, val, 128, 0);
unsigned long long bytes = 0;
close(fd);
if (ret > 0)
bytes = strtol(val, NULL, 10);
if (bytes > 0)
return true;
}
return false;
}
static int loop_init_tgt(struct ublksrv_dev *dev, int type, int argc, char
*argv[])
{
struct ublksrv_tgt_info *tgt = &dev->tgt;
const struct ublksrv_ctrl_dev_info *info = &dev->ctrl_dev->dev_info;
static const struct option lo_longopts[] = {
{ "file", 1, NULL, 'f' },
{ NULL }
};
unsigned long long bytes;
struct stat st;
int fd, opt;
char *file = NULL;
int jbuf_size, ret;
char *jbuf;
struct ublksrv_tgt_base_json tgt_json = {
.type = type,
};
struct ublk_params p = {
.types = UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD,
.basic = {
.logical_bs_shift = 9,
.physical_bs_shift = 12,
.io_opt_shift = 12,
.io_min_shift = 9,
.max_sectors = info->max_io_buf_bytes >> 9,
},
.discard = {
.max_discard_sectors = UINT_MAX >> 9,
.max_discard_segments = 1,
},
};
bool can_discard = false;
strcpy(tgt_json.name, "loop");
if (type != UBLKSRV_TGT_TYPE_LOOP)
return -1;
while ((opt = getopt_long(argc, argv, "-:f:",
lo_longopts, NULL)) != -1) {
switch (opt) {
case 'f':
file = strdup(optarg);
break;
}
}
if (!file)
return -1;
fd = open(file, O_RDWR | O_DIRECT);
if (fd < 0) {
fprintf(stderr, "__func__, backing file %s can't be opened\n",
__func__, file);
return -2;
}
if (fstat(fd, &st) < 0)
return -2;
if (S_ISBLK(st.st_mode)) {
if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
return -1;
can_discard = backing_supports_discard(file);
} else if (S_ISREG(st.st_mode)) {
bytes = st.st_size;
can_discard = true;
} else {
bytes = 0;
}
tgt->tgt_data = strdup(file);
tgt_json.dev_size = tgt->dev_size = bytes;
tgt->tgt_ring_depth = info->queue_depth;
tgt->nr_fds = 1;
tgt->fds[1] = fd;
p.basic.dev_sectors = bytes >> 9;
if (st.st_blksize && can_discard)
p.discard.discard_granularity = st.st_blksize;
else
p.types &= ~UBLK_PARAM_TYPE_DISCARD;
jbuf = ublksrv_tgt_realloc_json_buf(dev, &jbuf_size);
ublksrv_json_write_dev_info(dev->ctrl_dev, jbuf, jbuf_size);
ublksrv_json_write_target_base_info(jbuf, jbuf_size, &tgt_json);
do {
ret = ublksrv_json_write_target_str_info(jbuf, jbuf_size,
"backing_file", file);
if (ret < 0)
jbuf = ublksrv_tgt_realloc_json_buf(dev, &jbuf_size);
} while (ret < 0);
do {
ret = ublksrv_json_write_params(&p, jbuf, jbuf_size);
if (ret < 0)
jbuf = ublksrv_tgt_realloc_json_buf(dev, &jbuf_size);
} while (ret < 0);
return 0;
}
static void loop_usage_for_add(void)
{
printf(" loop: -f backing_file\n");
}
static void loop_handle_fallocate_async(struct io_uring_sqe *sqe,
const struct ublksrv_io_desc *iod)
{
__u16 ublk_op = ublksrv_get_op(iod);
__u32 flags = ublksrv_get_flags(iod);
__u32 mode = FALLOC_FL_KEEP_SIZE;
sqe->addr = iod->nr_sectors << 9;
/* follow logic of linux kernel loop */
if (ublk_op == UBLK_IO_OP_DISCARD) {
mode |= FALLOC_FL_PUNCH_HOLE;
} else if (ublk_op == UBLK_IO_OP_WRITE_ZEROES) {
if (flags & UBLK_IO_F_NOUNMAP)
mode |= FALLOC_FL_ZERO_RANGE;
else
mode |= FALLOC_FL_PUNCH_HOLE;
} else {
mode |= FALLOC_FL_ZERO_RANGE;
}
sqe->len = mode;
}
static int loop_queue_tgt_io(struct ublksrv_queue *q, struct ublk_io *io,
int tag)
{
const struct ublksrv_io_desc *iod = ublksrv_get_iod(q, tag);
unsigned io_op = ublksrv_convert_cmd_op(iod);
struct io_uring_sqe *sqe;
sqe = io_uring_get_sqe(&q->ring);
if (!sqe)
return 0;
/* bit63 marks us as tgt io */
sqe->flags = IOSQE_FIXED_FILE;
sqe->user_data = build_user_data(tag, io_op, 0, 1);
sqe->fd = 1;
sqe->opcode = io_op;
switch (io_op) {
case IORING_OP_FALLOCATE:
loop_handle_fallocate_async(sqe, iod);
break;
case IORING_OP_FSYNC:
sqe->fsync_flags = IORING_FSYNC_DATASYNC;
sqe->len = iod->nr_sectors << 9;
break;
default:
sqe->addr = iod->addr;
sqe->len = iod->nr_sectors << 9;
}
sqe->off = iod->start_sector << 9;
q->tgt_io_inflight += 1;
ublksrv_log(LOG_INFO, "%s: tag %d ublk io %x %llx %u\n", __func__, tag,
iod->op_flags, iod->start_sector, iod->nr_sectors << 9);
ublksrv_log(LOG_INFO, "%s: queue io op %d(%llu %llx %llx)"
" (qid %d tag %u, cmd_op %u target: %d, user_data %llx) iof %x\n",
__func__, io_op, sqe->off, sqe->len, sqe->addr,
q->q_id, tag, io_op, 1, sqe->user_data, io->flags);
return 1;
}
static co_io_job __loop_handle_io_async(struct ublksrv_queue *q,
struct ublk_io *io, int tag)
{
struct io_uring_cqe *cqe;
int ret;
io->queued_tgt_io = 0;
again:
ret = loop_queue_tgt_io(q, io, tag);
if (ret) {
if (io->queued_tgt_io)
ublksrv_log(LOG_INFO, "bad queued_tgt_io %d\n",
io->queued_tgt_io);
io->queued_tgt_io += 1;
co_io_job_submit_and_wait();
io->queued_tgt_io -= 1;
cqe = io->tgt_io_cqe;
if (cqe->res == -EAGAIN)
goto again;
ublksrv_complete_io(q, tag, cqe->res);
} else {
ublksrv_log(LOG_INFO, "no sqe %d\n", tag);
}
}
static int loop_handle_io_async(struct ublksrv_queue *q, int tag)
{
struct ublk_io_tgt *io = (struct ublk_io_tgt *)&q->ios[tag];
io->co = __loop_handle_io_async(q, (struct ublk_io *)io, tag);
return 0;
}
static void loop_tgt_io_done(struct ublksrv_queue *q, struct io_uring_cqe *cqe)
{
int tag = user_data_to_tag(cqe->user_data);
struct ublk_io *io = &q->ios[tag];
if (!io->queued_tgt_io)
syslog(LOG_WARNING, "%s: wrong queued_tgt_io: res %d qid %u tag %u, cmd_op %u\n",
__func__, cqe->res, q->q_id,
user_data_to_tag(cqe->user_data),
user_data_to_op(cqe->user_data));
io->tgt_io_cqe = cqe;
((struct ublk_io_tgt *)io)->co.resume();
}
static void loop_deinit_tgt(struct ublksrv_dev *dev)
{
free(dev->tgt.tgt_data);
}
struct ublksrv_tgt_type loop_tgt_type = {
.type = UBLKSRV_TGT_TYPE_LOOP,
.name = "loop",
.init_tgt = loop_init_tgt,
.handle_io_async = loop_handle_io_async,
.tgt_io_done = loop_tgt_io_done,
.usage_for_add = loop_usage_for_add,
.deinit_tgt = loop_deinit_tgt,
};
static void tgt_loop_init() __attribute__((constructor));
static void tgt_loop_init(void)
{
ublksrv_register_tgt_type(&loop_tgt_type);
}