summaryrefslogtreecommitdiffstats
path: root/utils/blkmapd/device-inq.c
blob: eabc70c3ebc1aa45c5deb5b980eca93751cbaa1d (plain)
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
/*
 * device-inq.c: inquire SCSI device information.
 *
 * Copyright (c) 2010 EMC Corporation, Haiying Tang <Tang_Haiying@emc.com>
 * All rights reserved.
 *
 * This program refers to "SCSI Primary Commands - 3 (SPC-3)
 * at http://www.t10.org and sg_inq.c in sg3_utils-1.26 for
 * Linux OS SCSI subsystem, by D. Gilbert.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/select.h>
#include <scsi/scsi.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/sg.h>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <dirent.h>
#include <ctype.h>
#include <fcntl.h>
#include <libgen.h>
#include <errno.h>

#include "device-discovery.h"

#define DEF_ALLOC_LEN	255
#define MX_ALLOC_LEN	(0xc000 + 0x80)

static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
{
	struct bl_serial *s;

	s = malloc(sizeof(*s) + len);
	if (s) {
		s->data = (char *)&s[1];
		s->len = len;
		memcpy(s->data, bytes, len);
	}
	return s;
}

static void bl_free_scsi_string(struct bl_serial *str)
{
	if (str)
		free(str);
}

#define sg_io_ok(io_hdr) \
	((((io_hdr).status & 0x7e) == 0) && \
	((io_hdr).host_status == 0) && \
	(((io_hdr).driver_status & 0x0f) == 0))

static int sg_timeout = 1 * 1000;

static int bldev_inquire_page(int fd, int page, char *buffer, int len)
{
	unsigned char cmd[] = { INQUIRY, 0, 0, 0, 0, 0 };
	unsigned char sense_b[28];
	struct sg_io_hdr io_hdr;
	if (page >= 0) {
		cmd[1] = 1;
		cmd[2] = page;
	}
	cmd[3] = (unsigned char)((len >> 8) & 0xff);
	cmd[4] = (unsigned char)(len & 0xff);

	memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
	io_hdr.interface_id = 'S';
	io_hdr.cmd_len = sizeof(cmd);
	io_hdr.mx_sb_len = sizeof(sense_b);
	io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
	io_hdr.dxfer_len = len;
	io_hdr.dxferp = buffer;
	io_hdr.cmdp = cmd;
	io_hdr.sbp = sense_b;
	io_hdr.timeout = sg_timeout;
	if (ioctl(fd, SG_IO, &io_hdr) < 0)
		return -1;

	if (sg_io_ok(io_hdr))
		return 0;
	return -1;
}

static int bldev_inquire_pages(int fd, int page, char **buffer)
{
	int status = 0;
	char *tmp;
	int len;

	*buffer = calloc(DEF_ALLOC_LEN, sizeof(char));
	if (!*buffer) {
		BL_LOG_ERR("%s: Out of memory!\n", __func__);
		return -ENOMEM;
	}

	status = bldev_inquire_page(fd, page, *buffer, DEF_ALLOC_LEN);
	if (status)
		goto out;

	status = -1;
	if ((*(*buffer + 1) & 0xff) != page)
		goto out;

	len = (*(*buffer + 2) << 8) + *(*buffer + 3) + 4;
	if (len > MX_ALLOC_LEN) {
		BL_LOG_ERR("SCSI response length too long: %d\n", len);
		goto out;
	}
	if (len > DEF_ALLOC_LEN) {
		tmp = realloc(*buffer, len);
		if (!tmp) {
			BL_LOG_ERR("%s: Out of memory!\n", __func__);
			status = -ENOMEM;
			goto out;
		}
		*buffer = tmp;
		status = bldev_inquire_page(fd, page, *buffer, len);
		if (status)
			goto out;
	}
	status = 0;
 out:
	return status;
}

/* For EMC multipath devices, use VPD page (0xc0) to get status.
 * For other devices, return ACTIVE for now
 */
extern enum bl_path_state_e bldev_read_ap_state(int fd)
{
	int status = 0;
	char *buffer = NULL;
	enum bl_path_state_e ap_state = BL_PATH_STATE_ACTIVE;

	status = bldev_inquire_pages(fd, 0xc0, &buffer);
	if (status)
		goto out;

	if (buffer[4] < 0x02)
		ap_state = BL_PATH_STATE_PASSIVE;
 out:
	if (buffer)
		free(buffer);
	return ap_state;
}

struct bl_serial *bldev_read_serial(int fd, const char *filename)
{
	struct bl_serial *serial_out = NULL;
	int status = 0;
	char *buffer;
	struct bl_dev_id *dev_root, *dev_id;
	unsigned int pos, len, current_id = 0;

	status = bldev_inquire_pages(fd, 0x83, &buffer);
	if (status)
		goto out;

	dev_root = (struct bl_dev_id *)buffer;

	pos = 0;
	current_id = 0;
	len = dev_root->len;
	while (pos < (len - sizeof(struct bl_dev_id) + sizeof(unsigned char))) {
		dev_id = (struct bl_dev_id *)&(dev_root->data[pos]);
		if ((dev_id->ids & 0xf) < current_id)
			continue;
		switch (dev_id->ids & 0xf) {
			/* We process SCSI ID with four ID cases: 0, 1, 2 and 3.
			 * When more than one ID is available, priority is
			 * 3>2>1>0.
			 */
		case 2:	/* EUI-64 based */
			if ((dev_id->len != 8) && (dev_id->len != 12) &&
			    (dev_id->len != 16))
				break;
		case 3:	/* NAA */
			/* TODO: NAA validity judgement too complicated,
			 * so just ingore it here.
			 */
			if ((dev_id->type & 0xf) != 1) {
				BL_LOG_ERR("Binary code_set expected\n");
				break;
			}
		case 0:	/* vendor specific */
		case 1:	/* T10 vendor identification */
			current_id = dev_id->ids & 0xf;
			if (serial_out)
				bl_free_scsi_string(serial_out);
			serial_out = bl_create_scsi_string(dev_id->len,
							   dev_id->data);
			break;
		}
		if (current_id == 3)
			break;
		pos += (dev_id->len + sizeof(struct bl_dev_id) -
			sizeof(unsigned char));
	}
 out:
	if (!serial_out)
		serial_out = bl_create_scsi_string(strlen(filename), filename);
	if (buffer)
		free(buffer);
	return serial_out;
}