From c875145cb33a42dfd57fad1a37c5001ba3920d77 Mon Sep 17 00:00:00 2001 From: Anand Jain Date: Wed, 7 Oct 2020 18:08:05 +0800 Subject: [PATCH] btrfs-progs: sb-mod: add devid to the modifiable list We need this patch to create a crafted image with bogus devid. For example: ./btrfs-sb-mod devid =0 Signed-off-by: Anand Jain Signed-off-by: David Sterba --- btrfs-sb-mod.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/btrfs-sb-mod.c b/btrfs-sb-mod.c index d0222db3..e46b3320 100644 --- a/btrfs-sb-mod.c +++ b/btrfs-sb-mod.c @@ -139,6 +139,7 @@ struct sb_field { { .name = "log_root_level", .type = TYPE_U8 }, { .name = "cache_generation", .type = TYPE_U64 }, { .name = "uuid_tree_generation", .type = TYPE_U64 }, + { .name = "devid", .type = TYPE_U64 }, }; #define MOD_FIELD_XX(fname, set, val, bits, f_dec, f_hex, f_type) \ @@ -154,11 +155,28 @@ struct sb_field { } \ } +#define MOD_DEV_FIELD_XX(fname, set, val, bits, f_dec, f_hex, f_type) \ + else if (strcmp(name, #fname) == 0) { \ + if (set) { \ + printf("SET: "#fname" "f_dec" (0x"f_hex")\n", \ + (f_type)*val, (f_type)*val); \ + sb->dev_item.fname = cpu_to_le##bits(*val); \ + } else { \ + *val = le##bits##_to_cpu(sb->dev_item.fname); \ + printf("GET: "#fname" "f_dec" (0x"f_hex")\n", \ + (f_type)*val, (f_type)*val); \ + } \ + } + #define MOD_FIELD64(fname, set, val) \ MOD_FIELD_XX(fname, set, val, 64, "%llu", "%llx", unsigned long long) +#define MOD_DEV_FIELD64(fname, set, val) \ + MOD_DEV_FIELD_XX(fname, set, val, 64, "%llu", "%llx", unsigned long long) + /* Alias for u64 */ #define MOD_FIELD(fname, set, val) MOD_FIELD64(fname, set, val) +#define MOD_DEV_FIELD(fname, set, val) MOD_DEV_FIELD64(fname, set, val) /* * Support only GET and SET properly, ADD and SUB may work @@ -202,6 +220,7 @@ static void mod_field_by_name(struct btrfs_super_block *sb, int set, const char MOD_FIELD8(log_root_level, set, val) MOD_FIELD(cache_generation, set, val) MOD_FIELD(uuid_tree_generation, set, val) + MOD_DEV_FIELD(devid, set, val) else { printf("ERROR: unhandled field: %s\n", name); exit(1); @@ -260,7 +279,8 @@ static int arg_to_op_value(const char *arg, enum field_op *op, u64 *val) return 0; } -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ int fd; loff_t off; int ret;