Whamcloud - gitweb
filefrag: Lustre changes to filefrag FIEMAP handling
authorAndreas Dilger <adilger@whamcloud.com>
Thu, 12 Apr 2012 21:31:35 +0000 (15:31 -0600)
committerAndreas Dilger <andreas.dilger@intel.com>
Wed, 24 Apr 2013 04:48:38 +0000 (22:48 -0600)
Add support for multiple-device filesystems by defining a new
fe_device field in the fiemap_extent structure.  This allows
printing the filesystem-relative or linux block device number
associated with each extent of a file.  If a single filesystem
extent is mirrored to multiple block devices, the fe_device
field can be used to disambiguate the multiple copies.

If the "-l" (device-logical) option is given to filefrag, then
all extents for a particular device of a file are returned
before returning extents for the next device.  This makes it
easier to see if extent allocation within a single device is
contiguous, instead of returning all of the blocks of a file
interleaved in file-logical-offset order.

Signed-off-by: Andreas Dilger <andreas.dilger@intel.com>
lib/ext2fs/fiemap.h
misc/filefrag.8.in
misc/filefrag.c

index 30bf555..71fc80a 100644 (file)
@@ -19,7 +19,8 @@ struct fiemap_extent {
        __u64 fe_length;   /* length in bytes for this extent */
        __u64 fe_reserved64[2];
        __u32 fe_flags;    /* FIEMAP_EXTENT_* flags for this extent */
-       __u32 fe_reserved[3];
+       __u32 fe_device;   /* device number (fs-specific if FIEMAP_EXTENT_NET)*/
+       __u32 fe_reserved[2];
 };
 
 struct fiemap {
@@ -42,6 +43,7 @@ struct fiemap {
 
 #define FIEMAP_FLAG_SYNC       0x00000001 /* sync file data before map */
 #define FIEMAP_FLAG_XATTR      0x00000002 /* map extended attribute tree */
+#define FIEMAP_FLAG_DEVICE_ORDER 0x40000000 /* return device ordered mapping */
 
 #define FIEMAP_FLAGS_COMPAT    (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
 
@@ -65,4 +67,9 @@ struct fiemap {
                                                    * support extents. Result
                                                    * merged for efficiency. */
 
+/* Network filesystem flags - use a high bit, don't conflict with upstream */
+#define FIEMAP_EXTENT_NO_DIRECT                0x40000000 /* Data mapping undefined */
+#define FIEMAP_EXTENT_NET              0x80000000 /* Data stored remotely.
+                                                   * Sets NO_DIRECT flag */
+
 #endif /* _LINUX_FIEMAP_H */
index a6d7b27..b0639a3 100644 (file)
@@ -8,7 +8,7 @@ filefrag \- report on file fragmentation
 .BI \-b blocksize
 ]
 [
-.B \-BeksvxX
+.B \-BeklsvxX
 ]
 [
 .I files...
@@ -46,6 +46,9 @@ Print output in extent format, even for block-mapped files.
 .BI \-k
 Use 1024\-byte blocksize for output (identical to '\-b 1024').
 .TP
+.B \-l
+Extents are displayed in device-logical offset order.
+.TP
 .B \-s
 Sync the file before requesting the mapping.
 .TP
index ee03a07..bd03e7a 100644 (file)
@@ -48,14 +48,16 @@ int verbose = 0;
 int blocksize;         /* Use specified blocksize (default 1kB) */
 int sync_file = 0;     /* fsync file before getting the mapping */
 int xattr_map = 0;     /* get xattr mapping */
-int force_bmap;        /* force use of FIBMAP instead of FIEMAP */
+int force_bmap;                /* force use of FIBMAP instead of FIEMAP */
 int force_extent;      /* print output in extent format always */
+int device_offset;     /* extents report device-relative offsets */
 int logical_width = 8;
 int physical_width = 10;
 char *ext_fmt = "%4d: %*llu..%*llu: %*llu..%*llu: %6llu: %s\n";
 char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
 
-#define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
+#define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR |\
+                                     FIEMAP_FLAG_DEVICE_ORDER)
 
 #define FIBMAP         _IO(0x00, 1)    /* bmap access */
 #define FIGETBSZ       _IO(0x00, 2)    /* get the block size used for bmap */
@@ -118,10 +120,10 @@ static void print_extent_header(void)
 {
        printf(" ext: %*s %*s length: %*s flags:\n",
               logical_width * 2 + 3,
-              "logical_offset:",
+              device_offset ? "device_logical:" : "logical_offset:",
               physical_width * 2 + 3, "physical_offset:",
-              physical_width + 1,
-              "expected:");
+              device_offset ? 5 : physical_width + 1,
+              device_offset ? " dev:" : "expected:");
 }
 
 static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
@@ -143,11 +145,11 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
        logical_blk = fm_extent->fe_logical >> blk_shift;
        physical_blk = fm_extent->fe_physical >> blk_shift;
 
-       if (expected)
-               sprintf(flags, ext_fmt == hex_fmt ? "%*llx: " : "%*llu: ",
+       if (device_offset)
+               sprintf(flags, "%04x: ", fm_extent->fe_device);
+       else if (expected)
+               sprintf(flags, ext_fmt == hex_fmt ? "%*llx:" : "%*llu: ",
                        physical_width, expected >> blk_shift);
-       else
-               sprintf(flags, "%.*s  ", physical_width, "                   ");
 
        if (fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN)
                strcat(flags, "unknown,");
@@ -165,6 +167,10 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
                strcat(flags, "unwritten,");
        if (fm_extent->fe_flags & FIEMAP_EXTENT_MERGED)
                strcat(flags, "merged,");
+       if (fm_extent->fe_flags & FIEMAP_EXTENT_NET)
+               strcat(flags, "network,");
+       if (fm_extent->fe_flags & FIEMAP_EXTENT_NO_DIRECT)
+               strcat(flags, "no_direct,");
 
        if (fm_extent->fe_logical + fm_extent->fe_length >= st->st_size)
                strcat(flags, "eof,");
@@ -194,6 +200,7 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
        static int fiemap_incompat_printed;
        int fiemap_header_printed = 0;
        int tot_extents = 0, n = 0;
+       int previous_device = 0;
        int last = 0;
        int rc;
 
@@ -205,6 +212,12 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
        if (xattr_map)
                flags |= FIEMAP_FLAG_XATTR;
 
+       if (device_offset) {
+               flags |= FIEMAP_FLAG_DEVICE_ORDER;
+               memset(fm_ext, 0, sizeof(struct fiemap_extent));
+       }
+
+retry_wo_device_order:
        do {
                fiemap->fm_length = ~0ULL;
                fiemap->fm_flags = flags;
@@ -215,6 +228,10 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
                                printf("FIEMAP failed with unsupported "
                                       "flags %x\n", fiemap->fm_flags);
                                fiemap_incompat_printed = 1;
+                       } else if (rc == EBADR && (fiemap->fm_flags &
+                                                  FIEMAP_FLAG_DEVICE_ORDER)) {
+                               flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
+                               goto retry_wo_device_order;
                        }
                        return rc;
                }
@@ -229,6 +246,9 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
                }
 
                for (i = 0; i < fiemap->fm_mapped_extents; i++) {
+                       if (previous_device != fm_ext[i].fe_device)
+                               previous_device = fm_ext[i].fe_device;
+
                        if (fm_ext[i].fe_logical != 0 &&
                            fm_ext[i].fe_physical != expected) {
                                tot_extents++;
@@ -247,8 +267,20 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
                        n++;
                }
 
-               fiemap->fm_start = (fm_ext[i - 1].fe_logical +
-                                   fm_ext[i - 1].fe_length);
+               /* For DEVICE_ORDER mappings, if EXTENT_LAST not yet found then
+                * fm_start needs to be the same as it was for earlier ioctl.
+                * The first extent is used to pass the end offset and device
+                * of the last FIEMAP call.  Otherwise, we ask for extents
+                * starting from where the last mapping ended. */
+               if (flags & FIEMAP_FLAG_DEVICE_ORDER) {
+                       fm_ext[0].fe_logical =  fm_ext[i - 1].fe_logical +
+                                               fm_ext[i - 1].fe_length;
+                       fm_ext[0].fe_device =   fm_ext[i - 1].fe_device;
+                       fiemap->fm_start =      0;
+               } else {
+                       fiemap->fm_start =      fm_ext[i - 1].fe_logical +
+                                               fm_ext[i - 1].fe_length;
+               }
        } while (last == 0);
 
        *num_extents = tot_extents;
