summaryrefslogtreecommitdiffstats
path: root/isys/dasd.c
blob: 6ad98183031d55e48f7f3b4361362f285bc2030f (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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>

#include "isys.h"

#if defined(__s390__) || defined(__s390x__)
#define u8 __u8
#define u16 __u16
#define u32 __u32
#define u64 __u64
#include <asm/vtoc.h>
#include <asm/dasd.h>
typedef struct vtoc_volume_label volume_label_t;
#endif


#if defined(__s390__) || defined(__s390x__)
/* s390 stuff to detect DASDs */
static int read_vlabel(dasd_information_t *dasd_info, int fd, int blksize,
                       volume_label_t *vlabel) {
	int rc;
	unsigned long vlabel_start = dasd_info->label_block * blksize;

	memset(vlabel, 0, sizeof(volume_label_t));

	if (lseek(fd, vlabel_start, SEEK_SET) < 0) {
		/* fprintf(stderr, "Could not read volume label.\n"); */
		return 2;
	}

	rc = read(fd, vlabel, sizeof(volume_label_t));
	if (rc != sizeof(volume_label_t)) {
		/* fprintf(stderr, "Could not read volume label, DASD is probably unformatted\n"); */
		return 1;
	}

	return 0;
}
#endif

int isUsableDasd(char *device) {
#if !defined(__s390__) && !defined(__s390x__)
    return 0;
#else
	char devname[16];
	char label[5], v4_hex[9];
	char l4ebcdic_hex[] = "d3d5e7f1";  /* LNX1 */
	char cms1_hex[] = "c3d4e2f1";      /* CMS1 */
	int f, ret, blksize;
	dasd_information_t dasd_info;
	volume_label_t vlabel;
	memset(&dasd_info, 0, sizeof(dasd_info));
	strcpy(devname, "/dev/");
	strcat(devname, device);
	devMakeInode(device, devname);
	if((f = open(devname, O_RDONLY)) == -1) {
		return 0;
	}
	if (ioctl(f, BLKSSZGET, &blksize) != 0) {
		close(f);
		/* fprintf(stderr, "Could not retrieve blocksize information!\n"); */
		return 0;
	}
	if (ioctl(f, BIODASDINFO, &dasd_info) != 0) {
		close(f);
		/* fprintf(stderr, "Could not retrieve disk information!\n"); */
		return 0;
	}
	ret = read_vlabel(&dasd_info, f, blksize, &vlabel);
        close(f);

	if (ret == 2) {
		return 0;
	} else if (ret == 1) { /* probably unformatted DASD */
		/* fprintf(stderr, "Found a usable device: %s\n", devname); */
		return 1;
	}
	memset(label, 0, 5);
	memset(v4_hex, 0, 9);
	strncpy(label, vlabel.volkey, 4);
	sprintf(v4_hex, "%02x%02x%02x%02x", label[0], label[1], label[2], label[3]);
        
	if(!strncmp(v4_hex, cms1_hex, 9)) {
		return 0;
	}
	if(!strncmp(v4_hex, l4ebcdic_hex, 9)) {
		return 2;
	}
        return 1;
#endif
}

int isLdlDasd(char * device) {
   return (isUsableDasd(device) == 2);
}

char *getDasdPorts() {
#if !defined(__s390__) && !defined(__s390x__)
    return 0;
#else
        char * line, *ports = NULL;
	char devname[7];
        char port[10];
        FILE *fd;
	int ret;
        fd = fopen ("/proc/dasd/devices", "r");
        if(!fd) {
                return NULL;
        }
        line = (char *)malloc(100*sizeof(char));
        while (fgets (line, 100, fd) != NULL) {
                if ((strstr(line, "unknown") != NULL)) {
                        continue;
                }
                if (strstr(line, "(FBA )") != NULL) {
                        ret = sscanf (line, "%[A-Za-z.0-9](FBA ) at ( %*d: %*d) is %s : %*s", port, devname);
                } else {
                        ret = sscanf (line, "%[A-Za-z.0-9](ECKD) at ( %*d: %*d) is %s : %*s", port, devname);
                }
		if (ret == 2) {
			if(!ports) {
				ports = (char *)malloc(strlen(port) + 1);
				strcpy(ports, port);
			} else {
				ports = (char *)realloc(ports, strlen(ports) + strlen(port) + 2);
				strcat(ports, ",");
				strcat(ports, port);
			}
                }
        }
        if (fd) fclose(fd);
        return ports;
#endif
}