Remove duplicate journal writing code (and fix it at the same time)
This commit is contained in:
+26
-64
@@ -59,87 +59,49 @@ int blockstore::continue_sync(blockstore_operation *op)
|
||||
{
|
||||
// 2nd step: Data device is synced, prepare & write journal entries
|
||||
// Check space in the journal and journal memory buffers
|
||||
int required = op->sync_big_writes.size(), sectors_required = 1;
|
||||
uint64_t next_pos = journal.next_free, next_sector = journal.cur_sector;
|
||||
while (1)
|
||||
blockstore_journal_check_t space_check(this);
|
||||
if (!space_check.check_available(op, op->sync_big_writes.size(), sizeof(journal_entry_big_write), 0))
|
||||
{
|
||||
int fits = (512 - journal.in_sector_pos) / sizeof(journal_entry_big_write);
|
||||
required -= fits;
|
||||
if (required <= 0)
|
||||
break;
|
||||
next_pos = (next_pos+512) < journal.len ? next_pos+512 : 512;
|
||||
sectors_required++;
|
||||
next_sector = ((next_sector + 1) % journal.sector_count);
|
||||
if (journal.sector_info[next_sector].usage_count > 0)
|
||||
{
|
||||
// No memory buffer available. Wait for it.
|
||||
op->wait_for = WAIT_JOURNAL_BUFFER;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (next_pos >= journal.used_start)
|
||||
{
|
||||
// No space in the journal. Wait for it.
|
||||
op->wait_for = WAIT_JOURNAL;
|
||||
op->wait_detail = next_pos;
|
||||
return 0;
|
||||
}
|
||||
// Get SQEs. Don't bother about merging, submit each journal sector as a separate request
|
||||
struct io_uring_sqe *sqe[sectors_required+1];
|
||||
for (int i = 0; i < sectors_required+1; i++)
|
||||
struct io_uring_sqe *sqe[space_check.sectors_required+1];
|
||||
for (int i = 0; i < space_check.sectors_required+1; i++)
|
||||
{
|
||||
BS_SUBMIT_GET_SQE_DECL(sqe[i]);
|
||||
}
|
||||
// Prepare and submit journal entries
|
||||
op->min_used_journal_sector = 1 + journal.cur_sector;
|
||||
sectors_required = 0;
|
||||
required = op->sync_big_writes.size();
|
||||
auto it = op->sync_big_writes.begin();
|
||||
while (1)
|
||||
int s = 0, cur_sector = -1;
|
||||
while (it != op->sync_big_writes.end())
|
||||
{
|
||||
int fits = (512 - journal.in_sector_pos) / sizeof(journal_entry_big_write);
|
||||
while (fits > 0 && required > 0)
|
||||
journal_entry_big_write *je = (journal_entry_big_write*)
|
||||
prefill_single_journal_entry(journal, JE_BIG_WRITE, sizeof(journal_entry_big_write));
|
||||
je->oid = it->oid;
|
||||
je->version = it->version;
|
||||
je->block = dirty_db[*it].location;
|
||||
je->crc32 = je_crc32((journal_entry*)je);
|
||||
journal.crc32_last = je->crc32;
|
||||
it++;
|
||||
if (cur_sector != journal.cur_sector)
|
||||
{
|
||||
journal_entry_big_write *je = (journal_entry_big_write*)(
|
||||
journal.sector_buf + 512*journal.cur_sector + journal.in_sector_pos
|
||||
cur_sector = journal.cur_sector;
|
||||
journal.sector_info[journal.cur_sector].usage_count++;
|
||||
struct ring_data_t *data = ((ring_data_t*)sqe[s]->user_data);
|
||||
data->iov = (struct iovec){ journal.sector_buf + 512*journal.cur_sector, 512 };
|
||||
data->op = op;
|
||||
io_uring_prep_writev(
|
||||
sqe[s], journal.fd, &data->iov, 1, journal.offset + journal.sector_info[journal.cur_sector].offset
|
||||
);
|
||||
*je = {
|
||||
.crc32 = 0,
|
||||
.magic = JOURNAL_MAGIC,
|
||||
.type = JE_BIG_WRITE,
|
||||
.size = sizeof(journal_entry_big_write),
|
||||
.crc32_prev = journal.crc32_last,
|
||||
.oid = it->oid,
|
||||
.version = it->version,
|
||||
.block = dirty_db[*it].location,
|
||||
};
|
||||
je->crc32 = je_crc32((journal_entry*)je);
|
||||
journal.crc32_last = je->crc32;
|
||||
journal.in_sector_pos += sizeof(journal_entry_big_write);
|
||||
required--;
|
||||
it++;
|
||||
s++;
|
||||
}
|
||||
if (required <= 0)
|
||||
break;
|
||||
journal.sector_info[journal.cur_sector].usage_count++;
|
||||
struct ring_data_t *data = ((ring_data_t*)sqe[sectors_required]->user_data);
|
||||
data->iov = (struct iovec){ journal.sector_buf + 512*journal.cur_sector, 512 };
|
||||
data->op = op;
|
||||
io_uring_prep_writev(
|
||||
sqe[sectors_required], journal.fd, &data->iov, 1, journal.offset + journal.sector_info[journal.cur_sector].offset
|
||||
);
|
||||
journal.cur_sector = ((journal.cur_sector + 1) % journal.sector_count);
|
||||
journal.sector_info[journal.cur_sector].offset = journal.next_free;
|
||||
journal.in_sector_pos = 0;
|
||||
journal.next_free = (journal.next_free + 512) < journal.len ? journal.next_free + 512 : 512;
|
||||
memset(journal.sector_buf + 512*journal.cur_sector, 0, 512);
|
||||
sectors_required++;
|
||||
}
|
||||
// ... And a journal fsync
|
||||
io_uring_prep_fsync(sqe[sectors_required], journal.fd, 0);
|
||||
struct ring_data_t *data = ((ring_data_t*)sqe[sectors_required]->user_data);
|
||||
io_uring_prep_fsync(sqe[s], journal.fd, 0);
|
||||
struct ring_data_t *data = ((ring_data_t*)sqe[s]->user_data);
|
||||
data->op = op;
|
||||
op->pending_ops = 1 + sectors_required;
|
||||
op->pending_ops = 1 + s;
|
||||
op->max_used_journal_sector = 1 + journal.cur_sector;
|
||||
op->sync_state = SYNC_JOURNAL_SYNC_SENT;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user