Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 1 | /* |
| 2 | * filefrag.c -- report if a particular file is fragmented |
| 3 | * |
| 4 | * Copyright 2003 by Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | */ |
| 11 | |
Theodore Ts'o | d605ba1 | 2004-02-03 19:18:03 -0500 | [diff] [blame] | 12 | #ifndef __linux__ |
Matthias Andree | b34cbdd | 2003-12-28 18:21:26 +0100 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | int main(void) { |
| 17 | fputs("This program is only supported on Linux!\n", stderr); |
| 18 | exit(EXIT_FAILURE); |
| 19 | } |
| 20 | #else |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 21 | #define _LARGEFILE64_SOURCE |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <string.h> |
| 27 | #include <time.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <errno.h> |
Theodore Ts'o | 6b394c1 | 2004-02-26 21:08:06 -0500 | [diff] [blame] | 30 | #ifdef HAVE_GETOPT_H |
| 31 | #include <getopt.h> |
| 32 | #else |
| 33 | extern char *optarg; |
| 34 | extern int optind; |
| 35 | #endif |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/vfs.h> |
Matthias Andree | aa3a2fe | 2003-12-21 00:52:48 +0100 | [diff] [blame] | 39 | #include <sys/ioctl.h> |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 40 | #include <linux/fd.h> |
| 41 | |
| 42 | int verbose = 0; |
| 43 | |
| 44 | #define FIBMAP _IO(0x00,1) /* bmap access */ |
| 45 | #define FIGETBSZ _IO(0x00,2) /* get the block size used for bmap */ |
| 46 | |
Andreas Dilger | 8fe81a3 | 2006-08-05 18:50:22 -0400 | [diff] [blame^] | 47 | #define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */ |
Theodore Ts'o | 3d16b3f | 2005-03-18 20:37:45 -0500 | [diff] [blame] | 48 | #define EXT3_IOC_GETFLAGS _IOR('f', 1, long) |
| 49 | |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 50 | static unsigned long get_bmap(int fd, unsigned long block) |
| 51 | { |
| 52 | int ret; |
Theodore Ts'o | 8198e79 | 2005-05-20 23:10:35 -0400 | [diff] [blame] | 53 | unsigned int b; |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 54 | |
| 55 | b = block; |
Theodore Ts'o | 8198e79 | 2005-05-20 23:10:35 -0400 | [diff] [blame] | 56 | ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes a pointer to an integer */ |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 57 | if (ret < 0) { |
| 58 | if (errno == EPERM) { |
| 59 | fprintf(stderr, "No permission to use FIBMAP ioctl; must have root privileges\n"); |
| 60 | exit(1); |
| 61 | } |
| 62 | perror("FIBMAP"); |
| 63 | } |
| 64 | return b; |
| 65 | } |
| 66 | |
| 67 | #define EXT2_DIRECT 12 |
| 68 | |
Theodore Ts'o | 6b394c1 | 2004-02-26 21:08:06 -0500 | [diff] [blame] | 69 | static void frag_report(const char *filename) |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 70 | { |
| 71 | struct statfs fsinfo; |
Theodore Ts'o | 9c07dc0 | 2006-05-29 11:06:16 -0400 | [diff] [blame] | 72 | #ifdef HAVE_FSTAT64 |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 73 | struct stat64 fileinfo; |
Theodore Ts'o | 9c07dc0 | 2006-05-29 11:06:16 -0400 | [diff] [blame] | 74 | #else |
| 75 | struct stat fileinfo; |
| 76 | #endif |
Theodore Ts'o | 8198e79 | 2005-05-20 23:10:35 -0400 | [diff] [blame] | 77 | int bs; |
Theodore Ts'o | 40198dd | 2006-04-22 04:49:09 -0400 | [diff] [blame] | 78 | long i, fd; |
| 79 | unsigned long block, last_block = 0, numblocks; |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 80 | long bpib; /* Blocks per indirect block */ |
| 81 | long cylgroups; |
| 82 | int discont = 0, expected; |
| 83 | int is_ext2 = 0; |
Theodore Ts'o | 3d16b3f | 2005-03-18 20:37:45 -0500 | [diff] [blame] | 84 | unsigned int flags; |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 85 | |
| 86 | if (statfs(filename, &fsinfo) < 0) { |
| 87 | perror("statfs"); |
| 88 | return; |
| 89 | } |
Theodore Ts'o | 9c07dc0 | 2006-05-29 11:06:16 -0400 | [diff] [blame] | 90 | #ifdef HAVE_FSTAT64 |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 91 | if (stat64(filename, &fileinfo) < 0) { |
Theodore Ts'o | 9c07dc0 | 2006-05-29 11:06:16 -0400 | [diff] [blame] | 92 | #else |
| 93 | if (stat(filename, &fileinfo) < 0) { |
| 94 | #endif |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 95 | perror("stat"); |
| 96 | return; |
| 97 | } |
| 98 | if (!S_ISREG(fileinfo.st_mode)) { |
| 99 | printf("%s: Not a regular file\n", filename); |
| 100 | return; |
| 101 | } |
| 102 | if ((fsinfo.f_type == 0xef51) || (fsinfo.f_type == 0xef52) || |
| 103 | (fsinfo.f_type == 0xef53)) |
| 104 | is_ext2++; |
| 105 | if (verbose) { |
Matthias Andree | aa3a2fe | 2003-12-21 00:52:48 +0100 | [diff] [blame] | 106 | printf("Filesystem type is: %x\n", fsinfo.f_type); |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 107 | } |
| 108 | cylgroups = (fsinfo.f_blocks + fsinfo.f_bsize*8-1) / fsinfo.f_bsize*8; |
| 109 | if (verbose) { |
| 110 | printf("Filesystem cylinder groups is approximately %ld\n", |
| 111 | cylgroups); |
| 112 | } |
Theodore Ts'o | 9c07dc0 | 2006-05-29 11:06:16 -0400 | [diff] [blame] | 113 | #ifdef HAVE_OPEN64 |
| 114 | fd = open64(filename, O_RDONLY); |
| 115 | #else |
| 116 | fd = open(filename, O_RDONLY); |
| 117 | #endif |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 118 | if (fd < 0) { |
| 119 | perror("open"); |
| 120 | return; |
| 121 | } |
Theodore Ts'o | 8198e79 | 2005-05-20 23:10:35 -0400 | [diff] [blame] | 122 | if (ioctl(fd, FIGETBSZ, &bs) < 0) { /* FIGETBSZ takes an int */ |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 123 | perror("FIGETBSZ"); |
Theodore Ts'o | 9290404 | 2005-01-17 14:32:20 -0500 | [diff] [blame] | 124 | close(fd); |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 125 | return; |
| 126 | } |
Theodore Ts'o | a4897d4 | 2005-04-09 01:24:04 -0400 | [diff] [blame] | 127 | if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) < 0) |
| 128 | flags = 0; |
Andreas Dilger | 8fe81a3 | 2006-08-05 18:50:22 -0400 | [diff] [blame^] | 129 | if (flags & EXT4_EXTENTS_FL) { |
Theodore Ts'o | 3d16b3f | 2005-03-18 20:37:45 -0500 | [diff] [blame] | 130 | printf("File is stored in extents format\n"); |
| 131 | is_ext2 = 0; |
| 132 | } |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 133 | if (verbose) |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 134 | printf("Blocksize of file %s is %d\n", filename, bs); |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 135 | bpib = bs / 4; |
| 136 | numblocks = (fileinfo.st_size + (bs-1)) / bs; |
Theodore Ts'o | 7f1faaa | 2005-02-04 09:50:41 -0500 | [diff] [blame] | 137 | if (verbose) { |
Matthias Andree | aa3a2fe | 2003-12-21 00:52:48 +0100 | [diff] [blame] | 138 | printf("File size of %s is %lld (%ld blocks)\n", filename, |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 139 | (long long) fileinfo.st_size, numblocks); |
Theodore Ts'o | 40198dd | 2006-04-22 04:49:09 -0400 | [diff] [blame] | 140 | printf("First block: %lu\nLast block: %lu\n", |
Theodore Ts'o | 7f1faaa | 2005-02-04 09:50:41 -0500 | [diff] [blame] | 141 | get_bmap(fd, 0), get_bmap(fd, numblocks - 1)); |
| 142 | } |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 143 | for (i=0; i < numblocks; i++) { |
Theodore Ts'o | bfd9762 | 2005-05-05 22:55:20 -0400 | [diff] [blame] | 144 | if (is_ext2 && last_block) { |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 145 | if (((i-EXT2_DIRECT) % bpib) == 0) |
| 146 | last_block++; |
| 147 | if (((i-EXT2_DIRECT-bpib) % (bpib*bpib)) == 0) |
| 148 | last_block++; |
| 149 | if (((i-EXT2_DIRECT-bpib-bpib*bpib) % (bpib*bpib*bpib)) == 0) |
| 150 | last_block++; |
| 151 | } |
| 152 | block = get_bmap(fd, i); |
Theodore Ts'o | 3d16b3f | 2005-03-18 20:37:45 -0500 | [diff] [blame] | 153 | if (block == 0) |
| 154 | continue; |
| 155 | if (last_block && (block != last_block +1) ) { |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 156 | if (verbose) |
Theodore Ts'o | 40198dd | 2006-04-22 04:49:09 -0400 | [diff] [blame] | 157 | printf("Discontinuity: Block %ld is at %lu (was %lu)\n", |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 158 | i, block, last_block); |
| 159 | discont++; |
| 160 | } |
Theodore Ts'o | 3d16b3f | 2005-03-18 20:37:45 -0500 | [diff] [blame] | 161 | last_block = block; |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 162 | } |
| 163 | if (discont==0) |
| 164 | printf("%s: 1 extent found", filename); |
| 165 | else |
| 166 | printf("%s: %d extents found", filename, discont+1); |
| 167 | expected = (numblocks/((bs*8)-(fsinfo.f_files/8/cylgroups)-3))+1; |
| 168 | if (is_ext2 && expected != discont+1) |
| 169 | printf(", perfection would be %d extent%s\n", expected, |
| 170 | (expected>1) ? "s" : ""); |
| 171 | else |
| 172 | fputc('\n', stdout); |
Theodore Ts'o | 9290404 | 2005-01-17 14:32:20 -0500 | [diff] [blame] | 173 | close(fd); |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 174 | } |
| 175 | |
Theodore Ts'o | 6b394c1 | 2004-02-26 21:08:06 -0500 | [diff] [blame] | 176 | static void usage(const char *progname) |
Theodore Ts'o | 9642413 | 2003-12-17 10:13:41 -0500 | [diff] [blame] | 177 | { |
| 178 | fprintf(stderr, "Usage: %s [-v] file ...\n", progname); |
| 179 | exit(1); |
| 180 | } |
| 181 | |
| 182 | int main(int argc, char**argv) |
| 183 | { |
| 184 | char **cpp; |
| 185 | int c; |
| 186 | |
| 187 | while ((c = getopt(argc, argv, "v")) != EOF) |
| 188 | switch (c) { |
| 189 | case 'v': |
| 190 | verbose++; |
| 191 | break; |
| 192 | default: |
| 193 | usage(argv[0]); |
| 194 | break; |
| 195 | } |
| 196 | if (optind == argc) |
| 197 | usage(argv[0]); |
| 198 | for (cpp=argv+optind; *cpp; cpp++) { |
| 199 | if (verbose) |
| 200 | printf("Checking %s\n", *cpp); |
| 201 | frag_report(*cpp); |
| 202 | } |
| 203 | return 0; |
| 204 | } |
Matthias Andree | b34cbdd | 2003-12-28 18:21:26 +0100 | [diff] [blame] | 205 | #endif |