summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@beefcake.parabola.nu>2018-05-18 12:51:36 -0400
committerLuke Shumaker <lukeshu@beefcake.parabola.nu>2018-05-18 13:00:59 -0400
commit1e13f3671739e98f4e6d8531b9b22c93d24589f8 (patch)
treec480dd88275d9ae41b6a00e239cb0a0da130d096
parent39eff20bd938bae630270a3f06b17f787663826c (diff)
pull strextentflags in to a function
-rw-r--r--Makefile2
-rw-r--r--lib/extent-map.c4
-rw-r--r--lib/extent-map.h1
-rw-r--r--lib/strextentflags.c92
-rw-r--r--src/cow-extent-map.c87
5 files changed, 99 insertions, 87 deletions
diff --git a/Makefile b/Makefile
index 6cf34d9..b76e7cc 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,7 @@ all: cow-extent-map
$(CC) $(LDFLAGS) -o $@ $^
cow-dedupe-range: lib/dedupe-range.o
-cow-extent-map: lib/extent-map.o
+cow-extent-map: lib/extent-map.o lib/strextentflags.o
.SECONDARY:
.DELETE_ON_ERROR:
diff --git a/lib/extent-map.c b/lib/extent-map.c
index 297448c..ab9cfb0 100644
--- a/lib/extent-map.c
+++ b/lib/extent-map.c
@@ -8,8 +8,6 @@
#include "extent-map.h"
-#define error(_exit_code, ...) do { error(0, __VA_ARGS__); if (_exit_code) return _exit_code; } while(0)
-
int fiemap(int fd, uint32_t flags, int (*handle_extent)(struct fiemap_extent)) {
const size_t fm_size = sysconf(_SC_PAGESIZE);;
struct fiemap *fm = calloc(1, fm_size);
@@ -25,7 +23,7 @@ int fiemap(int fd, uint32_t flags, int (*handle_extent)(struct fiemap_extent)) {
};
if (ioctl(fd, FS_IOC_FIEMAP, fm) < 0)
- error(EXIT_FAILURE, errno, "ioctl (FS_IOC_FIEMAP)");
+ return -errno;
for (size_t i = 0; i < fm->fm_extent_count; i++) {
int r = handle_extent(fm->fm_extents[i]);
diff --git a/lib/extent-map.h b/lib/extent-map.h
index 8b5e585..22ba483 100644
--- a/lib/extent-map.h
+++ b/lib/extent-map.h
@@ -2,3 +2,4 @@
#include <linux/fiemap.h> /* for struct fiemap_extent */
int fiemap(int fd, uint32_t flags, int (*handle_extent)(struct fiemap_extent));
+char *strextentflags(uint32_t flags);
diff --git a/lib/strextentflags.c b/lib/strextentflags.c
new file mode 100644
index 0000000..c053ce7
--- /dev/null
+++ b/lib/strextentflags.c
@@ -0,0 +1,92 @@
+#include <errno.h> /* for errno */
+#include <error.h> /* for error(3gnu) */
+#include <inttypes.h> /* PRI* */
+#include <linux/fiemap.h> /* for FIEMAP_EXTENT_* */
+#include <stdbool.h> /* for bool, true, false */
+#include <stdio.h> /* sprintf(3p) */
+#include <stdlib.h> /* for malloc(3p), EXIT_FAILURE */
+#include <string.h> /* for strlen(3p), strcpy(3p) */
+
+#include "extent-map.h"
+
+struct flag {
+ char *name;
+ uint32_t value;
+ bool active;
+};
+
+char *strextentflags(uint32_t flags) {
+ struct flag known_flags[] = {
+ {"LAST", FIEMAP_EXTENT_LAST, false},
+ {"UNKNOWN", FIEMAP_EXTENT_UNKNOWN, false},
+ {"DELALLOC", FIEMAP_EXTENT_DELALLOC, false},
+ {"ENCODED", FIEMAP_EXTENT_ENCODED, false},
+ {"DATA_ENCRYPTED", FIEMAP_EXTENT_DATA_ENCRYPTED, false},
+ {"NOT_ALIGNED", FIEMAP_EXTENT_NOT_ALIGNED, false},
+ {"DATA_INLINE", FIEMAP_EXTENT_DATA_INLINE, false},
+ {"DATA_TAIL", FIEMAP_EXTENT_DATA_TAIL, false},
+ {"UNWRITTEN", FIEMAP_EXTENT_UNWRITTEN, false},
+ {"MERGED", FIEMAP_EXTENT_MERGED, false},
+ {"SHARED", FIEMAP_EXTENT_SHARED, false},
+ };
+ size_t flagstr_cap = 0;
+ for (size_t i = 0; i < sizeof(known_flags)/sizeof(known_flags[0]); i++) {
+ if (!(flags & known_flags[i].value))
+ continue;
+ known_flags[i].active = true;
+ flags ^= known_flags[i].value;
+ switch (known_flags[i].value) {
+ case FIEMAP_EXTENT_UNKNOWN:
+ if (flags & FIEMAP_EXTENT_DELALLOC) {
+ known_flags[i].name = "(UNKNOWN|DELALLOC)";
+ flags ^= FIEMAP_EXTENT_DELALLOC;
+ }
+ break;
+ case FIEMAP_EXTENT_ENCODED:
+ if (flags & FIEMAP_EXTENT_DATA_ENCRYPTED) {
+ known_flags[i].name = "(ENCODED|DATA_ENCRYPTED)";
+ flags ^= FIEMAP_EXTENT_DATA_ENCRYPTED;
+ }
+ break;
+ case FIEMAP_EXTENT_NOT_ALIGNED:
+ switch (flags & (FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_DATA_TAIL)) {
+ case FIEMAP_EXTENT_DATA_INLINE:
+ known_flags[i].name = "(NOT_ALIGNED|DATA_INLINE)";
+ flags ^= FIEMAP_EXTENT_DATA_INLINE;
+ break;
+ case FIEMAP_EXTENT_DATA_TAIL:
+ known_flags[i].name = "(NOT_ALIGNED|DATA_TAIL)";
+ flags ^= FIEMAP_EXTENT_DATA_TAIL;
+ break;
+ case FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_DATA_TAIL:
+ known_flags[i].name = "(NOT_ALIGNED|DATA_INLINE|DATA_TAIL)";
+ flags ^= FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_DATA_TAIL;
+ break;
+ }
+ break;
+ }
+ if (flagstr_cap)
+ flagstr_cap++;
+ flagstr_cap += strlen(known_flags[i].name);
+ }
+ if (flags)
+ flagstr_cap += (flagstr_cap ? 1 : 0) + 10;
+ char *flagstr = malloc(flagstr_cap+1);
+ if (!flagstr)
+ error(EXIT_FAILURE, errno, "malloc");
+ size_t flagstr_len = 0;
+ for (size_t i = 0; i < sizeof(known_flags)/sizeof(known_flags[0]); i++) {
+ if (!known_flags[i].active)
+ continue;
+ if (flagstr_len)
+ flagstr[flagstr_len++] = '|';
+ strcpy(&flagstr[flagstr_len], known_flags[i].name);
+ flagstr_len += strlen(known_flags[i].name);
+ }
+ if (flags) {
+ if (flagstr_len)
+ flagstr[flagstr_len++] = '|';
+ sprintf(&flagstr[flagstr_len], "0x%08"PRIx32, flags);
+ };
+ return flagstr;
+}
diff --git a/src/cow-extent-map.c b/src/cow-extent-map.c
index 1a791a0..aeedadf 100644
--- a/src/cow-extent-map.c
+++ b/src/cow-extent-map.c
@@ -5,10 +5,9 @@
#include <fcntl.h> /* for open(2) and O_RDONLY */
#include <getopt.h> /* for getopt_long(3gnu), struct option, optind, optarg */
#include <inttypes.h> /* PRI* */
-#include <stdbool.h> /* for bool, true, false */
-#include <stdio.h> /* for printf(3p), fprintf(3p), sprintf(3p), stderr */
-#include <stdlib.h> /* malloc(3p), freep(3p), exit(3p), EXIT_SUCCESS, EXIT_FAILURE */
-#include <string.h> /* for strcmp(3p), strlen(3p), strcpy(3p) */
+#include <stdio.h> /* for printf(3p), fprintf(3p), stderr */
+#include <stdlib.h> /* free(3p), exit(3p), EXIT_SUCCESS, EXIT_FAILURE */
+#include <string.h> /* for strcmp(3p) */
#include "extent-map.h" /* for uint32_t, fiemap stuff */
@@ -19,86 +18,8 @@
exit(EXIT_INVALIDARGUMENT); \
} while(0)
-struct flag {
- char *name;
- uint32_t value;
- bool active;
-};
-
int print_extent(struct fiemap_extent extent) {
- struct flag known_flags[] = {
- {"LAST", FIEMAP_EXTENT_LAST, false},
- {"UNKNOWN", FIEMAP_EXTENT_UNKNOWN, false},
- {"DELALLOC", FIEMAP_EXTENT_DELALLOC, false},
- {"ENCODED", FIEMAP_EXTENT_ENCODED, false},
- {"DATA_ENCRYPTED", FIEMAP_EXTENT_DATA_ENCRYPTED, false},
- {"NOT_ALIGNED", FIEMAP_EXTENT_NOT_ALIGNED, false},
- {"DATA_INLINE", FIEMAP_EXTENT_DATA_INLINE, false},
- {"DATA_TAIL", FIEMAP_EXTENT_DATA_TAIL, false},
- {"UNWRITTEN", FIEMAP_EXTENT_UNWRITTEN, false},
- {"MERGED", FIEMAP_EXTENT_MERGED, false},
- {"SHARED", FIEMAP_EXTENT_SHARED, false},
- };
- uint32_t flags = extent.fe_flags;
- size_t flagstr_cap = 0;
- for (size_t i = 0; i < sizeof(known_flags)/sizeof(known_flags[0]); i++) {
- if (!(flags & known_flags[i].value))
- continue;
- known_flags[i].active = true;
- flags ^= known_flags[i].value;
- switch (known_flags[i].value) {
- case FIEMAP_EXTENT_UNKNOWN:
- if (flags & FIEMAP_EXTENT_DELALLOC) {
- known_flags[i].name = "(UNKNOWN|DELALLOC)";
- flags ^= FIEMAP_EXTENT_DELALLOC;
- }
- break;
- case FIEMAP_EXTENT_ENCODED:
- if (flags & FIEMAP_EXTENT_DATA_ENCRYPTED) {
- known_flags[i].name = "(ENCODED|DATA_ENCRYPTED)";
- flags ^= FIEMAP_EXTENT_DATA_ENCRYPTED;
- }
- break;
- case FIEMAP_EXTENT_NOT_ALIGNED:
- switch (flags & (FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_DATA_TAIL)) {
- case FIEMAP_EXTENT_DATA_INLINE:
- known_flags[i].name = "(NOT_ALIGNED|DATA_INLINE)";
- flags ^= FIEMAP_EXTENT_DATA_INLINE;
- break;
- case FIEMAP_EXTENT_DATA_TAIL:
- known_flags[i].name = "(NOT_ALIGNED|DATA_TAIL)";
- flags ^= FIEMAP_EXTENT_DATA_TAIL;
- break;
- case FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_DATA_TAIL:
- known_flags[i].name = "(NOT_ALIGNED|DATA_INLINE|DATA_TAIL)";
- flags ^= FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_DATA_TAIL;
- break;
- }
- break;
- }
- if (flagstr_cap)
- flagstr_cap++;
- flagstr_cap += strlen(known_flags[i].name);
- }
- if (flags)
- flagstr_cap += (flagstr_cap ? 1 : 0) + 10;
- char *flagstr = malloc(flagstr_cap+1);
- if (!flagstr)
- error(EXIT_FAILURE, errno, "malloc");
- size_t flagstr_len = 0;
- for (size_t i = 0; i < sizeof(known_flags)/sizeof(known_flags[0]); i++) {
- if (!known_flags[i].active)
- continue;
- if (flagstr_len)
- flagstr[flagstr_len++] = '|';
- strcpy(&flagstr[flagstr_len], known_flags[i].name);
- flagstr_len += strlen(known_flags[i].name);
- }
- if (flags) {
- if (flagstr_len)
- flagstr[flagstr_len++] = '|';
- sprintf(&flagstr[flagstr_len], "0x%08"PRIx32, flags);
- };
+ char *flagstr = strextentflags(extent.fe_flags);
printf("logical=%"PRIu64" "
"physical=%"PRIu64" "
"length=%"PRIu64" "