File: s390-zfcp.c

package info (click to toggle)
s390-zfcp 1.0.6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 144 kB
  • sloc: ansic: 1,651; makefile: 24; sh: 3
file content (342 lines) | stat: -rw-r--r-- 6,607 bytes parent folder | download | duplicates (3)
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
/*
 * zfcp helper functions for the s390-zfcp Debian-Installation
 * configuration module.
 *
 *
 * Copyright IBM Corp. 2015
 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
 */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>

#include <debian-installer.h>

#include "s390-dev.h"

int zfcp_check_allow_lun_scan(int *enabled)
{
	int rc;
	char *path, *value;

	rc = -1;
	path = path_get_sys_module_param(ZFCP_MOD_NAME, "allow_lun_scan");
	if (path == NULL)
		goto out;

	value = misc_read_text_file(path, 1);
	if (value == NULL)
		goto out_free_path;

	rc = 0;
	*enabled = !strncmp(value, "Y", 1);

	free(value);
out_free_path:
	free(path);
out:
	return rc;
}

static char *get_port_type_path(const char *id)
{
	int rc;
	int err;
	DIR *dir;
	char *devpath, *path;
	struct dirent de, *dep;

	devpath = path_get_ccw_device(ZFCP_CCWDRV_NAME, id);
	dir = opendir(devpath);
	if (dir == NULL)
		goto out;

	path = NULL;
	dir_for_each_entry_safe(dir, &de, dep, err) {
		if (!starts_with(de.d_name, "host"))
			continue;
		rc = asprintf(&path, "%s/%s/fc_host/%s/port_type",
			      devpath, de.d_name, de.d_name);
		if (rc == -1)
			path = NULL;
		break;
	}

	closedir(dir);
out:
	free(devpath);

	return path;
}

result_t zfcp_dev_check_npiv(const char *ccwid, int *enabled)
{
	result_t rc;
	char *path, *type;

	rc = RESULT_RUNTIME_ERROR;
	path = get_port_type_path(ccwid);
	if (path == NULL)
		goto out;

	type = misc_read_text_file(path, 1);
	if (type == NULL)
		goto out;

	/* Check FCP port type. */
	*enabled = !strcmp(type, "NPIV VPORT");

	rc = RESULT_OK;
	free(type);
out:
	free(path);

	return rc;
}

result_t zfcp_lun_op(const struct zfcp_lun_devid *devid, const char *op)
{
	int rc;
	result_t result;
	char *portpath, *path = NULL, *lun = NULL;

	result = RESULT_OUT_OF_MEMORY;

	portpath = path_get_zfcp_port_dev(devid);
	if (portpath == NULL)
		goto out;

	if (!dir_exists(portpath)) {
		result = RESULT_ZFCP_WWPN_NOT_FOUND;
		goto out;
	}

	rc = asprintf(&path, "%s/%s", portpath, op);
	if (rc == -1)
		goto out;
	di_debug("ZFCP_LUN_OP: path=%s\n", path);

	rc = asprintf(&lun, "0x%016" PRIx64, devid->lun);
	if (rc == -1)
		goto out;

	if (misc_write_text_file(path, lun) != RESULT_OK) {
		result = RESULT_ZFCP_INVALID_LUN;
		goto out;
	}
	di_debug("ZFCP_LUN_OP: modified LUN: %s\n", lun);

	result = RESULT_OK;
out:
	free(lun);
	free(path);
	free(portpath);

	return result;
}

result_t zfcp_lun_add(const struct zfcp_lun_devid *devid)
{
	result_t rc;
	char *lunpath;

	lunpath = path_get_zfcp_lun_dev(devid);
	if (lunpath == NULL)
		return RESULT_OUT_OF_MEMORY;

	if (dir_exists(lunpath)) {
		rc = RESULT_OK;
		goto out;
	}

	rc = zfcp_lun_op(devid, "unit_add");
out:
	free(lunpath);
	return rc;
}

result_t scsi_device_remove(const char *hctl)
{
	int rc;
	result_t result;
	char *path, *delete = NULL;

	path = path_get_sys_bus_dev("scsi", hctl);
	if (path == NULL)
		return RESULT_OUT_OF_MEMORY;

	if (!dir_exists(path)) {
		result = RESULT_ZFCP_SCSI_NOT_FOUND;
		goto out;
	}

	rc = asprintf(&delete, "%s/delete", path);
	if (rc == -1) {
		result = RESULT_OUT_OF_MEMORY;
		goto out;
	}

	result = misc_write_text_file(delete, "\n");
out:
	free(delete);
	free(path);

	return result;
}

