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.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/utils/llverdev.c
41 * Large Block Device Verification Tool.
42 * This program is used to test whether the block device is correctly
43 * handling IO beyond 2TB boundary.
44 * This tool have two working modes
48 * In full mode, the program writes a test pattern on the entire disk.
49 * The test pattern (device offset and timestamp) is written at the
50 * beginning of each 4kB block. When the whole device is full the read
51 * operation is performed to verify that the test pattern is correct.
53 * In partial mode, the program writes data at the critical locations
54 * of the device such as start of the device, before and after multiple of 1GB
55 * offset and at the end.
57 * A chunk buffer with default size of 1MB is used to write and read test
67 #ifndef _LARGEFILE64_SOURCE
68 #define _LARGEFILE64_SOURCE
70 #ifndef _FILE_OFFSET_BITS
71 #define _FILE_OFFSET_BITS 64
86 #include <sys/types.h>
88 #include <sys/ioctl.h>
89 #include <sys/mount.h>
91 #include <gnu/stubs.h>
93 #ifdef HAVE_EXT2FS_EXT2FS_H
94 # include <ext2fs/ext2fs.h>
97 #define ONE_MB (1024 * 1024)
98 #define ONE_GB (1024 * 1024 * 1024)
99 #define HALF_MB (ONE_MB / 2)
101 #define HALF_KB (ONE_KB / 2)
102 #define BLOCKSIZE 4096
104 /* Structure for writting test pattern */
106 unsigned long long bd_offset;
107 unsigned long long bd_time;
108 unsigned long long bd_inode;
110 static char *progname; /* name by which this program was run. */
111 static unsigned verbose = 1; /* prints offset in kB, operation rate */
112 static int readoption; /* run test in read-only (verify) mode */
113 static int writeoption; /* run test in write_only mode */
114 const char *devname; /* name of device to be tested. */
115 static unsigned full = 1; /* flag to full check */
116 static int error_count; /* number of IO errors hit during run */
117 static int isatty_flag;
119 static struct option const longopts[] =
121 { "chunksize", required_argument, 0, 'c' },
122 { "force", no_argument, 0, 'f' },
123 { "help", no_argument, 0, 'h' },
124 { "offset", required_argument, 0, 'o' },
125 { "partial", required_argument, 0, 'p' },
126 { "quiet", required_argument, 0, 'q' },
127 { "read", no_argument, 0, 'r' },
128 { "timestamp", required_argument, 0, 't' },
129 { "verbose", no_argument, 0, 'v' },
130 { "write", no_argument, 0, 'w' },
131 { "long", no_argument, 0, 'l' },
132 { "full", no_argument, 0, 'l' },
137 * Usage: displays help information, whenever user supply --help option in
138 * command or enters incorrect command line.
140 void usage(int status)
143 printf("\nUsage: %s [OPTION]... <device-name> ...\n",
145 printf("Block device verification tool.\n"
146 "\t-t {seconds}, --timestamp, "
147 "set test time (default=current time())\n"
148 "\t-o {offset}, --offset, "
149 "offset in kB of start of test, default=0\n"
150 "\t-r, --read, run in verify mode\n"
151 "\t-w, --write, run in test-pattern mode, default=rw\n"
154 "\t-l, --long, --full check of device\n"
155 "\t-p, --partial, for partial check (1GB steps)\n"
156 "\t-c {bytes}, --chunksize, IO size, default=1048576\n"
157 "\t-f, --force, force test to run without confirmation\n"
158 "\t-h, --help, display this help and exit\n");
164 * Open_dev: Opens device in specified mode and returns fd.
166 static int open_dev(const char *devname, int mode)
169 #ifdef HAVE_EXT2FS_EXT2FS_H
171 char mountpt[80] = "";
173 if (ext2fs_check_mount_point(devname, &mount_flags, mountpt,
175 fprintf(stderr, "%s: ext2fs_check_mount_point failed:%s",
176 progname, strerror(errno));
179 if (mount_flags & EXT2_MF_MOUNTED){
180 fprintf(stderr, "%s: %s is already mounted\n", progname,
185 fd = open(devname, mode | O_EXCL | O_LARGEFILE);
187 fprintf(stderr, "%s: Open failed: %s",progname,strerror(errno));
193 #ifdef HAVE_BLKID_BLKID_H
194 #include <blkid/blkid.h>
197 * sizeof_dev: Returns size of device in bytes
199 static loff_t sizeof_dev(int fd)
203 #ifdef HAVE_BLKID_BLKID_H
204 numbytes = blkid_get_dev_size(fd);
206 fprintf(stderr, "%s: blkid_get_dev_size(%s) failed",
212 # if defined BLKGETSIZE64 /* in sys/mount.h */
213 if (ioctl(fd, BLKGETSIZE64, &numbytes) >= 0)
216 # if defined BLKGETSIZE /* in sys/mount.h */
218 unsigned long sectors;
220 if (ioctl(fd, BLKGETSIZE, §ors) >= 0) {
221 numbytes = (loff_t)sectors << 9;
229 if (fstat(fd, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
230 numbytes = statbuf.st_size;
234 fprintf(stderr, "%s: unable to determine size of %s\n",
241 printf("%s: %s is %llu bytes (%g GB) in size\n",
243 (unsigned long long)numbytes, (double)numbytes / ONE_GB);
249 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
250 * Returns 0 if test offset and timestamp is correct otherwise 1.
252 int verify_chunk(char *chunk_buf, const size_t chunksize,
253 unsigned long long chunk_off, const unsigned long long time_st,
254 const unsigned long long inode_st, const char *file)
256 struct block_data *bd;
259 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
260 (char *)chunk_buf < chunk_end;
261 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
262 bd = (struct block_data *)chunk_buf;
263 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
264 (bd->bd_inode == inode_st))
267 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
268 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
269 progname, file, chunk_off, time_st, inode_st,
270 bd->bd_offset, bd->bd_time, bd->bd_inode);
278 * fill_chunk: Fills the chunk with current or user specified timestamp
279 * and offset. The test pattern is filled at the beginning of
280 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
282 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
283 const time_t time_st, const ino_t inode_st)
285 struct block_data *bd;
288 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
289 (char *)chunk_buf < chunk_end;
290 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
291 bd = (struct block_data *)chunk_buf;
292 bd->bd_offset = chunk_off;
293 bd->bd_time = time_st;
294 bd->bd_inode = inode_st;
298 void show_rate(char *op, unsigned long long offset, unsigned long long *count)
311 printf("%s offset: %14llukB %5g MB/s ", op,
312 offset / ONE_KB, (double)(*count) /ONE_MB /diff);
325 * Write a chunk to disk, handling errors, interrupted writes, etc.
327 * If there is an IO error hit during the write, it is possible that
328 * this will just show up as a short write, and a subsequent write
329 * will return the actual error. We want to continue in the face of
330 * minor media errors so that we can validate the whole device if
331 * possible, but if there are many errors we don't want to loop forever.
333 * The error count will be returned upon exit to ensure that the
334 * media errors are detected even if nobody is looking at the output.
336 * Returns 0 on success, or -ve errno on failure.
338 size_t write_retry(int fd, const char *chunk_buf, size_t nrequested,
339 unsigned long long offset, const char *file)
344 nwritten = write(fd, chunk_buf, nrequested);
346 if (errno != ENOSPC) {
347 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
348 progname, file, offset, nrequested,
350 if (error_count++ < 100)
355 if (nwritten < nrequested) {
356 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
357 progname, file, offset, nrequested, nwritten);
359 chunk_buf += nwritten;
360 nrequested -= nwritten;
368 * write_chunks: write the chunk_buf on the device. The number of write
369 * operations are based on the parameters write_end, offset, and chunksize.
371 * Returns 0 on success, or -ve error number on failure.
373 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
374 char *chunk_buf, size_t chunksize, const time_t time_st,
375 const ino_t inode_st, const char *file)
377 unsigned long long stride, count = 0;
379 stride = full ? chunksize : (ONE_GB - chunksize);
380 for (offset = offset & ~(chunksize - 1); offset < write_end;
384 if (lseek64(fd, offset, SEEK_SET) == -1) {
385 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
386 progname, file, offset, strerror(errno));
389 if (offset + chunksize > write_end)
390 chunksize = write_end - offset;
391 if (!full && offset > chunksize) {
392 fill_chunk(chunk_buf, chunksize, offset, time_st,
394 ret = write_retry(fd, chunk_buf, chunksize,
400 if (offset + chunksize > write_end)
401 chunksize = write_end - offset;
403 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
404 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
410 show_rate("write", offset, &count);
414 show_rate("write", offset, &count);
415 printf("\nwrite complete\n");
417 if (fsync(fd) == -1) {
418 fprintf(stderr, "%s: fsync failed: %s\n", progname,
426 * read_chunk: reads the chunk_buf from the device. The number of read
427 * operations are based on the parameters read_end, offset, and chunksize.
429 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
430 char *chunk_buf, size_t chunksize, const time_t time_st,
431 const ino_t inode_st, const char *file)
433 unsigned long long stride, count = 0;
435 if (ioctl(fd, BLKFLSBUF, 0) < 0 && verbose)
436 fprintf(stderr, "%s: ioctl BLKFLSBUF failed: %s (ignoring)\n",
437 progname, strerror(errno));
439 stride = full ? chunksize : (ONE_GB - chunksize);
440 for (offset = offset & ~(chunksize - 1); offset < read_end;
444 if (lseek64(fd, offset, SEEK_SET) == -1) {
445 fprintf(stderr, "\n%s: lseek64(%llu) failed: %s\n",
446 progname, offset, strerror(errno));
449 if (offset + chunksize > read_end)
450 chunksize = read_end - offset;
452 if (!full && offset > chunksize) {
453 nread = read(fd, chunk_buf, chunksize);
455 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
456 "%s\n", progname, file, offset,
457 chunksize, strerror(errno));
461 if (nread < chunksize) {
462 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
463 "%zi read\n", progname, file, offset,
467 if (verify_chunk(chunk_buf, nread, offset, time_st,
468 inode_st, file) != 0)
473 /* Need to reset position after read error */
474 if (nread < chunksize &&
475 lseek64(fd, offset, SEEK_SET) == -1) {
477 "\n%s: lseek64(%s@%llu) failed: %s\n",
478 progname, file, offset,strerror(errno));
481 if (offset + chunksize >= read_end)
482 chunksize = read_end - offset;
485 nread = read(fd, chunk_buf, chunksize);
487 fprintf(stderr, "\n%s: read failed: %s\n", progname,
492 if (nread < chunksize) {
493 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
494 "%zi read\n", progname, file, offset,
499 if (verify_chunk(chunk_buf, nread, offset, time_st,
500 inode_st, file) != 0)
505 show_rate("read", offset, &count);
508 show_rate("read", offset, &count);
509 printf("\nread complete\n");
514 int main(int argc, char **argv)
516 time_t time_st = 0; /* Default timestamp */
517 long long offset = 0, offset_orig; /* offset in kB */
518 size_t chunksize = ONE_MB; /* IO chunk size */
519 char *chunk_buf = NULL;
520 unsigned int force = 0; /* run test run without confirmation*/
521 unsigned long long dev_size = 0;
523 int mode = O_RDWR; /* mode which device should be opened */
527 progname = strrchr(argv[0], '/') == NULL ?
528 argv[0] : strrchr(argv[0], '/') + 1;
529 while ((c = getopt_long(argc, argv, "c:fhlo:pqrt:vw", longopts,
533 chunksize = (strtoul(optarg, NULL, 0) * ONE_MB);
535 fprintf(stderr, "%s: chunk size value should be"
536 "nonzero and multiple of 1MB\n",
548 offset = strtoull(optarg, NULL, 0) * ONE_KB;
561 time_st = (time_t)strtoul(optarg, NULL, 0);
576 offset_orig = offset;
577 devname = argv[optind];
579 fprintf(stderr, "%s: device name not given\n", progname);
584 if (readoption && writeoption)
586 if (!readoption && !writeoption) {
591 if (!force && writeoption) {
592 printf("%s: permanently overwrite all data on %s (yes/no)? ",
594 if (scanf("%3s", yesno) == EOF && ferror(stdin)) {
595 perror("reading from stdin");
598 if (!(strcasecmp("yes", yesno) || strcasecmp("y", yesno))) {
599 printf("Not continuing due to '%s' response", yesno);
604 if (!writeoption && time_st == 0) {
605 fprintf(stderr, "%s: must give timestamp for read-only test\n",
610 fd = open_dev(devname, mode);
611 dev_size = sizeof_dev(fd);
613 fprintf(stderr, "%s: cannot test on device size < 1MB\n",
619 if (dev_size < (offset * 2)) {
620 fprintf(stderr, "%s: device size %llu < offset %llu\n",
621 progname, dev_size, offset);
626 (void)time(&time_st);
628 isatty_flag = isatty(STDOUT_FILENO);
631 printf("Timestamp: %lu\n", time_st);
633 chunk_buf = (char *)calloc(chunksize, 1);
634 if (chunk_buf == NULL) {
635 fprintf(stderr, "%s: memory allocation failed for chunk_buf\n",
641 c = write_chunks(fd, offset, dev_size, chunk_buf, chunksize,
642 time_st, 0, devname);
643 if (c < 0 && c != -ENOSPC) {
647 if (!full) { /* end of device aligned to a block */
648 offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
650 c = write_chunks(fd, offset, dev_size, chunk_buf,
651 chunksize, time_st, 0, devname);
652 if (c < 0 && c != -ENOSPC) {
657 offset = offset_orig;
660 if (read_chunks(fd, offset, dev_size, chunk_buf, chunksize,
661 time_st, 0, devname)) {
665 if (!full) { /* end of device aligned to a block */
666 offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
668 if (read_chunks(fd, offset, dev_size, chunk_buf,
669 chunksize, time_st, 0, devname)) {
675 printf("\n%s: data verified successfully\n", progname);