* [PATCH 2/4] filefrag: improve extent flags printing
2013-12-17 1:05 [PATCH 1/4] filefrag: exit with error code on failure Andreas Dilger
@ 2013-12-17 1:05 ` Andreas Dilger
2013-12-17 1:05 ` [PATCH 3/4] filefrag: reserve fields and new extent flags Andreas Dilger
2013-12-17 1:05 ` [PATCH 4/4] filefrag: print out physical extent length if set Andreas Dilger
2 siblings, 0 replies; 4+ messages in thread
From: Andreas Dilger @ 2013-12-17 1:05 UTC (permalink / raw)
To: tytso; +Cc: linux-ext4, Andreas Dilger
Improve the handling of printing extent flags in verbose mode.
For unknown flags, print out the flag value in hexadecimal.
Signed-off-by: Andreas Dilger <adilger@dilger.ca>
---
misc/filefrag.c | 60 +++++++++++++++++++++++++++++++++----------------------
1 files changed, 36 insertions(+), 24 deletions(-)
diff --git a/misc/filefrag.c b/misc/filefrag.c
index 18dc1e5..6289899 100644
--- a/misc/filefrag.c
+++ b/misc/filefrag.c
@@ -120,6 +120,15 @@ static void print_extent_header(void)
"expected:");
}
+static void print_flag(__u32 *flags, __u32 mask, char *buf, const char *name)
+{
+ if ((*flags & mask) == 0)
+ return;
+
+ strcat(buf, name);
+ *flags &= ~mask;
+}
+
static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
unsigned long long expected, int blk_shift,
ext2fs_struct_stat *st)
@@ -128,10 +137,12 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
unsigned long long logical_blk;
unsigned long long ext_len;
unsigned long long ext_blks;
- char flags[256] = "";
+ __u32 flags, mask;
+ char buf[256] = "";
+ flags = fm_extent->fe_flags;
/* For inline data all offsets should be in bytes, not blocks */
- if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
+ if (flags & FIEMAP_EXTENT_DATA_INLINE)
blk_shift = 0;
ext_len = fm_extent->fe_length >> blk_shift;
@@ -140,34 +151,35 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
physical_blk = fm_extent->fe_physical >> blk_shift;
if (expected)
- sprintf(flags, ext_fmt == hex_fmt ? "%*llx: " : "%*llu: ",
+ sprintf(buf, 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,");
- if (fm_extent->fe_flags & FIEMAP_EXTENT_DELALLOC)
- strcat(flags, "delalloc,");
- if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_ENCRYPTED)
- strcat(flags, "encrypted,");
- if (fm_extent->fe_flags & FIEMAP_EXTENT_NOT_ALIGNED)
- strcat(flags, "not_aligned,");
- if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
- strcat(flags, "inline,");
- if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_TAIL)
- strcat(flags, "tail_packed,");
- if (fm_extent->fe_flags & FIEMAP_EXTENT_UNWRITTEN)
- strcat(flags, "unwritten,");
- if (fm_extent->fe_flags & FIEMAP_EXTENT_MERGED)
- strcat(flags, "merged,");
+ sprintf(buf, "%.*s ", physical_width, " ");
+
+ print_flag(&flags, FIEMAP_EXTENT_UNKNOWN, buf, "unknown,");
+ print_flag(&flags, FIEMAP_EXTENT_DELALLOC, buf, "delalloc,");
+ print_flag(&flags, FIEMAP_EXTENT_DATA_ENCRYPTED, buf, "encrypted,");
+ print_flag(&flags, FIEMAP_EXTENT_NOT_ALIGNED, buf, "not_aligned,");
+ print_flag(&flags, FIEMAP_EXTENT_DATA_INLINE, buf, "inline,");
+ print_flag(&flags, FIEMAP_EXTENT_DATA_TAIL, buf, "tail_packed,");
+ print_flag(&flags, FIEMAP_EXTENT_UNWRITTEN, buf, "unwritten,");
+ print_flag(&flags, FIEMAP_EXTENT_MERGED, buf, "merged,");
+ print_flag(&flags, FIEMAP_EXTENT_LAST, buf, "last,");
+ /* Print unknown flags in hex format. Known flags are already printed
+ * above and will have their bit cleared from "flags". */
+ for (mask = 1; flags != 0 && mask != 0; mask <<= 1) {
+ char hex[6];
+
+ sprintf(hex, "%04x,", mask);
+ print_flag(&flags, mask, buf, hex);
+ }
if (fm_extent->fe_logical + fm_extent->fe_length >= (__u64) st->st_size)
- strcat(flags, "eof,");
+ strcat(buf, "eof,");
/* Remove trailing comma, if any */
- if (flags[0])
- flags[strlen(flags) - 1] = '\0';
+ if (buf[0])
+ buf[strlen(buf) - 1] = '\0';
printf(ext_fmt, cur_ex, logical_width, logical_blk,
logical_width, logical_blk + ext_blks,
--
1.7.3.4
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 3/4] filefrag: reserve fields and new extent flags
2013-12-17 1:05 [PATCH 1/4] filefrag: exit with error code on failure Andreas Dilger
2013-12-17 1:05 ` [PATCH 2/4] filefrag: improve extent flags printing Andreas Dilger
@ 2013-12-17 1:05 ` Andreas Dilger
2013-12-17 1:05 ` [PATCH 4/4] filefrag: print out physical extent length if set Andreas Dilger
2 siblings, 0 replies; 4+ messages in thread
From: Andreas Dilger @ 2013-12-17 1:05 UTC (permalink / raw)
To: tytso; +Cc: linux-ext4, Andreas Dilger
Reserve the FIEMAP_EXTENT_DATA_COMPRESSED flag to allow reporting
extents with physical length different than logical length. This
also sets the FIEMAP_EXTENT_ENCODED flag to indicate that the data
cannot be accessed directly. This adds a new fe_phys_length field
to struct fiemap_extent which specifies the physical extent length
in bytes when DATA_COMPRESSED is set. Linux 3.14 and later kernels
will fill out the fe_phys_length field, but in older kernels the
contents are undefined (though typically zero).
Reserve the FIEMAP_EXTENT_NET flag, which indicates that the data
is on a network filesystem. This also sets FIEMAP_EXTENT_ENCODED
flag to indicate the data cannot be accessed directly.
Print out these flags in filefrag if detected.
Signed-off-by: Andreas Dilger <adilger@dilger.ca>
---
lib/ext2fs/fiemap.h | 14 ++++++++++----
misc/filefrag.c | 2 ++
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/lib/ext2fs/fiemap.h b/lib/ext2fs/fiemap.h
index 30bf555..df973da 100644
--- a/lib/ext2fs/fiemap.h
+++ b/lib/ext2fs/fiemap.h
@@ -17,15 +17,17 @@ struct fiemap_extent {
__u64 fe_physical; /* physical offset in bytes for the start
* of the extent from the beginning of the disk */
__u64 fe_length; /* length in bytes for this extent */
- __u64 fe_reserved64[2];
+ __u64 fe_phys_length; /* physical length in bytes for this extent,
+ * undefined if DATA_COMPRESSED not set */
+ __u64 fe_reserved64;
__u32 fe_flags; /* FIEMAP_EXTENT_* flags for this extent */
__u32 fe_reserved[3];
};
struct fiemap {
- __u64 fm_start; /* logical offset (inclusive) at
+ __u64 fm_start; /* logical offset in bytes (inclusive) at
* which to start mapping (in) */
- __u64 fm_length; /* logical length of mapping which
+ __u64 fm_length; /* logical length in bytes of mapping which
* userspace wants (in) */
__u32 fm_flags; /* FIEMAP_FLAG_* flags for request (in/out) */
__u32 fm_mapped_extents;/* number of extents that were mapped (out) */
@@ -51,8 +53,10 @@ struct fiemap {
* Sets EXTENT_UNKNOWN. */
#define FIEMAP_EXTENT_ENCODED 0x00000008 /* Data can not be read
* while fs is unmounted */
+#define FIEMAP_EXTENT_DATA_COMPRESSED 0x00000040 /* Data is compressed by fs.
+ * Sets EXTENT_ENCODED. */
#define FIEMAP_EXTENT_DATA_ENCRYPTED 0x00000080 /* Data is encrypted by fs.
- * Sets EXTENT_NO_BYPASS. */
+ * Sets EXTENT_ENCODED. */
#define FIEMAP_EXTENT_NOT_ALIGNED 0x00000100 /* Extent offsets may not be
* block aligned. */
#define FIEMAP_EXTENT_DATA_INLINE 0x00000200 /* Data mixed with metadata.
@@ -64,5 +68,7 @@ struct fiemap {
#define FIEMAP_EXTENT_MERGED 0x00001000 /* File does not natively
* support extents. Result
* merged for efficiency. */
+#define FIEMAP_EXTENT_NET 0x80000000 /* Data stored remotely.
+ * Sets EXTENT_ENCODED. */
#endif /* _LINUX_FIEMAP_H */
diff --git a/misc/filefrag.c b/misc/filefrag.c
index 6289899..7096abb 100644
--- a/misc/filefrag.c
+++ b/misc/filefrag.c
@@ -158,12 +158,14 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
print_flag(&flags, FIEMAP_EXTENT_UNKNOWN, buf, "unknown,");
print_flag(&flags, FIEMAP_EXTENT_DELALLOC, buf, "delalloc,");
+ print_flag(&flags, FIEMAP_EXTENT_DATA_COMPRESSED, buf, "compressed,");
print_flag(&flags, FIEMAP_EXTENT_DATA_ENCRYPTED, buf, "encrypted,");
print_flag(&flags, FIEMAP_EXTENT_NOT_ALIGNED, buf, "not_aligned,");
print_flag(&flags, FIEMAP_EXTENT_DATA_INLINE, buf, "inline,");
print_flag(&flags, FIEMAP_EXTENT_DATA_TAIL, buf, "tail_packed,");
print_flag(&flags, FIEMAP_EXTENT_UNWRITTEN, buf, "unwritten,");
print_flag(&flags, FIEMAP_EXTENT_MERGED, buf, "merged,");
+ print_flag(&flags, FIEMAP_EXTENT_NET, buf, "net,");
print_flag(&flags, FIEMAP_EXTENT_LAST, buf, "last,");
/* Print unknown flags in hex format. Known flags are already printed
* above and will have their bit cleared from "flags". */
--
1.7.3.4
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 4/4] filefrag: print out physical extent length if set
2013-12-17 1:05 [PATCH 1/4] filefrag: exit with error code on failure Andreas Dilger
2013-12-17 1:05 ` [PATCH 2/4] filefrag: improve extent flags printing Andreas Dilger
2013-12-17 1:05 ` [PATCH 3/4] filefrag: reserve fields and new extent flags Andreas Dilger
@ 2013-12-17 1:05 ` Andreas Dilger
2 siblings, 0 replies; 4+ messages in thread
From: Andreas Dilger @ 2013-12-17 1:05 UTC (permalink / raw)
To: tytso; +Cc: linux-ext4, Andreas Dilger
Rename fe_length to be fe_logi_length to distinguish it from the
fe_phys_length field.
If the extent's physical length is different from its logical length
(if FIEMAP_EXTENT_DATA_COMPRESSED is set) then use the fe_phys_length
for the physical length. If FIEMAP_EXTENT_DATA_COMPRESSED is unset,
then continue to use fe_logi_length for the extent length, but set
fe_phys_length = fe_logi_length to simplify the rest of the code.
Signed-off-by: Andreas Dilger <adilger@dilger.ca>
---
lib/ext2fs/fiemap.h | 2 +-
misc/filefrag.c | 62 +++++++++++++++++++++++++++++++++++++--------------
2 files changed, 46 insertions(+), 18 deletions(-)
diff --git a/lib/ext2fs/fiemap.h b/lib/ext2fs/fiemap.h
index df973da..3cf32f7 100644
--- a/lib/ext2fs/fiemap.h
+++ b/lib/ext2fs/fiemap.h
@@ -16,7 +16,7 @@ struct fiemap_extent {
* the extent from the beginning of the file */
__u64 fe_physical; /* physical offset in bytes for the start
* of the extent from the beginning of the disk */
- __u64 fe_length; /* length in bytes for this extent */
+ __u64 fe_logi_length; /* logical length in bytes for this extent */
__u64 fe_phys_length; /* physical length in bytes for this extent,
* undefined if DATA_COMPRESSED not set */
__u64 fe_reserved64;
diff --git a/misc/filefrag.c b/misc/filefrag.c
index 7096abb..6cefaf4 100644
--- a/misc/filefrag.c
+++ b/misc/filefrag.c
@@ -133,10 +133,14 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
unsigned long long expected, int blk_shift,
ext2fs_struct_stat *st)
{
- unsigned long long physical_blk;
- unsigned long long logical_blk;
- unsigned long long ext_len;
- unsigned long long ext_blks;
+ unsigned long long logical_blk; /* logical starting block of file */
+ unsigned long long logical_len; /* logical length of extent in blocks */
+ unsigned long long logical_unit;/* logical length to end of extent, may
+ * not be logical_len-1 if extent isn't
+ * aligned to requested block size */
+ unsigned long long physical_blk;/* physical starting block in LUN */
+ unsigned long long physical_len;/* physical length of extent in blocks*/
+ unsigned long long physical_unit;/* physical length to end of extent */
__u32 flags, mask;
char buf[256] = "";
@@ -145,10 +149,20 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
if (flags & FIEMAP_EXTENT_DATA_INLINE)
blk_shift = 0;
- ext_len = fm_extent->fe_length >> blk_shift;
- ext_blks = (fm_extent->fe_length - 1) >> blk_shift;
logical_blk = fm_extent->fe_logical >> blk_shift;
- physical_blk = fm_extent->fe_physical >> blk_shift;
+ logical_len = fm_extent->fe_logi_length >> blk_shift;
+ logical_unit = (fm_extent->fe_logi_length - 1) >> blk_shift;
+ if (flags & FIEMAP_EXTENT_UNKNOWN) {
+ physical_blk = 0;
+ physical_len = 0;
+ physical_unit = 0;
+ } else {
+ /* FIEMAP_EXTENT_DATA_COMPRESSED was checked by caller
+ * and fixed up fe_phys_length if unset. */
+ physical_blk = fm_extent->fe_physical >> blk_shift;
+ physical_len = fm_extent->fe_phys_length >> blk_shift;
+ physical_unit = (fm_extent->fe_phys_length - 1) >> blk_shift;
+ }
if (expected)
sprintf(buf, ext_fmt == hex_fmt ? "%*llx: " : "%*llu: ",
@@ -176,7 +190,8 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
print_flag(&flags, mask, buf, hex);
}
- if (fm_extent->fe_logical + fm_extent->fe_length >= (__u64) st->st_size)
+ if (fm_extent->fe_logical + fm_extent->fe_logi_length >=
+ (__u64)st->st_size)
strcat(buf, "eof,");
/* Remove trailing comma, if any */
@@ -184,10 +199,10 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
buf[strlen(buf) - 1] = '\0';
printf(ext_fmt, cur_ex, logical_width, logical_blk,
- logical_width, logical_blk + ext_blks,
+ logical_width, logical_blk + logical_unit,
physical_width, physical_blk,
- physical_width, physical_blk + ext_blks,
- ext_len, flags);
+ physical_width, physical_blk + physical_unit,
+ logical_len, buf);
}
static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
@@ -250,18 +265,29 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
if (!tot_extents)
tot_extents = 1;
}
+
+ /* If DATA_COMPRESSED is set, then the logical and
+ * physical extent lengths are different and the
+ * fe_phys_length field is known to be valid.
+ * Kernels older than 3.14 did not set fe_phys_length,
+ * so do that here to simplify the rest of the code. */
+ if (!(fm_ext[i].fe_flags &
+ FIEMAP_EXTENT_DATA_COMPRESSED))
+ fm_ext[i].fe_phys_length =
+ fm_ext[i].fe_logi_length;
+
if (verbose)
print_extent_info(&fm_ext[i], n, expected,
blk_shift, st);
-
- expected = fm_ext[i].fe_physical + fm_ext[i].fe_length;
+ expected = fm_ext[i].fe_physical +
+ fm_ext[i].fe_phys_length;
if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
last = 1;
n++;
}
fiemap->fm_start = (fm_ext[i - 1].fe_logical +
- fm_ext[i - 1].fe_length);
+ fm_ext[i - 1].fe_logi_length);
} while (last == 0);
*num_extents = tot_extents;
@@ -335,20 +361,22 @@ static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
count++;
if (force_extent && last_block != 0 &&
(block != last_block + 1 ||
- fm_ext.fe_logical + fm_ext.fe_length != logical)) {
+ fm_ext.fe_logical + fm_ext.fe_logi_length != logical)) {
print_extent_info(&fm_ext, *num_extents - 1,
(last_block + 1) * st->st_blksize,
blk_shift, st);
fm_ext.fe_logical = logical;
fm_ext.fe_physical = block * st->st_blksize;
- fm_ext.fe_length = 0;
+ fm_ext.fe_logi_length = 0;
+ fm_ext.fe_phys_length = 0;
(*num_extents)++;
} else if (verbose && last_block && (block != last_block + 1)) {
printf("Discontinuity: Block %ld is at %lu (was %lu)\n",
i, block, last_block + 1);
(*num_extents)++;
}
- fm_ext.fe_length += st->st_blksize;
+ fm_ext.fe_logi_length += st->st_blksize;
+ fm_ext.fe_phys_length += st->st_blksize;
last_block = block;
}
--
1.7.3.4
^ permalink raw reply related [flat|nested] 4+ messages in thread