summaryrefslogtreecommitdiff
path: root/extras/multipath-tools/multipath/devinfo.c
blob: 21890621e05654888821f44a7fda0bdf051ffeca (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
234
235
236
237
238
239
240
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sysfs/libsysfs.h>
#include "devinfo.h"
#include "sg_include.h"

#define FILE_NAME_SIZE 255

void
basename(char * str1, char * str2)
{
        char *p = str1 + (strlen(str1) - 1);
 
        while (*--p != '/')
                continue;
        strcpy(str2, ++p);
}

static int
do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
       void *resp, int mx_resp_len, int noisy)
{
        unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
            { INQUIRY_CMD, 0, 0, 0, 0, 0 };
        unsigned char sense_b[SENSE_BUFF_LEN];
        struct sg_io_hdr io_hdr;
                                                                                                                 
        if (cmddt)
                inqCmdBlk[1] |= 2;
        if (evpd)
                inqCmdBlk[1] |= 1;
        inqCmdBlk[2] = (unsigned char) pg_op;
        inqCmdBlk[4] = (unsigned char) mx_resp_len;
        memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
        io_hdr.interface_id = 'S';
        io_hdr.cmd_len = sizeof (inqCmdBlk);
        io_hdr.mx_sb_len = sizeof (sense_b);
        io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
        io_hdr.dxfer_len = mx_resp_len;
        io_hdr.dxferp = resp;
        io_hdr.cmdp = inqCmdBlk;
        io_hdr.sbp = sense_b;
        io_hdr.timeout = DEF_TIMEOUT;
 
        if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
                perror("SG_IO (inquiry) error");
                return -1;
        }
 
        /* treat SG_ERR here to get rid of sg_err.[ch] */
        io_hdr.status &= 0x7e;
        if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
            (0 == io_hdr.driver_status))
                return 0;
        if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
            (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
            (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
                if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
                        int sense_key;
                        unsigned char * sense_buffer = io_hdr.sbp;
                        if (sense_buffer[0] & 0x2)
                                sense_key = sense_buffer[1] & 0xf;
                        else
                                sense_key = sense_buffer[2] & 0xf;
                        if(RECOVERED_ERROR == sense_key)
                                return 0;
                }
        }
        return -1;
}

int
get_serial (char * str, char * devname)
{
	int fd;
        int len;
        char buff[MX_ALLOC_LEN + 1];

	if ((fd = open(devname, O_RDONLY)) < 0)
                return 0;

	if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN, 0)) {
		len = buff[3];
		if (len > 0) {
			memcpy(str, buff + 4, len);
			buff[len] = '\0';
		}
		close(fd);
		return 1;
	}
	close(fd);
        return 0;
}

int
get_lun_strings(char * vendor_id, char * product_id, char * rev, char * devname)
{
        int fd;
        char buff[36];
        char attr_path[FILE_NAME_SIZE];
	char sysfs_path[FILE_NAME_SIZE];
        char basedev[FILE_NAME_SIZE];
                                                                                                                 
	if (0 == sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
                /* sysfs style */
                basename(devname, basedev);
 
                sprintf(attr_path, "%s/block/%s/device/vendor",
                        sysfs_path, basedev);
                if (0 > sysfs_read_attribute_value(attr_path,
                    vendor_id, 8)) return 0;
 
                sprintf(attr_path, "%s/block/%s/device/model",
                        sysfs_path, basedev);
                if (0 > sysfs_read_attribute_value(attr_path,
                    product_id, 16)) return 0;
 
                sprintf(attr_path, "%s/block/%s/device/rev",
                        sysfs_path, basedev);
                if (0 > sysfs_read_attribute_value(attr_path,
                    rev, 4)) return 0;
        } else {
                /* ioctl style */
                if ((fd = open(devname, O_RDONLY)) < 0)
                        return 0;
                if (0 != do_inq(fd, 0, 0, 0, buff, 36, 1))
                        return 0;
                memcpy(vendor_id, &buff[8], 8);
                memcpy(product_id, &buff[16], 16);
                memcpy(rev, &buff[32], 4);
                close(fd);
                return 1;
        }
        return 0;
}

static void
sprint_wwid(char * buff, const char * str)
{
        int i;
        const char *p;
        char *cursor;
        unsigned char c;
                                                                                                                 
        p = str;
        cursor = buff;
        for (i = 0; i <= WWID_SIZE / 2 - 1; i++) {
                c = *p++;
                sprintf(cursor, "%.2x", (int) (unsigned char) c);
                cursor += 2;
        }
        buff[WWID_SIZE - 1] = '\0';
}
                                                                                                                 
/* get EVPD page 0x83 off 8 */
/* tested ok with StorageWorks */
int
get_evpd_wwid(char * devname, char * wwid)
{
        int fd;
        char buff[MX_ALLOC_LEN + 1];
                                                                                                                 
        if ((fd = open(devname, O_RDONLY)) < 0)
                        return 0;

        if (0 == do_inq(fd, 0, 1, 0x83, buff, MX_ALLOC_LEN, 1)) {
                sprint_wwid(wwid, &buff[8]);
                close(fd);
                return 1; /* success */
        }

        close(fd);
        return 0; /* not good */
}

long
get_disk_size (char * devname) {
        long size;
        int fd;
        char attr_path[FILE_NAME_SIZE];
        char sysfs_path[FILE_NAME_SIZE];
        char buff[FILE_NAME_SIZE];
        char basedev[FILE_NAME_SIZE];
                                                                                                                 
        if (0 == sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
                basename(devname, basedev);
                sprintf(attr_path, "%s/block/%s/size",
                        sysfs_path, basedev);
                if (0 > sysfs_read_attribute_value(attr_path, buff,
                                         FILE_NAME_SIZE * sizeof(char)))
                        return -1;
                size = atoi(buff);
                return size;
        } else {
                if ((fd = open(devname, O_RDONLY)) < 0)
                        return -1;
                if(!ioctl(fd, BLKGETSIZE, &size))
                        return size;
        }
        return -1;
}

int
do_tur(char *dev)
{
	unsigned char turCmdBlk[TUR_CMD_LEN] = { 0x00, 0, 0, 0, 0, 0 };
	struct sg_io_hdr io_hdr;
	unsigned char sense_buffer[32];
	int fd;

	fd = open(dev, O_RDONLY);
	
	if (fd < 0)
		return 0;

	memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
	io_hdr.interface_id = 'S';
	io_hdr.cmd_len = sizeof (turCmdBlk);
	io_hdr.mx_sb_len = sizeof (sense_buffer);
	io_hdr.dxfer_direction = SG_DXFER_NONE;
	io_hdr.cmdp = turCmdBlk;
	io_hdr.sbp = sense_buffer;
	io_hdr.timeout = 20000;
	io_hdr.pack_id = 0;

	if (ioctl(fd, SG_IO, &io_hdr) < 0) {
		close(fd);
		return 0;
	}

	close(fd);
	
	if (io_hdr.info & SG_INFO_OK_MASK)
		return 0;

	return 1;
}