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