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 
12 #include "config.h"
13 #ifndef __linux__
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 
main(void)18 int main(void) {
19 	fputs("This program is only supported on Linux!\n", stderr);
20 	exit(EXIT_FAILURE);
21 }
22 #else
23 #ifndef _LARGEFILE_SOURCE
24 #define _LARGEFILE_SOURCE
25 #endif
26 #ifndef _LARGEFILE64_SOURCE
27 #define _LARGEFILE64_SOURCE
28 #endif
29 
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <time.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #ifdef HAVE_GETOPT_H
39 #include <getopt.h>
40 #else
41 extern char *optarg;
42 extern int optind;
43 #endif
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/vfs.h>
47 #include <sys/ioctl.h>
48 #ifdef HAVE_LINUX_FD_H
49 #include <linux/fd.h>
50 #endif
51 #include <ext2fs/ext2fs.h>
52 #include <ext2fs/ext2_types.h>
53 #include <ext2fs/fiemap.h>
54 #include "../version.h"
55 
56 int verbose = 0;
57 unsigned int blocksize;	/* Use specified blocksize (default 1kB) */
58 int sync_file = 0;	/* fsync file before getting the mapping */
59 int precache_file = 0;	/* precache the file before getting the mapping */
60 int xattr_map = 0;	/* get xattr mapping */
61 int force_bmap;		/* force use of FIBMAP instead of FIEMAP */
62 int force_extent;	/* print output in extent format always */
63 int use_extent_cache;	/* Use extent cache */
64 int logical_width = 8;
65 int physical_width = 10;
66 const char *ext_fmt = "%4d: %*llu..%*llu: %*llu..%*llu: %6llu: %s\n";
67 const char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
68 
69 #define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
70 
71 #define FIBMAP		_IO(0x00, 1)	/* bmap access */
72 #define FIGETBSZ	_IO(0x00, 2)	/* get the block size used for bmap */
73 
74 #define LUSTRE_SUPER_MAGIC 0x0BD00BD0
75 
76 #define	EXT4_EXTENTS_FL			0x00080000 /* Inode uses extents */
77 #define	EXT3_IOC_GETFLAGS		_IOR('f', 1, long)
78 
ulong_log2(unsigned long arg)79 static int ulong_log2(unsigned long arg)
80 {
81 	int     l = 0;
82 
83 	arg >>= 1;
84 	while (arg) {
85 		l++;
86 		arg >>= 1;
87 	}
88 	return l;
89 }
90 
ulong_log10(unsigned long long arg)91 static int ulong_log10(unsigned long long arg)
92 {
93 	int     l = 0;
94 
95 	arg = arg / 10;
96 	while (arg) {
97 		l++;
98 		arg = arg / 10;
99 	}
100 	return l;
101 }
102 
div_ceil(unsigned int a,unsigned int b)103 static unsigned int div_ceil(unsigned int a, unsigned int b)
104 {
105 	if (!a)
106 		return 0;
107 	return ((a - 1) / b) + 1;
108 }
109 
get_bmap(int fd,unsigned long block,unsigned long * phy_blk)110 static int get_bmap(int fd, unsigned long block, unsigned long *phy_blk)
111 {
112 	int	ret;
113 	unsigned int b;
114 
115 	b = block;
116 	ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes pointer to integer */
117 	if (ret < 0)
118 		return -errno;
119 	*phy_blk = b;
120 
121 	return ret;
122 }
123 
print_extent_header(void)124 static void print_extent_header(void)
125 {
126 	printf(" ext: %*s %*s length: %*s flags:\n",
127 	       logical_width * 2 + 3,
128 	       "logical_offset:",
129 	       physical_width * 2 + 3, "physical_offset:",
130 	       physical_width + 1,
131 	       "expected:");
132 }
133 
print_flag(__u32 * flags,__u32 mask,char * buf,const char * name)134 static void print_flag(__u32 *flags, __u32 mask, char *buf, const char *name)
135 {
136 	char hex[sizeof(mask) * 2 + 4]; /* 2 chars/byte + 0x, + NUL */
137 
138 	if ((*flags & mask) == 0)
139 		return;
140 
141 	if (name == NULL) {
142 		sprintf(hex, "%#04x,", mask);
143 		name = hex;
144 	}
145 	strcat(buf, name);
146 	*flags &= ~mask;
147 }
148 
print_flags(__u32 fe_flags,char * flags,int len,int print_unknown)149 static void print_flags(__u32 fe_flags, char *flags, int len, int print_unknown)
150 {
151 	__u32 mask;
152 
153 	print_flag(&fe_flags, FIEMAP_EXTENT_LAST, flags, "last,");
154 	print_flag(&fe_flags, FIEMAP_EXTENT_UNKNOWN, flags, "unknown_loc,");
155 	print_flag(&fe_flags, FIEMAP_EXTENT_DELALLOC, flags, "delalloc,");
156 	print_flag(&fe_flags, FIEMAP_EXTENT_ENCODED, flags, "encoded,");
157 	print_flag(&fe_flags, FIEMAP_EXTENT_DATA_ENCRYPTED, flags,"encrypted,");
158 	print_flag(&fe_flags, FIEMAP_EXTENT_NOT_ALIGNED, flags, "not_aligned,");
159 	print_flag(&fe_flags, FIEMAP_EXTENT_DATA_INLINE, flags, "inline,");
160 	print_flag(&fe_flags, FIEMAP_EXTENT_DATA_TAIL, flags, "tail_packed,");
161 	print_flag(&fe_flags, FIEMAP_EXTENT_UNWRITTEN, flags, "unwritten,");
162 	print_flag(&fe_flags, FIEMAP_EXTENT_MERGED, flags, "merged,");
163 	print_flag(&fe_flags, FIEMAP_EXTENT_SHARED, flags, "shared,");
164 	print_flag(&fe_flags, EXT4_FIEMAP_EXTENT_HOLE, flags, "hole,");
165 
166 	if (!print_unknown)
167 		goto out;
168 
169 	/* print any unknown flags as hex values */
170 	for (mask = 1; fe_flags != 0 && mask != 0; mask <<= 1)
171 		print_flag(&fe_flags, mask, flags, NULL);
172 out:
173 	/* Remove trailing comma, if any */
174 	if (flags[0])
175 		flags[strnlen(flags, len) - 1] = '\0';
176 
177 }
178 
print_extent_info(struct fiemap_extent * fm_extent,int cur_ex,unsigned long long expected,int blk_shift,ext2fs_struct_stat * st)179 static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
180 			      unsigned long long expected, int blk_shift,
181 			      ext2fs_struct_stat *st)
182 {
183 	unsigned long long physical_blk;
184 	unsigned long long logical_blk;
185 	unsigned long long ext_len;
186 	unsigned long long ext_blks;
187 	unsigned long long ext_blks_phys;
188 	char flags[256] = "";
189 
190 	/* For inline data all offsets should be in bytes, not blocks */
191 	if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
192 		blk_shift = 0;
193 
194 	ext_len = fm_extent->fe_length >> blk_shift;
195 	ext_blks = (fm_extent->fe_length - 1) >> blk_shift;
196 	logical_blk = fm_extent->fe_logical >> blk_shift;
197 	if (fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN) {
198 		physical_blk = 0;
199 	} else {
200 		physical_blk = fm_extent->fe_physical >> blk_shift;
201 	}
202 
203 	if (expected &&
204 	    !(fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN) &&
205 	    !(fm_extent->fe_flags & EXT4_FIEMAP_EXTENT_HOLE))
206 		sprintf(flags, ext_fmt == hex_fmt ? "%*llx: " : "%*llu: ",
207 			physical_width, expected >> blk_shift);
208 	else
209 		sprintf(flags, "%.*s  ", physical_width, "                   ");
210 
211 	print_flags(fm_extent->fe_flags, flags, sizeof(flags), 1);
212 
213 	if (fm_extent->fe_logical + fm_extent->fe_length >=
214 	    (unsigned long long)st->st_size)
215 		strcat(flags, flags[0] ? ",eof" : "eof");
216 
217 	if ((fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN) ||
218 	    (fm_extent->fe_flags & EXT4_FIEMAP_EXTENT_HOLE)) {
219 		ext_len = 0;
220 		ext_blks_phys = 0;
221 	} else
222 		ext_blks_phys = ext_blks;
223 
224 	printf(ext_fmt, cur_ex, logical_width, logical_blk,
225 	       logical_width, logical_blk + ext_blks,
226 	       physical_width, physical_blk,
227 	       physical_width, physical_blk + ext_blks_phys,
228 	       ext_len, flags);
229 }
230 
filefrag_fiemap(int fd,int blk_shift,int * num_extents,ext2fs_struct_stat * st)231 static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
232 			   ext2fs_struct_stat *st)
233 {
234 	__u64 buf[2048];	/* __u64 for proper field alignment */
235 	struct fiemap *fiemap = (struct fiemap *)buf;
236 	struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
237 	struct fiemap_extent fm_last;
238 	int count = (sizeof(buf) - sizeof(*fiemap)) /
239 			sizeof(struct fiemap_extent);
240 	unsigned long long expected = 0;
241 	unsigned long long expected_dense = 0;
242 	unsigned long flags = 0;
243 	unsigned int i;
244 	unsigned long cmd = FS_IOC_FIEMAP;
245 	int fiemap_header_printed = 0;
246 	int tot_extents = 0, n = 0;
247 	int last = 0;
248 	int rc;
249 
250 	memset(fiemap, 0, sizeof(struct fiemap));
251 	memset(&fm_last, 0, sizeof(fm_last));
252 
253 	if (sync_file)
254 		flags |= FIEMAP_FLAG_SYNC;
255 
256 	if (precache_file)
257 		flags |= FIEMAP_FLAG_CACHE;
258 
259 	if (xattr_map)
260 		flags |= FIEMAP_FLAG_XATTR;
261 
262 	if (use_extent_cache)
263 		cmd = EXT4_IOC_GET_ES_CACHE;
264 
265 	do {
266 		fiemap->fm_length = ~0ULL;
267 		fiemap->fm_flags = flags;
268 		fiemap->fm_extent_count = count;
269 		rc = ioctl(fd, cmd, (unsigned long) fiemap);
270 		if (rc < 0) {
271 			static int fiemap_incompat_printed;
272 
273 			rc = -errno;
274 			if (rc == -EBADR && !fiemap_incompat_printed) {
275 				fprintf(stderr, "FIEMAP failed with unknown "
276 						"flags %x\n",
277 				       fiemap->fm_flags);
278 				fiemap_incompat_printed = 1;
279 			}
280 			return rc;
281 		}
282 
283 		/* If 0 extents are returned, then more ioctls are not needed */
284 		if (fiemap->fm_mapped_extents == 0)
285 			break;
286 
287 		if (verbose && !fiemap_header_printed) {
288 			print_extent_header();
289 			fiemap_header_printed = 1;
290 		}
291 
292 		for (i = 0; i < fiemap->fm_mapped_extents; i++) {
293 			expected_dense = fm_last.fe_physical +
294 					 fm_last.fe_length;
295 			expected = fm_last.fe_physical +
296 				   fm_ext[i].fe_logical - fm_last.fe_logical;
297 			if (fm_ext[i].fe_logical != 0 &&
298 			    fm_ext[i].fe_physical != expected &&
299 			    fm_ext[i].fe_physical != expected_dense) {
300 				tot_extents++;
301 			} else {
302 				expected = 0;
303 				if (!tot_extents)
304 					tot_extents = 1;
305 			}
306 			if (verbose)
307 				print_extent_info(&fm_ext[i], n, expected,
308 						  blk_shift, st);
309 			if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
310 				last = 1;
311 			fm_last = fm_ext[i];
312 			n++;
313 		}
314 
315 		fiemap->fm_start = (fm_ext[i - 1].fe_logical +
316 				    fm_ext[i - 1].fe_length);
317 	} while (last == 0);
318 
319 	*num_extents = tot_extents;
320 
321 	return 0;
322 }
323 
324 #define EXT2_DIRECT	12
325 
filefrag_fibmap(int fd,int blk_shift,int * num_extents,ext2fs_struct_stat * st,unsigned long numblocks,int is_ext2)326 static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
327 			   ext2fs_struct_stat *st,
328 			   unsigned long numblocks, int is_ext2)
329 {
330 	struct fiemap_extent	fm_ext, fm_last;
331 	unsigned long		i, last_block;
332 	unsigned long long	logical, expected = 0;
333 				/* Blocks per indirect block */
334 	const long		bpib = st->st_blksize / 4;
335 	int			count;
336 
337 	memset(&fm_ext, 0, sizeof(fm_ext));
338 	memset(&fm_last, 0, sizeof(fm_last));
339 	if (force_extent) {
340 		fm_ext.fe_flags = FIEMAP_EXTENT_MERGED;
341 	}
342 
343 	if (sync_file && fsync(fd) != 0)
344 		return -errno;
345 
346 	for (i = 0, logical = 0, *num_extents = 0, count = last_block = 0;
347 	     i < numblocks;
348 	     i++, logical += st->st_blksize) {
349 		unsigned long block = 0;
350 		int rc;
351 
352 		if (is_ext2 && last_block) {
353 			if (((i - EXT2_DIRECT) % bpib) == 0)
354 				last_block++;
355 			if (((i - EXT2_DIRECT - bpib) % (bpib * bpib)) == 0)
356 				last_block++;
357 			if (((i - EXT2_DIRECT - bpib - bpib * bpib) %
358 			     (((unsigned long long)bpib) * bpib * bpib)) == 0)
359 				last_block++;
360 		}
361 		rc = get_bmap(fd, i, &block);
362 		if (rc < 0)
363 			return rc;
364 		if (block == 0)
365 			continue;
366 
367 		if (*num_extents == 0 || block != last_block + 1 ||
368 		    fm_ext.fe_logical + fm_ext.fe_length != logical) {
369 			/*
370 			 * This is the start of a new extent; figure out where
371 			 * we expected it to be and report the extent.
372 			 */
373 			if (*num_extents != 0 && fm_last.fe_length) {
374 				expected = fm_last.fe_physical +
375 					(fm_ext.fe_logical - fm_last.fe_logical);
376 				if (expected == fm_ext.fe_physical)
377 					expected = 0;
378 			}
379 			if (force_extent && *num_extents == 0)
380 				print_extent_header();
381 			if (force_extent && *num_extents != 0) {
382 				print_extent_info(&fm_ext, *num_extents - 1,
383 						  expected, blk_shift, st);
384 			}
385 			if (verbose && expected != 0) {
386 				printf("Discontinuity: Block %llu is at %llu "
387 				       "(was %llu)\n",
388 				       (unsigned long long) (fm_ext.fe_logical / st->st_blksize),
389 				       (unsigned long long) (fm_ext.fe_physical / st->st_blksize),
390 				       (unsigned long long) (expected / st->st_blksize));
391 			}
392 			/* create the new extent */
393 			fm_last = fm_ext;
394 			(*num_extents)++;
395 			fm_ext.fe_physical = block * st->st_blksize;
396 			fm_ext.fe_logical = logical;
397 			fm_ext.fe_length = 0;
398 		}
399 		fm_ext.fe_length += st->st_blksize;
400 		last_block = block;
401 	}
402 	if (force_extent && *num_extents != 0) {
403 		if (fm_last.fe_length) {
404 			expected = fm_last.fe_physical +
405 				   (fm_ext.fe_logical - fm_last.fe_logical);
406 			if (expected == fm_ext.fe_physical)
407 				expected = 0;
408 		}
409 		print_extent_info(&fm_ext, *num_extents - 1, expected,
410 				  blk_shift, st);
411 	}
412 
413 	return count;
414 }
415 
frag_report(const char * filename)416 static int frag_report(const char *filename)
417 {
418 	static struct statfs fsinfo;
419 	static unsigned int blksize;
420 	ext2fs_struct_stat st;
421 	int		blk_shift;
422 	long		fd;
423 	unsigned long long	numblocks;
424 	int		data_blocks_per_cyl = 1;
425 	int		num_extents = 1, expected = ~0;
426 	int		is_ext2 = 0;
427 	static dev_t	last_device;
428 	int		width;
429 	int		rc = 0;
430 
431 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
432 	fd = open64(filename, O_RDONLY);
433 #else
434 	fd = open(filename, O_RDONLY);
435 #endif
436 	if (fd < 0) {
437 		rc = -errno;
438 		perror("open");
439 		return rc;
440 	}
441 
442 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
443 	if (fstat64(fd, &st) < 0) {
444 #else
445 	if (fstat(fd, &st) < 0) {
446 #endif
447 		rc = -errno;
448 		perror("stat");
449 		goto out_close;
450 	}
451 
452 	if ((last_device != st.st_dev) || !st.st_dev) {
453 		if (fstatfs(fd, &fsinfo) < 0) {
454 			rc = -errno;
455 			perror("fstatfs");
456 			goto out_close;
457 		}
458 		if ((ioctl(fd, FIGETBSZ, &blksize) < 0) || !blksize)
459 			blksize = fsinfo.f_bsize;
460 		if (verbose)
461 			printf("Filesystem type is: %lx\n",
462 			       (unsigned long)fsinfo.f_type);
463 	}
464 	st.st_blksize = blksize;
465 	if (fsinfo.f_type == 0xef51 || fsinfo.f_type == 0xef52 ||
466 	    fsinfo.f_type == 0xef53) {
467 		unsigned int	flags;
468 
469 		if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) == 0 &&
470 		    !(flags & EXT4_EXTENTS_FL))
471 			is_ext2 = 1;
472 	}
473 
474 	if (is_ext2) {
475 		long cylgroups = div_ceil(fsinfo.f_blocks, blksize * 8);
476 
477 		if (verbose && last_device != st.st_dev)
478 			printf("Filesystem cylinder groups approximately %ld\n",
479 			       cylgroups);
480 
481 		data_blocks_per_cyl = blksize * 8 -
482 					(fsinfo.f_files / 8 / cylgroups) - 3;
483 	}
484 	last_device = st.st_dev;
485 
486 	width = ulong_log10(fsinfo.f_blocks);
487 	if (width > physical_width)
488 		physical_width = width;
489 
490 	numblocks = (st.st_size + blksize - 1) / blksize;
491 	if (blocksize != 0)
492 		blk_shift = ulong_log2(blocksize);
493 	else
494 		blk_shift = ulong_log2(blksize);
495 
496 	if (use_extent_cache)
497 		width = 10;
498 	else
499 		width = ulong_log10(numblocks);
500 	if (width > logical_width)
501 		logical_width = width;
502 	if (verbose) {
503 		__u32 state;
504 
505 		printf("File size of %s is %llu (%llu block%s of %d bytes)",
506 		       filename, (unsigned long long) st.st_size,
507 		       (unsigned long long) (numblocks * blksize >> blk_shift),
508 		       numblocks == 1 ? "" : "s", 1 << blk_shift);
509 		if (use_extent_cache &&
510 		    ioctl(fd, EXT4_IOC_GETSTATE, &state) == 0 &&
511 		    (state & EXT4_STATE_FLAG_EXT_PRECACHED))
512 			fputs(" -- pre-cached", stdout);
513 		fputc('\n', stdout);
514 	}
515 
516 	if (!force_bmap) {
517 		rc = filefrag_fiemap(fd, blk_shift, &num_extents, &st);
518 		expected = 0;
519 		if (rc < 0 &&
520 		    (use_extent_cache || precache_file || xattr_map)) {
521 			if (rc != -EBADR)
522 				fprintf(stderr, "%s: %s: %s\n ",
523 					filename,
524 					use_extent_cache ?
525 					"EXT4_IOC_GET_ES_CACHE" :
526 					"FS_IOC_FIEMAP", strerror(-rc));
527 			goto out_close;
528 		}
529 	}
530 
531 	if (force_bmap || rc < 0) { /* FIEMAP failed, try FIBMAP instead */
532 		expected = filefrag_fibmap(fd, blk_shift, &num_extents,
533 					   &st, numblocks, is_ext2);
534 		if (expected < 0) {
535 			if (expected == -EINVAL || expected == -ENOTTY) {
536 				fprintf(stderr, "%s: FIBMAP%s unsupported\n",
537 					filename, force_bmap ? "" : "/FIEMAP");
538 			} else if (expected == -EPERM) {
539 				fprintf(stderr,
540 					"%s: FIBMAP requires root privileges\n",
541 					filename);
542 			} else {
543 				fprintf(stderr, "%s: FIBMAP error: %s",
544 					filename, strerror(expected));
545 			}
546 			rc = expected;
547 			goto out_close;
548 		} else {
549 			rc = 0;
550 		}
551 		expected = expected / data_blocks_per_cyl + 1;
552 	}
553 
554 	if (num_extents == 1)
555 		printf("%s: 1 extent found", filename);
556 	else
557 		printf("%s: %d extents found", filename, num_extents);
558 	/* count, and thus expected, only set for indirect FIBMAP'd files */
559 	if (is_ext2 && expected && expected < num_extents)
560 		printf(", perfection would be %d extent%s\n", expected,
561 			(expected > 1) ? "s" : "");
562 	else
563 		fputc('\n', stdout);
564 out_close:
565 	close(fd);
566 
567 	return rc;
568 }
569 
570 static void usage(const char *progname)
571 {
572 	fprintf(stderr, "Usage: %s [-b{blocksize}[KMG]] [-BeEksvxX] file ...\n",
573 		progname);
574 	exit(1);
575 }
576 
577 int main(int argc, char**argv)
578 {
579 	char **cpp;
580 	int rc = 0, c;
581 	int version = 0;
582 
583 	while ((c = getopt(argc, argv, "Bb::eEkPsvVxX")) != EOF) {
584 		switch (c) {
585 		case 'B':
586 			force_bmap++;
587 			break;
588 		case 'b':
589 			if (optarg) {
590 				char *end;
591 				unsigned long val;
592 
593 				val = strtoul(optarg, &end, 0);
594 				if (end) {
595 #if __GNUC_PREREQ (7, 0)
596 #pragma GCC diagnostic push
597 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
598 #endif
599 					switch (end[0]) {
600 					case 'g':
601 					case 'G':
602 						val *= 1024;
603 						/* fall through */
604 					case 'm':
605 					case 'M':
606 						val *= 1024;
607 						/* fall through */
608 					case 'k':
609 					case 'K':
610 						val *= 1024;
611 						break;
612 					default:
613 						break;
614 					}
615 #if __GNUC_PREREQ (7, 0)
616 #pragma GCC diagnostic pop
617 #endif
618 				}
619 				/* Specifying too large a blocksize will just
620 				 * shift all extents down to zero length. Even
621 				 * 1GB is questionable, but caveat emptor. */
622 				if (val > 1024 * 1024 * 1024) {
623 					fprintf(stderr,
624 						"%s: blocksize %lu over 1GB\n",
625 						argv[0], val);
626 					usage(argv[0]);
627 				}
628 				blocksize = val;
629 			} else { /* Allow -b without argument for compat. Remove
630 				  * this eventually so "-b {blocksize}" works */
631 				fprintf(stderr, "%s: -b needs a blocksize "
632 					"option, assuming 1024-byte blocks.\n",
633 					argv[0]);
634 				blocksize = 1024;
635 			}
636 			break;
637 		case 'E':
638 			use_extent_cache++;
639 			/* fallthrough */
640 		case 'e':
641 			force_extent++;
642 			if (!verbose)
643 				verbose++;
644 			break;
645 		case 'k':
646 			blocksize = 1024;
647 			break;
648 		case 'P':
649 			precache_file++;
650 			break;
651 		case 's':
652 			sync_file++;
653 			break;
654 		case 'v':
655 			verbose++;
656 			break;
657 		case 'V':
658 			version++;
659 			break;
660 		case 'x':
661 			xattr_map++;
662 			break;
663 		case 'X':
664 			ext_fmt = hex_fmt;
665 			break;
666 		default:
667 			usage(argv[0]);
668 			break;
669 		}
670 	}
671 	if (version) {
672 		/* Print version number and exit */
673 		printf("filefrag %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
674 		if (version + verbose > 1) {
675 			char flags[256] = "";
676 
677 			print_flags(0xffffffff, flags, sizeof(flags), 0);
678 			printf("supported: %s\n", flags);
679 		}
680 		exit(0);
681 	}
682 
683 	if (optind == argc)
684 		usage(argv[0]);
685 
686 	for (cpp = argv + optind; *cpp != NULL; cpp++) {
687 		int rc2 = frag_report(*cpp);
688 
689 		if (rc2 < 0 && rc == 0)
690 			rc = rc2;
691 	}
692 
693 	return -rc;
694 }
695 #endif
696