2 * filefrag.c -- report if a particular file is fragmented
4 * Copyright 2003 by Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
19 fputs("This program is only supported on Linux!\n", stderr);
23 #ifndef _LARGEFILE_SOURCE
24 #define _LARGEFILE_SOURCE
26 #ifndef _LARGEFILE64_SOURCE
27 #define _LARGEFILE64_SOURCE
44 #include <sys/types.h>
47 #include <sys/ioctl.h>
49 #include <ext2fs/ext2fs.h>
50 #include <ext2fs/ext2_types.h>
51 #include <ext2fs/fiemap.h>
54 int blocksize; /* Use specified blocksize (default 1kB) */
55 int sync_file = 0; /* fsync file before getting the mapping */
56 int xattr_map = 0; /* get xattr mapping */
57 int force_bmap; /* force use of FIBMAP instead of FIEMAP */
58 int force_extent; /* print output in extent format always */
59 int logical_width = 8;
60 int physical_width = 10;
61 const char *ext_fmt = "%4d: %*llu..%*llu: %*llu..%*llu: %6llu: %s\n";
62 const char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
64 #define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
66 #define FIBMAP _IO(0x00, 1) /* bmap access */
67 #define FIGETBSZ _IO(0x00, 2) /* get the block size used for bmap */
69 #define LUSTRE_SUPER_MAGIC 0x0BD00BD0
71 #define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
72 #define EXT3_IOC_GETFLAGS _IOR('f', 1, long)
74 static int int_log2(int arg)
86 static int int_log10(unsigned long long arg)
98 static unsigned int div_ceil(unsigned int a, unsigned int b)
102 return ((a - 1) / b) + 1;
105 static int get_bmap(int fd, unsigned long block, unsigned long *phy_blk)
111 ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes pointer to integer */
119 static void print_extent_header(void)
121 printf(" ext: %*s %*s length: %*s flags:\n",
122 logical_width * 2 + 3,
124 physical_width * 2 + 3, "physical_offset:",
129 static void print_flag(__u32 *flags, __u32 mask, char *buf, const char *name)
131 if ((*flags & mask) == 0)
138 static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
139 unsigned long long expected, int blk_shift,
140 ext2fs_struct_stat *st)
142 unsigned long long physical_blk;
143 unsigned long long logical_blk;
144 unsigned long long ext_len;
145 unsigned long long ext_blks;
146 __u32 fe_flags, mask;
147 char flags[256] = "";
149 /* For inline data all offsets should be in bytes, not blocks */
150 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
153 ext_len = fm_extent->fe_length >> blk_shift;
154 ext_blks = (fm_extent->fe_length - 1) >> blk_shift;
155 logical_blk = fm_extent->fe_logical >> blk_shift;
156 if (fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN) {
159 physical_blk = fm_extent->fe_physical >> blk_shift;
163 sprintf(flags, ext_fmt == hex_fmt ? "%*llx: " : "%*llu: ",
164 physical_width, expected >> blk_shift);
166 sprintf(flags, "%.*s ", physical_width, " ");
168 fe_flags = fm_extent->fe_flags;
169 print_flag(&fe_flags, FIEMAP_EXTENT_LAST, flags, "last,");
170 print_flag(&fe_flags, FIEMAP_EXTENT_UNKNOWN, flags, "unknown_loc,");
171 print_flag(&fe_flags, FIEMAP_EXTENT_DELALLOC, flags, "delalloc,");
172 print_flag(&fe_flags, FIEMAP_EXTENT_ENCODED, flags, "encoded,");
173 print_flag(&fe_flags, FIEMAP_EXTENT_DATA_ENCRYPTED, flags,"encrypted,");
174 print_flag(&fe_flags, FIEMAP_EXTENT_NOT_ALIGNED, flags, "not_aligned,");
175 print_flag(&fe_flags, FIEMAP_EXTENT_DATA_INLINE, flags, "inline,");
176 print_flag(&fe_flags, FIEMAP_EXTENT_DATA_TAIL, flags, "tail_packed,");
177 print_flag(&fe_flags, FIEMAP_EXTENT_UNWRITTEN, flags, "unwritten,");
178 print_flag(&fe_flags, FIEMAP_EXTENT_MERGED, flags, "merged,");
179 print_flag(&fe_flags, FIEMAP_EXTENT_SHARED, flags, "shared,");
180 /* print any unknown flags as hex values */
181 for (mask = 1; fe_flags != 0 && mask != 0; mask <<= 1) {
184 if ((fe_flags & mask) == 0)
186 sprintf(hex, "%#04x,", mask);
187 print_flag(&fe_flags, mask, flags, hex);
190 if (fm_extent->fe_logical + fm_extent->fe_length >=
191 (unsigned long long) st->st_size)
192 strcat(flags, "eof,");
194 /* Remove trailing comma, if any */
195 if (flags[0] != '\0')
196 flags[strnlen(flags, sizeof(flags)) - 1] = '\0';
198 printf(ext_fmt, cur_ex, logical_width, logical_blk,
199 logical_width, logical_blk + ext_blks,
200 physical_width, physical_blk,
201 physical_width, physical_blk + ext_blks,
205 static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
206 ext2fs_struct_stat *st)
208 __u64 buf[2048]; /* __u64 for proper field alignment */
209 struct fiemap *fiemap = (struct fiemap *)buf;
210 struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
211 struct fiemap_extent fm_last = {0};
212 int count = (sizeof(buf) - sizeof(*fiemap)) /
213 sizeof(struct fiemap_extent);
214 unsigned long long expected = 0;
215 unsigned long long expected_dense = 0;
216 unsigned long flags = 0;
218 int fiemap_header_printed = 0;
219 int tot_extents = 0, n = 0;
223 memset(fiemap, 0, sizeof(struct fiemap));
226 flags |= FIEMAP_FLAG_SYNC;
229 flags |= FIEMAP_FLAG_XATTR;
232 fiemap->fm_length = ~0ULL;
233 fiemap->fm_flags = flags;
234 fiemap->fm_extent_count = count;
235 rc = ioctl(fd, FS_IOC_FIEMAP, (unsigned long) fiemap);
237 static int fiemap_incompat_printed;
240 if (rc == -EBADR && !fiemap_incompat_printed) {
241 fprintf(stderr, "FIEMAP failed with unknown "
244 fiemap_incompat_printed = 1;
249 /* If 0 extents are returned, then more ioctls are not needed */
250 if (fiemap->fm_mapped_extents == 0)
253 if (verbose && !fiemap_header_printed) {
254 print_extent_header();
255 fiemap_header_printed = 1;
258 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
259 expected_dense = fm_last.fe_physical +
261 expected = fm_last.fe_physical +
262 fm_ext[i].fe_logical - fm_last.fe_logical;
263 if (fm_ext[i].fe_logical != 0 &&
264 fm_ext[i].fe_physical != expected &&
265 fm_ext[i].fe_physical != expected_dense) {
273 print_extent_info(&fm_ext[i], n, expected,
275 if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
281 fiemap->fm_start = (fm_ext[i - 1].fe_logical +
282 fm_ext[i - 1].fe_length);
285 *num_extents = tot_extents;
290 #define EXT2_DIRECT 12
292 static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
293 ext2fs_struct_stat *st,
294 unsigned long numblocks, int is_ext2)
296 struct fiemap_extent fm_ext, fm_last;
297 unsigned long i, last_block;
298 unsigned long long logical, expected = 0;
299 /* Blocks per indirect block */
300 const long bpib = st->st_blksize / 4;
303 memset(&fm_ext, 0, sizeof(fm_ext));
304 memset(&fm_last, 0, sizeof(fm_last));
306 fm_ext.fe_flags = FIEMAP_EXTENT_MERGED;
312 for (i = 0, logical = 0, *num_extents = 0, count = last_block = 0;
314 i++, logical += st->st_blksize) {
315 unsigned long block = 0;
318 if (is_ext2 && last_block) {
319 if (((i - EXT2_DIRECT) % bpib) == 0)
321 if (((i - EXT2_DIRECT - bpib) % (bpib * bpib)) == 0)
323 if (((i - EXT2_DIRECT - bpib - bpib * bpib) %
324 (((unsigned long long)bpib) * bpib * bpib)) == 0)
327 rc = get_bmap(fd, i, &block);
333 if (*num_extents == 0 || block != last_block + 1 ||
334 fm_ext.fe_logical + fm_ext.fe_length != logical) {
336 * This is the start of a new extent; figure out where
337 * we expected it to be and report the extent.
339 if (*num_extents != 0 && fm_last.fe_length) {
340 expected = fm_last.fe_physical +
341 (fm_ext.fe_logical - fm_last.fe_logical);
342 if (expected == fm_ext.fe_physical)
345 if (force_extent && *num_extents == 0)
346 print_extent_header();
347 if (force_extent && *num_extents != 0) {
348 print_extent_info(&fm_ext, *num_extents - 1,
349 expected, blk_shift, st);
351 if (verbose && expected != 0) {
352 printf("Discontinuity: Block %llu is at %llu "
354 fm_ext.fe_logical / st->st_blksize,
355 fm_ext.fe_physical / st->st_blksize,
356 expected / st->st_blksize);
358 /* create the new extent */
361 fm_ext.fe_physical = block * st->st_blksize;
362 fm_ext.fe_logical = logical;
363 fm_ext.fe_length = 0;
365 fm_ext.fe_length += st->st_blksize;
368 if (force_extent && *num_extents != 0) {
369 if (fm_last.fe_length) {
370 expected = fm_last.fe_physical +
371 (fm_ext.fe_logical - fm_last.fe_logical);
372 if (expected == fm_ext.fe_physical)
375 print_extent_info(&fm_ext, *num_extents - 1, expected,
382 static int frag_report(const char *filename)
384 static struct statfs fsinfo;
385 static unsigned int blksize;
386 ext2fs_struct_stat st;
389 unsigned long long numblocks;
390 int data_blocks_per_cyl = 1;
391 int num_extents = 1, expected = ~0;
393 static dev_t last_device;
397 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
398 fd = open64(filename, O_RDONLY);
400 fd = open(filename, O_RDONLY);
408 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
409 if (fstat64(fd, &st) < 0) {
411 if (fstat(fd, &st) < 0) {
418 if (last_device != st.st_dev) {
419 if (fstatfs(fd, &fsinfo) < 0) {
424 if (ioctl(fd, FIGETBSZ, &blksize) < 0)
425 blksize = fsinfo.f_bsize;
427 printf("Filesystem type is: %lx\n",
428 (unsigned long)fsinfo.f_type);
430 st.st_blksize = blksize;
431 if (fsinfo.f_type == 0xef51 || fsinfo.f_type == 0xef52 ||
432 fsinfo.f_type == 0xef53) {
435 if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) == 0 &&
436 !(flags & EXT4_EXTENTS_FL))
441 long cylgroups = div_ceil(fsinfo.f_blocks, blksize * 8);
443 if (verbose && last_device != st.st_dev)
444 printf("Filesystem cylinder groups approximately %ld\n",
447 data_blocks_per_cyl = blksize * 8 -
448 (fsinfo.f_files / 8 / cylgroups) - 3;
450 last_device = st.st_dev;
452 width = int_log10(fsinfo.f_blocks);
453 if (width > physical_width)
454 physical_width = width;
456 numblocks = (st.st_size + blksize - 1) / blksize;
458 blk_shift = int_log2(blocksize);
460 blk_shift = int_log2(blksize);
462 width = int_log10(numblocks);
463 if (width > logical_width)
464 logical_width = width;
466 printf("File size of %s is %llu (%llu block%s of %d bytes)\n",
467 filename, (unsigned long long)st.st_size,
468 numblocks * blksize >> blk_shift,
469 numblocks == 1 ? "" : "s", 1 << blk_shift);
472 rc = filefrag_fiemap(fd, blk_shift, &num_extents, &st);
476 if (force_bmap || rc < 0) { /* FIEMAP failed, try FIBMAP instead */
477 expected = filefrag_fibmap(fd, blk_shift, &num_extents,
478 &st, numblocks, is_ext2);
480 if (expected == -EINVAL || expected == -ENOTTY) {
481 fprintf(stderr, "%s: FIBMAP unsupported\n",
483 } else if (expected == -EPERM) {
485 "%s: FIBMAP requires root privileges\n",
488 fprintf(stderr, "%s: FIBMAP error: %s",
489 filename, strerror(expected));
496 expected = expected / data_blocks_per_cyl + 1;
499 if (num_extents == 1)
500 printf("%s: 1 extent found", filename);
502 printf("%s: %d extents found", filename, num_extents);
503 /* count, and thus expected, only set for indirect FIBMAP'd files */
504 if (is_ext2 && expected && expected < num_extents)
505 printf(", perfection would be %d extent%s\n", expected,
506 (expected > 1) ? "s" : "");
515 static void usage(const char *progname)
517 fprintf(stderr, "Usage: %s [-b{blocksize}] [-BeksvxX] file ...\n",
522 int main(int argc, char**argv)
527 while ((c = getopt(argc, argv, "Bb::eksvxX")) != EOF) {
535 blocksize = strtoul(optarg, &end, 0);
554 } else { /* Allow -b without argument for compat. Remove
555 * this eventually so "-b {blocksize}" works */
556 fprintf(stderr, "%s: -b needs a blocksize "
557 "option, assuming 1024-byte blocks.\n",
591 for (cpp = argv + optind; *cpp != '\0'; cpp++) {
592 int rc2 = frag_report(*cpp);
594 if (rc2 < 0 && rc == 0)