static int raw_open_common(BlockDriverState *bs, const char *filename,

                           int bdrv_flags, int open_flags)

{

    BDRVRawState *s = bs->opaque;

    int fd, ret;



    ret = raw_normalize_devicepath(&filename);

    if (ret != 0) {

        return ret;

    }



    s->open_flags = open_flags | O_BINARY;

    s->open_flags &= ~O_ACCMODE;

    if (bdrv_flags & BDRV_O_RDWR) {

        s->open_flags |= O_RDWR;

    } else {

        s->open_flags |= O_RDONLY;

    }



    /* Use O_DSYNC for write-through caching, no flags for write-back caching,

     * and O_DIRECT for no caching. */

    if ((bdrv_flags & BDRV_O_NOCACHE))

        s->open_flags |= O_DIRECT;

    if (!(bdrv_flags & BDRV_O_CACHE_WB))

        s->open_flags |= O_DSYNC;



    s->fd = -1;

    fd = qemu_open(filename, s->open_flags, 0644);

    if (fd < 0) {

        ret = -errno;

        if (ret == -EROFS)

            ret = -EACCES;

        return ret;

    }

    s->fd = fd;

    s->aligned_buf = NULL;



    if ((bdrv_flags & BDRV_O_NOCACHE)) {

        /*

         * Allocate a buffer for read/modify/write cycles.  Chose the size

         * pessimistically as we don't know the block size yet.

         */

        s->aligned_buf_size = 32 * MAX_BLOCKSIZE;

        s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size);

        if (s->aligned_buf == NULL) {

            goto out_close;

        }

    }



    /* We're falling back to POSIX AIO in some cases so init always */

    if (paio_init() < 0) {

        goto out_free_buf;

    }



#ifdef CONFIG_LINUX_AIO

    /*

     * Currently Linux do AIO only for files opened with O_DIRECT

     * specified so check NOCACHE flag too

     */

    if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==

                      (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {



        s->aio_ctx = laio_init();

        if (!s->aio_ctx) {

            goto out_free_buf;

        }

        s->use_aio = 1;

    } else

#endif

    {

#ifdef CONFIG_LINUX_AIO

        s->use_aio = 0;

#endif

    }



#ifdef CONFIG_XFS

    if (platform_test_xfs_fd(s->fd)) {

        s->is_xfs = 1;

    }

#endif



    return 0;



out_free_buf:

    qemu_vfree(s->aligned_buf);

out_close:

    qemu_close(fd);

    return -errno;

}

static int raw_open_common(BlockDriverState *bs, const char *filename,

                           int bdrv_flags, int open_flags)

{

    BDRVRawState *s = bs->opaque;

    int fd, ret;



    ret = raw_normalize_devicepath(&filename);

    if (ret != 0) {

        return ret;

    }



    s->open_flags = open_flags | O_BINARY;

    s->open_flags &= ~O_ACCMODE;

    if (bdrv_flags & BDRV_O_RDWR) {

        s->open_flags |= O_RDWR;

    } else {

        s->open_flags |= O_RDONLY;

    }



    



    if ((bdrv_flags & BDRV_O_NOCACHE))

        s->open_flags |= O_DIRECT;

    if (!(bdrv_flags & BDRV_O_CACHE_WB))

        s->open_flags |= O_DSYNC;



    s->fd = -1;

    fd = qemu_open(filename, s->open_flags, 0644);

    if (fd < 0) {

        ret = -errno;

        if (ret == -EROFS)

            ret = -EACCES;

        return ret;

    }

    s->fd = fd;

    s->aligned_buf = NULL;



    if ((bdrv_flags & BDRV_O_NOCACHE)) {

        







        s->aligned_buf_size = 32 * MAX_BLOCKSIZE;

        s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size);

        if (s->aligned_buf == NULL) {

            goto out_close;

        }

    }



    

    if (paio_init() < 0) {

        goto out_free_buf;

    }



#ifdef CONFIG_LINUX_AIO

    







    if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==

                      (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {



        s->aio_ctx = laio_init();

        if (!s->aio_ctx) {

            goto out_free_buf;

        }

        s->use_aio = 1;

    } else

#endif

    {

#ifdef CONFIG_LINUX_AIO

        s->use_aio = 0;

#endif

    }



#ifdef CONFIG_XFS

    if (platform_test_xfs_fd(s->fd)) {

        s->is_xfs = 1;

    }

#endif



    return 0;



out_free_buf:

    qemu_vfree(s->aligned_buf);

out_close:

    qemu_close(fd);

    return -errno;

}

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

                           int VAR_2, int VAR_3)

