File: libsg.c

package info (click to toggle)
multipath-tools 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 1,800 kB
  • sloc: ansic: 26,772; sh: 844; makefile: 374
file content (97 lines) | stat: -rw-r--r-- 2,304 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2004, 2005 Christophe Varoqui
 */
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/stat.h>

#include "checkers.h"
#include "libsg.h"
#include "../libmultipath/sg_include.h"

int
sg_read (int sg_fd, unsigned char * buff, int buff_len,
	 unsigned char * sense, int sense_len, unsigned int timeout)
{
	/* defaults */
	int blocks;
	long long start_block = 0;
	int bs = 512;
	int cdbsz = 10;
	int * diop = NULL;

	unsigned char rdCmd[cdbsz];
	unsigned char *sbb = sense;
	struct sg_io_hdr io_hdr;
	int res;
	int rd_opcode[] = {0x8, 0x28, 0xa8, 0x88};
	int sz_ind;
	struct stat filestatus;
	int retry_count = 3;

	if (fstat(sg_fd, &filestatus) != 0)
		return PATH_DOWN;
	bs = (filestatus.st_blksize > 4096)? 4096: filestatus.st_blksize;
	blocks = buff_len / bs;
	memset(rdCmd, 0, cdbsz);
	sz_ind = 1;
	rdCmd[0] = rd_opcode[sz_ind];
	rdCmd[2] = (unsigned char)((start_block >> 24) & 0xff);
	rdCmd[3] = (unsigned char)((start_block >> 16) & 0xff);
	rdCmd[4] = (unsigned char)((start_block >> 8) & 0xff);
	rdCmd[5] = (unsigned char)(start_block & 0xff);
	rdCmd[7] = (unsigned char)((blocks >> 8) & 0xff);
	rdCmd[8] = (unsigned char)(blocks & 0xff);

	memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
	io_hdr.interface_id = 'S';
	io_hdr.cmd_len = cdbsz;
	io_hdr.cmdp = rdCmd;
	io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
	io_hdr.dxfer_len = bs * blocks;
	io_hdr.dxferp = buff;
	io_hdr.mx_sb_len = sense_len;
	io_hdr.sbp = sense;
	io_hdr.timeout = timeout * 1000;
	io_hdr.pack_id = (int)start_block;
	if (diop && *diop)
	io_hdr.flags |= SG_FLAG_DIRECT_IO;

retry:
	memset(sense, 0, sense_len);
	while (((res = ioctl(sg_fd, SG_IO, &io_hdr)) < 0) && (EINTR == errno));

	if (res < 0) {
		if (ENOMEM == errno) {
			return PATH_UP;
		}
		return PATH_DOWN;
	}

	if ((0 == io_hdr.status) &&
	    (0 == io_hdr.host_status) &&
	    (0 == io_hdr.driver_status)) {
		return PATH_UP;
	} else {
		int key = 0;

		if (io_hdr.sb_len_wr > 3) {
			if (sbb[0] == 0x72 || sbb[0] == 0x73)
				key = sbb[1] & 0x0f;
			else if (io_hdr.sb_len_wr > 13 &&
				 ((sbb[0] & 0x7f) == 0x70 ||
				  (sbb[0] & 0x7f) == 0x71))
				key = sbb[2] & 0x0f;
		}

		/*
		 * Retry if UNIT_ATTENTION check condition.
		 */
		if (key == 0x6) {
			if (--retry_count)
				goto retry;
		}
		return PATH_DOWN;
	}
}