blob: 1a30a28b602f553add9b4485733eef69745789df [file] [log] [blame]
Theodore Ts'o96424132003-12-17 10:13:41 -05001/*
2 * filefrag.c -- report if a particular file is fragmented
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o96424132003-12-17 10:13:41 -05004 * 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'od605ba12004-02-03 19:18:03 -050012#ifndef __linux__
Matthias Andreeb34cbdd2003-12-28 18:21:26 +010013#include <stdio.h>
14#include <stdlib.h>
Kalpak Shahe62847c2009-05-02 21:02:29 -040015#include <unistd.h>
Matthias Andreeb34cbdd2003-12-28 18:21:26 +010016
17int main(void) {
JP Abgralle0ed7402014-03-19 19:08:39 -070018 fputs("This program is only supported on Linux!\n", stderr);
19 exit(EXIT_FAILURE);
Matthias Andreeb34cbdd2003-12-28 18:21:26 +010020}
21#else
Theodore Ts'o96424132003-12-17 10:13:41 -050022#define _LARGEFILE64_SOURCE
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <string.h>
28#include <time.h>
29#include <fcntl.h>
30#include <errno.h>
Theodore Ts'o6b394c12004-02-26 21:08:06 -050031#ifdef HAVE_GETOPT_H
32#include <getopt.h>
33#else
34extern char *optarg;
35extern int optind;
36#endif
Theodore Ts'o96424132003-12-17 10:13:41 -050037#include <sys/types.h>
38#include <sys/stat.h>
39#include <sys/vfs.h>
Matthias Andreeaa3a2fe2003-12-21 00:52:48 +010040#include <sys/ioctl.h>
Theodore Ts'o96424132003-12-17 10:13:41 -050041#include <linux/fd.h>
JP Abgralle0ed7402014-03-19 19:08:39 -070042#include <ext2fs/ext2fs.h>
Kalpak Shahe62847c2009-05-02 21:02:29 -040043#include <ext2fs/ext2_types.h>
44#include <ext2fs/fiemap.h>
Theodore Ts'o96424132003-12-17 10:13:41 -050045
46int verbose = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -070047int blocksize; /* Use specified blocksize (default 1kB) */
Kalpak Shahe62847c2009-05-02 21:02:29 -040048int sync_file = 0; /* fsync file before getting the mapping */
49int xattr_map = 0; /* get xattr mapping */
JP Abgralle0ed7402014-03-19 19:08:39 -070050int force_bmap; /* force use of FIBMAP instead of FIEMAP */
51int force_extent; /* print output in extent format always */
52int logical_width = 8;
53int physical_width = 10;
54const char *ext_fmt = "%4d: %*llu..%*llu: %*llu..%*llu: %6llu: %s\n";
55const char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
Theodore Ts'o96424132003-12-17 10:13:41 -050056
Kalpak Shahe62847c2009-05-02 21:02:29 -040057#define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
Theodore Ts'o96424132003-12-17 10:13:41 -050058
Kalpak Shahe62847c2009-05-02 21:02:29 -040059#define FIBMAP _IO(0x00, 1) /* bmap access */
60#define FIGETBSZ _IO(0x00, 2) /* get the block size used for bmap */
JP Abgralle0ed7402014-03-19 19:08:39 -070061
62#define LUSTRE_SUPER_MAGIC 0x0BD00BD0
Andreas Dilger2508eaa2012-11-22 15:06:12 +000063
Kalpak Shahe62847c2009-05-02 21:02:29 -040064#define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
Theodore Ts'o3d16b3f2005-03-18 20:37:45 -050065#define EXT3_IOC_GETFLAGS _IOR('f', 1, long)
66
Kalpak Shahe62847c2009-05-02 21:02:29 -040067static int int_log2(int arg)
68{
69 int l = 0;
70
71 arg >>= 1;
72 while (arg) {
73 l++;
74 arg >>= 1;
75 }
76 return l;
77}
78
79static int int_log10(unsigned long long arg)
80{
81 int l = 0;
82
83 arg = arg / 10;
84 while (arg) {
85 l++;
86 arg = arg / 10;
87 }
88 return l;
89}
90
Theodore Ts'o69022e02006-08-30 01:57:00 -040091static unsigned int div_ceil(unsigned int a, unsigned int b)
92{
93 if (!a)
94 return 0;
95 return ((a - 1) / b) + 1;
96}
97
Kalpak Shahe62847c2009-05-02 21:02:29 -040098static int get_bmap(int fd, unsigned long block, unsigned long *phy_blk)
Theodore Ts'o96424132003-12-17 10:13:41 -050099{
100 int ret;
Theodore Ts'o8198e792005-05-20 23:10:35 -0400101 unsigned int b;
Theodore Ts'o96424132003-12-17 10:13:41 -0500102
103 b = block;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400104 ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes pointer to integer */
Theodore Ts'o96424132003-12-17 10:13:41 -0500105 if (ret < 0) {
106 if (errno == EPERM) {
Kalpak Shahe62847c2009-05-02 21:02:29 -0400107 fprintf(stderr, "No permission to use FIBMAP ioctl; "
108 "must have root privileges\n");
Theodore Ts'o96424132003-12-17 10:13:41 -0500109 }
Theodore Ts'o96424132003-12-17 10:13:41 -0500110 }
Kalpak Shahe62847c2009-05-02 21:02:29 -0400111 *phy_blk = b;
112
113 return ret;
114}
115
JP Abgralle0ed7402014-03-19 19:08:39 -0700116static void print_extent_header(void)
Andreas Dilger2508eaa2012-11-22 15:06:12 +0000117{
JP Abgralle0ed7402014-03-19 19:08:39 -0700118 printf(" ext: %*s %*s length: %*s flags:\n",
119 logical_width * 2 + 3,
120 "logical_offset:",
121 physical_width * 2 + 3, "physical_offset:",
122 physical_width + 1,
123 "expected:");
124}
125
126static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
127 unsigned long long expected, int blk_shift,
128 ext2fs_struct_stat *st)
129{
130 unsigned long long physical_blk;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400131 unsigned long long logical_blk;
JP Abgralle0ed7402014-03-19 19:08:39 -0700132 unsigned long long ext_len;
133 unsigned long long ext_blks;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400134 char flags[256] = "";
135
JP Abgralle0ed7402014-03-19 19:08:39 -0700136 /* For inline data all offsets should be in bytes, not blocks */
Kalpak Shahe62847c2009-05-02 21:02:29 -0400137 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
138 blk_shift = 0;
139
140 ext_len = fm_extent->fe_length >> blk_shift;
JP Abgralle0ed7402014-03-19 19:08:39 -0700141 ext_blks = (fm_extent->fe_length - 1) >> blk_shift;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400142 logical_blk = fm_extent->fe_logical >> blk_shift;
JP Abgralle0ed7402014-03-19 19:08:39 -0700143 physical_blk = fm_extent->fe_physical >> blk_shift;
144
145 if (expected)
146 sprintf(flags, ext_fmt == hex_fmt ? "%*llx: " : "%*llu: ",
147 physical_width, expected >> blk_shift);
148 else
149 sprintf(flags, "%.*s ", physical_width, " ");
Kalpak Shahe62847c2009-05-02 21:02:29 -0400150
151 if (fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN)
152 strcat(flags, "unknown,");
153 if (fm_extent->fe_flags & FIEMAP_EXTENT_DELALLOC)
154 strcat(flags, "delalloc,");
155 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_ENCRYPTED)
156 strcat(flags, "encrypted,");
157 if (fm_extent->fe_flags & FIEMAP_EXTENT_NOT_ALIGNED)
158 strcat(flags, "not_aligned,");
159 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
160 strcat(flags, "inline,");
161 if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_TAIL)
162 strcat(flags, "tail_packed,");
163 if (fm_extent->fe_flags & FIEMAP_EXTENT_UNWRITTEN)
164 strcat(flags, "unwritten,");
165 if (fm_extent->fe_flags & FIEMAP_EXTENT_MERGED)
166 strcat(flags, "merged,");
167
JP Abgralle0ed7402014-03-19 19:08:39 -0700168 if (fm_extent->fe_logical + fm_extent->fe_length >= (__u64) st->st_size)
Kalpak Shahe62847c2009-05-02 21:02:29 -0400169 strcat(flags, "eof,");
170
171 /* Remove trailing comma, if any */
172 if (flags[0])
173 flags[strlen(flags) - 1] = '\0';
174
JP Abgralle0ed7402014-03-19 19:08:39 -0700175 printf(ext_fmt, cur_ex, logical_width, logical_blk,
176 logical_width, logical_blk + ext_blks,
177 physical_width, physical_blk,
178 physical_width, physical_blk + ext_blks,
179 ext_len, flags);
Kalpak Shahe62847c2009-05-02 21:02:29 -0400180}
181
JP Abgralle0ed7402014-03-19 19:08:39 -0700182static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
183 ext2fs_struct_stat *st)
Kalpak Shahe62847c2009-05-02 21:02:29 -0400184{
JP Abgralle0ed7402014-03-19 19:08:39 -0700185 char buf[16384];
Kalpak Shahe62847c2009-05-02 21:02:29 -0400186 struct fiemap *fiemap = (struct fiemap *)buf;
187 struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
188 int count = (sizeof(buf) - sizeof(*fiemap)) /
189 sizeof(struct fiemap_extent);
JP Abgralle0ed7402014-03-19 19:08:39 -0700190 unsigned long long expected = 0;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400191 unsigned long flags = 0;
Theodore Ts'o9d4bade2009-07-11 23:44:56 -0400192 unsigned int i;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400193 static int fiemap_incompat_printed;
Eric Sandeen334cfcc2009-08-05 22:30:48 -0500194 int fiemap_header_printed = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700195 int tot_extents = 0, n = 0;
Theodore Ts'o9d4bade2009-07-11 23:44:56 -0400196 int last = 0;
197 int rc;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400198
Kalpak Shahe62847c2009-05-02 21:02:29 -0400199 memset(fiemap, 0, sizeof(struct fiemap));
200
Kalpak Shahe62847c2009-05-02 21:02:29 -0400201 if (sync_file)
202 flags |= FIEMAP_FLAG_SYNC;
203
204 if (xattr_map)
205 flags |= FIEMAP_FLAG_XATTR;
206
Kalpak Shahe62847c2009-05-02 21:02:29 -0400207 do {
208 fiemap->fm_length = ~0ULL;
209 fiemap->fm_flags = flags;
210 fiemap->fm_extent_count = count;
211 rc = ioctl(fd, FS_IOC_FIEMAP, (unsigned long) fiemap);
Theodore Ts'oe78968f2009-06-02 08:45:22 -0400212 if (rc < 0) {
213 if (errno == EBADR && fiemap_incompat_printed == 0) {
Kalpak Shahe62847c2009-05-02 21:02:29 -0400214 printf("FIEMAP failed with unsupported "
215 "flags %x\n", fiemap->fm_flags);
216 fiemap_incompat_printed = 1;
217 }
Kalpak Shahe62847c2009-05-02 21:02:29 -0400218 return rc;
Theodore Ts'oe78968f2009-06-02 08:45:22 -0400219 }
Kalpak Shahe62847c2009-05-02 21:02:29 -0400220
Liu Bo036fda62012-07-28 17:29:13 -0400221 /* If 0 extents are returned, then more ioctls are not needed */
222 if (fiemap->fm_mapped_extents == 0)
223 break;
224
JP Abgralle0ed7402014-03-19 19:08:39 -0700225 if (verbose && !fiemap_header_printed) {
226 print_extent_header();
227 fiemap_header_printed = 1;
228 }
229
Kalpak Shahe62847c2009-05-02 21:02:29 -0400230 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700231 if (fm_ext[i].fe_logical != 0 &&
232 fm_ext[i].fe_physical != expected) {
JP Abgrall65f0aab2014-03-06 13:50:20 -0800233 tot_extents++;
JP Abgralle0ed7402014-03-19 19:08:39 -0700234 } else {
235 expected = 0;
236 if (!tot_extents)
237 tot_extents = 1;
238 }
239 if (verbose)
240 print_extent_info(&fm_ext[i], n, expected,
241 blk_shift, st);
JP Abgrall65f0aab2014-03-06 13:50:20 -0800242
JP Abgralle0ed7402014-03-19 19:08:39 -0700243 expected = fm_ext[i].fe_physical + fm_ext[i].fe_length;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400244 if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
245 last = 1;
246 n++;
247 }
248
JP Abgralle0ed7402014-03-19 19:08:39 -0700249 fiemap->fm_start = (fm_ext[i - 1].fe_logical +
250 fm_ext[i - 1].fe_length);
Kalpak Shahe62847c2009-05-02 21:02:29 -0400251 } while (last == 0);
252
253 *num_extents = tot_extents;
JP Abgralle0ed7402014-03-19 19:08:39 -0700254
Kalpak Shahe62847c2009-05-02 21:02:29 -0400255 return 0;
Theodore Ts'o96424132003-12-17 10:13:41 -0500256}
257
258#define EXT2_DIRECT 12
259
JP Abgralle0ed7402014-03-19 19:08:39 -0700260static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
261 ext2fs_struct_stat *st,
262 unsigned long numblocks, int is_ext2)
263{
264 struct fiemap_extent fm_ext;
265 unsigned long i, last_block;
266 unsigned long long logical;
267 /* Blocks per indirect block */
268 const long bpib = st->st_blksize / 4;
269 int count;
270
271 if (force_extent) {
272 memset(&fm_ext, 0, sizeof(fm_ext));
273 fm_ext.fe_flags = FIEMAP_EXTENT_MERGED;
274 }
275
276 if (sync_file)
277 fsync(fd);
278
279 for (i = 0, logical = 0, *num_extents = 0, count = last_block = 0;
280 i < numblocks;
281 i++, logical += st->st_blksize) {
282 unsigned long block = 0;
283 int rc;
284
285 if (is_ext2 && last_block) {
286 if (((i - EXT2_DIRECT) % bpib) == 0)
287 last_block++;
288 if (((i - EXT2_DIRECT - bpib) % (bpib * bpib)) == 0)
289 last_block++;
290 if (((i - EXT2_DIRECT - bpib - bpib * bpib) %
291 (((unsigned long long)bpib) * bpib * bpib)) == 0)
292 last_block++;
293 }
294 rc = get_bmap(fd, i, &block);
295 if (rc < 0)
296 return rc;
297 if (block == 0)
298 continue;
299 if (*num_extents == 0) {
300 (*num_extents)++;
301 if (force_extent) {
302 print_extent_header();
303 fm_ext.fe_physical = block * st->st_blksize;
304 }
305 }
306 count++;
307 if (force_extent && last_block != 0 &&
308 (block != last_block + 1 ||
309 fm_ext.fe_logical + fm_ext.fe_length != logical)) {
310 print_extent_info(&fm_ext, *num_extents - 1,
311 (last_block + 1) * st->st_blksize,
312 blk_shift, st);
313 fm_ext.fe_logical = logical;
314 fm_ext.fe_physical = block * st->st_blksize;
315 fm_ext.fe_length = 0;
316 (*num_extents)++;
317 } else if (verbose && last_block && (block != last_block + 1)) {
318 printf("Discontinuity: Block %ld is at %lu (was %lu)\n",
319 i, block, last_block + 1);
320 (*num_extents)++;
321 }
322 fm_ext.fe_length += st->st_blksize;
323 last_block = block;
324 }
325
326 if (force_extent)
327 print_extent_info(&fm_ext, *num_extents - 1,
328 last_block * st->st_blksize, blk_shift, st);
329
330 return count;
331}
332
Theodore Ts'o6b394c12004-02-26 21:08:06 -0500333static void frag_report(const char *filename)
Theodore Ts'o96424132003-12-17 10:13:41 -0500334{
JP Abgralle0ed7402014-03-19 19:08:39 -0700335 static struct statfs fsinfo;
336 ext2fs_struct_stat st;
337 int blk_shift;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500338 long fd;
JP Abgralle0ed7402014-03-19 19:08:39 -0700339 unsigned long numblocks;
340 int data_blocks_per_cyl = 1;
341 int num_extents = 1, expected = ~0;
Theodore Ts'o96424132003-12-17 10:13:41 -0500342 int is_ext2 = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700343 static dev_t last_device;
Theodore Ts'o3d16b3f2005-03-18 20:37:45 -0500344 unsigned int flags;
JP Abgralle0ed7402014-03-19 19:08:39 -0700345 int width;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400346
JP Abgralle0ed7402014-03-19 19:08:39 -0700347#if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
Kalpak Shahe62847c2009-05-02 21:02:29 -0400348 fd = open64(filename, O_RDONLY);
349#else
350 fd = open(filename, O_RDONLY);
351#endif
352 if (fd < 0) {
353 perror("open");
354 return;
355 }
Theodore Ts'o96424132003-12-17 10:13:41 -0500356
JP Abgralle0ed7402014-03-19 19:08:39 -0700357#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
358 if (fstat64(fd, &st) < 0) {
Theodore Ts'o9c07dc02006-05-29 11:06:16 -0400359#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700360 if (fstat(fd, &st) < 0) {
Theodore Ts'o9c07dc02006-05-29 11:06:16 -0400361#endif
JP Abgralle0ed7402014-03-19 19:08:39 -0700362 close(fd);
Theodore Ts'o96424132003-12-17 10:13:41 -0500363 perror("stat");
364 return;
365 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700366
367 if (last_device != st.st_dev) {
368 if (fstatfs(fd, &fsinfo) < 0) {
369 close(fd);
370 perror("fstatfs");
371 return;
372 }
373 if (verbose)
374 printf("Filesystem type is: %lx\n",
375 (unsigned long) fsinfo.f_type);
376 }
377 st.st_blksize = fsinfo.f_bsize;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400378 if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) < 0)
379 flags = 0;
380 if (!(flags & EXT4_EXTENTS_FL) &&
381 ((fsinfo.f_type == 0xef51) || (fsinfo.f_type == 0xef52) ||
382 (fsinfo.f_type == 0xef53)))
Theodore Ts'o96424132003-12-17 10:13:41 -0500383 is_ext2++;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400384
JP Abgralle0ed7402014-03-19 19:08:39 -0700385 if (is_ext2) {
386 long cylgroups = div_ceil(fsinfo.f_blocks, fsinfo.f_bsize * 8);
Kalpak Shahe62847c2009-05-02 21:02:29 -0400387
JP Abgralle0ed7402014-03-19 19:08:39 -0700388 if (verbose && last_device != st.st_dev)
389 printf("Filesystem cylinder groups approximately %ld\n",
390 cylgroups);
Kalpak Shahe62847c2009-05-02 21:02:29 -0400391
JP Abgralle0ed7402014-03-19 19:08:39 -0700392 data_blocks_per_cyl = fsinfo.f_bsize * 8 -
393 (fsinfo.f_files / 8 / cylgroups) - 3;
Theodore Ts'o96424132003-12-17 10:13:41 -0500394 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700395 last_device = st.st_dev;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400396
JP Abgralle0ed7402014-03-19 19:08:39 -0700397 width = int_log10(fsinfo.f_blocks);
398 if (width > physical_width)
399 physical_width = width;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400400
JP Abgralle0ed7402014-03-19 19:08:39 -0700401 numblocks = (st.st_size + fsinfo.f_bsize - 1) / fsinfo.f_bsize;
402 if (blocksize != 0)
403 blk_shift = int_log2(blocksize);
404 else
405 blk_shift = int_log2(fsinfo.f_bsize);
406
407 width = int_log10(numblocks);
408 if (width > logical_width)
409 logical_width = width;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400410 if (verbose)
JP Abgralle0ed7402014-03-19 19:08:39 -0700411 printf("File size of %s is %llu (%lu block%s of %d bytes)\n",
412 filename, (unsigned long long)st.st_size,
413 numblocks * fsinfo.f_bsize >> blk_shift,
414 numblocks == 1 ? "" : "s", 1 << blk_shift);
415
Theodore Ts'o5d5e01d2009-08-15 23:15:44 -0400416 if (force_bmap ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700417 filefrag_fiemap(fd, blk_shift, &num_extents, &st) != 0) {
418 expected = filefrag_fibmap(fd, blk_shift, &num_extents,
419 &st, numblocks, is_ext2);
420 if (expected < 0) {
421 if (errno == EINVAL || errno == ENOTTY) {
422 fprintf(stderr, "%s: FIBMAP unsupported\n",
423 filename);
424 } else if (errno != EPERM) {
425 fprintf(stderr, "%s: FIBMAP error: %s",
426 filename, strerror(errno));
Kalpak Shahe62847c2009-05-02 21:02:29 -0400427 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700428 goto out_close;
Theodore Ts'o96424132003-12-17 10:13:41 -0500429 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700430 expected = expected / data_blocks_per_cyl + 1;
Theodore Ts'o96424132003-12-17 10:13:41 -0500431 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700432
Kalpak Shahe62847c2009-05-02 21:02:29 -0400433 if (num_extents == 1)
Theodore Ts'o96424132003-12-17 10:13:41 -0500434 printf("%s: 1 extent found", filename);
435 else
Kalpak Shahe62847c2009-05-02 21:02:29 -0400436 printf("%s: %d extents found", filename, num_extents);
JP Abgralle0ed7402014-03-19 19:08:39 -0700437 /* count, and thus expected, only set for indirect FIBMAP'd files */
438 if (is_ext2 && expected && expected < num_extents)
Andreas Dilger2508eaa2012-11-22 15:06:12 +0000439 printf(", perfection would be %d extent%s\n", expected,
JP Abgralle0ed7402014-03-19 19:08:39 -0700440 (expected > 1) ? "s" : "");
Andreas Dilger2508eaa2012-11-22 15:06:12 +0000441 else
Theodore Ts'o96424132003-12-17 10:13:41 -0500442 fputc('\n', stdout);
JP Abgralle0ed7402014-03-19 19:08:39 -0700443out_close:
Theodore Ts'o92904042005-01-17 14:32:20 -0500444 close(fd);
Theodore Ts'o96424132003-12-17 10:13:41 -0500445}
446
Theodore Ts'o6b394c12004-02-26 21:08:06 -0500447static void usage(const char *progname)
Theodore Ts'o96424132003-12-17 10:13:41 -0500448{
JP Abgralle0ed7402014-03-19 19:08:39 -0700449 fprintf(stderr, "Usage: %s [-b{blocksize}] [-BeklsvxX] file ...\n",
450 progname);
Theodore Ts'o96424132003-12-17 10:13:41 -0500451 exit(1);
452}
453
454int main(int argc, char**argv)
455{
456 char **cpp;
457 int c;
458
JP Abgralle0ed7402014-03-19 19:08:39 -0700459 while ((c = getopt(argc, argv, "Bb::eksvxX")) != EOF)
Theodore Ts'o96424132003-12-17 10:13:41 -0500460 switch (c) {
Theodore Ts'o5d5e01d2009-08-15 23:15:44 -0400461 case 'B':
462 force_bmap++;
463 break;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400464 case 'b':
JP Abgralle0ed7402014-03-19 19:08:39 -0700465 if (optarg) {
466 char *end;
467 blocksize = strtoul(optarg, &end, 0);
468 if (end) {
469 switch (end[0]) {
470 case 'g':
471 case 'G':
472 blocksize *= 1024;
473 /* no break */
474 case 'm':
475 case 'M':
476 blocksize *= 1024;
477 /* no break */
478 case 'k':
479 case 'K':
480 blocksize *= 1024;
481 break;
482 default:
483 break;
484 }
485 }
486 } else { /* Allow -b without argument for compat. Remove
487 * this eventually so "-b {blocksize}" works */
488 fprintf(stderr, "%s: -b needs a blocksize "
489 "option, assuming 1024-byte blocks.\n",
490 argv[0]);
491 blocksize = 1024;
492 }
Kalpak Shahe62847c2009-05-02 21:02:29 -0400493 break;
JP Abgralle0ed7402014-03-19 19:08:39 -0700494 case 'e':
495 force_extent++;
496 if (!verbose)
497 verbose++;
498 break;
499 case 'k':
500 blocksize = 1024;
Andreas Dilger2508eaa2012-11-22 15:06:12 +0000501 break;
JP Abgrall65f0aab2014-03-06 13:50:20 -0800502 case 's':
503 sync_file++;
504 break;
JP Abgralle0ed7402014-03-19 19:08:39 -0700505 case 'v':
506 verbose++;
507 break;
Kalpak Shahe62847c2009-05-02 21:02:29 -0400508 case 'x':
509 xattr_map++;
510 break;
JP Abgralle0ed7402014-03-19 19:08:39 -0700511 case 'X':
512 ext_fmt = hex_fmt;
513 break;
Theodore Ts'o96424132003-12-17 10:13:41 -0500514 default:
515 usage(argv[0]);
516 break;
517 }
518 if (optind == argc)
519 usage(argv[0]);
Kalpak Shahe62847c2009-05-02 21:02:29 -0400520 for (cpp=argv+optind; *cpp; cpp++)
Theodore Ts'o96424132003-12-17 10:13:41 -0500521 frag_report(*cpp);
Theodore Ts'o96424132003-12-17 10:13:41 -0500522 return 0;
523}
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100524#endif