@@ -271,6 +303,7 @@ static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
 
        if (force_extent) {
                memset(&fm_ext, 0, sizeof(fm_ext));
+               fm_ext.fe_device = st->st_dev;
                fm_ext.fe_flags = FIEMAP_EXTENT_MERGED;
        }
 
@@ -381,6 +414,13 @@ static void frag_report(const char *filename)
             (fsinfo.f_type == 0xef53)))
                is_ext2++;
 
+       /* Check if filesystem is Lustre.  Always print in extent format
+        * with 1kB blocks, using the device-relative logical offsets. */
+       if (fsinfo.f_type == LUSTRE_SUPER_MAGIC) {
+               device_offset = 1;
+               blocksize = blocksize ?: 1024;
+       }
+
        if (is_ext2) {
                long cylgroups = div_ceil(fsinfo.f_blocks, fsinfo.f_bsize * 8);
 
@@ -455,10 +495,11 @@ int main(int argc, char**argv)
        char **cpp;
        int c;
 
-       while ((c = getopt(argc, argv, "Bb::eksvxX")) != EOF)
+       while ((c = getopt(argc, argv, "Bb::eklsvxX")) != EOF)
                switch (c) {
                case 'B':
                        force_bmap++;
+                       force_extent = 0;
                        break;
                case 'b':
                        if (optarg) {
@@ -498,6 +539,9 @@ int main(int argc, char**argv)
                case 'k':
                        blocksize = 1024;
                        break;
+               case 'l':
+                       device_offset++;
+                       break;
                case 's':
                        sync_file++;
                        break;