Hi folks,
My time schedule doesn't allow me to wait for the community's solution, so I started to work on quick fix, which is to add a 'bdrv_truncate' function to the current NBD's BlockDriver. Basically it's an 'active resize' implementation.
I also realized that the 'bdrv_truncate' caller stack is not in a coroutine, seemed to be the main thread? Then I tried some synchronous code as below:
int nbd_truncate(BlockDriverState *bs, int64_t offset)
{
//...
nbd_client_detach_aio_context(bs);
qio_channel_set_blocking(client->ioc, true, NULL);
ret = nbd_send_request(client->ioc, &request); // step 1, send custom NBD_CMD_RESIZE request
ret = nbd_receive_reply(client->ioc, &reply);
read_sync(client->ioc, &new_size, sizeof(new_size)); // step 2, expected to receive the confirmed new_size as data
new_size = be64_to_cpu(new_size);
qio_channel_set_blocking(client->ioc, false, NULL);
nbd_client_attach_aio_context(bs, aio_context);
//...
}
However at step 2, the 'new_size' I read is not always correct. Sometimes the bytes are repeating, for instance 1073741824 (1GB) became 1073741824073741824 ...
Could you help me figure out what went wrong?
Regards,
Bob