int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)

{

    BlockDriverAIOCB *acb;

    MultiwriteCB *mcb;

    int i;



    if (num_reqs == 0) {

        return 0;

    }



    // Create MultiwriteCB structure

    mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));

    mcb->num_requests = 0;

    mcb->num_callbacks = num_reqs;



    for (i = 0; i < num_reqs; i++) {

        mcb->callbacks[i].cb = reqs[i].cb;

        mcb->callbacks[i].opaque = reqs[i].opaque;

    }



    // Check for mergable requests

    num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);



    // Run the aio requests

    for (i = 0; i < num_reqs; i++) {

        acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,

            reqs[i].nb_sectors, multiwrite_cb, mcb);



        if (acb == NULL) {

            // We can only fail the whole thing if no request has been

            // submitted yet. Otherwise we'll wait for the submitted AIOs to

            // complete and report the error in the callback.

            if (mcb->num_requests == 0) {

                reqs[i].error = -EIO;

                goto fail;

            } else {

                mcb->error = -EIO;

                break;

            }

        } else {

            mcb->num_requests++;

        }

    }



    return 0;



fail:

    free(mcb);

    return -1;

}

int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)

{

    BlockDriverAIOCB *acb;

    MultiwriteCB *mcb;

    int i;



    if (num_reqs == 0) {

        return 0;

    }



    

    mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));

    mcb->num_requests = 0;

    mcb->num_callbacks = num_reqs;



    for (i = 0; i < num_reqs; i++) {

        mcb->callbacks[i].cb = reqs[i].cb;

        mcb->callbacks[i].opaque = reqs[i].opaque;

    }



    

    num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);



    

    for (i = 0; i < num_reqs; i++) {

        acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,

            reqs[i].nb_sectors, multiwrite_cb, mcb);



        if (acb == NULL) {

            

            

            

            if (mcb->num_requests == 0) {

                reqs[i].error = -EIO;

                goto fail;

            } else {

                mcb->error = -EIO;

                break;

            }

        } else {

            mcb->num_requests++;

        }

    }



    return 0;



fail:

    free(mcb);

    return -1;

}

                mcb->error = -EIO;
int FUNC_0(BlockDriverState *VAR_0, BlockRequest *VAR_1, int VAR_2)

{

    BlockDriverAIOCB *acb;

    MultiwriteCB *mcb;

    int VAR_3;



    if (VAR_2 == 0) {

        return 0;

    }



    

    mcb = qemu_mallocz(sizeof(*mcb) + VAR_2 * sizeof(*mcb->callbacks));

    mcb->num_requests = 0;

    mcb->num_callbacks = VAR_2;



    for (VAR_3 = 0; VAR_3 < VAR_2; VAR_3++) {

        mcb->callbacks[VAR_3].cb = VAR_1[VAR_3].cb;

        mcb->callbacks[VAR_3].opaque = VAR_1[VAR_3].opaque;

    }



    

    VAR_2 = multiwrite_merge(VAR_0, VAR_1, VAR_2, mcb);



    

    for (VAR_3 = 0; VAR_3 < VAR_2; VAR_3++) {

        acb = bdrv_aio_writev(VAR_0, VAR_1[VAR_3].sector, VAR_1[VAR_3].qiov,

            VAR_1[VAR_3].nb_sectors, multiwrite_cb, mcb);



        if (acb == NULL) {

            

            

            

            if (mcb->num_requests == 0) {

                VAR_1[VAR_3].error = -EIO;

                goto fail;

            } else {

                mcb->error = -EIO;

                break;

            }

        } else {

            mcb->num_requests++;

        }

    }



    return 0;



fail:

    free(mcb);

    return -1;

}

int FUNC_0(BlockDriverState *VAR_0, BlockRequest *VAR_1, int VAR_2)
{ BlockDriverAIOCB *acb; MultiwriteCB *mcb; int VAR_3; if (VAR_2 == 0) { return 0; } mcb = qemu_mallocz(sizeof(*mcb) + VAR_2 * sizeof(*mcb->callbacks)); mcb->num_requests = 0; mcb->num_callbacks = VAR_2; for (VAR_3 = 0; VAR_3 < VAR_2; VAR_3++) { mcb->callbacks[VAR_3].cb = VAR_1[VAR_3].cb; mcb->callbacks[VAR_3].opaque = VAR_1[VAR_3].opaque; } VAR_2 = multiwrite_merge(VAR_0, VAR_1, VAR_2, mcb); for (VAR_3 = 0; VAR_3 < VAR_2; VAR_3++) { acb = bdrv_aio_writev(VAR_0, VAR_1[VAR_3].sector, VAR_1[VAR_3].qiov,
VAR_1[VAR_3].nb_sectors, multiwrite_cb, mcb); if (acb == NULL) { if (mcb->num_requests == 0) { VAR_1[VAR_3].error = -EIO; goto fail; } else { mcb->error = -EIO; break; } } else { mcb->num_requests++; } } return 0; fail:
free(mcb); return -1; }