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 #define _LARGEFILE64_SOURCE
38 #include <sys/types.h>
41 #include <sys/ioctl.h>
43 #include <ext2fs/ext2fs.h>
44 #include <ext2fs/ext2_types.h>
45 #include <ext2fs/fiemap.h>
48 int blocksize; /* Use specified blocksize (default 1kB) */
49 int sync_file = 0; /* fsync file before getting the mapping */
50 int xattr_map = 0; /* get xattr mapping */
51 int force_bmap; /* force use of FIBMAP instead of FIEMAP */
52 int force_extent; /* print output in extent format always */
53 int logical_width = 8;
54 int physical_width = 10;
55 const char *ext_fmt = "%4d: %*llu..%*llu: %*llu..%*llu: %6llu: %s\n";
56 const char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
58 #define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
60 #define FIBMAP _IO(0x00, 1) /* bmap access */
61 #define FIGETBSZ _IO(0x00, 2) /* get the block size used for bmap */
63 #define LUSTRE_SUPER_MAGIC 0x0BD00BD0
65 #define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
66 #define EXT3_IOC_GETFLAGS _IOR('f', 1, long)
68 static int int_log2(int arg)
80 static int int_log10(unsigned long long arg)
92 static unsigned int div_ceil(unsigned int a, unsigned int b)
96 return ((a - 1) / b) + 1;
99 static int get_bmap(int fd, unsigned long block, unsigned long *phy_blk)
105 ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes pointer to integer */
113 static void print_extent_header(void)
115 printf(" ext: %*s %*s length: %*s flags:\n",
116 logical_width * 2 + 3,
118 physical_width * 2 + 3, "physical_offset:",
123 static void print_flag(__u32 *flags, __u32 mask, char *buf, const char *name)
125 if ((*flags & mask) == 0)
132 static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
133 unsigned long long expected, int blk_shift,
134 ext2fs_struct_stat *st)
136 unsigned long long physical_blk;
137 unsigned long long logical_blk;
138 unsigned long long ext_len;
139 unsigned long long ext_blks;
140 __u32 fe_flags, mask;
141 char flags[256] = "";
143 /* For inline data all offsets should be in bytes, not blocks */
144 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
147 ext_len = fm_extent->fe_length >> blk_shift;
148 ext_blks = (fm_extent->fe_length - 1) >> blk_shift;
149 logical_blk = fm_extent->fe_logical >> blk_shift;
150 if (fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN) {
153 physical_blk = fm_extent->fe_physical >> blk_shift;
157 sprintf(flags, ext_fmt == hex_fmt ? "%*llx: " : "%*llu: ",
158 physical_width, expected >> blk_shift);
160 sprintf(flags, "%.*s ", physical_width, " ");
162 fe_flags = fm_extent->fe_flags;
163 print_flag(&fe_flags, FIEMAP_EXTENT_LAST, flags, "last,");
164 print_flag(&fe_flags, FIEMAP_EXTENT_UNKNOWN, flags, "unknown_loc,");
165 print_flag(&fe_flags, FIEMAP_EXTENT_DELALLOC, flags, "delalloc,");
166 print_flag(&fe_flags, FIEMAP_EXTENT_ENCODED, flags, "encoded,");
167 print_flag(&fe_flags, FIEMAP_EXTENT_DATA_ENCRYPTED, flags,"encrypted,");
168 print_flag(&fe_flags, FIEMAP_EXTENT_NOT_ALIGNED, flags, "not_aligned,");
169 print_flag(&fe_flags, FIEMAP_EXTENT_DATA_INLINE, flags, "inline,");
170 print_flag(&fe_flags, FIEMAP_EXTENT_DATA_TAIL, flags, "tail_packed,");
171 print_flag(&fe_flags, FIEMAP_EXTENT_UNWRITTEN, flags, "unwritten,");
172 print_flag(&fe_flags, FIEMAP_EXTENT_MERGED, flags, "merged,");
173 print_flag(&fe_flags, FIEMAP_EXTENT_SHARED, flags, "shared,");
174 /* print any unknown flags as hex values */
175 for (mask = 1; fe_flags != 0 && mask != 0; mask <<= 1) {
178 if ((fe_flags & mask) == 0)
180 sprintf(hex, "%#04x,", mask);
181 print_flag(&fe_flags, mask, flags, hex);
184 if (fm_extent->fe_logical + fm_extent->fe_length >= st->st_size)
185 strcat(flags, "eof,");
187 /* Remove trailing comma, if any */
188 if (flags[0] != '\0')
189 flags[strnlen(flags, sizeof(flags)) - 1] = '\0';
191 printf(ext_fmt, cur_ex, logical_width, logical_blk,
192 logical_width, logical_blk + ext_blks,
193 physical_width, physical_blk,
194 physical_width, physical_blk + ext_blks,
198 static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
199 ext2fs_struct_stat *st)
201 __u64 buf[2048]; /* __u64 for proper field alignment */
202 struct fiemap *fiemap = (struct fiemap *)buf;
203 struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
204 int count = (sizeof(buf) - sizeof(*fiemap)) /
205 sizeof(struct fiemap_extent);
206 unsigned long long expected = 0;
207 unsigned long flags = 0;
209 int fiemap_header_printed = 0;
210 int tot_extents = 0, n = 0;
214 memset(fiemap, 0, sizeof(struct fiemap));
217 flags |= FIEMAP_FLAG_SYNC;
220 flags |= FIEMAP_FLAG_XATTR;
223 fiemap->fm_length = ~0ULL;
224 fiemap->fm_flags = flags;
225 fiemap->fm_extent_count = count;
226 rc = ioctl(fd, FS_IOC_FIEMAP, (unsigned long) fiemap);
228 static int fiemap_incompat_printed;
231 if (rc == -EBADR && !fiemap_incompat_printed) {
232 fprintf(stderr, "FIEMAP failed with unknown "
235 fiemap_incompat_printed = 1;
240 /* If 0 extents are returned, then more ioctls are not needed */
241 if (fiemap->fm_mapped_extents == 0)
244 if (verbose && !fiemap_header_printed) {
245 print_extent_header();
246 fiemap_header_printed = 1;
249 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
250 if (fm_ext[i].fe_logical != 0 &&
251 fm_ext[i].fe_physical != expected) {
259 print_extent_info(&fm_ext[i], n, expected,
262 expected = fm_ext[i].fe_physical + fm_ext[i].fe_length;
263 if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
268 fiemap->fm_start = (fm_ext[i - 1].fe_logical +
269 fm_ext[i - 1].fe_length);
272 *num_extents = tot_extents;
277 #define EXT2_DIRECT 12
279 static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
280 ext2fs_struct_stat *st,
281 unsigned long numblocks, int is_ext2)
283 struct fiemap_extent fm_ext;
284 unsigned long i, last_block;
285 unsigned long long logical;
286 /* Blocks per indirect block */
287 const long bpib = st->st_blksize / 4;
291 memset(&fm_ext, 0, sizeof(fm_ext));
292 fm_ext.fe_flags = FIEMAP_EXTENT_MERGED;
298 for (i = 0, logical = 0, *num_extents = 0, count = last_block = 0;
300 i++, logical += st->st_blksize) {
301 unsigned long block = 0;
304 if (is_ext2 && last_block) {
305 if (((i - EXT2_DIRECT) % bpib) == 0)
307 if (((i - EXT2_DIRECT - bpib) % (bpib * bpib)) == 0)
309 if (((i - EXT2_DIRECT - bpib - bpib * bpib) %
310 (((unsigned long long)bpib) * bpib * bpib)) == 0)
313 rc = get_bmap(fd, i, &block);
318 if (*num_extents == 0) {
321 print_extent_header();
322 fm_ext.fe_physical = block * st->st_blksize;
326 if (force_extent && last_block != 0 &&
327 (block != last_block + 1 ||
328 fm_ext.fe_logical + fm_ext.fe_length != logical)) {
329 print_extent_info(&fm_ext, *num_extents - 1,
330 (last_block + 1) * st->st_blksize,
332 fm_ext.fe_length = 0;
334 } else if (last_block && (block != last_block + 1)) {
336 printf("Discontinuity: Block %ld is at %lu (was "
337 "%lu)\n", i, block, last_block + 1);
338 fm_ext.fe_length = 0;
341 fm_ext.fe_logical = logical;
342 fm_ext.fe_physical = block * st->st_blksize;
343 fm_ext.fe_length += st->st_blksize;
348 print_extent_info(&fm_ext, *num_extents - 1,
349 last_block * st->st_blksize, blk_shift, st);
354 static int frag_report(const char *filename)
356 static struct statfs fsinfo;
357 static unsigned int blksize;
358 ext2fs_struct_stat st;
361 unsigned long long numblocks;
362 int data_blocks_per_cyl = 1;
363 int num_extents = 1, expected = ~0;
365 static dev_t last_device;
369 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
370 fd = open64(filename, O_RDONLY);
372 fd = open(filename, O_RDONLY);
380 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
381 if (fstat64(fd, &st) < 0) {
383 if (fstat(fd, &st) < 0) {
390 if (last_device != st.st_dev) {
391 if (fstatfs(fd, &fsinfo) < 0) {
396 if (ioctl(fd, FIGETBSZ, &blksize) < 0)
397 blksize = fsinfo.f_bsize;
399 printf("Filesystem type is: %lx\n",
400 (unsigned long)fsinfo.f_type);
402 st.st_blksize = blksize;
403 if (fsinfo.f_type == 0xef51 || fsinfo.f_type == 0xef52 ||
404 fsinfo.f_type == 0xef53) {
407 if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) == 0 &&
408 !(flags & EXT4_EXTENTS_FL))
413 long cylgroups = div_ceil(fsinfo.f_blocks, blksize * 8);
415 if (verbose && last_device != st.st_dev)
416 printf("Filesystem cylinder groups approximately %ld\n",
419 data_blocks_per_cyl = blksize * 8 -
420 (fsinfo.f_files / 8 / cylgroups) - 3;
422 last_device = st.st_dev;
424 width = int_log10(fsinfo.f_blocks);
425 if (width > physical_width)
426 physical_width = width;
428 numblocks = (st.st_size + blksize - 1) / blksize;
430 blk_shift = int_log2(blocksize);
432 blk_shift = int_log2(blksize);
434 width = int_log10(numblocks);
435 if (width > logical_width)
436 logical_width = width;
438 printf("File size of %s is %llu (%llu block%s of %d bytes)\n",
439 filename, (unsigned long long)st.st_size,
440 numblocks * blksize >> blk_shift,
441 numblocks == 1 ? "" : "s", 1 << blk_shift);
444 rc = filefrag_fiemap(fd, blk_shift, &num_extents, &st);
448 if (force_bmap || rc < 0) { /* FIEMAP failed, try FIBMAP instead */
449 expected = filefrag_fibmap(fd, blk_shift, &num_extents,
450 &st, numblocks, is_ext2);
452 if (expected == -EINVAL || expected == -ENOTTY) {
453 fprintf(stderr, "%s: FIBMAP unsupported\n",
455 } else if (expected == -EPERM) {
457 "%s: FIBMAP requires root privileges\n",
460 fprintf(stderr, "%s: FIBMAP error: %s",
461 filename, strerror(expected));
468 expected = expected / data_blocks_per_cyl + 1;
471 if (num_extents == 1)
472 printf("%s: 1 extent found", filename);
474 printf("%s: %d extents found", filename, num_extents);
475 /* count, and thus expected, only set for indirect FIBMAP'd files */
476 if (is_ext2 && expected && expected < num_extents)
477 printf(", perfection would be %d extent%s\n", expected,
478 (expected > 1) ? "s" : "");
487 static void usage(const char *progname)
489 fprintf(stderr, "Usage: %s [-b{blocksize}] [-BeklsvxX] file ...\n",
494 int main(int argc, char**argv)
499 while ((c = getopt(argc, argv, "Bb::eksvxX")) != EOF) {
507 blocksize = strtoul(optarg, &end, 0);
526 } else { /* Allow -b without argument for compat. Remove
527 * this eventually so "-b {blocksize}" works */
528 fprintf(stderr, "%s: -b needs a blocksize "
529 "option, assuming 1024-byte blocks.\n",
563 for (cpp = argv + optind; *cpp != '\0'; cpp++) {
564 int rc2 = frag_report(*cpp);
566 if (rc2 < 0 && rc == 0)