File: disks.c

package info (click to toggle)
cpudyn 1.0-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 184 kB
  • ctags: 118
  • sloc: ansic: 1,114; sh: 203; makefile: 122
file content (393 lines) | stat: -rw-r--r-- 8,691 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* disks.c (cpudyn) - Functions to put a disk in stand by            */
/*          - by Ricardo Galli (C) 2003      -- Licence GPL          */
/* Some code extracted from hdparm v 5.3 by Mark Lord                */

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <endian.h>
#include <sys/ioctl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <linux/types.h>
#include <linux/hdreg.h>
#include <linux/major.h>
#include <asm/byteorder.h>
#include <fcntl.h>


#include "cpudynd.h"
#include "disks.h"


static device_t *devices;
static get_opts_t get_device_t_ops;
static char *proc_stat_dir = NULL;

extern unsigned standby_timeout;

int is_active(device_t *dev)
{
	unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
	unsigned state;

	if (ioctl(dev->fd, HDIO_DRIVE_CMD, &args)
		&& (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
		&& ioctl(dev->fd, HDIO_DRIVE_CMD, &args)) {
		if (errno != EIO || args[0] != 0 || args[1] != 0)
			state = 1;
		else {
			state = 0; // it's sleeping
   			errprintf("Drive state is:  sleeping\n");
		}
	} else {
		state = (args[2] == 255) ? 1 : 0; // 1=active, 0=standby
   		errprintf("Drive state is:  %s\n", state==1 ? "active" : "standby");
	}
	return state;
}
		
int flush_buffer_cache (device_t *dev)
{
	int res;
	res = fsync (dev->fd);				  /* flush buffers */
	if (ioctl(dev->fd, BLKFLSBUF, NULL)) { /* do it again, big time */
		errprintf("BLKFLSBUF failed");
		return -1;
	}
	if (ioctl(dev->fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
		            errprintf("HDIO_DRIVE_CMD(null) (wait for flush complete) failed\n");
	dev->flushed = time(NULL);
	errprintf("Buffer cache flushed\n");
	return 0;
}


int get_dev (char *devname)
{
	int fd;
	static long parm, multcount;
	struct stat stat_buf;
	if (stat(devname,&stat_buf)) {
		perror(devname);
		return -1;
	}

	switch(major(stat_buf.st_rdev)) {
#ifdef SCSI_DISK0_MAJOR
	case (SCSI_DISK0_MAJOR):
	case (SCSI_DISK1_MAJOR):
	case (SCSI_DISK2_MAJOR):
	case (SCSI_DISK3_MAJOR):
	case (SCSI_DISK4_MAJOR):
	case (SCSI_DISK5_MAJOR):
	case (SCSI_DISK6_MAJOR):
	case (SCSI_DISK7_MAJOR):
#else
	case (SCSI_DISK_MAJOR):
#endif
#ifdef MD_MAJOR
	case (MD_MAJOR):
#endif
	case (VXVM_MAJOR):
#ifdef LVM_BLK_MAJOR
	case (LVM_BLK_MAJOR):
#endif
		return -1;
		break;
#ifdef XT_DISK_MAJOR
	case (XT_DISK_MAJOR):
#endif
	case IDE0_MAJOR:
	case IDE1_MAJOR:
#ifdef IDE2_MAJOR
	case IDE2_MAJOR:
#endif
#ifdef IDE3_MAJOR
	case IDE3_MAJOR:
#endif
#ifdef IDE4_MAJOR
	case IDE4_MAJOR:
#endif
#ifdef IDE5_MAJOR
	case IDE5_MAJOR:
#endif
#ifdef IDE6_MAJOR
	case IDE6_MAJOR:
#endif
#ifdef IDE7_MAJOR
	case IDE7_MAJOR:
#endif
#ifdef IDE8_MAJOR
	case IDE8_MAJOR:
#endif
#ifdef IDE9_MAJOR
	case IDE9_MAJOR:
#endif
		// Do nothing
		break;
	default:
		return -1;
		break;
	}
	fd = open (devname, O_RDONLY|O_NONBLOCK);
	if (fd < 0) {
		errprintf("Error opening %d\n", devname);
		return -1;
	}
	return fd;
}

int send_standby(device_t *dev)
{

#ifndef WIN_STANDBYNOW1
#define WIN_STANDBYNOW1 0xE0
#endif
#ifndef WIN_STANDBYNOW2
#define WIN_STANDBYNOW2 0x94
#endif
	unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
	unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};

	errprintf("------Issuing standby command\n");
	if (ioctl(dev->fd, HDIO_DRIVE_CMD, &args1)
	 && ioctl(dev->fd, HDIO_DRIVE_CMD, &args2)) {
		errprintf(" HDIO_DRIVE_CMD(standby) failed");
		return -1;
	}
	dev->sleeping = 1;
	return 0;
}



void add_device_t(char *dev, int fd)
{
//	device_t *ptr = devices, *prev = NULL;
	device_t *new;
	struct stat stat_buf;
	char *tmp;
				

	new = malloc(sizeof(device_t));
	if (!new) 
		die(FALSE, "Couldn't malloc\n");
	new->dev = malloc(strlen(dev) + 1);
	if (!new->dev) 
		die(FALSE, "Couldn't malloc\n");
	strcpy(new->dev, dev);
	if(proc_stat_dir != NULL) {
		// Linux 2.5, prepare for /sys/block/dev
		tmp = dev + (strlen(dev) -1);
		while(*tmp != '/' && tmp > dev) tmp--;
		if (*tmp == '/') tmp++;
		new->sys_stat = malloc(strlen(proc_stat_dir) + strlen(tmp) + 8);
		if (!new->sys_stat) 
			die(FALSE, "Couldn't malloc\n");
		sprintf(new->sys_stat, "%s/%s/stat", proc_stat_dir, tmp);
		errprintf("The proc device is %s\n", new->sys_stat);
	} else {
		// Linux 2.4, we don't need it
		new->sys_stat = NULL;
	}
	new->fd = fd;
	stat(dev,&stat_buf);
	new->major = major(stat_buf.st_rdev);
	new->minor = minor(stat_buf.st_rdev);
	new->ops = 0;
	new->timestamp = time(NULL);
	new->sleeping = 0;
	new->flushed = 0;
	new->next = devices;
	devices = new;
}


int standby_init(unsigned timeout, char *devnames)
{
	char *dev_s = NULL;
	char devs[1024];
	int fd;
	int count = 0;

	strncpy(devs, devnames, 1023);
	dev_s = strtok(devs, ", ");

	if( (get_device_t_ops = get_device_t_ops_version()) == NULL) {
			errprintf("/proc/stat | /sys/block | /sysfs are not available in the kernel\n");
			return 0;
	}
	while ( dev_s != NULL) {
		fd = get_dev(dev_s);
		if (fd >= 0 ) {
			errprintf("Addind device %s (%d)\n", dev_s, fd);
			count ++;
			add_device_t(dev_s, fd);
		} else {
			errprintf("Device %s doesn't allow standby\n", dev_s);
		}
		dev_s = strtok(NULL, ", ");
	}
	errprintf("Devices for standby: %d\n", count);
	return count;
}

void check_standby()
{
	int count = 0;
	device_t *ptr = devices;
	unsigned ops;
	time_t	now;
	int res;
	unsigned elapsed;

	now = time(NULL);
	while (ptr != NULL) {
		ops = get_device_t_ops(ptr, count);
		count++;
		if ( ops >= 0) {
			if ( ops - ptr->ops  == 0 ) {
				if (ptr->sleeping) {
					// Do nothing for this disk
					ptr = ptr->next;
					continue;
				}
				elapsed = now - ptr->timestamp;
				if( elapsed > standby_timeout) {
					if (is_active(ptr)) {
						if (ptr->flushed == 0) 
							flush_buffer_cache(ptr);
						res = send_standby(ptr);
						if( res < 0) {
							errprintf("Error in standby %s\n", ptr->dev);
							ptr->timestamp = now;
						}
					} else {
						 ptr->sleeping = 1;
					}
					ptr->flushed = 0;
				} else if ( elapsed > (standby_timeout - TIME_TO_FLUSH) 
						&& ptr->flushed == 0 && is_active(ptr))  {
					// We flush in advance before trying to sleep
					flush_buffer_cache(ptr);
				}
			} else {
				if (ptr->flushed > 0 && now - ptr->flushed < TIME_TO_FLUSH) {
					errprintf("Forgiving, it's flushing\n");
				} else {
					ptr->flushed = 0;
					ptr->timestamp = now;
				}
				ptr->sleeping = 0;
				ptr->ops = ops;
			}
		}
		ptr = ptr->next;
	}

}

unsigned get_device_t_ops_24(device_t *dev, int count)
{
	static char *dev_line = NULL;
	static char major_minor[64];
	FILE *fp;
	char *str = NULL; 
	unsigned ops;

	if(dev_line == NULL) 
		dev_line = malloc(1024);
	
	if (!dev) 
		return 0;
	if (count == 0) {
		fp = fopen("/proc/stat", "r");
		while ( fp && (str=fgets(dev_line, 1024, fp)) ) {
			if (strncmp(dev_line, "disk_io:", 8) == 0)
				break;
		}
		fclose(fp);
		if (str == NULL) {
			errprintf("/proc/stat not available for disk_io\n");
			return -1;
		}
	}
	sprintf(major_minor, "(%u,%u):", dev->major, dev->minor);
	str = strstr(dev_line, major_minor);
	if(str != NULL) {
		str = str + strlen(major_minor);
		//"(%u,%u):(%u,%u,%u,%u,%u) "
		if(sscanf(str, "(%u,%*u,%*u,%*u,%*u)", &ops) == 1) {
			errprintf("%s %u operations\n", dev->dev, ops);
			return ops;
		}
	}
	return 0;

}

unsigned get_device_t_ops_25(device_t *dev, int count)
{
	static char *dev_line = NULL;
	static char major_minor[64];
	int fd, bytes;
	unsigned long long in, out;

	if(dev_line == NULL) 
		dev_line = malloc(1024);
	
	if (!dev) 
		return 0;
	fd = open(dev->sys_stat, O_RDONLY);
	if (fd < 0 || (bytes = read(fd, dev_line, 1024)) <= 0) {
		errprintf("%s not available for disk_io\n", dev->sys_stat);
		close(fd);
		return -1;
	}
	close(fd);
	dev_line[bytes] = 0;
		//"%u %u %u %u %u %u %u %u %u %u %u"
		//"%8u %8u %8llu %8u "
		//"%8u %8u %8llu %8u "
		//"%8u %8u %8u"
	if(sscanf(dev_line, "%*u %*u %llu %*u %*u %*u %llu %*u %*u %*u %*u", &in, &out) == 2) {
		errprintf("%s %u operations\n", dev->dev, in+out);
		return (unsigned) (in+out) ;
	}
	return 0;
}


get_opts_t get_device_t_ops_version()
{
	struct stat stat_buf;

	// Linux >2.5
	if (stat("/sys/block",&stat_buf) == 0) {
		errprintf("Detected kernel 2.5\n");
		proc_stat_dir = "/sys/block";
		return get_device_t_ops_25;
	}
	// Linux >2.5
	if (stat("/sysfs/block",&stat_buf) == 0) {
		errprintf("Detected kernel 2.5\n");
		proc_stat_dir = "/sysfs/block";
		return get_device_t_ops_25;
	}
	// Linux 2.4
	if (stat("/proc/stat",&stat_buf) == 0) {
		errprintf("Detected kernel 2.4\n");
		return get_device_t_ops_24;
	}
	return NULL;
}