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 2008 Sun Microsystems, Inc. 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
44 * The full mode is basic mode in which program writes the test pattern
45 * on entire disk. The test pattern (device offset and timestamp) is written
46 * at the beginning of each 4kB block. When the whole device is full then
47 * read operation is performed to verify that the test pattern is correct.
48 * In the fast mode the program writes data at the critical locations
49 * of the device such as start of the device, before and after multiple of 1GB
50 * offset and at the end.
51 * A chunk buffer with default size of 1MB is used to write and read test
61 #ifndef _LARGEFILE64_SOURCE
62 #define _LARGEFILE64_SOURCE
64 #ifndef _FILE_OFFSET_BITS
65 #define _FILE_OFFSET_BITS 64
80 #include <sys/types.h>
82 #include <sys/ioctl.h>
83 #include <sys/mount.h>
85 #include <gnu/stubs.h>
87 #ifdef HAVE_EXT2FS_EXT2FS_H
88 # include <ext2fs/ext2fs.h>
91 #define ONE_MB (1024 * 1024)
92 #define ONE_GB (1024 * 1024 * 1024)
93 #define HALF_MB (ONE_MB / 2)
95 #define HALF_KB (ONE_KB / 2)
96 #define BLOCKSIZE 4096
98 /* Structure for writting test pattern */
103 static char *progname; /* name by which this program was run. */
104 static unsigned verbose = 1; /* prints offset in kB, operation rate */
105 static int readoption; /* run test in read-only (verify) mode */
106 static int writeoption; /* run test in write_only mode */
107 const char *devname; /* name of device to be tested. */
108 static unsigned full = 1; /* flag to full check */
110 static int isatty_flag;
112 static struct option const longopts[] =
114 { "chunksize", required_argument, 0, 'c' },
115 { "force", no_argument, 0, 'f' },
116 { "help", no_argument, 0, 'h' },
117 { "offset", required_argument, 0, 'o' },
118 { "partial", required_argument, 0, 'p' },
119 { "quiet", required_argument, 0, 'q' },
120 { "read", no_argument, 0, 'r' },
121 { "timestamp", required_argument, 0, 't' },
122 { "verbose", no_argument, 0, 'v' },
123 { "write", no_argument, 0, 'w' },
124 { "long", no_argument, 0, 'l' },
129 * Usage: displays help information, whenever user supply --help option in
130 * command or enters incorrect command line.
132 void usage(int status)
135 printf("\nUsage: %s [OPTION]... <device-name> ...\n",
137 printf("Block device verification tool.\n"
138 "\t-t {seconds}, --timestamp, "
139 "set test time (default=current time())\n"
140 "\t-o {offset}, --offset, "
141 "offset in kB of start of test, default=0\n"
142 "\t-r, --read run test in verify mode\n"
143 "\t-w, --write run test in test-pattern mode, default=rw\n"
146 "\t-l, --long, full check of device\n"
147 "\t-p, --partial, for partial check (1GB steps)\n"
148 "\t-c, --chunksize, IO chunk size, default=1048576\n"
149 "\t-f, --force, force test to run without confirmation\n"
150 "\t-h, --help display this help and exit\n");
156 * Open_dev: Opens device in specified mode and returns fd.
158 static int open_dev(const char *devname, int mode)
160 #ifdef HAVE_EXT2FS_EXT2FS_H
162 char mountpt[80] = "";
164 if (ext2fs_check_mount_point(devname, &mount_flags, mountpt,
166 fprintf(stderr, "%s: ext2fs_check_mount_point failed:%s",
167 progname, strerror(errno));
170 if (mount_flags & EXT2_MF_MOUNTED){
171 fprintf(stderr, "%s: %s is already mounted\n", progname,
176 fd = open(devname, mode | O_EXCL | O_LARGEFILE);
178 fprintf(stderr, "%s: Open failed: %s",progname,strerror(errno));
184 #undef HAVE_BLKID_BLKID_H /* sigh, RHEL3 systems do not have libblkid.so.1 */
185 #ifdef HAVE_BLKID_BLKID_H
186 #include <blkid/blkid.h>
189 * sizeof_dev: Returns size of device in bytes
191 static loff_t sizeof_dev(int fd)
195 #ifdef HAVE_BLKID_BLKID_H
196 numbytes = blkid_get_dev_size(fd);
198 fprintf(stderr, "%s: blkid_get_dev_size(%s) failed",
204 # if defined BLKGETSIZE64 /* in sys/mount.h */
205 if (ioctl(fd, BLKGETSIZE64, &numbytes) >= 0)
208 # if defined BLKGETSIZE /* in sys/mount.h */
210 unsigned long sectors;
212 if (ioctl(fd, BLKGETSIZE, §ors) >= 0) {
213 numbytes = (loff_t)sectors << 9;
221 if (fstat(fd, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
222 numbytes = statbuf.st_size;
226 fprintf(stderr, "%s: unable to determine size of %s\n",
233 printf("%s: %s is %llu bytes (%g GB) in size\n",
235 (unsigned long long)numbytes, (double)numbytes / ONE_GB);
241 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
242 * Returns 0 if test offset and timestamp is correct otherwise 1.
244 int verify_chunk(char *chunk_buf, size_t chunksize,
245 unsigned long long chunk_off, time_t time_st)
247 struct block_data *bd;
250 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
251 (char *)chunk_buf < chunk_end;
252 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
253 bd = (struct block_data *)chunk_buf;
254 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st))
257 fprintf(stderr, "\n%s: verify failed at offset/timestamp "
258 "%llu/%lu: found %llu/%lu instead\n", progname,
259 chunk_off, time_st, bd->bd_offset, bd->bd_time);
266 * fill_chunk: Fills the chunk with current or user specified timestamp
267 * and offset. The test patters is filled at the beginning of
268 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
270 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
273 struct block_data *bd;
276 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
277 (char *)chunk_buf < chunk_end;
278 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
279 bd = (struct block_data *)chunk_buf;
280 bd->bd_offset = chunk_off;
281 bd->bd_time = time_st;
285 void show_rate(char *op, unsigned long long offset, unsigned long long *count)
298 printf("%s offset: %14llukB %5g MB/s ", op,
299 offset / ONE_KB, (double)(*count) /ONE_MB /diff);
312 * write_chunk: write the chunk_buf on the device. The number of write
313 * operations are based on the parameters write_end, offset, and chunksize.
315 int write_chunks(unsigned long long offset, unsigned long long write_end,
316 char *chunk_buf, size_t chunksize, time_t time_st)
318 unsigned long long stride, count = 0;
320 stride = full ? chunksize : (ONE_GB - chunksize);
322 for (offset = offset & ~(chunksize - 1); offset < write_end;
324 if (lseek64(fd, offset, SEEK_SET) == -1) {
325 fprintf(stderr, "\n%s: lseek64(%llu) failed: %s\n",
326 progname, offset, strerror(errno));
329 if (offset + chunksize > write_end)
330 chunksize = write_end - offset;
332 if (!full && offset > chunksize) {
333 fill_chunk(chunk_buf, chunksize, offset, time_st);
334 if (write(fd, chunk_buf, chunksize) < 0) {
335 fprintf(stderr, "\n%s: write %llu failed: %s\n",
336 progname, offset, strerror(errno));
340 if (offset + chunksize > write_end)
341 chunksize = write_end - offset;
344 fill_chunk(chunk_buf, chunksize, offset, time_st);
345 if (write(fd, chunk_buf, chunksize) < 0) {
346 fprintf(stderr, "\n%s: write %llu failed: %s\n",
347 progname, offset, strerror(errno));
353 show_rate("write", offset, &count);
356 show_rate("write", offset, &count);
357 printf("\nwrite complete\n");
359 if (fsync(fd) == -1) {
360 fprintf(stderr, "%s: fsync faild: %s\n", progname,
368 * read_chunk: reads the chunk_buf from the device. The number of read
369 * operations are based on the parameters read_end, offset, and chunksize.
371 int read_chunks(unsigned long long offset, unsigned long long read_end,
372 char *chunk_buf, size_t chunksize, time_t time_st)
374 unsigned long long stride, count = 0;
376 stride = full ? chunksize : (ONE_GB - chunksize);
378 if (ioctl(fd, BLKFLSBUF, 0) < 0 && verbose)
379 fprintf(stderr, "%s: ioctl BLKFLSBUF failed: %s (ignoring)\n",
380 progname, strerror(errno));
382 for (offset = offset & ~(chunksize - 1); offset < read_end;
384 if (lseek64(fd, offset, SEEK_SET) == -1) {
385 fprintf(stderr, "\n%s: lseek64(%llu) failed: %s\n",
386 progname, offset, strerror(errno));
389 if (offset + chunksize > read_end)
390 chunksize = read_end - offset;
392 if (!full && offset > chunksize) {
393 if (read (fd, chunk_buf, chunksize) < 0) {
394 fprintf(stderr, "\n%s: read %llu failed: %s\n",
395 progname, offset, strerror(errno));
398 if (verify_chunk(chunk_buf, chunksize, offset,
402 if (offset + chunksize >= read_end)
403 chunksize = read_end - offset;
406 if (read(fd, chunk_buf, chunksize) < 0) {
407 fprintf(stderr, "\n%s: read failed: %s\n", progname,
412 if (verify_chunk(chunk_buf, chunksize, offset, time_st) != 0)
417 show_rate("read", offset, &count);
420 show_rate("read", offset, &count);
421 printf("\nread complete\n");
426 int main(int argc, char **argv)
428 time_t time_st = 0; /* Default timestamp */
429 long long offset = 0, offset_orig; /* offset in kB */
430 size_t chunksize = ONE_MB; /* IO chunk size */
431 char *chunk_buf = NULL;
432 unsigned int force = 0; /* run test run without confirmation*/
433 unsigned long long dev_size = 0;
435 int mode = O_RDWR; /* mode which device should be opened */
438 progname = strrchr(argv[0], '/') == NULL ?
439 argv[0] : strrchr(argv[0], '/') + 1;
440 while ((c = getopt_long(argc, argv, "c:fhlo:pqrt:vw", longopts,
444 chunksize = (strtoul(optarg, NULL, 0) * ONE_MB);
446 fprintf(stderr, "%s: chunk size value should be"
447 "nonzero and multiple of 1MB\n",
459 offset = strtoull(optarg, NULL, 0) * ONE_KB;
472 time_st = (time_t)strtoul(optarg, NULL, 0);
487 offset_orig = offset;
488 devname = argv[optind];
490 fprintf(stderr, "%s: device name not given\n", progname);
495 if (readoption && writeoption)
497 if (!readoption && !writeoption) {
502 if (!force && writeoption) {
503 printf("%s: permanently overwrite all data on %s (yes/no)? ",
506 if (!(strcasecmp("yes", yesno) || strcasecmp("y", yesno))) {
507 printf("Not continuing due to '%s' response", yesno);
512 if (!writeoption && time_st == 0) {
513 fprintf(stderr, "%s: must give timestamp for read-only test\n",
518 fd = open_dev(devname, mode);
519 dev_size = sizeof_dev(fd);
521 fprintf(stderr, "%s: cannot test on device size < 1MB\n",
527 if (dev_size < (offset * 2)) {
528 fprintf(stderr, "%s: device size %llu < offset %llu\n",
529 progname, dev_size, offset);
534 (void)time(&time_st);
536 isatty_flag = isatty(STDOUT_FILENO);
539 printf("Timestamp: %lu\n", time_st);
541 chunk_buf = (char *)calloc(chunksize, 1);
542 if (chunk_buf == NULL) {
543 fprintf(stderr, "%s: memory allocation failed for chunk_buf\n",
549 if (write_chunks(offset, dev_size, chunk_buf, chunksize,
554 if (!full) { /* end of device aligned to a block */
555 offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
557 if (write_chunks(offset, dev_size, chunk_buf, chunksize,
563 offset = offset_orig;
566 if (read_chunks(offset, dev_size, chunk_buf, chunksize,
571 if (!full) { /* end of device aligned to a block */
572 offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
574 if (read_chunks(offset, dev_size, chunk_buf, chunksize,
581 printf("\n%s: data verified successfully\n", progname);