Add write-journal command (for debug)

This commit is contained in:
Vitaliy Filippov
2022-08-20 14:05:53 +03:00
parent 1407db9c08
commit a52f2b0e8f
3 changed files with 236 additions and 15 deletions
+200 -4
View File
@@ -5,6 +5,8 @@
int disk_tool_t::dump_journal()
{
dump_with_blocks = options["format"] == "blocks";
dump_with_data = options["format"] == "data" || options["format"] == "blocks,data";
if (dsk.journal_block_size < DIRECT_IO_ALIGNMENT || (dsk.journal_block_size % DIRECT_IO_ALIGNMENT) ||
dsk.journal_block_size > 128*1024)
{
@@ -83,13 +85,17 @@ int disk_tool_t::dump_journal()
{
if (json && first2)
{
printf("%s{\"offset\":\"0x%lx\",\"entries\":[\n", first ? "" : ",\n", pos);
if (dump_with_blocks)
printf("%s{\"offset\":\"0x%lx\",\"entries\":[\n", first ? "" : ",\n", pos);
first = false;
}
dump_journal_entry(num, je, json);
});
if (json)
printf(first2 ? "" : "\n]}");
{
if (dump_with_blocks && !first2)
printf("\n]}");
}
else if (r <= 0)
printf("end of the journal\n");
return r;
@@ -240,6 +246,24 @@ void disk_tool_t::dump_journal_entry(int num, journal_entry *je, bool json)
printf(json ? ",\"bad_loc\":true,\"calc_loc\":\"0x%lx\""
: " (mismatched, calculated = %lu)", journal_pos);
}
if (je->small_write.size > sizeof(journal_entry_small_write))
{
printf(json ? ",\"bitmap\":\"" : " (bitmap: ");
for (int i = sizeof(journal_entry_small_write); i < je->small_write.size; i++)
{
printf("%02x", ((uint8_t*)je)[i]);
}
printf(json ? "\"" : ")");
}
if (dump_with_data)
{
printf(json ? ",\"data\":\"" : " (data: ");
for (int i = 0; i < je->small_write.len; i++)
{
printf("%02x", ((uint8_t*)small_write_data)[i]);
}
printf(json ? "\"" : ")");
}
printf(
json ? ",\"data_crc32\":\"%08x\",\"data_valid\":%s}" : " data_crc32=%08x%s\n",
je->small_write.crc32_data,
@@ -251,11 +275,21 @@ void disk_tool_t::dump_journal_entry(int num, journal_entry *je, bool json)
else if (je->type == JE_BIG_WRITE || je->type == JE_BIG_WRITE_INSTANT)
{
printf(
json ? ",\"type\":\"big_write%s\",\"inode\":\"0x%lx\",\"stripe\":\"0x%lx\",\"ver\":\"%lu\",\"loc\":\"0x%lx\"}"
: "je_big_write%s oid=%lx:%lx ver=%lu loc=%08lx\n",
json ? ",\"type\":\"big_write%s\",\"inode\":\"0x%lx\",\"stripe\":\"0x%lx\",\"ver\":\"%lu\",\"loc\":\"0x%lx\""
: "je_big_write%s oid=%lx:%lx ver=%lu loc=%08lx",
je->type == JE_BIG_WRITE_INSTANT ? "_instant" : "",
je->big_write.oid.inode, je->big_write.oid.stripe, je->big_write.version, je->big_write.location
);
if (je->big_write.size > sizeof(journal_entry_big_write))
{
printf(json ? ",\"bitmap\":\"" : " (bitmap: ");
for (int i = sizeof(journal_entry_big_write); i < je->small_write.size; i++)
{
printf("%02x", ((uint8_t*)je)[i]);
}
printf(json ? "\"" : ")");
}
printf(json ? "}" : "\n");
}
else if (je->type == JE_STABLE)
{
@@ -282,3 +316,165 @@ void disk_tool_t::dump_journal_entry(int num, journal_entry *je, bool json)
);
}
}
static uint64_t sscanf_num(const char *fmt, const std::string & str)
{
uint64_t value = 0;
sscanf(str.c_str(), fmt, &value);
return value;
}
static int fromhex(char c)
{
if (c >= '0' && c <= '9')
return (c-'0');
else if (c >= 'a' && c <= 'f')
return (c-'a'+10);
else if (c >= 'A' && c <= 'F')
return (c-'A'+10);
return -1;
}
static void fromhexstr(const std::string & from, int bytes, uint8_t *to)
{
for (int i = 0; i < from.size() && i < bytes; i++)
{
int x = fromhex(from[2*i]), y = fromhex(from[2*i+1]);
if (x < 0 || y < 0)
break;
to[i] = x*16 + y;
}
}
int disk_tool_t::write_json_journal(json11::Json entries)
{
new_journal_buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, new_journal_len);
new_journal_ptr = new_journal_buf;
new_journal_data = new_journal_ptr + dsk.journal_block_size;
new_journal_in_pos = 0;
memset(new_journal_buf, 0, new_journal_len);
std::map<std::string,uint16_t> type_by_name = {
{ "start", JE_START },
{ "small_write", JE_SMALL_WRITE },
{ "small_write_instant", JE_SMALL_WRITE_INSTANT },
{ "big_write", JE_BIG_WRITE },
{ "big_write_instant", JE_BIG_WRITE_INSTANT },
{ "stable", JE_STABLE },
{ "delete", JE_DELETE },
{ "rollback", JE_ROLLBACK },
};
for (const auto & rec: entries.array_items())
{
auto t_it = type_by_name.find(rec["type"].string_value());
if (t_it == type_by_name.end())
{
fprintf(stderr, "Unknown journal entry type \"%s\", skipping\n", rec["type"].string_value().c_str());
continue;
}
uint16_t type = t_it->second;
uint32_t entry_size = (type == JE_START
? sizeof(journal_entry_start)
: (type == JE_SMALL_WRITE || type == JE_SMALL_WRITE_INSTANT
? sizeof(journal_entry_small_write) + dsk.clean_entry_bitmap_size
: (type == JE_BIG_WRITE || type == JE_BIG_WRITE_INSTANT
? sizeof(journal_entry_big_write) + dsk.clean_entry_bitmap_size
: sizeof(journal_entry_del))));
if (dsk.journal_block_size < new_journal_in_pos + entry_size)
{
new_journal_ptr = new_journal_data;
if (new_journal_ptr-new_journal_buf >= new_journal_len)
{
fprintf(stderr, "Error: entries don't fit to the new journal\n");
free(new_journal_buf);
return 1;
}
new_journal_data = new_journal_ptr+dsk.journal_block_size;
new_journal_in_pos = 0;
if (dsk.journal_block_size < entry_size)
{
fprintf(stderr, "Error: journal entry too large (%u bytes)\n", entry_size);
free(new_journal_buf);
return 1;
}
}
journal_entry *ne = (journal_entry*)(new_journal_ptr + new_journal_in_pos);
if (type == JE_START)
{
*((journal_entry_start*)ne) = (journal_entry_start){
.magic = JOURNAL_MAGIC,
.type = type,
.size = entry_size,
.journal_start = dsk.journal_block_size,
.version = JOURNAL_VERSION,
};
new_journal_ptr += dsk.journal_block_size;
new_journal_data = new_journal_ptr+dsk.journal_block_size;
new_journal_in_pos = 0;
}
else if (type == JE_SMALL_WRITE || type == JE_SMALL_WRITE_INSTANT)
{
if (new_journal_data - new_journal_buf + ne->small_write.len > new_journal_len)
{
fprintf(stderr, "Error: entries don't fit to the new journal\n");
free(new_journal_buf);
return 1;
}
*((journal_entry_small_write*)ne) = (journal_entry_small_write){
.magic = JOURNAL_MAGIC,
.type = type,
.size = entry_size,
.crc32_prev = new_crc32_prev,
.oid = {
.inode = sscanf_num("0x%lx", rec["inode"].string_value()),
.stripe = sscanf_num("0x%lx", rec["stripe"].string_value()),
},
.version = rec["ver"].uint64_value(),
.offset = (uint32_t)rec["offset"].uint64_value(),
.len = (uint32_t)rec["len"].uint64_value(),
.data_offset = (uint64_t)(new_journal_data-new_journal_buf),
.crc32_data = (uint32_t)sscanf_num("%x", rec["data_crc32"].string_value()),
};
fromhexstr(rec["bitmap"].string_value(), dsk.clean_entry_bitmap_size, ((uint8_t*)ne) + sizeof(journal_entry_small_write));
fromhexstr(rec["data"].string_value(), ne->small_write.len, new_journal_data);
if (rec["data"].is_string())
ne->small_write.crc32_data = crc32c(0, new_journal_data, ne->small_write.len);
new_journal_data += ne->small_write.len;
}
else if (type == JE_BIG_WRITE || type == JE_BIG_WRITE_INSTANT)
{
*((journal_entry_big_write*)ne) = (journal_entry_big_write){
.magic = JOURNAL_MAGIC,
.type = type,
.size = entry_size,
.crc32_prev = new_crc32_prev,
.oid = {
.inode = sscanf_num("0x%lx", rec["inode"].string_value()),
.stripe = sscanf_num("0x%lx", rec["stripe"].string_value()),
},
.version = rec["ver"].uint64_value(),
.location = sscanf_num("0x%lx", rec["loc"].string_value()),
};
fromhexstr(rec["bitmap"].string_value(), dsk.clean_entry_bitmap_size, ((uint8_t*)ne) + sizeof(journal_entry_big_write));
}
else if (type == JE_STABLE || type == JE_ROLLBACK || type == JE_DELETE)
{
*((journal_entry_del*)ne) = (journal_entry_del){
.magic = JOURNAL_MAGIC,
.type = type,
.size = entry_size,
.crc32_prev = new_crc32_prev,
.oid = {
.inode = sscanf_num("0x%lx", rec["inode"].string_value()),
.stripe = sscanf_num("0x%lx", rec["stripe"].string_value()),
},
.version = rec["ver"].uint64_value(),
};
}
ne->crc32 = je_crc32(ne);
new_crc32_prev = ne->crc32;
new_journal_in_pos += ne->size;
}
int r = resize_write_new_journal();
free(new_journal_buf);
return r;
}