Whamcloud - gitweb
Update copyrights on source files changed since 2010-02-15.
[fs/lustre-release.git] / lustre / utils / llverdev.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/llverdev.c
37  *
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
42  * 1. full mode
43  * 2. fast mode
44  *
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.
49  *
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.
53  *
54  * A chunk buffer with default size of 1MB is used to write and read test
55  * pattern in bulk.
56  */
57
58 #ifndef _GNU_SOURCE
59 #define _GNU_SOURCE
60 #endif
61 #ifndef LUSTRE_UTILS
62 #define LUSTRE_UTILS
63 #endif
64 #ifndef _LARGEFILE64_SOURCE
65 #define _LARGEFILE64_SOURCE
66 #endif
67 #ifndef _FILE_OFFSET_BITS
68 #define _FILE_OFFSET_BITS 64
69 #endif
70
71 #include <features.h>
72 #include <stdlib.h>
73 #include <stdio.h>
74 #include <string.h>
75 #include <ctype.h>
76 #include <fcntl.h>
77 #include <unistd.h>
78 #include <limits.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <getopt.h>
82 #include <time.h>
83 #include <sys/types.h>
84 #include <sys/stat.h>
85 #include <sys/ioctl.h>
86 #include <sys/mount.h>
87 #include <sys/time.h>
88 #include <gnu/stubs.h>
89
90 #ifdef HAVE_EXT2FS_EXT2FS_H
91 #  include <ext2fs/ext2fs.h>
92 #endif
93
94 #define ONE_MB (1024 * 1024)
95 #define ONE_GB (1024 * 1024 * 1024)
96 #define HALF_MB (ONE_MB / 2)
97 #define ONE_KB 1024
98 #define HALF_KB (ONE_KB / 2)
99 #define BLOCKSIZE 4096
100
101 /* Structure for writting test pattern */
102 struct block_data {
103         unsigned long long bd_offset;
104         unsigned long long bd_time;
105         unsigned long long bd_inode;
106 };
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;
115
116 static struct option const longopts[] =
117 {
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' },
130         { 0, 0, 0, 0}
131 };
132
133 /*
134  * Usage: displays help information, whenever user supply --help option in
135  * command or enters incorrect command line.
136  */
137 void usage(int status)
138 {
139         if (status != 0) {
140                 printf("\nUsage: %s [OPTION]... <device-name> ...\n",
141                        progname);
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"
149                        "\t-v, --verbose\n"
150                        "\t-q, --quiet\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");
156         }
157         exit(status);
158 }
159
160 /*
161  * Open_dev: Opens device in specified mode and returns fd.
162  */
163 static int open_dev(const char *devname, int mode)
164 {
165         int     fd;
166 #ifdef HAVE_EXT2FS_EXT2FS_H
167         int     mount_flags;
168         char    mountpt[80] = "";
169
170         if (ext2fs_check_mount_point(devname, &mount_flags, mountpt,
171                                      sizeof(mountpt))) {
172                 fprintf(stderr, "%s: ext2fs_check_mount_point failed:%s",
173                         progname, strerror(errno));
174                 exit(1);
175         }
176         if (mount_flags & EXT2_MF_MOUNTED){
177                 fprintf(stderr, "%s: %s is already mounted\n", progname,
178                         devname);
179                 exit(1);
180         }
181 #endif
182         fd = open(devname, mode | O_EXCL | O_LARGEFILE);
183         if (fd < 0) {
184                 fprintf(stderr, "%s: Open failed: %s",progname,strerror(errno));
185                 exit(3);
186         }
187         return fd;
188 }
189
190 #ifdef HAVE_BLKID_BLKID_H
191 #include <blkid/blkid.h>
192 #endif
193 /*
194  * sizeof_dev: Returns size of device in bytes
195  */
196 static loff_t sizeof_dev(int fd)
197 {
198         loff_t numbytes;
199
200 #ifdef HAVE_BLKID_BLKID_H
201         numbytes = blkid_get_dev_size(fd);
202         if (numbytes <= 0) {
203                 fprintf(stderr, "%s: blkid_get_dev_size(%s) failed",
204                         progname, devname);
205                 return 1;
206         }
207         goto out;
208 #else
209 # if defined BLKGETSIZE64       /* in sys/mount.h */
210         if (ioctl(fd, BLKGETSIZE64, &numbytes) >= 0)
211                 goto out;
212 # endif
213 # if defined BLKGETSIZE         /* in sys/mount.h */
214         {
215                 unsigned long sectors;
216
217                 if (ioctl(fd, BLKGETSIZE, &sectors) >= 0) {
218                         numbytes = (loff_t)sectors << 9;
219                         goto out;
220                 }
221         }
222 # endif
223         {
224                 struct stat statbuf;
225
226                 if (fstat(fd, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
227                         numbytes = statbuf.st_size;
228                         goto out;
229                 }
230         }
231         fprintf(stderr, "%s: unable to determine size of %s\n",
232                 progname, devname);
233         return 0;
234 #endif
235
236 out:
237         if (verbose)
238                 printf("%s: %s is %llu bytes (%g GB) in size\n",
239                        progname, devname,
240                        (unsigned long long)numbytes, (double)numbytes / ONE_GB);
241
242         return numbytes;
243 }
244
245 /*
246  * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
247  * Returns 0 if test offset and timestamp is correct otherwise 1.
248  */
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)
252 {
253         struct block_data *bd;
254         char *chunk_end;
255
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))
262                         continue;
263
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);
268                 error_count++;
269                 return 1;
270         }
271         return 0;
272 }
273
274 /*
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.
278  */
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)
281 {
282         struct block_data *bd;
283         char *chunk_end;
284
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;
292         }
293 }
294
295 void show_rate(char *op, unsigned long long offset, unsigned long long *count)
296 {
297         static time_t last;
298         time_t now;
299         double diff;
300
301         now = time(NULL);
302         diff = now - last;
303
304         if (diff > 4) {
305                 if (last != 0) {
306                         if (isatty_flag)
307                                 printf("\r");
308                         printf("%s offset: %14llukB %5g MB/s            ", op,
309                                offset / ONE_KB, (double)(*count) /ONE_MB /diff);
310                         if (isatty_flag)
311                                 fflush(stdout);
312                         else
313                                 printf("\n");
314
315                         *count = 0;
316                 }
317                 last = now;
318         }
319 }
320
321 /*
322  * Write a chunk to disk, handling errors, interrupted writes, etc.
323  *
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.
329  *
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.
332  *
333  * Returns 0 on success, or -ve errno on failure.
334  */
335 size_t write_retry(int fd, const char *chunk_buf, size_t nrequested,
336                    unsigned long long offset, const char *file)
337 {
338         long nwritten;
339
340 retry:
341         nwritten = write(fd, chunk_buf, nrequested);
342         if (nwritten < 0) {
343                 if (errno != ENOSPC) {
344                         fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
345                                 progname, file, offset, nrequested,
346                                 strerror(errno));
347                         if (error_count++ < 100)
348                                 return 0;
349                 }
350                 return -errno;
351         }
352         if (nwritten < nrequested) {
353                 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
354                         progname, file, offset, nrequested, nwritten);
355                 offset += nwritten;
356                 nrequested -= nwritten;
357                 goto retry;
358         }
359
360         return 0;
361 }
362
363 /*
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.
366  *
367  * Returns 0 on success, or -ve error number on failure.
368  */
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)
372 {
373         unsigned long long stride, count = 0;
374
375         stride = full ? chunksize : (ONE_GB - chunksize);
376         for (offset = offset & ~(chunksize - 1); offset < write_end;
377              offset += stride) {
378                 int ret;
379
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));
383                         return -errno;
384                 }
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,
389                                    inode_st);
390                         ret = write_retry(fd, chunk_buf, chunksize,
391                                           offset, file);
392                         if (ret < 0)
393                                 return ret;
394                         offset += chunksize;
395                         count += chunksize;
396                         if (offset + chunksize > write_end)
397                                 chunksize = write_end - offset;
398                 }
399                 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
400                 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
401                 if (ret < 0)
402                         return ret;
403
404                 count += chunksize;
405                 if (verbose > 1)
406                         show_rate("write", offset, &count);
407         }
408
409         if (verbose > 1) {
410                 show_rate("write", offset, &count);
411                 printf("\nwrite complete\n");
412         }
413         if (fsync(fd) == -1) {
414                 fprintf(stderr, "%s: fsync failed: %s\n", progname,
415                         strerror(errno));
416                 return -errno;
417         }
418         return 0;
419 }
420
421 /*
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.
424  */
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)
428 {
429         unsigned long long stride, count = 0;
430
431         if (ioctl(fd, BLKFLSBUF, 0) < 0 && verbose)
432                 fprintf(stderr, "%s: ioctl BLKFLSBUF failed: %s (ignoring)\n",
433                         progname, strerror(errno));
434
435         stride = full ? chunksize : (ONE_GB - chunksize);
436         for (offset = offset & ~(chunksize - 1); offset < read_end;
437              offset += stride) {
438                 ssize_t nread;
439
440                 if (lseek64(fd, offset, SEEK_SET) == -1) {
441                         fprintf(stderr, "\n%s: lseek64(%llu) failed: %s\n",
442                                 progname, offset, strerror(errno));
443                         return 1;
444                 }
445                 if (offset + chunksize > read_end)
446                         chunksize = read_end - offset;
447
448                 if (!full && offset > chunksize) {
449                         nread = read(fd, chunk_buf, chunksize);
450                         if (nread < 0) {
451                                 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
452                                         "%s\n", progname, file, offset,
453                                         chunksize, strerror(errno));
454                                 error_count++;
455                                 return 1;
456                         }
457                         if (nread < chunksize) {
458                                 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
459                                         "%zi read\n", progname, file, offset,
460                                         chunksize, nread);
461                                 error_count++;
462                         }
463                         if (verify_chunk(chunk_buf, nread, offset, time_st,
464                                          inode_st, file) != 0)
465                                 return 1;
466                         offset += chunksize;
467                         count += chunksize;
468
469                         /* Need to reset position after read error */
470                         if (nread < chunksize &&
471                             lseek64(fd, offset, SEEK_SET) == -1) {
472                                 fprintf(stderr,
473                                         "\n%s: lseek64(%s@%llu) failed: %s\n",
474                                         progname, file, offset,strerror(errno));
475                                 return 1;
476                         }
477                         if (offset + chunksize >= read_end)
478                                 chunksize = read_end - offset;
479                 }
480
481                 nread = read(fd, chunk_buf, chunksize);
482                 if (nread < 0) {
483                         fprintf(stderr, "\n%s: read failed: %s\n", progname,
484                                 strerror(errno));
485                         error_count++;
486                         return 1;
487                 }
488                 if (nread < chunksize) {
489                         fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
490                                 "%zi read\n", progname, file, offset,
491                                 chunksize, nread);
492                         error_count++;
493                 }
494
495                 if (verify_chunk(chunk_buf, nread, offset, time_st,
496                                  inode_st, file) != 0)
497                         return 1;
498
499                 count += chunksize;
500                 if (verbose > 1)
501                         show_rate("read", offset, &count);
502         }
503         if (verbose > 1) {
504                 show_rate("read", offset, &count);
505                 printf("\nread complete\n");
506         }
507         return 0;
508 }
509
510 int main(int argc, char **argv)
511 {
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;
518         char yesno[4];
519         int mode = O_RDWR;              /* mode which device should be opened */
520         int fd;
521         int error = 0, c;
522
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,
526                                 NULL)) != -1) {
527                 switch (c) {
528                 case 'c':
529                         chunksize = (strtoul(optarg, NULL, 0) * ONE_MB);
530                         if (!chunksize) {
531                                 fprintf(stderr, "%s: chunk size value should be"
532                                         "nonzero and multiple of 1MB\n",
533                                         progname);
534                                 return -1;
535                         }
536                         break;
537                 case 'f':
538                         force = 1;
539                         break;
540                 case 'l':
541                         full = 1;
542                         break;
543                 case 'o':
544                         offset = strtoull(optarg, NULL, 0) * ONE_KB;
545                         break;
546                 case 'p':
547                         full = 0;
548                         break;
549                 case 'q':
550                         verbose = 0;
551                         break;
552                 case 'r':
553                         readoption = 1;
554                         mode = O_RDONLY;
555                         break;
556                 case 't':
557                         time_st = (time_t)strtoul(optarg, NULL, 0);
558                         break;
559                 case 'v':
560                         verbose++;
561                         break;
562                 case 'w':
563                         writeoption = 1;
564                         mode = O_WRONLY;
565                         break;
566                 case 'h':
567                 default:
568                         usage (1);
569                         return 0;
570                 }
571         }
572         offset_orig = offset;
573         devname = argv[optind];
574         if (!devname) {
575                 fprintf(stderr, "%s: device name not given\n", progname);
576                 usage (1);
577                 return -1;
578         }
579
580         if (readoption && writeoption)
581                 mode = O_RDWR;
582         if (!readoption && !writeoption) {
583                 readoption = 1;
584                 writeoption = 1;
585         }
586
587         if (!force && writeoption) {
588                 printf("%s: permanently overwrite all data on %s (yes/no)? ",
589                        progname, devname);
590                 if (scanf("%3s", yesno) == EOF && ferror(stdin)) {
591                         perror("reading from stdin");
592                         return -1;
593                 }
594                 if (!(strcasecmp("yes", yesno) || strcasecmp("y", yesno))) {
595                         printf("Not continuing due to '%s' response", yesno);
596                         return 0;
597                 }
598         }
599
600         if (!writeoption && time_st == 0) {
601                 fprintf(stderr, "%s: must give timestamp for read-only test\n",
602                         progname);
603                 usage(1);
604         }
605
606         fd = open_dev(devname, mode);
607         dev_size = sizeof_dev(fd);
608         if (!dev_size) {
609                 fprintf(stderr, "%s: cannot test on device size < 1MB\n",
610                         progname);
611                 error = 7;
612                 goto close_dev;
613         }
614
615         if (dev_size < (offset * 2)) {
616                 fprintf(stderr, "%s: device size %llu < offset %llu\n",
617                         progname, dev_size, offset);
618                 error = 6;
619                 goto close_dev;
620         }
621         if (!time_st)
622                 (void)time(&time_st);
623
624         isatty_flag = isatty(STDOUT_FILENO);
625
626         if (verbose)
627                 printf("Timestamp: %lu\n", time_st);
628
629         chunk_buf = (char *)calloc(chunksize, 1);
630         if (chunk_buf == NULL) {
631                 fprintf(stderr, "%s: memory allocation failed for chunk_buf\n",
632                         progname);
633                 error = 4;
634                 goto close_dev;
635         }
636         if (writeoption) {
637                 c = write_chunks(fd, offset, dev_size, chunk_buf, chunksize,
638                                  time_st, 0, devname);
639                 if (c < 0 && c != -ENOSPC) {
640                         error = 3;
641                         goto chunk_buf;
642                 }
643                 if (!full) { /* end of device aligned to a block */
644                         offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
645                                   ~(BLOCKSIZE - 1));
646                         c = write_chunks(fd, offset, dev_size, chunk_buf,
647                                          chunksize, time_st, 0, devname);
648                         if (c < 0 && c != -ENOSPC) {
649                                 error = 3;
650                                 goto chunk_buf;
651                         }
652                 }
653                 offset = offset_orig;
654         }
655         if (readoption) {
656                 if (read_chunks(fd, offset, dev_size, chunk_buf, chunksize,
657                                 time_st, 0, devname)) {
658                         error = 2;
659                         goto chunk_buf;
660                 }
661                 if (!full) { /* end of device aligned to a block */
662                         offset = ((dev_size - chunksize + BLOCKSIZE - 1) &
663                                   ~(BLOCKSIZE - 1));
664                         if (read_chunks(fd, offset, dev_size, chunk_buf,
665                                         chunksize, time_st, 0, devname)) {
666                                 error = 2;
667                                 goto chunk_buf;
668                         }
669                 }
670                 if (verbose)
671                         printf("\n%s: data verified successfully\n", progname);
672         }
673         error = error_count;
674 chunk_buf:
675         free(chunk_buf);
676 close_dev:
677         close(fd);
678         return error;
679 }