-
Notifications
You must be signed in to change notification settings - Fork 148
/
target.c
347 lines (294 loc) · 7.83 KB
/
target.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
/*
* Copyright (c) 2017 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 <stdint.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <inttypes.h>
#include <limits.h>
#include <pthread.h>
#include "ccan/list/list.h"
#include "libtcmu_log.h"
#include "libtcmu_common.h"
#include "tcmur_device.h"
#include "tcmur_work.h"
#include "target.h"
#include "alua.h"
static struct list_head tpg_recovery_list = LIST_HEAD_INIT(tpg_recovery_list);
/*
* Locking ordering:
* rdev->rdev_lock
* tpg_recovery_lock
*/
static pthread_mutex_t tpg_recovery_lock = PTHREAD_MUTEX_INITIALIZER;
struct tgt_port_grp {
char *wwn;
char *fabric;
uint16_t tpgt;
/* entry on tpg_recovery_list */
struct list_node recovery_entry;
/* list of devs to recover */
struct list_head devs;
pthread_t recovery_thread;
};
static int tcmu_set_tpg_int(struct tgt_port_grp *tpg, const char *name,
int val)
{
char path[PATH_MAX];
snprintf(path, sizeof(path), CFGFS_ROOT"/%s/%s/tpgt_%hu/%s",
tpg->fabric, tpg->wwn, tpg->tpgt, name);
return tcmu_cfgfs_set_u32(path, val);
}
static int tcmu_get_tpg_int(struct tgt_port *port, const char *name)
{
char path[PATH_MAX];
snprintf(path, sizeof(path),
CFGFS_ROOT"/%s/%s/tpgt_%hu/%s",
port->fabric, port->wwn, port->tpgt, name);
return tcmu_cfgfs_get_int(path);
}
static int tcmu_get_lun_int_stat(struct tgt_port *port, uint64_t lun,
const char *stat_name)
{
char path[PATH_MAX];
snprintf(path, sizeof(path),
CFGFS_ROOT"/%s/%s/tpgt_%hu/lun/lun_%"PRIu64"/statistics/%s",
port->fabric, port->wwn, port->tpgt, lun, stat_name);
return tcmu_cfgfs_get_int(path);
}
void tcmu_free_tgt_port(struct tgt_port *port)
{
if (port->wwn)
free(port->wwn);
if (port->fabric)
free(port->fabric);
free(port);
}
struct tgt_port *tcmu_get_tgt_port(char *member_str)
{
struct tgt_port *port;
char fabric[17], wwn[224];
uint64_t lun;
uint16_t tpgt;
int ret;
if (!strlen(member_str))
return NULL;
ret = sscanf(member_str, "%16[^/]/%223[^/]/tpgt_%hu/lun_%"PRIu64,
fabric, wwn, &tpgt, &lun);
if (ret != 4) {
tcmu_err("Invalid ALUA member %s:%s\n", member_str,
strerror(errno));
return NULL;
}
port = calloc(1, sizeof(*port));
if (!port)
return NULL;
list_node_init(&port->entry);
if (!strcmp(fabric, "iSCSI"))
/*
* iSCSI's fabric name and target_core_fabric_ops name do
* not match.
*/
port->fabric = strdup("iscsi");
else
port->fabric = strdup(fabric);
if (!port->fabric)
goto free_port;
port->wwn = strdup(wwn);
if (!port->wwn)
goto free_port;
port->tpgt = tpgt;
ret = tcmu_get_lun_int_stat(port, lun, "scsi_port/indx");
if (ret < 0)
goto free_port;
port->rel_port_id = ret;
ret = tcmu_get_lun_int_stat(port, lun, "scsi_transport/proto_id");
if (ret < 0)
goto free_port;
port->proto_id = ret;
ret = tcmu_get_tpg_int(port, "enable");
if (ret < 0)
goto free_port;
port->enabled = ret;
return port;
free_port:
tcmu_free_tgt_port(port);
return NULL;
}
static bool port_is_on_tgt_port_grp(struct tgt_port *port,
struct tgt_port_grp *tpg)
{
if (!strcmp(port->fabric, tpg->fabric) &&
!strcmp(port->wwn, tpg->wwn) && port->tpgt == tpg->tpgt)
return true;
return false;
}
static struct tgt_port_grp *port_is_on_recovery_list(struct tgt_port *port)
{
struct tgt_port_grp *tpg;
list_for_each(&tpg_recovery_list, tpg, recovery_entry) {
if (port_is_on_tgt_port_grp(port, tpg))
return tpg;
}
return NULL;
}
static void free_tgt_port_grp(struct tgt_port_grp *tpg)
{
free(tpg->fabric);
free(tpg->wwn);
free(tpg);
}
static struct tgt_port_grp *setup_tgt_port_grp(struct tgt_port *port)
{
struct tgt_port_grp *tpg;
tpg = calloc(1, sizeof(*tpg));
if (!tpg)
goto fail;
list_head_init(&tpg->devs);
list_node_init(&tpg->recovery_entry);
tpg->tpgt = port->tpgt;
tpg->wwn = strdup(port->wwn);
if (!tpg->wwn)
goto free_tpg;
tpg->fabric = strdup(port->fabric);
if (!tpg->fabric)
goto free_wwn;
return tpg;
free_wwn:
free(tpg->wwn);
free_tpg:
free(tpg);
fail:
return NULL;
}
/*
* Disable the target tpg to avoid flip flopping between paths
* (transport path is ok so multipath layer switches to it, but
* then sends IO only for it to fail due to the handler not
* being able to reach its backend).
*/
static void tgt_port_grp_recovery_work_fn(void *arg)
{
struct tgt_port_grp *tpg = arg;
struct tcmur_device *rdev, *tmp_rdev;
bool enable_tpg = false;
int ret;
tcmu_dbg("Disabling %s/%s/tpgt_%hu.\n", tpg->fabric, tpg->wwn,
tpg->tpgt);
/*
* This will return when all running commands have completed at
* the target layer. Handlers must call tcmu_notify_lock_lost
* before completing the failed command, so the device will be on
* the list reopen list when setting enable=0 returns..
*/
ret = tcmu_set_tpg_int(tpg, "enable", 0);
pthread_mutex_lock(&tpg_recovery_lock);
list_del(&tpg->recovery_entry);
pthread_mutex_unlock(&tpg_recovery_lock);
if (ret < 0) {
tcmu_err("Could not disable %s/%s/tpgt_%hu (err %d).\n",
tpg->fabric, tpg->wwn, tpg->tpgt, ret);
/* just recover the devs and leave the tpg in curr state */
goto done;
}
enable_tpg = true;
tcmu_info("Disabled %s/%s/tpgt_%hu.\n", tpg->fabric, tpg->wwn,
tpg->tpgt);
done:
/*
* TODO - the transport is stopped, so we should use the
* cmdproc thread to reopen all these in parallel.
*/
list_for_each_safe(&tpg->devs, rdev, tmp_rdev, recovery_entry) {
list_del(&rdev->recovery_entry);
ret = __tcmu_reopen_dev(rdev->dev, -1);
if (ret) {
tcmu_dev_err(rdev->dev, "Could not reinitialize device. (err %d).\n",
ret);
if (!(rdev->flags & TCMUR_DEV_FLAG_STOPPING))
/* assume fatal error so do not enable tpg */
enable_tpg = false;
}
}
if (enable_tpg) {
ret = tcmu_set_tpg_int(tpg, "enable", 1);
if (ret) {
tcmu_err("Could not enable %s/%s/tpgt_%hu (err %d).\n",
tpg->fabric, tpg->wwn, tpg->tpgt, ret);
} else {
tcmu_info("Enabled %s/%s/tpgt_%hu.\n", tpg->fabric, tpg->wwn,
tpg->tpgt);
}
}
free_tgt_port_grp(tpg);
}
int tcmu_add_dev_to_recovery_list(struct tcmu_device *dev)
{
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
struct list_head alua_list;
struct alua_grp *group;
struct tgt_port_grp *tpg;
struct tgt_port *port, *enabled_port = NULL;
int ret;
pthread_mutex_lock(&tpg_recovery_lock);
list_head_init(&alua_list);
ret = tcmu_get_alua_grps(dev, &alua_list);
if (ret) {
/* User is deleting device so fast fail */
tcmu_dev_warn(dev, "Could not find any tpgs.\n");
goto done;
}
/*
* This assumes a tcmu_dev is only exported though one local
* enabled tpg. The kernel members file only returns
* the one and runner is not passed info about which
* tpg/port IO was received on.
*/
list_for_each(&alua_list, group, entry) {
list_for_each(&group->tgt_ports, port, entry) {
if (port->enabled)
enabled_port = port;
/*
* If another device already kicked off recovery
* the enabled bit might not be set.
*/
tpg = port_is_on_recovery_list(port);
if (tpg)
goto add_to_list;
}
}
if (!enabled_port) {
ret = -EIO;
/* User disabled port from under us? */
tcmu_dev_err(dev, "Could not find enabled port.\n");
goto done;
}
tpg = setup_tgt_port_grp(enabled_port);
if (!tpg) {
ret = -ENOMEM;
goto done;
}
ret = tcmur_run_work(rdev->event_work, tpg, tgt_port_grp_recovery_work_fn);
if (ret) {
tcmu_dev_err(dev, "Could not start recovery thread. Err %d\n",
ret);
free_tgt_port_grp(tpg);
goto done;
}
list_add(&tpg_recovery_list, &tpg->recovery_entry);
add_to_list:
list_add(&tpg->devs, &rdev->recovery_entry);
rdev->flags |= TCMUR_DEV_FLAG_IN_RECOVERY;
done:
tcmu_release_alua_grps(&alua_list);
pthread_mutex_unlock(&tpg_recovery_lock);
return ret;
}