1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/llverdev.c
38 * Large Block Device Verification Tool.
39 * This program is used to test whether the block device is correctly
40 * handling IO beyond 2TB boundary.
41 * This tool have two working modes
45 * In full mode, the program writes a test pattern on the entire disk.
46 * The test pattern (device offset and timestamp) is written at the
47 * beginning of each 4kB block. When the whole device is full the read
48 * operation is performed to verify that the test pattern is correct.
50 * In fast mode, the program writes data at the critical locations
51 * of the device such as start of the device, before and after multiple of 1GB
52 * offset and at the end.
54 * A chunk buffer with default size of 1MB is used to write and read test
64 #ifndef _LARGEFILE64_SOURCE
65 #define _LARGEFILE64_SOURCE
67 #ifndef _FILE_OFFSET_BITS
68 #define _FILE_OFFSET_BITS 64
83 #include <sys/types.h>
85 #include <sys/ioctl.h>
86 #include <sys/mount.h>
88 #include <gnu/stubs.h>
90 #ifdef HAVE_EXT2FS_EXT2FS_H
91 # include <ext2fs/ext2fs.h>
94 #define ONE_MB (1024 * 1024)
95 #define ONE_GB (1024 * 1024 * 1024)
96 #define HALF_MB (ONE_MB / 2)
98 #define HALF_KB (ONE_KB / 2)
99 #define BLOCKSIZE 4096
101 /* Structure for writting test pattern */
103 unsigned long long bd_offset;
104 unsigned long long bd_time;
105 unsigned long long bd_inode;
107 static char *progname; /* name by which this program was run. */
108 static unsigned verbose = 1; /* prints offset in kB, operation rate */
109 static int readoption; /* run test in read-only (verify) mode */
110 static int writeoption; /* run test in write_only mode */
111 const char *devname; /* name of device to be tested. */
112 static unsigned full = 1; /* flag to full check */
113 static int error_count; /* number of IO errors hit during run */
114 static int isatty_flag;
116 static struct option const longopts[] =
118 { "chunksize", required_argument, 0, 'c' },
119 { "force", no_argument, 0, 'f' },
120 { "help", no_argument, 0, 'h' },
121 { "offset", required_argument, 0, 'o' },
122 { "partial", required_argument, 0, 'p' },
123 { "quiet", required_argument, 0, 'q' },
124 { "read", no_argument, 0, 'r' },
125 { "timestamp", required_argument, 0, 't' },
126 { "verbose", no_argument, 0, 'v' },
127 { "write", no_argument, 0, 'w' },
128 { "long", no_argument, 0, 'l' },
129 { "full", no_argument, 0, 'l' },
134 * Usage: displays help information, whenever user supply --help option in
135 * command or enters incorrect command line.
137 void usage(int status)
140 printf("\nUsage: %s [OPTION]... <device-name> ...\n",
142 printf("Block device verification tool.\n"
143 "\t-t {seconds}, --timestamp, "
144 "set test time (default=current time())\n"
145 "\t-o {offset}, --offset, "
146 "offset in kB of start of test, default=0\n"
147 "\t-r, --read, run in verify mode\n"
148 "\t-w, --write, run in test-pattern mode, default=rw\n"
151 "\t-l, --long, --full check of device\n"
152 "\t-p, --partial, for partial check (1GB steps)\n"
153 "\t-c {bytes}, --chunksize, IO size, default=1048576\n"
154 "\t-f, --force, force test to run without confirmation\n"
155 "\t-h, --help, display this help and exit\n");
161 * Open_dev: Opens device in specified mode and returns fd.
163 static int open_dev(const char *devname, int mode)
166 #ifdef HAVE_EXT2FS_EXT2FS_H
168 char mountpt[80] = "";
170 if (ext2fs_check_mount_point(devname, &mount_flags, mountpt,
172 fprintf(stderr, "%s: ext2fs_check_mount_point failed:%s",
173 progname, strerror(errno));
176 if (mount_flags & EXT2_MF_MOUNTED){
177 fprintf(stderr, "%s: %s is already mounted\n", progname,
182 fd = open(devname, mode | O_EXCL | O_LARGEFILE);
184 fprintf(stderr, "%s: Open failed: %s",progname,strerror(errno));
190 #ifdef HAVE_BLKID_BLKID_H
191 #include <blkid/blkid.h>
194 * sizeof_dev: Returns size of device in bytes
196 static loff_t sizeof_dev(int fd)
200 #ifdef HAVE_BLKID_BLKID_H
201 numbytes = blkid_get_dev_size(fd);
203 fprintf(stderr, "%s: blkid_get_dev_size(%s) failed",
209 # if defined BLKGETSIZE64 /* in sys/mount.h */
210 if (ioctl(fd, BLKGETSIZE64, &numbytes) >= 0)
213 # if defined BLKGETSIZE /* in sys/mount.h */
215 unsigned long sectors;
217 if (ioctl(fd, BLKGETSIZE, §ors) >= 0) {
218 numbytes = (loff_t)sectors << 9;
226 if (fstat(fd, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
227 numbytes = statbuf.st_size;
231 fprintf(stderr, "%s: unable to determine size of %s\n",
238 printf("%s: %s is %llu bytes (%g GB) in size\n",
240 (unsigned long long)numbytes, (double)numbytes / ONE_GB);
246 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
247 * Returns 0 if test offset and timestamp is correct otherwise 1.
249 int verify_chunk(char *chunk_buf, const size_t chunksize,
250 unsigned long long chunk_off, const unsigned long long time_st,
251 const unsigned long long inode_st, const char *file)
253 struct block_data *bd;
256 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
257 (char *)chunk_buf < chunk_end;
258 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
259 bd = (struct block_data *)chunk_buf;
260 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
261 (bd->bd_inode == inode_st))
264 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
265 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
266 progname, file, chunk_off, time_st, inode_st,
267 bd->bd_offset, bd->bd_time, bd->bd_inode);
275 * fill_chunk: Fills the chunk with current or user specified timestamp
276 * and offset. The test patters is filled at the beginning of
277 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
279 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
280 const time_t time_st, const ino_t inode_st)
282 struct block_data *bd;
285 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
286 (char *)chunk_buf < chunk_end;
287 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
288 bd = (struct block_data *)chunk_buf;
289 bd->bd_offset = chunk_off;
290 bd->bd_time = time_st;
291 bd->bd_inode = inode_st;
295 void show_rate(char *op, unsigned long long offset, unsigned long long *count)
308 printf("%s offset: %14llukB %5g MB/s ", op,
309 offset / ONE_KB, (double)(*count) /ONE_MB /diff);
322 * Write a chunk to disk, handling errors, interrupted writes, etc.
324 * If there is an IO error hit during the write, it is possible that
325 * this will just show up as a short write, and a subsequent write
326 * will return the actual error. We want to continue in the face of
327 * minor media errors so that we can validate the whole device if
328 * possible, but if there are many errors we don't want to loop forever.
330 * The error count will be returned upon exit to ensure that the
331 * media errors are detected even if nobody is looking at the output.
333 * Returns 0 on success, or -ve errno on failure.
335 size_t write_retry(int fd, const char *chunk_buf, size_t nrequested,
336 unsigned long long offset, const char *file)
341 nwritten = write(fd, chunk_buf, nrequested);
343 if (errno != ENOSPC) {
344 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
345 progname, file, offset, nrequested,
347 if (error_count++ < 100)
352 if (nwritten < nrequested) {
353 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
354 progname, file, offset, nrequested, nwritten);
356 nrequested -= nwritten;
364 * write_chunks: write the chunk_buf on the device. The number of write
365 * operations are based on the parameters write_end, offset, and chunksize.
367 * Returns 0 on success, or -ve error number on failure.
369 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
370 char *chunk_buf, size_t chunksize, const time_t time_st,
371 const ino_t inode_st, const char *file)
373 unsigned long long stride, count = 0;
375 stride = full ? chunksize : (ONE_GB - chunksize);
376 for (offset = offset & ~(chunksize - 1); offset < write_end;
380 if (lseek64(fd, offset, SEEK_SET) == -1) {
381 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
382 progname, file, offset, strerror(errno));
385 if (offset + chunksize > write_end)
386 chunksize = write_end - offset;
387 if (!full && offset > chunksize) {
388 fill_chunk(chunk_buf, chunksize, offset, time_st,
390 ret = write_retry(fd, chunk_buf, chunksize,
396 if (offset + chunksize > write_end)
397 chunksize = write_end - offset;
399 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
400 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
406 show_rate("write", offset, &count);
410 show_rate("write", offset, &count);
411 printf("\nwrite complete\n");
413 if (fsync(fd) == -1) {
414 fprintf(stderr, "%s: fsync failed: %s\n", progname,
422 * read_chunk: reads the chunk_buf from the device. The number of read
423 * operations are based on the parameters read_end, offset, and chunksize.
425 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
426 char *chunk_buf, size_t chunksize, const time_t time_st,
427 const ino_t inode_st, const char *file)
429 unsigned long long stride, count = 0;
431 if (ioctl(fd, BLKFLSBUF, 0) < 0 && verbose)
432 fprintf(stderr, "%s: ioctl BLKFLSBUF failed: %s (ignoring)\n",
433 progname, strerror(errno));
435 stride = full ? chunksize : (ONE_GB - chunksize);
436 for (offset = offset & ~(chunksize - 1); offset < read_end;
440 if (lseek64(fd, offset, SEEK_SET) == -1) {
441 fprintf(stderr, "\n%s: lseek64(%llu) failed: %s\n",
442 progname, offset, strerror(errno));
445 if (offset + chunksize > read_end)
446 chunksize = read_end - offset;
448 if (!full && offset > chunksize) {
449 nread = read(fd, chunk_buf, chunksize);
451 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
452 "%s\n", progname, file, offset,
453 chunksize, strerror(errno));
457 if (nread < chunksize) {
458 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
459 "%zi read\n", progname, file, offset,
463 if (verify_chunk(chunk_buf, nread, offset, time_st,
464 inode_st, file) != 0)
469 /* Need to reset position after read error */
470 if (nread < chunksize &&
471 lseek64(fd, offset, SEEK_SET) == -1) {
473 "\n%s: lseek64(%s@%llu) failed: %s\n",
474 progname, file, offset,strerror(errno));
477 if (offset + chunksize >= read_end)
478 chunksize = read_end - offset;
481 nread = read(fd, chunk_buf, chunksize);
483 fprintf(stderr, "\n%s: read failed: %s\n", progname,
488 if (nread < chunksize) {
489 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
490 "%zi read\n", progname, file, offset,
495 if (verify_chunk(chunk_buf, nread, offset, time_st,
496 inode_st, file) != 0)
501 show_rate("read", offset, &count);
504 show_rate("read", offset, &count);
505 printf("\nread complete\n");
510 int main(int argc, char **argv)
512 time_t time_st = 0; /* Default timestamp */
513 long long offset = 0, offset_orig; /* offset in kB */
514 size_t chunksize = ONE_MB; /* IO chunk size */
515 char *chunk_buf = NULL;
516 unsigned int force = 0; /* run test run without confirmation*/
517 unsigned long long dev_size = 0;
519 int mode = O_RDWR; /* mode which device should be opened */
523 progname = strrchr(argv[0], '/') == NULL ?
524 argv[0] : strrchr(argv[0], '/') + 1;
525 while ((c = getopt_long(argc, argv, "c:fhlo:pqrt:vw", longopts,
529 chunksize = (strtoul(optarg, NULL, 0) * ONE_MB);
531 fprintf(stderr, "%s: chunk size value should be"
532 "nonzero and multiple of 1MB\n",
544 offset = strtoull(optarg, NULL, 0) * ONE_KB;
557 time_st = (time_t)strtoul(optarg, NULL, 0);
572 offset_orig = offset;
573 devname = argv[optind];
575 fprintf(stderr, "%s: device name not given\n", progname);
580 if (readoption && writeoption)
582 if (!readoption && !writeoption) {
587 if (!force && writeoption) {
588 printf("%s: permanently overwrite all data on %s (yes/no)? ",
590 if (scanf("%3s", yesno) == EOF && ferror(stdin)) {
591 perror("reading from stdin");
594 if (!(strcasecmp("yes", yesno) || strcasecmp("y", yesno))) {
595 printf("Not continuing due to '%s' response", yesno);
600 if (!writeoption && time_st == 0) {
601 fprintf(stderr, "%s: must give timestamp for read-only test\n",
606 fd = open_dev(devname, mode);
607 dev_size = sizeof_dev(fd);
609 fprintf(stderr, "%s: cannot test on device size < 1MB\n",
615 if (dev_size < (offset * 2)) {
616 fprintf(stderr, "%s: device size %llu < offset %llu\n",
617 progname, dev_size, offset);
622 (void)time(&time_st);
624 isatty_flag = isatty(STDOUT_FILENO);
627 printf("Timestamp: %lu\n", time_st);
629 chunk_buf = (char *)calloc(chunksize, 1);
630 if (chunk_buf == NULL) {
631 fprintf(stderr, "%s: memory allocation failed for chunk_buf\n",
637 c = write_chunks(fd, offset, dev_size, chunk_buf, chunksize,
638 time_st, 0, devname);
639 if (c < 0 && c != -ENOSPC) {
643 if (!full) { /* end of device aligned to a block */
644 offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
646 c = write_chunks(fd, offset, dev_size, chunk_buf,
647 chunksize, time_st, 0, devname);
648 if (c < 0 && c != -ENOSPC) {
653 offset = offset_orig;
656 if (read_chunks(fd, offset, dev_size, chunk_buf, chunksize,
657 time_st, 0, devname)) {
661 if (!full) { /* end of device aligned to a block */
662 offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
664 if (read_chunks(fd, offset, dev_size, chunk_buf,
665 chunksize, time_st, 0, devname)) {
671 printf("\n%s: data verified successfully\n", progname);