Browse Source

WIP Data area resize tool

Vitaliy Filippov 2 weeks ago
parent
commit
4560ae393e
  1. 2
      src/CMakeLists.txt
  2. 609
      src/disk_tool.cpp

2
src/CMakeLists.txt

@ -195,7 +195,7 @@ configure_file(vitastor.pc.in vitastor.pc @ONLY)
# vitastor-disk
add_executable(vitastor-disk
disk_tool.cpp crc32c.c rw_blocking.cpp
disk_tool.cpp crc32c.c rw_blocking.cpp allocator.cpp
)
if (${WITH_QEMU})

609
src/disk_tool.cpp

@ -29,9 +29,10 @@ struct meta_tool_t
uint32_t journal_block_size;
uint64_t journal_offset;
uint64_t journal_len;
uint64_t journal_pos;
bool all;
uint64_t journal_pos, journal_calc_data_pos;
char *meta_device;
uint32_t meta_block_size;
uint64_t meta_offset;
@ -41,16 +42,52 @@ struct meta_tool_t
char *data_device;
uint64_t data_offset;
uint64_t data_len;
uint64_t data_block_size;
// resize data and/or move metadata and journal
char *new_meta_device, *new_journal_device;
uint64_t new_data_offset, new_data_len;
uint64_t new_journal_offset, new_journal_len;
uint64_t new_meta_offset, new_meta_len;
uint64_t clean_entry_bitmap_size, clean_entry_size;
uint32_t bitmap_granularity;
int journal_fd;
int meta_fd;
int journal_fd, meta_fd;
bool first;
int data_fd;
allocator *data_alloc;
std::map<uint64_t, uint64_t> data_remap;
uint8_t *new_buf, *new_journal_ptr, *new_journal_data;
uint64_t new_journal_in_pos;
int64_t data_idx_diff;
uint64_t total_blocks, free_first, free_last;
uint64_t new_clean_entry_bitmap_size, new_clean_entry_size, new_entries_per_block;
int new_journal_fd, new_meta_fd;
bool started;
void *small_write_data;
uint32_t data_crc32;
uint32_t crc32_last;
uint32_t new_crc32_prev;
int dump_journal();
int dump_journal_block(void *buf);
void dump_journal_entry(int num, journal_entry *je);
int process_journal(std::function<int(void*)> block_fn);
int process_journal_block(void *buf, std::function<void(int, journal_entry*)> iter_fn);
int process_meta(std::function<void(blockstore_meta_header_v1_t *)> hdr_fn,
std::function<void(uint64_t, clean_disk_entry*, uint8_t*)> record_fn);
int dump_meta();
void dump_meta_header(blockstore_meta_header_v1_t *hdr);
void dump_meta_entry(uint64_t block_num, clean_disk_entry *entry, uint8_t *bitmap);
int resize_data();
void resize_init(blockstore_meta_header_v1_t *hdr);
int resize_remap_blocks();
int resize_copy_data();
int resize_rewrite_journal();
int resize_rewrite_meta();
};
int main(int argc, char *argv[])
@ -131,27 +168,27 @@ int meta_tool_t::dump_journal()
if (self.journal_block_size < DIRECT_IO_ALIGNMENT || (self.journal_block_size % DIRECT_IO_ALIGNMENT) ||
self.journal_block_size > 128*1024)
{
printf("Invalid journal block size\n");
fprintf(stderr, "Invalid journal block size\n");
return 1;
}
self.journal_fd = open(self.journal_device, O_DIRECT|O_RDONLY);
if (self.journal_fd == -1)
if (self.journal_fd < 0)
{
printf("Failed to open journal\n");
fprintf(stderr, "Failed to open journal device %s: %s\n", journal_device, strerror(errno));
return 1;
}
void *data = memalign(MEM_ALIGNMENT, self.journal_block_size);
self.journal_pos = 0;
if (self.all)
{
void *journal_buf = memalign_or_die(MEM_ALIGNMENT, self.journal_block_size);
self.journal_pos = 0;
while (self.journal_pos < self.journal_len)
{
int r = pread(self.journal_fd, data, self.journal_block_size, self.journal_offset+self.journal_pos);
int r = pread(self.journal_fd, journal_buf, self.journal_block_size, self.journal_offset+self.journal_pos);
assert(r == self.journal_block_size);
uint64_t s;
for (s = 0; s < self.journal_block_size; s += 8)
{
if (*((uint64_t*)((uint8_t*)data+s)) != 0)
if (*((uint64_t*)((uint8_t*)journal_buf+s)) != 0)
break;
}
if (s == self.journal_block_size)
@ -159,55 +196,67 @@ int meta_tool_t::dump_journal()
printf("offset %08lx: zeroes\n", self.journal_pos);
self.journal_pos += self.journal_block_size;
}
else if (((journal_entry*)data)->magic == JOURNAL_MAGIC)
else if (((journal_entry*)journal_buf)->magic == JOURNAL_MAGIC)
{
printf("offset %08lx:\n", self.journal_pos);
self.dump_journal_block(data);
self.process_journal_block(journal_buf, [this](int num, journal_entry *je) { dump_journal_entry(num, je); });
}
else
{
printf("offset %08lx: no magic in the beginning, looks like random data (pattern=%lx)\n", self.journal_pos, *((uint64_t*)data));
printf("offset %08lx: no magic in the beginning, looks like random data (pattern=%lx)\n", self.journal_pos, *((uint64_t*)journal_buf));
self.journal_pos += self.journal_block_size;
}
}
free(journal_buf);
}
else
{
int r = pread(self.journal_fd, data, self.journal_block_size, self.journal_offset+self.journal_pos);
assert(r == self.journal_block_size);
journal_entry *je = (journal_entry*)(data);
if (je->magic != JOURNAL_MAGIC || je->type != JE_START || je_crc32(je) != je->crc32)
process_journal([this](void *data)
{
printf("offset %08lx: journal superblock is invalid\n", self.journal_pos);
}
else
printf("offset %08lx:\n", journal_pos);
int r = process_journal_block(data, [this](int num, journal_entry *je) { dump_journal_entry(num, je); });
if (r <= 0)
printf("end of the journal\n");
return r;
});
}
close(self.journal_fd);
return 0;
}
int meta_tool_t::process_journal(std::function<int(void*)> block_fn)
{
void *data = memalign_or_die(MEM_ALIGNMENT, this->journal_block_size);
this->journal_pos = 0;
int r = pread(this->journal_fd, data, this->journal_block_size, this->journal_offset+this->journal_pos);
assert(r == this->journal_block_size);
journal_entry *je = (journal_entry*)(data);
if (je->magic != JOURNAL_MAGIC || je->type != JE_START || je_crc32(je) != je->crc32)
{
printf("offset %08lx: journal superblock is invalid\n", this->journal_pos);
return 1;
}
else
{
block_fn(data);
this->started = false;
this->journal_pos = je->start.journal_start;
while (1)
{
printf("offset %08lx:\n", self.journal_pos);
self.dump_journal_block(data);
self.started = false;
self.journal_pos = je->start.journal_start;
while (1)
{
if (self.journal_pos >= self.journal_len)
self.journal_pos = self.journal_block_size;
r = pread(self.journal_fd, data, self.journal_block_size, self.journal_offset+self.journal_pos);
assert(r == self.journal_block_size);
printf("offset %08lx:\n", self.journal_pos);
r = self.dump_journal_block(data);
if (r <= 0)
{
printf("end of the journal\n");
break;
}
}
if (this->journal_pos >= this->journal_len)
this->journal_pos = this->journal_block_size;
r = pread(this->journal_fd, data, this->journal_block_size, this->journal_offset+this->journal_pos);
assert(r == this->journal_block_size);
r = block_fn(data);
if (r <= 0)
break;
}
}
free(data);
close(self.journal_fd);
return 0;
}
int meta_tool_t::dump_journal_block(void *buf)
int meta_tool_t::process_journal_block(void *buf, std::function<void(int, journal_entry*)> iter_fn)
{
uint32_t pos = 0;
journal_pos += journal_block_size;
@ -228,66 +277,30 @@ int meta_tool_t::dump_journal_block(void *buf)
}
started = true;
crc32_last = je->crc32;
printf("entry % 3d: crc32=%08x %s prev=%08x ", entry, je->crc32, (crc32_valid ? "(valid)" : "(invalid)"), je->crc32_prev);
if (je->type == JE_START)
{
printf("je_start start=%08lx\n", je->start.journal_start);
}
else if (je->type == JE_SMALL_WRITE || je->type == JE_SMALL_WRITE_INSTANT)
if (je->type == JE_SMALL_WRITE || je->type == JE_SMALL_WRITE_INSTANT)
{
printf(
"je_small_write%s oid=%lx:%lx ver=%lu offset=%u len=%u loc=%08lx",
je->type == JE_SMALL_WRITE_INSTANT ? "_instant" : "",
je->small_write.oid.inode, je->small_write.oid.stripe,
je->small_write.version, je->small_write.offset, je->small_write.len,
je->small_write.data_offset
);
this->journal_calc_data_pos = journal_pos;
if (journal_pos + je->small_write.len > journal_len)
{
// data continues from the beginning of the journal
journal_pos = journal_block_size;
this->journal_calc_data_pos = journal_pos = journal_block_size;
wrapped = true;
}
if (journal_pos != je->small_write.data_offset)
{
printf(" (mismatched, calculated = %lu)", journal_pos);
}
journal_pos += je->small_write.len;
if (journal_pos >= journal_len)
{
journal_pos = journal_block_size;
wrapped = true;
}
uint32_t data_crc32 = 0;
void *data = memalign(MEM_ALIGNMENT, je->small_write.len);
assert(pread(this->journal_fd, data, je->small_write.len, journal_offset+je->small_write.data_offset) == je->small_write.len);
data_crc32 = crc32c(0, data, je->small_write.len);
free(data);
printf(
" data_crc32=%08x%s", je->small_write.crc32_data,
(data_crc32 != je->small_write.crc32_data) ? " (invalid)" : " (valid)"
);
printf("\n");
}
else if (je->type == JE_BIG_WRITE || je->type == JE_BIG_WRITE_INSTANT)
{
printf(
"je_big_write%s oid=%lx:%lx ver=%lu loc=%08lx\n",
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
);
small_write_data = memalign_or_die(MEM_ALIGNMENT, je->small_write.len);
assert(pread(this->journal_fd, small_write_data, je->small_write.len, journal_offset+je->small_write.data_offset) == je->small_write.len);
data_crc32 = crc32c(0, small_write_data, je->small_write.len);
}
else if (je->type == JE_STABLE)
iter_fn(entry, je);
if (je->type == JE_SMALL_WRITE || je->type == JE_SMALL_WRITE_INSTANT)
{
printf("je_stable oid=%lx:%lx ver=%lu\n", je->stable.oid.inode, je->stable.oid.stripe, je->stable.version);
}
else if (je->type == JE_ROLLBACK)
{
printf("je_rollback oid=%lx:%lx ver=%lu\n", je->rollback.oid.inode, je->rollback.oid.stripe, je->rollback.version);
}
else if (je->type == JE_DELETE)
{
printf("je_delete oid=%lx:%lx ver=%lu\n", je->del.oid.inode, je->del.oid.stripe, je->del.version);
free(small_write_data);
small_write_data = NULL;
}
pos += je->size;
entry++;
@ -299,7 +312,56 @@ int meta_tool_t::dump_journal_block(void *buf)
return entry;
}
int meta_tool_t::dump_meta()
void meta_tool_t::dump_journal_entry(int num, journal_entry *je)
{
printf("entry % 3d: crc32=%08x %s prev=%08x ", num, je->crc32, (je_crc32(je) == je->crc32 ? "(valid)" : "(invalid)"), je->crc32_prev);
if (je->type == JE_START)
{
printf("je_start start=%08lx\n", je->start.journal_start);
}
else if (je->type == JE_SMALL_WRITE || je->type == JE_SMALL_WRITE_INSTANT)
{
printf(
"je_small_write%s oid=%lx:%lx ver=%lu offset=%u len=%u loc=%08lx",
je->type == JE_SMALL_WRITE_INSTANT ? "_instant" : "",
je->small_write.oid.inode, je->small_write.oid.stripe,
je->small_write.version, je->small_write.offset, je->small_write.len,
je->small_write.data_offset
);
if (journal_calc_data_pos != je->small_write.data_offset)
{
printf(" (mismatched, calculated = %lu)", journal_pos);
}
printf(
" data_crc32=%08x%s", je->small_write.crc32_data,
(data_crc32 != je->small_write.crc32_data) ? " (invalid)" : " (valid)"
);
printf("\n");
}
else if (je->type == JE_BIG_WRITE || je->type == JE_BIG_WRITE_INSTANT)
{
printf(
"je_big_write%s oid=%lx:%lx ver=%lu loc=%08lx\n",
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
);
}
else if (je->type == JE_STABLE)
{
printf("je_stable oid=%lx:%lx ver=%lu\n", je->stable.oid.inode, je->stable.oid.stripe, je->stable.version);
}
else if (je->type == JE_ROLLBACK)
{
printf("je_rollback oid=%lx:%lx ver=%lu\n", je->rollback.oid.inode, je->rollback.oid.stripe, je->rollback.version);
}
else if (je->type == JE_DELETE)
{
printf("je_delete oid=%lx:%lx ver=%lu\n", je->del.oid.inode, je->del.oid.stripe, je->del.version);
}
}
int meta_tool_t::process_meta(std::function<void(blockstore_meta_header_v1_t *)> hdr_fn,
std::function<void(uint64_t, clean_disk_entry*, uint8_t*)> record_fn)
{
if (this->meta_block_size % DIRECT_IO_ALIGNMENT)
{
@ -307,9 +369,9 @@ int meta_tool_t::dump_meta()
return 1;
}
this->meta_fd = open(this->meta_device, O_DIRECT|O_RDONLY);
if (this->meta_fd == -1)
if (this->meta_fd < 0)
{
printf("Failed to open metadata device\n");
printf("Failed to open metadata device %s: %s\n", meta_device, strerror(errno));
return 1;
}
int buf_size = 1024*1024;
@ -338,17 +400,13 @@ int meta_tool_t::dump_meta()
data = memalign_or_die(MEM_ALIGNMENT, buf_size);
}
}
this->meta_offset += this->meta_block_size;
this->meta_len -= this->meta_block_size;
uint64_t clean_entry_bitmap_size = hdr->data_block_size / hdr->bitmap_granularity / 8;
uint64_t clean_entry_size = sizeof(clean_disk_entry) + 2*clean_entry_bitmap_size;
this->bitmap_granularity = hdr->bitmap_granularity;
this->clean_entry_bitmap_size = hdr->data_block_size / hdr->bitmap_granularity / 8;
this->clean_entry_size = sizeof(clean_disk_entry) + 2*clean_entry_bitmap_size;
uint64_t block_num = 0;
printf(
"{\"version\":\"0.6\",\"meta_block_size\":%u,\"data_block_size\":%u,\"bitmap_granularity\":%u,\"entries\":[\n",
hdr->meta_block_size, hdr->data_block_size, hdr->bitmap_granularity
);
bool first = true;
lseek64(this->meta_fd, this->meta_offset, 0);
hdr_fn(hdr);
this->meta_pos = this->meta_block_size;
lseek64(this->meta_fd, this->meta_offset+this->meta_pos, 0);
while (this->meta_pos < this->meta_len)
{
uint64_t read_len = buf_size < this->meta_len-this->meta_pos ? buf_size : this->meta_len-this->meta_pos;
@ -361,37 +419,19 @@ int meta_tool_t::dump_meta()
clean_disk_entry *entry = (clean_disk_entry*)(data + blk + ioff);
if (entry->oid.inode)
{
printf(
#define ENTRY_FMT "{\"block\":%lu,\"pool\":%u,\"inode\":%lu,\"stripe\":%lu,\"version\":%lu,\"bitmap\":\""
(first ? ENTRY_FMT : (",\n" ENTRY_FMT)),
#undef ENTRY_FMT
block_num, INODE_POOL(entry->oid.inode), INODE_NO_POOL(entry->oid.inode),
entry->oid.stripe, entry->version
);
first = false;
for (uint64_t i = 0; i < clean_entry_bitmap_size; i++)
{
printf("%02x", entry->bitmap[i]);
}
printf("\",\"ext_bitmap\":\"");
for (uint64_t i = 0; i < clean_entry_bitmap_size; i++)
{
printf("%02x", entry->bitmap[clean_entry_bitmap_size + i]);
}
printf("\"}");
record_fn(block_num, entry, entry->bitmap);
}
}
}
}
printf("\n]}\n");
}
else
{
// Vitastor 0.4-0.5 - static array of clean_disk_entry
uint64_t clean_entry_size = sizeof(clean_disk_entry);
this->clean_entry_bitmap_size = 0;
this->clean_entry_size = sizeof(clean_disk_entry);
uint64_t block_num = 0;
printf("{\"version\":\"0.5\",\"meta_block_size\":%u,\"entries\":[\n", this->meta_block_size);
bool first = true;
hdr_fn(NULL);
while (this->meta_pos < this->meta_len)
{
uint64_t read_len = buf_size < this->meta_len-this->meta_pos ? buf_size : this->meta_len-this->meta_pos;
@ -404,21 +444,328 @@ int meta_tool_t::dump_meta()
clean_disk_entry *entry = (clean_disk_entry*)(data + blk + ioff);
if (entry->oid.inode)
{
printf(
#define ENTRY_FMT "{\"block\":%lu,\"pool\":%u,\"inode\":%lu,\"stripe\":%lu,\"version\":%lu}"
(first ? ENTRY_FMT : (",\n" ENTRY_FMT)),
#undef ENTRY_FMT
block_num, INODE_POOL(entry->oid.inode), INODE_NO_POOL(entry->oid.inode),
entry->oid.stripe, entry->version
);
first = false;
record_fn(block_num, entry, NULL);
}
}
}
}
printf("\n]}\n");
}
free(data);
close(this->meta_fd);
return 0;
}
int meta_tool_t::dump_meta()
{
int r = process_meta(
[this](blockstore_meta_header_v1_t *hdr) { dump_meta_header(hdr); },
[this](uint64_t block_num, clean_disk_entry *entry, uint8_t *bitmap) { dump_meta_entry(block_num, entry, bitmap); }
);
printf("\n]}\n");
return r;
}
void meta_tool_t::dump_meta_header(blockstore_meta_header_v1_t *hdr)
{
if (hdr)
{
printf(
"{\"version\":\"0.6\",\"meta_block_size\":%u,\"data_block_size\":%u,\"bitmap_granularity\":%u,\"entries\":[\n",
hdr->meta_block_size, hdr->data_block_size, hdr->bitmap_granularity
);
}
else
{
printf("{\"version\":\"0.5\",\"meta_block_size\":%u,\"entries\":[\n", this->meta_block_size);
}
this->first = true;
}
void meta_tool_t::dump_meta_entry(uint64_t block_num, clean_disk_entry *entry, uint8_t *bitmap)
{
printf(
#define ENTRY_FMT "{\"block\":%lu,\"pool\":%u,\"inode\":%lu,\"stripe\":%lu,\"version\":%lu"
(this->first ? ENTRY_FMT : (",\n" ENTRY_FMT)),
#undef ENTRY_FMT
block_num, INODE_POOL(entry->oid.inode), INODE_NO_POOL(entry->oid.inode),
entry->oid.stripe, entry->version
);
if (bitmap)
{
printf(",\"bitmap\":\"");
for (uint64_t i = 0; i < clean_entry_bitmap_size; i++)
{
printf("%02x", bitmap[i]);
}
printf("\",\"ext_bitmap\":\"");
for (uint64_t i = 0; i < clean_entry_bitmap_size; i++)
{
printf("%02x", bitmap[clean_entry_bitmap_size + i]);
}
printf("\"}");
}
else
{
printf("}");
}
this->first = false;
}
int meta_tool_t::resize_data()
{
int r;
// Check parameters and fill allocator
process_meta(
[this](blockstore_meta_header_v1_t *hdr)
{
resize_init(hdr);
},
[this](uint64_t block_num, clean_disk_entry *entry, uint8_t *bitmap)
{
data_alloc->set(block_num, true);
}
);
process_journal([this](void *buf)
{
return process_journal_block(buf, [this](int num, journal_entry *je)
{
if (je->type == JE_BIG_WRITE || je->type == JE_BIG_WRITE_INSTANT)
{
data_alloc->set(je->big_write.location / data_block_size, true);
}
});
});
// Remap blocks
r = resize_remap_blocks();
if (r != 0)
return r;
// Copy data blocks into new places
r = resize_copy_data();
if (r != 0)
return r;
// Rewrite journal
r = resize_rewrite_journal();
if (r != 0)
return r;
// Rewrite metadata
r = resize_rewrite_meta();
if (r != 0)
return r;
return 0;
}
void meta_tool_t::resize_init(blockstore_meta_header_v1_t *hdr)
{
if (hdr && data_block_size != hdr->data_block_size)
{
if (data_block_size)
{
fprintf(stderr, "Using data block size of %u bytes from metadata superblock\n", hdr->data_block_size);
}
data_block_size = hdr->data_block_size;
}
if (((new_data_len-data_len) % data_block_size) ||
((new_data_offset-data_offset) % data_block_size))
{
fprintf(stderr, "Data alignment mismatch\n");
exit(1);
}
data_idx_diff = (new_data_offset-data_offset) / data_block_size;
free_first = new_data_offset > data_offset ? data_idx_diff : 0;
free_last = (new_data_offset+new_data_len < data_offset+data_len)
? (data_offset+data_len-new_data_offset-new_data_len) / data_block_size
: 0;
new_clean_entry_bitmap_size = data_block_size / (hdr ? hdr->bitmap_granularity : 4096) / 8;
new_clean_entry_size = sizeof(clean_disk_entry) + 2 * new_clean_entry_bitmap_size;
new_entries_per_block = meta_block_size/new_clean_entry_size;
uint64_t new_meta_blocks = 1 + (new_data_len/data_block_size + new_entries_per_block-1) / new_entries_per_block;
if (new_meta_len < meta_block_size*new_meta_blocks)
{
fprintf(stderr, "New metadata area size is too small, should be at least %lu bytes\n", meta_block_size*new_meta_blocks);
exit(1);
}
}
int meta_tool_t::resize_remap_blocks()
{
total_blocks = data_len / data_block_size;
for (uint64_t i = 0; i < free_first; i++)
{
if (data_alloc->get(i))
data_remap[i] = 0;
else
data_alloc->set(i, true);
}
for (uint64_t i = 0; i < free_last; i++)
{
if (data_alloc->get(total_blocks-i))
data_remap[total_blocks-i] = 0;
else
data_alloc->set(total_blocks-i, true);
}
for (auto & p: data_remap)
{
uint64_t new_loc = data_alloc->find_free();
if (new_loc == UINT64_MAX)
{
fprintf(stderr, "Not enough space to move data\n");
return 1;
}
data_remap[p.first] = new_loc;
}
return 0;
}
int meta_tool_t::resize_copy_data()
{
// FIXME: Use io_uring
data_fd = open(data_device, O_DIRECT|O_RDWR);
if (data_fd < 0)
{
fprintf(stderr, "Failed to open data device %s: %s\n", data_device, strerror(errno));
return 1;
}
close(data_fd);
return 0;
}
int meta_tool_t::resize_rewrite_journal()
{
// Simply overwriting on the fly may be impossible because old and new areas may overlap
// For now, just build new journal data in memory
new_buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, this->new_journal_len);
new_journal_ptr = new_buf;
new_journal_data = new_journal_ptr + journal_block_size;
memset(new_buf, 0, this->new_journal_len);
process_journal([this](void *buf)
{
return process_journal_block(buf, [this](int num, journal_entry *je)
{
journal_entry *ne = (journal_entry*)(new_journal_ptr + new_journal_in_pos);
if (je->type == JE_START)
{
*((journal_entry_start*)ne) = (journal_entry_start){
.magic = JOURNAL_MAGIC,
.type = JE_START,
.size = sizeof(ne->start),
.journal_start = journal_block_size,
.version = JOURNAL_VERSION,
};
ne->crc32 = je_crc32(ne);
new_journal_ptr += journal_block_size;
}
else
{
if (journal_block_size < new_journal_in_pos+je->size)
{
new_journal_ptr = new_journal_data;
if (new_journal_ptr-new_buf >= new_journal_len)
{
fprintf(stderr, "Error: live entries don't fit to the new journal\n");
exit(1);
}
new_journal_data += journal_block_size;
new_journal_in_pos = 0;
if (journal_block_size < je->size)
{
fprintf(stderr, "Error: journal entry too large (%u bytes)\n", je->size);
exit(1);
}
}
memcpy(ne, je, je->size);
ne->crc32_prev = new_crc32_prev;
if (je->type == JE_BIG_WRITE || je->type == JE_BIG_WRITE_INSTANT)
{
// Change the block reference
auto remap_it = data_remap.find(ne->big_write.location / data_block_size);
if (remap_it != data_remap.end())
{
ne->big_write.location = remap_it->second * data_block_size;
}
ne->big_write.location += data_idx_diff;
}
else if (je->type == JE_SMALL_WRITE || je->type == JE_SMALL_WRITE_INSTANT)
{
ne->small_write.data_offset = new_journal_data-new_buf;
if (ne->small_write.data_offset + ne->small_write.len > new_journal_len)
{
fprintf(stderr, "Error: live entries don't fit to the new journal\n");
exit(1);
}
memcpy(new_journal_data, small_write_data, ne->small_write.len);
new_journal_data += ne->small_write.len;
}
ne->crc32 = je_crc32(ne);
new_journal_in_pos += ne->size;
new_crc32_prev = ne->crc32;
}
});
});
// FIXME: Write new journal and metadata with journaling if they overlap with old
new_journal_fd = open(new_journal_device, O_DIRECT|O_RDWR);
if (new_journal_fd < 0)
{
fprintf(stderr, "Failed to open new journal device %s: %s\n", new_journal_device, strerror(errno));
return 1;
}
lseek64(new_journal_fd, new_journal_offset, 0);
write_blocking(new_journal_fd, new_buf, new_journal_len);
fsync(new_journal_fd);
close(new_journal_fd);
free(new_buf);
return 0;
}
int meta_tool_t::resize_rewrite_meta()
{
new_buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, new_meta_len);
memset(new_buf, 0, new_meta_len);
int r = process_meta(
[this](blockstore_meta_header_v1_t *hdr)
{
blockstore_meta_header_v1_t *new_hdr = (blockstore_meta_header_v1_t *)new_buf;
new_hdr->zero = 0;
new_hdr->magic = BLOCKSTORE_META_MAGIC_V1;
new_hdr->version = BLOCKSTORE_META_VERSION_V1;
new_hdr->meta_block_size = this->meta_block_size;
new_hdr->data_block_size = this->data_block_size;
new_hdr->bitmap_granularity = this->bitmap_granularity ? this->bitmap_granularity : 4096;
},
[this](uint64_t block_num, clean_disk_entry *entry, uint8_t *bitmap)
{
auto remap_it = data_remap.find(block_num);
if (remap_it != data_remap.end())
block_num = remap_it->second;
if (block_num < free_first || block_num >= total_blocks-free_last)
return;
block_num += data_idx_diff;
clean_disk_entry *new_entry = (clean_disk_entry*)(new_buf + meta_block_size +
meta_block_size*(block_num / new_entries_per_block) +
new_clean_entry_size*(block_num % new_entries_per_block));
new_entry->oid = entry->oid;
new_entry->version = entry->version;
if (bitmap)
memcpy(new_entry->bitmap, bitmap, 2*new_clean_entry_bitmap_size);
else
memset(new_entry->bitmap, 0xff, 2*new_clean_entry_bitmap_size);
}
);
if (r != 0)
{
free(new_buf);
return r;
}
new_meta_fd = open(new_meta_device, O_DIRECT|O_RDWR);
if (new_meta_fd < 0)
{
fprintf(stderr, "Failed to open new metadata device %s: %s\n", new_meta_device, strerror(errno));
return 1;
}
lseek64(new_meta_fd, new_meta_offset, 0);
write_blocking(new_meta_fd, new_buf, new_meta_len);
fsync(new_meta_fd);
close(new_meta_fd);
free(new_buf);
return 0;
}

Loading…
Cancel
Save