4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
32 * lustre/utils/llverdev.c
34 * Large Block Device Verification Tool.
35 * This program is used to test whether the block device is correctly
36 * handling IO beyond 2TB boundary.
37 * This tool have two working modes
41 * In full mode, the program writes a test pattern on the entire disk.
42 * The test pattern (device offset and timestamp) is written at the
43 * beginning of each 4kB block. When the whole device is full the read
44 * operation is performed to verify that the test pattern is correct.
46 * In partial mode, the program writes data at the critical locations
47 * of the device such as start of the device, before and after multiple of 1GB
48 * offset and at the end.
50 * A chunk buffer with default size of 1MB is used to write and read test
60 #ifndef _LARGEFILE64_SOURCE
61 #define _LARGEFILE64_SOURCE
63 #ifndef _FILE_OFFSET_BITS
64 #define _FILE_OFFSET_BITS 64
79 #include <sys/types.h>
81 #include <sys/ioctl.h>
82 #include <sys/mount.h>
84 #include <gnu/stubs.h>
86 #define ONE_MB (1024 * 1024)
87 #define ONE_GB (1024 * 1024 * 1024)
88 #define HALF_MB (ONE_MB / 2)
90 #define HALF_KB (ONE_KB / 2)
91 #define BLOCKSIZE 4096
93 /* Structure for writting test pattern */
95 unsigned long long bd_offset;
96 unsigned long long bd_time;
97 unsigned long long bd_inode;
99 static char *progname; /* name by which this program was run. */
100 static unsigned verbose = 1; /* prints offset in kB, operation rate */
101 static int readoption; /* run test in read-only (verify) mode */
102 static int writeoption; /* run test in write_only mode */
103 const char *devname; /* name of device to be tested. */
104 static unsigned full = 1; /* flag to full check */
105 static int error_count; /* number of IO errors hit during run */
106 static int isatty_flag;
108 static struct option const long_opts[] = {
109 { .val = 'c', .name = "chunksize", .has_arg = required_argument },
110 { .val = 'f', .name = "force", .has_arg = no_argument },
111 { .val = 'h', .name = "help", .has_arg = no_argument },
112 { .val = 'l', .name = "long", .has_arg = no_argument },
113 { .val = 'l', .name = "full", .has_arg = no_argument },
114 { .val = 'o', .name = "offset", .has_arg = required_argument },
115 { .val = 'p', .name = "partial", .has_arg = required_argument },
116 { .val = 'q', .name = "quiet", .has_arg = required_argument },
117 { .val = 'r', .name = "read", .has_arg = no_argument },
118 { .val = 't', .name = "timestamp", .has_arg = required_argument },
119 { .val = 'v', .name = "verbose", .has_arg = no_argument },
120 { .val = 'w', .name = "write", .has_arg = no_argument },
124 * Usage: displays help information, whenever user supply --help option in
125 * command or enters incorrect command line.
127 void usage(int status)
130 printf("\nUsage: %s [OPTION]... <device-name> ...\n",
132 printf("Block device verification tool.\n"
133 "\t-t {seconds}, --timestamp, "
134 "set test time (default=current time())\n"
135 "\t-o {offset}, --offset, "
136 "offset in kB of start of test, default=0\n"
137 "\t-r, --read, run in verify mode\n"
138 "\t-w, --write, run in test-pattern mode, default=rw\n"
141 "\t-l, --long, --full check of device\n"
142 "\t-p, --partial, for partial check (1GB steps)\n"
143 "\t-c {bytes}, --chunksize, IO size, default=1048576\n"
144 "\t-f, --force, force test to run without confirmation\n"
145 "\t-h, --help, display this help and exit\n");
151 * Open_dev: Opens device in specified mode and returns fd.
153 static int open_dev(const char *devname, int mode)
157 fd = open(devname, mode | O_EXCL | O_LARGEFILE);
159 fprintf(stderr, "%s: Open failed: %s",progname,strerror(errno));
166 * sizeof_dev: Returns size of device in bytes
168 static size_t sizeof_dev(int fd)
172 # if defined BLKGETSIZE64 /* in sys/mount.h */
173 if (ioctl(fd, BLKGETSIZE64, &numbytes) >= 0)
176 # if defined BLKGETSIZE /* in sys/mount.h */
178 unsigned long sectors;
180 if (ioctl(fd, BLKGETSIZE, §ors) >= 0) {
181 numbytes = (loff_t)sectors << 9;
189 if (fstat(fd, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
190 numbytes = statbuf.st_size;
194 fprintf(stderr, "%s: unable to determine size of %s\n",
200 printf("%s: %s is %llu bytes (%g GB) in size\n",
202 (unsigned long long)numbytes, (double)numbytes / ONE_GB);
208 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
209 * Returns 0 if test offset and timestamp is correct otherwise 1.
211 int verify_chunk(char *chunk_buf, const size_t chunksize,
212 unsigned long long chunk_off, const unsigned long long time_st,
213 const unsigned long long inode_st, const char *file)
215 struct block_data *bd;
218 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
219 (char *)chunk_buf < chunk_end;
220 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
221 bd = (struct block_data *)chunk_buf;
222 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
223 (bd->bd_inode == inode_st))
226 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
227 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
228 progname, file, chunk_off, time_st, inode_st,
229 bd->bd_offset, bd->bd_time, bd->bd_inode);
237 * fill_chunk: Fills the chunk with current or user specified timestamp
238 * and offset. The test pattern is filled at the beginning of
239 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
241 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
242 const time_t time_st, const ino_t inode_st)
244 struct block_data *bd;
247 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
248 (char *)chunk_buf < chunk_end;
249 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
250 bd = (struct block_data *)chunk_buf;
251 bd->bd_offset = chunk_off;
252 bd->bd_time = time_st;
253 bd->bd_inode = inode_st;
257 void show_rate(char *op, unsigned long long offset, unsigned long long *count)
270 printf("%s offset: %14llukB %5g MB/s ", op,
271 offset / ONE_KB, (double)(*count) /ONE_MB /diff);
284 * Write a chunk to disk, handling errors, interrupted writes, etc.
286 * If there is an IO error hit during the write, it is possible that
287 * this will just show up as a short write, and a subsequent write
288 * will return the actual error. We want to continue in the face of
289 * minor media errors so that we can validate the whole device if
290 * possible, but if there are many errors we don't want to loop forever.
292 * The error count will be returned upon exit to ensure that the
293 * media errors are detected even if nobody is looking at the output.
295 * Returns 0 on success, or -ve errno on failure.
297 size_t write_retry(int fd, const char *chunk_buf, size_t nrequested,
298 unsigned long long offset, const char *file)
303 nwritten = write(fd, chunk_buf, nrequested);
305 if (errno != ENOSPC) {
306 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
307 progname, file, offset, nrequested,
309 if (error_count++ < 100)
314 if (nwritten < nrequested) {
315 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
316 progname, file, offset, nrequested, nwritten);
318 chunk_buf += nwritten;
319 nrequested -= nwritten;
327 * write_chunks: write the chunk_buf on the device. The number of write
328 * operations are based on the parameters write_end, offset, and chunksize.
330 * Returns 0 on success, or -ve error number on failure.
332 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
333 char *chunk_buf, size_t chunksize, const time_t time_st,
334 const ino_t inode_st, const char *file)
336 unsigned long long stride, count = 0;
338 stride = full ? chunksize : (ONE_GB - chunksize);
339 for (offset = offset & ~(chunksize - 1); offset < write_end;
343 if (lseek64(fd, offset, SEEK_SET) == -1) {
344 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
345 progname, file, offset, strerror(errno));
348 if (offset + chunksize > write_end)
349 chunksize = write_end - offset;
350 if (!full && offset > chunksize) {
351 fill_chunk(chunk_buf, chunksize, offset, time_st,
353 ret = write_retry(fd, chunk_buf, chunksize,
359 if (offset + chunksize > write_end)
360 chunksize = write_end - offset;
362 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
363 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
369 show_rate("write", offset, &count);
373 show_rate("write", offset, &count);
374 printf("\nwrite complete\n");
376 if (fsync(fd) == -1) {
377 fprintf(stderr, "%s: fsync failed: %s\n", progname,
385 * read_chunk: reads the chunk_buf from the device. The number of read
386 * operations are based on the parameters read_end, offset, and chunksize.
388 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
389 char *chunk_buf, size_t chunksize, const time_t time_st,
390 const ino_t inode_st, const char *file)
392 unsigned long long stride, count = 0;
394 if (ioctl(fd, BLKFLSBUF, 0) < 0 && verbose)
395 fprintf(stderr, "%s: ioctl BLKFLSBUF failed: %s (ignoring)\n",
396 progname, strerror(errno));
398 stride = full ? chunksize : (ONE_GB - chunksize);
399 for (offset = offset & ~(chunksize - 1); offset < read_end;
403 if (lseek64(fd, offset, SEEK_SET) == -1) {
404 fprintf(stderr, "\n%s: lseek64(%llu) failed: %s\n",
405 progname, offset, strerror(errno));
408 if (offset + chunksize > read_end)
409 chunksize = read_end - offset;
411 if (!full && offset > chunksize) {
412 nread = read(fd, chunk_buf, chunksize);
414 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
415 "%s\n", progname, file, offset,
416 chunksize, strerror(errno));
420 if (nread < chunksize) {
421 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
422 "%zi read\n", progname, file, offset,
426 if (verify_chunk(chunk_buf, nread, offset, time_st,
427 inode_st, file) != 0)
432 /* Need to reset position after read error */
433 if (nread < chunksize &&
434 lseek64(fd, offset, SEEK_SET) == -1) {
436 "\n%s: lseek64(%s@%llu) failed: %s\n",
437 progname, file, offset,strerror(errno));
440 if (offset + chunksize >= read_end)
441 chunksize = read_end - offset;
444 nread = read(fd, chunk_buf, chunksize);
446 fprintf(stderr, "\n%s: read failed: %s\n", progname,
451 if (nread < chunksize) {
452 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
453 "%zi read\n", progname, file, offset,
458 if (verify_chunk(chunk_buf, nread, offset, time_st,
459 inode_st, file) != 0)
464 show_rate("read", offset, &count);
467 show_rate("read", offset, &count);
468 printf("\nread complete\n");
473 int main(int argc, char **argv)
475 time_t time_st = 0; /* Default timestamp */
476 long long offset = 0, offset_orig; /* offset in kB */
477 size_t chunksize = ONE_MB; /* IO chunk size */
478 char *chunk_buf = NULL;
479 unsigned int force = 0; /* run test run without confirmation*/
480 unsigned long long dev_size = 0;
482 int mode = O_RDWR; /* mode which device should be opened */
486 progname = strrchr(argv[0], '/') == NULL ?
487 argv[0] : strrchr(argv[0], '/') + 1;
488 while ((c = getopt_long(argc, argv, "c:fhlo:pqrt:vw", long_opts,
492 chunksize = (strtoul(optarg, NULL, 0) * ONE_MB);
495 "%s: chunk size value should be nonzero and multiple of 1MB\n",
507 offset = strtoull(optarg, NULL, 0) * ONE_KB;
520 time_st = (time_t)strtoul(optarg, NULL, 0);
535 offset_orig = offset;
536 devname = argv[optind];
538 fprintf(stderr, "%s: device name not given\n", progname);
543 if (readoption && writeoption)
545 if (!readoption && !writeoption) {
550 if (!force && writeoption) {
551 printf("%s: permanently overwrite all data on %s (yes/no)? ",
553 if (scanf("%3s", yesno) == EOF && ferror(stdin)) {
554 perror("reading from stdin");
557 if (!(strcasecmp("yes", yesno) || strcasecmp("y", yesno))) {
558 printf("Not continuing due to '%s' response", yesno);
563 if (!writeoption && time_st == 0) {
564 fprintf(stderr, "%s: must give timestamp for read-only test\n",
569 fd = open_dev(devname, mode);
570 dev_size = sizeof_dev(fd);
572 fprintf(stderr, "%s: cannot test on device size < 1MB\n",
578 if (dev_size < (offset * 2)) {
579 fprintf(stderr, "%s: device size %llu < offset %llu\n",
580 progname, dev_size, offset);
585 (void)time(&time_st);
587 isatty_flag = isatty(STDOUT_FILENO);
590 printf("Timestamp: %lu\n", time_st);
592 chunk_buf = (char *)calloc(chunksize, 1);
593 if (chunk_buf == NULL) {
594 fprintf(stderr, "%s: memory allocation failed for chunk_buf\n",
600 c = write_chunks(fd, offset, dev_size, chunk_buf, chunksize,
601 time_st, 0, devname);
602 if (c < 0 && c != -ENOSPC) {
606 if (!full) { /* end of device aligned to a block */
607 offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
609 c = write_chunks(fd, offset, dev_size, chunk_buf,
610 chunksize, time_st, 0, devname);
611 if (c < 0 && c != -ENOSPC) {
616 offset = offset_orig;
619 if (read_chunks(fd, offset, dev_size, chunk_buf, chunksize,
620 time_st, 0, devname)) {
624 if (!full) { /* end of device aligned to a block */
625 offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
627 if (read_chunks(fd, offset, dev_size, chunk_buf,
628 chunksize, time_st, 0, devname)) {
634 printf("\n%s: data verified successfully\n", progname);