result_t zfcp_lun_remove(const struct zfcp_lun_devid *devid)
{
	result_t rc;
	char *lunpath;

	lunpath = path_get_zfcp_lun_dev(devid);
	if (lunpath == NULL)
		return RESULT_OUT_OF_MEMORY;

	if (!dir_exists(lunpath)) {
		rc = RESULT_ZFCP_INVALID_LUN;
		goto out;
	}

	rc = zfcp_lun_op(devid, "unit_remove");
out:
	free(lunpath);
	return rc;
}

/* Retrieve CCW device ID of HBA for specified SCSI device path. */
static char *devpath_to_hba_id(const char *path)
{
	char *copy, *start, *end, *hba_id = NULL;

	copy = strdup(path);
	if (copy == NULL)
		return NULL;

	/* copy=/devices/css0/0.0.001c/0.0.1940/host0/... */
	end = strstr(copy, "/host");
	if (end == NULL)
		goto out;
	*end = 0;
	/* copy=/devices/css0/0.0.001c/0.0.1940 */
	start = strrchr(copy, '/');
	if (start == NULL)
		goto out;
	start++;
	hba_id = strdup(start);
out:
	free(copy);

	return hba_id;
}

/* Retrieve WWPN from specified SCSI device path. */
static char *devpath_to_wwpn(const char *devpath)
{
	char *copy, *rport, *end, *path = NULL, *wwpn = NULL;

	/* devpath=/devices/css0/0.0.001c/0.0.1940/host0/rport-0:0-16/
	 *          target0:0:16/0:0:16:1085030433/ */
	copy = strdup(devpath);
	rport = strstr(copy, "/rport-");
	end = skip_comp(rport);
	if (end == NULL)
		goto out;
	*end = 0;

	/* devpath=/devices/css0/0.0.001c/0.0.1940/host0/rport-0:0-16
	 * rport=rport-0:0-16 */
	path = path_get("/sys%s/fc_remote_ports%s/port_name", copy, rport);
	if (path != NULL)
		wwpn = misc_read_text_file(path, 1);
out:
	free(path);
	free(copy);

	return wwpn;
}

static char *lun_to_str(uint64_t lun)
{
	int rc;
	char *fcplun = NULL;

	rc = asprintf(&fcplun, "0x%16" PRIx64, lun);
	if (rc == -1)
		return NULL;
	return fcplun;
}

static char *scsi_htcl_to_zfcp_lun_devpath(const char *hctl)
{
	char *buspath, *link = NULL;

	buspath = path_get_sys_bus_dev("scsi", hctl);
	if (buspath == NULL)
		return NULL;
	link = misc_readlink(buspath);
	free(buspath);

	return link;
}

int zfcp_for_each_scsi_dev(int (*cb)(const char *, const char *, const char *,
				     const char*, void *data), void *data)
{
	int rc, err;
	DIR *devices;
	struct dirent scsi, *dep;
	struct scsi_hctl_devid hctl;
	char *path, *devpath, *hba_id, *wwpn, *lun;

	path = path_get_sys_bus_dev("scsi", NULL);
	if (path == NULL)
		return 0;
	rc = 0;
	devices = opendir(path);
	if (devices == NULL) {
		di_info("Could not open directory: %s: %s\n", path,
			strerror(errno));
		free(path);
		/* There are likely no SCSI devices */
		return 0;
	}

	/*
	 * Scan all devices on the SCSI bus to spot SCSI devices managed by
	 * the zfcp driver.
	 */
	dir_for_each_entry_safe(devices, &scsi, dep, err) {
		if (starts_with(scsi.d_name, "host") ||
		    starts_with(scsi.d_name, "target"))
			continue;
		if (scsi_hctl_parse_devid(&hctl, scsi.d_name))
			continue;

		devpath = scsi_htcl_to_zfcp_lun_devpath(scsi.d_name);
		if (devpath == NULL)
			continue;
		path = strstr(devpath, "/devices/");
		if (path == NULL) {
			free(devpath);
			continue;
		}
		hba_id = devpath_to_hba_id(path);
		wwpn = devpath_to_wwpn(path);
		lun = lun_to_str(scsi_lun_to_fcp_lun(hctl.lun));
		if (hba_id != NULL && wwpn != NULL && lun != NULL)
			rc = cb(scsi.d_name, hba_id, wwpn, lun, data);
		free(lun);
		free(wwpn);
		free(hba_id);
		free(devpath);
		if (rc)
			break;
	}
	closedir(devices);

	return rc;
}