2 * Copyright (C) 2004 Jens Axboe <axboe@suse.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
23 #if BITS_PER_LONG == 32
24 typedef unsigned long long u64;
25 typedef unsigned long u32;
26 #elif BITS_PER_LONG == 64
27 typedef unsigned long u64;
28 typedef unsigned int u32;
32 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
38 #define ALIGN(buf) (((unsigned long) (buf) + MASK) & ~(MASK))
40 #define BITMAP_BLOCKS(blocks) (((blocks) / BITS_PER_LONG) + 1)
41 #define BITMAP_SIZE(blocks) (BITMAP_BLOCKS(blocks) * sizeof(unsigned long))
43 #define RANDOM_COVERAGE (90)
45 static char device[256];
46 static char state_file[256];
47 static int progress_interval;
48 static int block_size;
49 static int restore_state = 1;
50 static struct timeval start;
51 static int sequential;
52 static int disk_util = 100;
53 static int disk_util_set;
54 static int state_file_fd = -1;
55 static int state_restored;
57 static unsigned long *read_bitmap;
58 static unsigned long find_next_bit_index;
59 static unsigned long blocks_read, blocks_read_now;
62 static struct drand48_data random_state;
70 #define DRIVESCAN_MAGIC 0xabc0ffee
71 #define DRIVESCAN_VERSION 2
73 struct progress_state {
80 struct drand48_data random_state;
83 inline int test_block_read(unsigned long block)
85 unsigned long index, bitnr;
87 index = block / BITS_PER_LONG;
88 bitnr = block & (BITS_PER_LONG - 1);
89 return (read_bitmap[index] & (1UL << bitnr)) != 0;
92 inline int mark_block_read(unsigned long block)
94 unsigned long index, bitnr, retval;
96 index = block / BITS_PER_LONG;
97 bitnr = block & (BITS_PER_LONG - 1);
99 retval = (read_bitmap[index] & (1UL << bitnr)) != 0;
100 read_bitmap[index] |= 1UL << bitnr;
104 inline unsigned long find_next_unread_block(void)
106 unsigned int i, bitnr;
108 for (i = find_next_bit_index; i < BITMAP_BLOCKS(dev_size); i++) {
109 if (read_bitmap[i] == ~0UL)
117 find_next_bit_index = i;
118 bitnr = ffz(read_bitmap[i]);
119 read_bitmap[i] |= 1UL << bitnr;
120 return bitnr + BITS_PER_LONG * i;
123 int load_progress_state(void)
125 struct progress_state state;
129 if (state_file_fd == -1)
132 if (lseek(state_file_fd, 0, SEEK_SET) == -1) {
133 perror("state file seek");
137 ret = read(state_file_fd, &state, sizeof(state));
139 perror("load progress file");
144 if (state.magic != DRIVESCAN_MAGIC) {
145 printf("Bad magic %lx\n", (unsigned long) state.magic);
148 if (state.version != DRIVESCAN_VERSION) {
149 printf("Unsupported file version (%d)\n", (int) state.version);
152 if (state.arch != ARCH) {
153 printf("State file from different arch\n");
157 read_bitmap = malloc(BITMAP_SIZE(state.blocks));
159 printf("Unable to allocate bitmap (blocks=%Lu)\n", (unsigned long long) state.blocks);
162 memset(read_bitmap, 0, BITMAP_SIZE(dev_size));
164 if (read(state_file_fd, (char *) read_bitmap, BITMAP_SIZE(state.blocks)) < BITMAP_SIZE(state.blocks)) {
165 printf("Unable to read bitmap\n");
170 for (i = 0; i < state.blocks; i++)
171 if (test_block_read(i))
174 if (blocks_read == dev_size)
177 memcpy(&random_state, &state.random_state, sizeof(random_state));
179 dev_size = state.blocks;
180 block_size = state.bs;
182 disk_util = state.disk_util;
183 printf("Restored state: %lu blocks\n", blocks_read);
188 void save_progress_state(void)
190 struct progress_state state;
193 if (ftruncate(state_file_fd, 0) == -1) {
194 perror("truncate state file");
197 if (lseek(state_file_fd, 0, SEEK_SET) == -1) {
198 perror("lseek state file");
202 memset(&state, 0, sizeof(state));
203 state.magic = DRIVESCAN_MAGIC;
205 state.version = DRIVESCAN_VERSION;
206 state.bs = block_size;
207 state.blocks = dev_size;
208 state.disk_util = disk_util;
209 memcpy(&state.random_state, &random_state, sizeof(random_state));
211 ret = write(state_file_fd, &state, sizeof(state));
213 perror("write state file");
217 ret = write(state_file_fd, (char *) read_bitmap, BITMAP_SIZE(dev_size));
219 perror("write state bitmap");
223 fsync(state_file_fd);
224 printf("Saved state: %lu blocks\n", blocks_read);
227 unsigned long utime_since(struct timeval *s)
232 gettimeofday(&now, NULL);
233 sec = now.tv_sec - s->tv_sec;
234 usec = now.tv_usec - s->tv_usec;
235 if (sec > 0 && usec < 0) {
239 return usec + (sec * (double) 1000000);
242 unsigned long time_since_now(void)
244 double sec, usec, ret;
247 gettimeofday(&now, NULL);
248 sec = now.tv_sec - start.tv_sec;
249 usec = now.tv_usec - start.tv_usec;
250 if (sec > 0 && usec < 0) {
254 ret = sec + usec / (double) 1000000;
258 return (unsigned long) ret;
261 void normalize_time(struct our_time *ot, unsigned long seconds)
263 ot->hour = seconds / 3600;
264 seconds -= ot->hour * 3600;
265 ot->min = seconds / 60;
266 seconds -= ot->min * 60;
270 void show_progress_state(void)
272 unsigned long rate, now;
273 struct our_time comp, left;
275 now = time_since_now();
277 normalize_time(&comp, now);
282 rate = blocks_read_now / now;
284 memset(&left, 0, sizeof(left));
286 normalize_time(&left, (dev_size * now / blocks_read_now) - now);
289 printf("\tRun: [%luh%lum%lusec, %luh%lum%lusec], %lu blocks/sec, %lu KiB/sec\n", comp.hour, comp.min, comp.sec, left.hour, left.min, left.sec, rate, rate * block_size / 1024);
290 printf("\tRead:[%lu, %lu]: %lu%% coverage\n", blocks_read, (unsigned long) dev_size, (unsigned long) (blocks_read * 100 / dev_size));
293 void sig_handler(int sig)
295 show_progress_state();
300 save_progress_state();
303 if (progress_interval)
304 alarm(progress_interval);
309 unsigned long get_next_offset(unsigned long blocks)
311 unsigned long b, retries = dev_size;
317 lrand48_r(&random_state, &r);
318 b = (1+(double) (blocks-1) * r / (RAND_MAX+1.0));
320 if (!mark_block_read(b))
326 printf("Gave up finding new block\n");
332 b = find_next_unread_block();
340 void idle_drive(struct timeval *s)
342 unsigned long msleep, sleep_time;
345 if (disk_util == 100)
348 msleep = utime_since(s);
349 ratio = (double) 100 / disk_util;
350 sleep_time = msleep * ratio - msleep;
354 void read_error_report(int fd, char *buffer, unsigned long long offset)
356 unsigned long long bad_block = offset >> 9;
357 int blocks = block_size / 512;
360 if (lseek(fd, offset, SEEK_SET) == -1) {
365 for (i = 0; i < blocks; i++) {
366 ret = read(fd, buffer, 512);
370 else if (ret < 512 && ret >= 0) {
371 printf("Short read at sector %Lu\n", bad_block + i);
374 if (errno == EINVAL) {
375 printf("512-b O_DIRECT does not work\n");
379 printf("Sector %Lu bad\n", bad_block + i);
383 void do_random_reads(int fd, unsigned long blocks)
389 ptr = malloc(block_size + MASK + 1);
390 buffer = (char *) ALIGN(ptr);
393 off_t offset = get_next_offset(blocks);
396 offset = get_next_offset(blocks);
400 offset = offset * block_size;
402 if (lseek(fd, offset, SEEK_SET) == -1) {
407 gettimeofday(&s, NULL);
408 ret = read(fd, buffer, block_size);
411 if (errno == EINVAL) {
412 printf("EINVAL on read, check if O_DIRECT works\n");
414 } else if (errno == EIO) {
415 read_error_report(fd, buffer, offset);
421 if (ret < block_size)
422 read_error_report(fd, buffer, offset);
424 coverage = blocks_read * 100 / dev_size;
425 if (!sequential && coverage >= RANDOM_COVERAGE) {
426 printf("Switching to scan for rest of blocks\n");
430 if (blocks_read == dev_size)
437 show_progress_state();
442 int seed_randomizer(void)
447 fd = open("/dev/random", O_RDONLY);
453 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
460 printf("Randomizer seed: %lx\n", seed);
461 srand48_r(seed, &random_state);
465 void device_to_state_file(void)
467 unsigned int i, offset;
469 offset = sprintf(state_file, "scanstate");
471 for (i = 0; i < strlen(device); i++) {
472 if (device[i] == '/')
473 state_file[offset+i] = '_';
475 state_file[offset+i] = device[i];
479 void init_state_file(void)
482 device_to_state_file();
484 state_file_fd = open(state_file, O_RDWR | O_CREAT, 0644);
486 if (state_file_fd == -1) {
487 perror("open state file");
492 int get_options(int argc, char *argv[])
494 static struct option longoptions[] = {
495 { "device", 1, NULL, 'd' },
496 { "block-size", 1, NULL, 'b' },
497 { "progress", 1, NULL, 'p' },
498 { "file", 1, NULL, 'f' },
499 { "restore", 1, NULL, 'r' },
500 { "utilization",1, NULL, 'i' },
504 while ((c = getopt_long(argc, argv, "d:b:p:f:ru:", longoptions, &res)) != -1) {
507 strncpy(device, optarg, sizeof(device) - 1);
510 block_size = atoi(optarg);
511 if (block_size < 1 || block_size > 1024) {
512 printf("bad block size %d\n", block_size);
518 progress_interval = atoi(optarg);
521 strncpy(state_file, optarg, sizeof(state_file)-1);
524 restore_state = !!atoi(optarg);
527 disk_util = atoi(optarg);
538 printf("Must give device with -d\n");
547 int main(int argc, char *argv[])
553 if (get_options(argc, argv))
561 load_progress_state();
563 if (stat(device, &statbuf) == -1) {
568 if (!S_ISBLK(statbuf.st_mode)) {
569 printf("%s does not appear to be a block device\n", device);
573 fd = open(device, O_RDONLY | O_DIRECT | O_LARGEFILE);
579 if (ioctl(fd, BLKGETSIZE64, &size) == -1) {
580 perror("BLKGETSIZE64");
584 blocks = size / block_size;
586 if (state_restored && dev_size != blocks) {
587 printf("Device size seems different %lu != %lu\n", (unsigned long) dev_size, (unsigned long) blocks);
594 read_bitmap = malloc(BITMAP_SIZE(dev_size));
595 memset(read_bitmap, 0, BITMAP_SIZE(dev_size));
598 signal(SIGUSR1, sig_handler);
599 signal(SIGINT, sig_handler);
601 if (progress_interval) {
602 signal(SIGALRM, sig_handler);
603 alarm(progress_interval);
606 gettimeofday(&start, NULL);
607 do_random_reads(fd, blocks);
609 save_progress_state();
611 free((void *) read_bitmap);
613 if (state_file_fd != -1)
614 close(state_file_fd);