int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size)

{

    TRACE("Setting NBD socket");



    if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) {

        int serrno = errno;

        LOG("Failed to set NBD socket");

        return -serrno;

    }



    TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);



    if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {

        int serrno = errno;

        LOG("Failed setting NBD block size");

        return -serrno;

    }



    TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));



    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) {

        int serrno = errno;

        LOG("Failed setting size (in blocks)");

        return -serrno;

    }



    if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {

        if (errno == ENOTTY) {

            int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;

            TRACE("Setting readonly attribute");



            if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {

                int serrno = errno;

                LOG("Failed setting read-only attribute");

                return -serrno;

            }

        } else {

            int serrno = errno;

            LOG("Failed setting flags");

            return -serrno;

        }

    }



    TRACE("Negotiation ended");



    return 0;

}

int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size)

{

    TRACE("Setting NBD socket");



    if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) {

        int serrno = errno;

        LOG("Failed to set NBD socket");

        return -serrno;

    }



    TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);



    if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {

        int serrno = errno;

        LOG("Failed setting NBD block size");

        return -serrno;

    }



    TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));



    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) {

        int serrno = errno;

        LOG("Failed setting size (in blocks)");

        return -serrno;

    }



    if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {

        if (errno == ENOTTY) {

            int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;

            TRACE("Setting readonly attribute");



            if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {

                int serrno = errno;

                LOG("Failed setting read-only attribute");

                return -serrno;

            }

        } else {

            int serrno = errno;

            LOG("Failed setting flags");

            return -serrno;

        }

    }



    TRACE("Negotiation ended");



    return 0;

}

int FUNC_0(int VAR_0, QIOChannelSocket *VAR_1, uint32_t VAR_2, off_t VAR_3)

{

    TRACE("Setting NBD socket");



    if (ioctl(VAR_0, NBD_SET_SOCK, VAR_1->VAR_0) < 0) {

        int VAR_6 = errno;

        LOG("Failed to set NBD socket");

        return -VAR_6;

    }



    TRACE("Setting block VAR_3 to %lu", (unsigned long)BDRV_SECTOR_SIZE);



    if (ioctl(VAR_0, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {

        int VAR_6 = errno;

        LOG("Failed setting NBD block VAR_3");

        return -VAR_6;

    }



    TRACE("Setting VAR_3 to %zd block(s)", (size_t)(VAR_3 / BDRV_SECTOR_SIZE));



    if (ioctl(VAR_0, NBD_SET_SIZE_BLOCKS, (size_t)(VAR_3 / BDRV_SECTOR_SIZE)) < 0) {

        int VAR_6 = errno;

        LOG("Failed setting VAR_3 (in blocks)");

        return -VAR_6;

    }



    if (ioctl(VAR_0, NBD_SET_FLAGS, VAR_2) < 0) {

        if (errno == ENOTTY) {

            int VAR_5 = (VAR_2 & NBD_FLAG_READ_ONLY) != 0;

            TRACE("Setting readonly attribute");



            if (ioctl(VAR_0, BLKROSET, (unsigned long) &VAR_5) < 0) {

                int VAR_6 = errno;

                LOG("Failed setting read-only attribute");

                return -VAR_6;

            }

        } else {

            int VAR_6 = errno;

            LOG("Failed setting VAR_2");

            return -VAR_6;

        }

    }



    TRACE("Negotiation ended");



    return 0;

}

int FUNC_0(int VAR_0, QIOChannelSocket *VAR_1, uint32_t VAR_2, off_t VAR_3)
{ TRACE("Setting NBD socket"); if (ioctl(VAR_0, NBD_SET_SOCK, VAR_1->VAR_0) < 0) { int VAR_6 = errno; LOG("Failed to set NBD socket"); return -VAR_6; } TRACE("Setting block VAR_3 to %lu", (unsigned long)BDRV_SECTOR_SIZE); if (ioctl(VAR_0, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) { int VAR_6 = errno; LOG("Failed setting NBD block VAR_3"); return -VAR_6; } TRACE("Setting VAR_3 to %zd block(s)", (size_t)(VAR_3 / BDRV_SECTOR_SIZE)); if (ioctl(VAR_0, NBD_SET_SIZE_BLOCKS, (size_t)(VAR_3 / BDRV_SECTOR_SIZE)) < 0) { int VAR_6 = errno; LOG("Failed setting VAR_3 (in blocks)"); return -VAR_6; } if (ioctl(VAR_0, NBD_SET_FLAGS, VAR_2) < 0) { if (errno == ENOTTY) { int VAR_5 = (VAR_2 & NBD_FLAG_READ_ONLY) != 0; TRACE("Setting readonly attribute"); if (ioctl(VAR_0, BLKROSET, (unsigned long) &VAR_5) < 0) { int VAR_6 = errno; LOG("Failed setting read-only attribute"); return -VAR_6; } } else { int VAR_6 = errno; LOG("Failed setting VAR_2"); return -VAR_6; } } TRACE("Negotiation ended"); return 0; }