File: dasd_sys.c

package info (click to toggle)
s390-tools 2.40.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,288 kB
  • sloc: ansic: 187,079; sh: 12,157; cpp: 5,049; makefile: 2,812; perl: 2,541; asm: 1,097; python: 697; xml: 29
file content (227 lines) | stat: -rw-r--r-- 4,683 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
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
/*
 * dasd - Library for DASD related functions
 *
 * DASD related helper functions for accessing device information via sysfs
 *
 * Copyright IBM Corp. 2016, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <errno.h>
#include <stdlib.h>

#include "lib/dasd_base.h"
#include "lib/dasd_sys.h"
#include "lib/util_file.h"
#include "lib/util_path.h"
#include "lib/util_sys.h"

/**
 * Get raw-track access mode status
 *
 * The "devnode" parameter can be any valid relative or absolute path to
 * a DASD device node, for example:
 *
 * - /dev/dasda
 * - /dev/disk/by-path/ccw-0.0.bf20
 *
 * @param[in]	devnode		Device node of interest
 *
 * @retval	1		Raw-track access mode is enabled
 * @retval	0		Raw-track access mode is disabled or
 *				cannot be determined
 */
int dasd_sys_raw_track_access(char *devnode)
{
	char busid[DASD_BUS_ID_SIZE];
	char *path;
	FILE *fp;
	int rc;

	if (util_sys_get_dev_addr(devnode, busid) != 0)
		return 0;

	path = util_path_sysfs("bus/ccw/devices/%s/raw_track_access", busid);
	fp = fopen(path, "r");
	if (!fp) {
		free(path);
		return 0;
	}

	rc = fgetc(fp) - '0';
	fclose(fp);
	free(path);

	return (rc == 1) ? 1 : 0;
}

/**
 * Is volume extent space efficient a.k.a. thin-provisioned
 *
 * The "devnode" parameter can be any valid relative or absolute path to
 * a DASD device node, for example:
 *
 * - /dev/dasda
 * - /dev/disk/by-path/ccw-0.0.bf20
 *
 * @param[in]	devnode		Device node of interest
 *
 * @retval	1		Volume is extent space efficient
 * @retval	0		Volume is not extent space efficient or
 *				cannot be determined
 */
int dasd_sys_ese(char *devnode)
{
	char busid[DASD_BUS_ID_SIZE];
	char *path;
	FILE *fp;
	int rc;

	if (util_sys_get_dev_addr(devnode, busid) != 0)
		return 0;

	path = util_path_sysfs("bus/ccw/devices/%s/ese", busid);
	fp = fopen(path, "r");
	if (!fp) {
		free(path);
		return 0;
	}

	rc = fgetc(fp) - '0';
	fclose(fp);
	free(path);

	return (rc == 1) ? 1 : 0;
}

static int dasd_get_pm_from_chpid(char *busid, unsigned int chpid, int *mask)
{
	unsigned int val;
	int count, i;
	char *path;
	FILE *fp;

	path = util_path_sysfs("bus/ccw/devices/%s/../chpids", busid);
	*mask = 0;
	fp = fopen(path, "r");
	if (!fp) {
		free(path);
		return ENODEV;
	}

	for (i = 0; i < 8; i++) {
		count = fscanf(fp, " %x", &val);
		if (count != 1) {
			fclose(fp);
			return EIO;
		}
		if (val == chpid)
			*mask = 0x80 >> i;
	}
	fclose(fp);
	free(path);

	return 0;
}

/**
 * reset chpid
 *
 * The "devnode" parameter can be any valid relative or absolute path to
 * a DASD device node, for example:
 *
 * - /dev/dasda
 * - /dev/disk/by-path/ccw-0.0.bf20
 *
 * @param[in]	devnode		Device node of interest
 * @param[in]	chpid		The chpid to reset
 *                              If NULL all chpids will be reset
 *
 * @return     0 on success, otherwise one of the following error codes:
 *   - EINVAL  No valid chpid specified.
 *   - ENODEV  Could not open device.
 *   - ENOENT  Specified chpid not found.
 *   - EIO     Other I/O error
 *
 */
int dasd_reset_chpid(char *devnode, char *chpid_char)
{
	unsigned int chpid;
	char busid[DASD_BUS_ID_SIZE];
	int  mask, rc;
	char *endptr;
	char *path;
	FILE *fp;

	if (util_sys_get_dev_addr(devnode, busid) != 0)
		return ENODEV;

	if (!chpid_char) {
		path = util_path_sysfs("bus/ccw/devices/%s/path_reset", busid);
		fp = fopen(path, "w");
		if (!fp) {
			free(path);
			return ENODEV;
		}
		fprintf(fp, "%s", "all\n");
		fclose(fp);
		free(path);
		return 0;
	}

	errno = 0;
	chpid = strtoul(chpid_char, &endptr, 16);
	if (errno || (endptr && (*endptr != '\0')))
		return EINVAL;

	rc = dasd_get_pm_from_chpid(busid, chpid, &mask);
	if (rc)
		return rc;
	if (!mask)
		return ENOENT;

	path = util_path_sysfs("bus/ccw/devices/%s/path_reset", busid);
	fp = fopen(path, "w");
	if (!fp) {
		free(path);
		return ENODEV;
	}
	fprintf(fp, "%02x", mask);
	fclose(fp);
	free(path);

	return 0;
}

/**
 * Read amount of host with access to \p device
 *
 * The \p device can be any valid relative or absolute path to a DASD device
 * node, for example:
 *
 * - /dev/dasda
 * - /dev/disk/by-path/ccw-0.0.bf20
 *
 * @param[in]	device	Device node of interest
 *
 * @retval	n	Number of hosts with access to \p device
 * @retval	0	Value could not be determined
 */
int dasd_get_host_access_count(char *device)
{
	char busid[DASD_BUS_ID_SIZE];
	char *path;
	long value;

	if (util_sys_get_dev_addr(device, busid) != 0)
		return 0;

	path = util_path_sysfs("bus/ccw/devices/%s/host_access_count", busid);
	if (util_file_read_l(&value, 10, path))
		value = 0;
	free(path);

	return value;
}