static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)

{

    BDRVSheepdogState *s = bs->opaque;

    BDRVSheepdogState *old_s;

    char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];

    char *buf = NULL;

    uint32_t vid;

    uint32_t snapid = 0;

    int ret = 0, fd;



    old_s = g_malloc(sizeof(BDRVSheepdogState));



    memcpy(old_s, s, sizeof(BDRVSheepdogState));



    pstrcpy(vdi, sizeof(vdi), s->name);



    snapid = strtoul(snapshot_id, NULL, 10);

    if (snapid) {

        tag[0] = 0;

    } else {

        pstrcpy(tag, sizeof(tag), s->name);

    }



    ret = find_vdi_name(s, vdi, snapid, tag, &vid, 1);

    if (ret) {

        error_report("Failed to find_vdi_name");

        goto out;

    }



    fd = connect_to_sdog(s->addr, s->port);

    if (fd < 0) {

        error_report("failed to connect");

        ret = fd;

        goto out;

    }



    buf = g_malloc(SD_INODE_SIZE);

    ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies,

                      SD_INODE_SIZE, 0, s->cache_enabled);



    closesocket(fd);



    if (ret) {

        goto out;

    }



    memcpy(&s->inode, buf, sizeof(s->inode));



    if (!s->inode.vm_state_size) {

        error_report("Invalid snapshot");

        ret = -ENOENT;

        goto out;

    }



    s->is_snapshot = true;



    g_free(buf);

    g_free(old_s);



    return 0;

out:

    /* recover bdrv_sd_state */

    memcpy(s, old_s, sizeof(BDRVSheepdogState));

    g_free(buf);

    g_free(old_s);



    error_report("failed to open. recover old bdrv_sd_state.");



    return ret;

}

static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)

{

    BDRVSheepdogState *s = bs->opaque;

    BDRVSheepdogState *old_s;

    char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];

    char *buf = NULL;

    uint32_t vid;

    uint32_t snapid = 0;

    int ret = 0, fd;



    old_s = g_malloc(sizeof(BDRVSheepdogState));



    memcpy(old_s, s, sizeof(BDRVSheepdogState));



    pstrcpy(vdi, sizeof(vdi), s->name);



    snapid = strtoul(snapshot_id, NULL, 10);

    if (snapid) {

        tag[0] = 0;

    } else {

        pstrcpy(tag, sizeof(tag), s->name);

    }



    ret = find_vdi_name(s, vdi, snapid, tag, &vid, 1);

    if (ret) {

        error_report("Failed to find_vdi_name");

        goto out;

    }



    fd = connect_to_sdog(s->addr, s->port);

    if (fd < 0) {

        error_report("failed to connect");

        ret = fd;

        goto out;

    }



    buf = g_malloc(SD_INODE_SIZE);

    ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies,

                      SD_INODE_SIZE, 0, s->cache_enabled);



    closesocket(fd);



    if (ret) {

        goto out;

    }



    memcpy(&s->inode, buf, sizeof(s->inode));



    if (!s->inode.vm_state_size) {

        error_report("Invalid snapshot");

        ret = -ENOENT;

        goto out;

    }



    s->is_snapshot = true;



    g_free(buf);

    g_free(old_s);



    return 0;

out:

    

    memcpy(s, old_s, sizeof(BDRVSheepdogState));

    g_free(buf);

    g_free(old_s);



    error_report("failed to open. recover old bdrv_sd_state.");



    return ret;

}

static int FUNC_0(BlockDriverState *VAR_0, const char *VAR_1)

{

    BDRVSheepdogState *s = VAR_0->opaque;

    BDRVSheepdogState *old_s;

    char VAR_2[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];

    char *VAR_3 = NULL;

    uint32_t vid;

    uint32_t snapid = 0;

    int VAR_4 = 0, VAR_5;



    old_s = g_malloc(sizeof(BDRVSheepdogState));



    memcpy(old_s, s, sizeof(BDRVSheepdogState));



    pstrcpy(VAR_2, sizeof(VAR_2), s->name);



    snapid = strtoul(VAR_1, NULL, 10);

    if (snapid) {

        tag[0] = 0;

    } else {

        pstrcpy(tag, sizeof(tag), s->name);

    }



    VAR_4 = find_vdi_name(s, VAR_2, snapid, tag, &vid, 1);

    if (VAR_4) {

        error_report("Failed to find_vdi_name");

        goto out;

    }



    VAR_5 = connect_to_sdog(s->addr, s->port);

    if (VAR_5 < 0) {

        error_report("failed to connect");

        VAR_4 = VAR_5;

        goto out;

    }



    VAR_3 = g_malloc(SD_INODE_SIZE);

    VAR_4 = read_object(VAR_5, VAR_3, vid_to_vdi_oid(vid), s->inode.nr_copies,

                      SD_INODE_SIZE, 0, s->cache_enabled);



    closesocket(VAR_5);



    if (VAR_4) {

        goto out;

    }



    memcpy(&s->inode, VAR_3, sizeof(s->inode));



    if (!s->inode.vm_state_size) {

        error_report("Invalid snapshot");

        VAR_4 = -ENOENT;

        goto out;

    }



    s->is_snapshot = true;



    g_free(VAR_3);

    g_free(old_s);



    return 0;

out:

    

    memcpy(s, old_s, sizeof(BDRVSheepdogState));

    g_free(VAR_3);

    g_free(old_s);



    error_report("failed to open. recover old bdrv_sd_state.");



    return VAR_4;

}

static int FUNC_0(BlockDriverState *VAR_0, const char *VAR_1)
{ BDRVSheepdogState *s = VAR_0->opaque; BDRVSheepdogState *old_s; char VAR_2[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN]; char *VAR_3 = NULL; uint32_t vid; uint32_t snapid = 0; int VAR_4 = 0, VAR_5; old_s = g_malloc(sizeof(BDRVSheepdogState)); memcpy(old_s, s, sizeof(BDRVSheepdogState)); pstrcpy(VAR_2, sizeof(VAR_2), s->name); snapid = strtoul(VAR_1, NULL, 10); if (snapid) { tag[0] = 0; } else { pstrcpy(tag, sizeof(tag), s->name); } VAR_4 = find_vdi_name(s, VAR_2, snapid, tag, &vid, 1); if (VAR_4) { error_report("Failed to find_vdi_name"); goto out; } VAR_5 = connect_to_sdog(s->addr, s->port); if (VAR_5 < 0) { error_report("failed to connect"); VAR_4 = VAR_5; goto out; } VAR_3 = g_malloc(SD_INODE_SIZE); VAR_4 = read_object(VAR_5, VAR_3, vid_to_vdi_oid(vid), s->inode.nr_copies,
SD_INODE_SIZE, 0, s->cache_enabled); closesocket(VAR_5); if (VAR_4) { goto out; } memcpy(&s->inode, VAR_3, sizeof(s->inode)); if (!s->inode.vm_state_size) { error_report("Invalid snapshot"); VAR_4 = -ENOENT; goto out; } s->is_snapshot = true; g_free(VAR_3); g_free(old_s); return 0; out:
memcpy(s, old_s, sizeof(BDRVSheepdogState)); g_free(VAR_3); g_free(old_s); error_report("failed to open. recover old bdrv_sd_state."); return VAR_4; }