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
18 fputs("This program is only supported on Linux!\n", stderr);
22 #define _LARGEFILE64_SOURCE
37 #include <sys/types.h>
40 #include <sys/ioctl.h>
42 #include <ext2fs/ext2_types.h>
43 #include <ext2fs/fiemap.h>
46 int no_bs = 0; /* Don't use the files blocksize, use 1K blocksize */
47 int sync_file = 0; /* fsync file before getting the mapping */
48 int xattr_map = 0; /* get xattr mapping */
50 int logical_width = 12;
51 int physical_width = 14;
52 unsigned long long filesize;
54 #define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
56 #define FIBMAP _IO(0x00, 1) /* bmap access */
57 #define FIGETBSZ _IO(0x00, 2) /* get the block size used for bmap */
59 #define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
60 #define EXT3_IOC_GETFLAGS _IOR('f', 1, long)
62 static int int_log2(int arg)
74 static int int_log10(unsigned long long arg)
86 static unsigned int div_ceil(unsigned int a, unsigned int b)
90 return ((a - 1) / b) + 1;
93 static int get_bmap(int fd, unsigned long block, unsigned long *phy_blk)
99 ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes pointer to integer */
101 if (errno == EPERM) {
102 fprintf(stderr, "No permission to use FIBMAP ioctl; "
103 "must have root privileges\n");
113 static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
114 unsigned long long expected, int blk_shift)
117 unsigned long long logical_blk;
118 unsigned long ext_len;
119 char flags[256] = "";
121 /* For inline data all offsets should be in terms of bytes, not blocks */
122 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
125 ext_len = fm_extent->fe_length >> blk_shift;
126 logical_blk = fm_extent->fe_logical >> blk_shift;
127 phy_blk = fm_extent->fe_physical >> blk_shift;
129 if (fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN)
130 strcat(flags, "unknown,");
131 if (fm_extent->fe_flags & FIEMAP_EXTENT_DELALLOC)
132 strcat(flags, "delalloc,");
133 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_ENCRYPTED)
134 strcat(flags, "encrypted,");
135 if (fm_extent->fe_flags & FIEMAP_EXTENT_NOT_ALIGNED)
136 strcat(flags, "not_aligned,");
137 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
138 strcat(flags, "inline,");
139 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_TAIL)
140 strcat(flags, "tail_packed,");
141 if (fm_extent->fe_flags & FIEMAP_EXTENT_UNWRITTEN)
142 strcat(flags, "unwritten,");
143 if (fm_extent->fe_flags & FIEMAP_EXTENT_MERGED)
144 strcat(flags, "merged,");
146 if (fm_extent->fe_logical + fm_extent->fe_length >= filesize)
147 strcat(flags, "eof,");
149 /* Remove trailing comma, if any */
151 flags[strlen(flags) - 1] = '\0';
154 printf("%4d %*llu %*llu %*llu %6lu %s\n",
155 cur_ex, logical_width, logical_blk,
156 physical_width, phy_blk, physical_width, expected,
159 printf("%4d %*llu %*llu %*s %6lu %s\n",
160 cur_ex, logical_width, logical_blk,
161 physical_width, phy_blk, physical_width, "",
165 static int filefrag_fiemap(int fd, int blk_shift, int *num_extents)
168 struct fiemap *fiemap = (struct fiemap *)buf;
169 struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
170 int count = (sizeof(buf) - sizeof(*fiemap)) /
171 sizeof(struct fiemap_extent);
172 unsigned long long last_blk = 0;
173 unsigned long flags = 0;
175 static int fiemap_incompat_printed;
176 int fiemap_header_printed = 0;
177 int tot_extents = 1, n = 0;
181 fiemap->fm_length = ~0ULL;
183 memset(fiemap, 0, sizeof(struct fiemap));
189 flags |= FIEMAP_FLAG_SYNC;
192 flags |= FIEMAP_FLAG_XATTR;
195 fiemap->fm_length = ~0ULL;
196 fiemap->fm_flags = flags;
197 fiemap->fm_extent_count = count;
198 rc = ioctl(fd, FS_IOC_FIEMAP, (unsigned long) fiemap);
200 if (errno == EBADR && fiemap_incompat_printed == 0) {
201 printf("FIEMAP failed with unsupported "
202 "flags %x\n", fiemap->fm_flags);
203 fiemap_incompat_printed = 1;
208 if (verbose && !fiemap_header_printed) {
209 printf(" ext %*s %*s %*s length flags\n", logical_width,
210 "logical", physical_width, "physical",
211 physical_width, "expected");
212 fiemap_header_printed = 1;
216 *num_extents = fiemap->fm_mapped_extents;
220 /* If 0 extents are returned, then more ioctls are not needed */
221 if (fiemap->fm_mapped_extents == 0)
224 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
225 __u64 phy_blk, logical_blk;
226 unsigned long ext_len;
228 phy_blk = fm_ext[i].fe_physical >> blk_shift;
229 ext_len = fm_ext[i].fe_length >> blk_shift;
230 logical_blk = fm_ext[i].fe_logical >> blk_shift;
232 if (logical_blk && phy_blk != last_blk + 1)
236 print_extent_info(&fm_ext[i], n, last_blk, blk_shift);
238 last_blk = phy_blk + ext_len - 1;
239 if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
244 fiemap->fm_start = (fm_ext[i-1].fe_logical +
245 fm_ext[i-1].fe_length);
248 *num_extents = tot_extents;
253 #define EXT2_DIRECT 12
255 static void frag_report(const char *filename)
257 struct statfs fsinfo;
259 struct stat64 fileinfo;
261 struct stat fileinfo;
265 unsigned long block, last_block = 0, numblocks, i, count;
266 long bpib; /* Blocks per indirect block */
268 int num_extents = 0, expected;
275 fd = open64(filename, O_RDONLY);
277 fd = open(filename, O_RDONLY);
284 if (statfs(filename, &fsinfo) < 0) {
289 if (stat64(filename, &fileinfo) < 0) {
291 if (stat(filename, &fileinfo) < 0) {
296 if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) < 0)
298 if (!(flags & EXT4_EXTENTS_FL) &&
299 ((fsinfo.f_type == 0xef51) || (fsinfo.f_type == 0xef52) ||
300 (fsinfo.f_type == 0xef53)))
303 printf("Filesystem type is: %lx\n",
304 (unsigned long) fsinfo.f_type);
306 cylgroups = div_ceil(fsinfo.f_blocks, fsinfo.f_bsize*8);
307 if (verbose && is_ext2 && once)
308 printf("Filesystem cylinder groups is approximately %ld\n",
311 physical_width = int_log10(fsinfo.f_blocks);
312 if (physical_width < 8)
315 if (ioctl(fd, FIGETBSZ, &bs) < 0) { /* FIGETBSZ takes an int */
325 numblocks = (fileinfo.st_size + (bs-1)) / bs;
326 logical_width = int_log10(numblocks);
327 if (logical_width < 7)
329 filesize = (long long)fileinfo.st_size;
331 printf("File size of %s is %lld (%ld block%s, blocksize %d)\n",
332 filename, (long long) fileinfo.st_size, numblocks,
333 numblocks == 1 ? "" : "s", bs);
335 filefrag_fiemap(fd, int_log2(bs), &num_extents) != 0) {
336 for (i = 0, count = 0; i < numblocks; i++) {
337 if (is_ext2 && last_block) {
338 if (((i-EXT2_DIRECT) % bpib) == 0)
340 if (((i-EXT2_DIRECT-bpib) % (bpib*bpib)) == 0)
342 if (((i-EXT2_DIRECT-bpib-bpib*bpib) %
343 (((__u64) bpib)*bpib*bpib)) == 0)
346 rc = get_bmap(fd, i, &block);
352 if (last_block && (block != last_block+1) ) {
354 printf("Discontinuity: Block %ld is at "
356 i, block, last_block+1);
362 if (num_extents == 1)
363 printf("%s: 1 extent found", filename);
365 printf("%s: %d extents found", filename, num_extents);
366 expected = (count/((bs*8)-(fsinfo.f_files/8/cylgroups)-3))+1;
367 if (is_ext2 && expected < num_extents)
368 printf(", perfection would be %d extent%s\n", expected,
369 (expected>1) ? "s" : "");
376 static void usage(const char *progname)
378 fprintf(stderr, "Usage: %s [-Bbvsx] file ...\n", progname);
382 int main(int argc, char**argv)
387 while ((c = getopt(argc, argv, "Bbsvx")) != EOF)
410 for (cpp=argv+optind; *cpp; cpp++)