{

    BDRVRawState *s = VAR_0->opaque;

    int VAR_4, VAR_5;



    VAR_5 = raw_normalize_devicepath(&VAR_1);

    if (VAR_5 != 0) {

        return VAR_5;

    }



    s->VAR_3 = VAR_3 | O_BINARY;

    s->VAR_3 &= ~O_ACCMODE;

    if (VAR_2 & BDRV_O_RDWR) {

        s->VAR_3 |= O_RDWR;

    } else {

        s->VAR_3 |= O_RDONLY;

    }



    



    if ((VAR_2 & BDRV_O_NOCACHE))

        s->VAR_3 |= O_DIRECT;

    if (!(VAR_2 & BDRV_O_CACHE_WB))

        s->VAR_3 |= O_DSYNC;



    s->VAR_4 = -1;

    VAR_4 = qemu_open(VAR_1, s->VAR_3, 0644);

    if (VAR_4 < 0) {

        VAR_5 = -errno;

        if (VAR_5 == -EROFS)

            VAR_5 = -EACCES;

        return VAR_5;

    }

    s->VAR_4 = VAR_4;

    s->aligned_buf = NULL;



    if ((VAR_2 & BDRV_O_NOCACHE)) {

        







        s->aligned_buf_size = 32 * MAX_BLOCKSIZE;

        s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size);

        if (s->aligned_buf == NULL) {

            goto out_close;

        }

    }



    

    if (paio_init() < 0) {

        goto out_free_buf;

    }



#ifdef CONFIG_LINUX_AIO

    







    if ((VAR_2 & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==

                      (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {



        s->aio_ctx = laio_init();

        if (!s->aio_ctx) {

            goto out_free_buf;

        }

        s->use_aio = 1;

    } else

#endif

    {

#ifdef CONFIG_LINUX_AIO

        s->use_aio = 0;

#endif

    }



#ifdef CONFIG_XFS

    if (platform_test_xfs_fd(s->VAR_4)) {

        s->is_xfs = 1;

    }

#endif



    return 0;



out_free_buf:

    qemu_vfree(s->aligned_buf);

out_close:

    qemu_close(VAR_4);

    return -errno;

}

static int FUNC_0(BlockDriverState *VAR_0, const char *VAR_1,
int VAR_2, int VAR_3)
{ BDRVRawState *s = VAR_0->opaque; int VAR_4, VAR_5; VAR_5 = raw_normalize_devicepath(&VAR_1); if (VAR_5 != 0) { return VAR_5; } s->VAR_3 = VAR_3 | O_BINARY; s->VAR_3 &= ~O_ACCMODE; if (VAR_2 & BDRV_O_RDWR) { s->VAR_3 |= O_RDWR; } else { s->VAR_3 |= O_RDONLY; } if ((VAR_2 & BDRV_O_NOCACHE))
s->VAR_3 |= O_DIRECT; if (!(VAR_2 & BDRV_O_CACHE_WB))
s->VAR_3 |= O_DSYNC; s->VAR_4 = -1; VAR_4 = qemu_open(VAR_1, s->VAR_3, 0644); if (VAR_4 < 0) { VAR_5 = -errno; if (VAR_5 == -EROFS)
VAR_5 = -EACCES; return VAR_5; } s->VAR_4 = VAR_4; s->aligned_buf = NULL; if ((VAR_2 & BDRV_O_NOCACHE)) { s->aligned_buf_size = 32 * MAX_BLOCKSIZE; s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size); if (s->aligned_buf == NULL) { goto out_close; } } if (paio_init() < 0) { goto out_free_buf; } #ifdef CONFIG_LINUX_AIO
if ((VAR_2 & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
(BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) { s->aio_ctx = laio_init(); if (!s->aio_ctx) { goto out_free_buf; } s->use_aio = 1; } else #endif
{ #ifdef CONFIG_LINUX_AIO
s->use_aio = 0; #endif
} #ifdef CONFIG_XFS
if (platform_test_xfs_fd(s->VAR_4)) { s->is_xfs = 1; } #endif
return 0; out_free_buf:
qemu_vfree(s->aligned_buf); out_close:
qemu_close(VAR_4); return -errno; }