forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcmur_aio.c
338 lines (268 loc) · 7.79 KB
/
tcmur_aio.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
/*
* Copyright 2017, 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.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <assert.h>
#include <stdint.h>
#include <pthread.h>
#include "ccan/list/list.h"
#include "libtcmu.h"
#include "libtcmu_priv.h"
#include "tcmur_device.h"
#include "tcmur_aio.h"
#include "tcmu-runner.h"
struct tcmu_work {
struct tcmu_device *dev;
struct tcmulib_cmd *cmd;
tcmu_work_fn_t fn;
struct list_node entry;
};
static void _cleanup_mutex_lock(void *arg)
{
pthread_mutex_unlock(arg);
}
void track_aio_request_start(struct tcmur_device *rdev)
{
struct tcmu_track_aio *aio_track = &rdev->track_queue;
pthread_cleanup_push(_cleanup_mutex_lock, (void *)&aio_track->track_lock);
pthread_mutex_lock(&aio_track->track_lock);
++aio_track->tracked_aio_ops;
pthread_mutex_unlock(&aio_track->track_lock);
pthread_cleanup_pop(0);
}
void track_aio_request_finish(struct tcmur_device *rdev, int *is_idle)
{
struct tcmu_track_aio *aio_track = &rdev->track_queue;
pthread_cond_t *cond;
pthread_cleanup_push(_cleanup_mutex_lock, (void *)&aio_track->track_lock);
pthread_mutex_lock(&aio_track->track_lock);
assert(aio_track->tracked_aio_ops > 0);
--aio_track->tracked_aio_ops;
if (is_idle) {
*is_idle = (aio_track->tracked_aio_ops == 0) ? 1 : 0;
}
if (!aio_track->tracked_aio_ops && aio_track->is_empty_cond) {
cond = aio_track->is_empty_cond;
aio_track->is_empty_cond = NULL;
pthread_cond_signal(cond);
}
pthread_mutex_unlock(&aio_track->track_lock);
pthread_cleanup_pop(0);
}
static void cleanup_empty_queue_wait(void *arg)
{
struct tcmu_track_aio *aio_track = arg;
pthread_cond_destroy(aio_track->is_empty_cond);
aio_track->is_empty_cond = NULL;
pthread_mutex_unlock(&aio_track->track_lock);
}
int aio_wait_for_empty_queue(struct tcmur_device *rdev)
{
struct tcmu_track_aio *aio_track = &rdev->track_queue;
pthread_cond_t cond;
int ret;
ret = pthread_cond_init(&cond, NULL);
if (ret)
return ret;
pthread_cleanup_push(cleanup_empty_queue_wait, aio_track);
pthread_mutex_lock(&aio_track->track_lock);
if (!aio_track->tracked_aio_ops)
goto unlock;
tcmu_dev_dbg(rdev->dev, "waiting for %d commands\n",
rdev->track_queue.tracked_aio_ops);
aio_track->is_empty_cond = &cond;
ret = pthread_cond_wait(&cond, &aio_track->track_lock);
aio_track->is_empty_cond = NULL;
unlock:
pthread_mutex_unlock(&aio_track->track_lock);
pthread_cleanup_pop(0);
pthread_cond_destroy(&cond);
return ret;
}
static void _cleanup_io_work(void *arg)
{
free(arg);
}
static void *io_work_queue(void *arg)
{
struct tcmu_device *dev = arg;
struct tcmur_device *rdev = tcmu_get_daemon_dev_private(dev);
struct tcmu_io_queue *io_wq = &rdev->work_queue;
int ret;
while (1) {
struct tcmu_work *work;
struct tcmulib_cmd *cmd;
pthread_cleanup_push(_cleanup_mutex_lock, &io_wq->io_lock);
pthread_mutex_lock(&io_wq->io_lock);
while (list_empty(&io_wq->io_queue)) {
pthread_cond_wait(&io_wq->io_cond,
&io_wq->io_lock);
}
work = list_first_entry(&io_wq->io_queue, struct tcmu_work,
entry);
list_del(&work->entry);
pthread_mutex_unlock(&io_wq->io_lock);
pthread_cleanup_pop(0);
/* kick start I/O request */
cmd = work->cmd;
pthread_cleanup_push(_cleanup_io_work, work);
ret = work->fn(work->dev, cmd);
if (ret)
cmd->done(dev, cmd, ret);
pthread_cleanup_pop(1); /* cleanup work */
}
return NULL;
}
static int aio_schedule(struct tcmu_device *dev, struct tcmulib_cmd *cmd,
tcmu_work_fn_t fn)
{
struct tcmu_work *work;
struct tcmur_device *rdev = tcmu_get_daemon_dev_private(dev);
struct tcmu_io_queue *io_wq = &rdev->work_queue;
work = malloc(sizeof(*work));
if (!work)
return SAM_STAT_TASK_SET_FULL;
work->fn = fn;
work->dev = dev;
work->cmd = cmd;
list_node_init(&work->entry);
/* cleanup push/pop not _really_ required here atm */
pthread_cleanup_push(_cleanup_mutex_lock, &io_wq->io_lock);
pthread_mutex_lock(&io_wq->io_lock);
list_add_tail(&io_wq->io_queue, &work->entry);
pthread_cond_signal(&io_wq->io_cond); // TODO: conditional
pthread_mutex_unlock(&io_wq->io_lock);
pthread_cleanup_pop(0);
return TCMU_ASYNC_HANDLED;
}
int async_handle_cmd(struct tcmu_device *dev, struct tcmulib_cmd *cmd,
tcmu_work_fn_t work_fn)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
int ret;
if (!rhandler->nr_threads) {
ret = work_fn(dev, cmd);
if (!ret)
ret = TCMU_ASYNC_HANDLED;
} else {
ret = aio_schedule(dev, cmd, work_fn);
}
return ret;
}
int setup_aio_tracking(struct tcmur_device *rdev)
{
int ret;
struct tcmu_track_aio *aio_track = &rdev->track_queue;
aio_track->tracked_aio_ops = 0;
ret = pthread_mutex_init(&aio_track->track_lock, NULL);
if (ret != 0) {
return ret;
}
return 0;
}
void cleanup_aio_tracking(struct tcmur_device *rdev)
{
int ret;
struct tcmu_track_aio *aio_track = &rdev->track_queue;
assert(aio_track->tracked_aio_ops == 0);
ret = pthread_mutex_destroy(&aio_track->track_lock);
if (ret != 0) {
tcmu_err("failed to destroy track lock\n");
}
}
void cleanup_io_work_queue_threads(struct tcmu_device *dev)
{
struct tcmur_handler *r_handler = tcmu_get_runner_handler(dev);
struct tcmur_device *rdev = tcmu_get_daemon_dev_private(dev);
struct tcmu_io_queue *io_wq = &rdev->work_queue;
int i, nr_threads = r_handler->nr_threads;
if (!io_wq->io_wq_threads) {
return;
}
for (i = 0; i < nr_threads; i++) {
if (io_wq->io_wq_threads[i]) {
tcmu_cancel_thread(io_wq->io_wq_threads[i]);
}
}
}
int setup_io_work_queue(struct tcmu_device *dev)
{
struct tcmur_handler *r_handler = tcmu_get_runner_handler(dev);
struct tcmur_device *rdev = tcmu_get_daemon_dev_private(dev);
struct tcmu_io_queue *io_wq = &rdev->work_queue;
int ret, i, nr_threads = r_handler->nr_threads;
if (!nr_threads)
return 0;
list_head_init(&io_wq->io_queue);
ret = pthread_mutex_init(&io_wq->io_lock, NULL);
if (ret != 0) {
goto out;
}
ret = pthread_cond_init(&io_wq->io_cond, NULL);
if (ret != 0) {
goto cleanup_lock;
}
/* TODO: Allow user to override device defaults */
io_wq->io_wq_threads = calloc(nr_threads, sizeof(pthread_t));
if (!io_wq->io_wq_threads)
goto cleanup_cond;
for (i = 0; i < nr_threads; i++) {
ret = pthread_create(&io_wq->io_wq_threads[i], NULL,
io_work_queue, dev);
if (ret != 0) {
goto cleanup_threads;
}
}
return 0;
cleanup_threads:
cleanup_io_work_queue_threads(dev);
free(io_wq->io_wq_threads);
cleanup_cond:
pthread_cond_destroy(&io_wq->io_cond);
cleanup_lock:
pthread_mutex_destroy(&io_wq->io_lock);
out:
return ret;
}
void cleanup_io_work_queue(struct tcmu_device *dev, bool cancel)
{
struct tcmur_device *rdev = tcmu_get_daemon_dev_private(dev);
struct tcmu_io_queue *io_wq = &rdev->work_queue;
int ret;
if (!io_wq->io_wq_threads) {
return;
}
if (cancel) {
cleanup_io_work_queue_threads(dev);
}
/*
* Note that there's no need to drain ->io_queue at this point
* as it _should_ be empty (target layer would call this path
* when no commands are running - thanks Mike).
*
* Out of tree handlers which do not use the aio code are not
* supported in this path.
*/
ret = pthread_mutex_destroy(&io_wq->io_lock);
if (ret != 0) {
tcmu_err("failed to destroy io workqueue lock\n");
}
ret = pthread_cond_destroy(&io_wq->io_cond);
if (ret != 0) {
tcmu_err("failed to destroy io workqueue cond\n");
}
free(io_wq->io_wq_threads);
}