static void *file_ram_alloc(RAMBlock *block,

                            ram_addr_t memory,

                            const char *path,

                            Error **errp)

{

    bool unlink_on_error = false;

    char *filename;

    char *sanitized_name;

    char *c;

    void *area = MAP_FAILED;

    int fd = -1;

    int64_t file_size;



    if (kvm_enabled() && !kvm_has_sync_mmu()) {

        error_setg(errp,

                   "host lacks kvm mmu notifiers, -mem-path unsupported");

        return NULL;

    }



    for (;;) {

        fd = open(path, O_RDWR);

        if (fd >= 0) {

            /* @path names an existing file, use it */

            break;

        }

        if (errno == ENOENT) {

            /* @path names a file that doesn't exist, create it */

            fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644);

            if (fd >= 0) {

                unlink_on_error = true;

                break;

            }

        } else if (errno == EISDIR) {

            /* @path names a directory, create a file there */

            /* Make name safe to use with mkstemp by replacing '/' with '_'. */

            sanitized_name = g_strdup(memory_region_name(block->mr));

            for (c = sanitized_name; *c != '\0'; c++) {

                if (*c == '/') {

                    *c = '_';

                }

            }



            filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,

                                       sanitized_name);

            g_free(sanitized_name);



            fd = mkstemp(filename);

            if (fd >= 0) {

                unlink(filename);

                g_free(filename);

                break;

            }

            g_free(filename);

        }

        if (errno != EEXIST && errno != EINTR) {

            error_setg_errno(errp, errno,

                             "can't open backing store %s for guest RAM",

                             path);

            goto error;

        }

        /*

         * Try again on EINTR and EEXIST.  The latter happens when

         * something else creates the file between our two open().

         */

    }



    block->page_size = qemu_fd_getpagesize(fd);

    block->mr->align = block->page_size;

#if defined(__s390x__)

    if (kvm_enabled()) {

        block->mr->align = MAX(block->mr->align, QEMU_VMALLOC_ALIGN);

    }

#endif



    file_size = get_file_size(fd);



    if (memory < block->page_size) {

        error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "

                   "or larger than page size 0x%zx",

                   memory, block->page_size);

        goto error;

    }



    if (file_size > 0 && file_size < memory) {

        error_setg(errp, "backing store %s size 0x%" PRIx64

                   " does not match 'size' option 0x" RAM_ADDR_FMT,

                   path, file_size, memory);

        goto error;

    }



    memory = ROUND_UP(memory, block->page_size);



    /*

     * ftruncate is not supported by hugetlbfs in older

     * hosts, so don't bother bailing out on errors.

     * If anything goes wrong with it under other filesystems,

     * mmap will fail.

     *

     * Do not truncate the non-empty backend file to avoid corrupting

     * the existing data in the file. Disabling shrinking is not

     * enough. For example, the current vNVDIMM implementation stores

     * the guest NVDIMM labels at the end of the backend file. If the

     * backend file is later extended, QEMU will not be able to find

     * those labels. Therefore, extending the non-empty backend file

     * is disabled as well.

     */

    if (!file_size && ftruncate(fd, memory)) {

        perror("ftruncate");

    }



    area = qemu_ram_mmap(fd, memory, block->mr->align,

                         block->flags & RAM_SHARED);

    if (area == MAP_FAILED) {

        error_setg_errno(errp, errno,

                         "unable to map backing store for guest RAM");

        goto error;

    }



    if (mem_prealloc) {

        os_mem_prealloc(fd, area, memory, errp);

        if (errp && *errp) {

            goto error;

        }

    }



    block->fd = fd;

    return area;



error:

    if (area != MAP_FAILED) {

        qemu_ram_munmap(area, memory);

    }

    if (unlink_on_error) {

        unlink(path);

    }

    if (fd != -1) {

        close(fd);

    }

    return NULL;

}

static void *file_ram_alloc(RAMBlock *block,

                            ram_addr_t memory,

                            const char *path,

                            Error **errp)

{

    bool unlink_on_error = false;

    char *filename;

    char *sanitized_name;

    char *c;

    void *area = MAP_FAILED;

    int fd = -1;

    int64_t file_size;



    if (kvm_enabled() && !kvm_has_sync_mmu()) {

        error_setg(errp,

                   "host lacks kvm mmu notifiers, -mem-path unsupported");

        return NULL;

    }



    for (;;) {

        fd = open(path, O_RDWR);

        if (fd >= 0) {

            

            break;

        }

        if (errno == ENOENT) {

            

            fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644);

            if (fd >= 0) {

                unlink_on_error = true;

                break;

            }

        } else if (errno == EISDIR) {

            

            

            sanitized_name = g_strdup(memory_region_name(block->mr));

            for (c = sanitized_name; *c != '\0'; c++) {

                if (*c == '/') {

                    *c = '_';

                }

            }



            filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,

                                       sanitized_name);

            g_free(sanitized_name);



            fd = mkstemp(filename);

            if (fd >= 0) {

                unlink(filename);

                g_free(filename);

                break;

            }

            g_free(filename);

        }

        if (errno != EEXIST && errno != EINTR) {

            error_setg_errno(errp, errno,

                             "can't open backing store %s for guest RAM",

                             path);

            goto error;

        }

        







    }



    block->page_size = qemu_fd_getpagesize(fd);

    block->mr->align = block->page_size;

#if defined(__s390x__)

    if (kvm_enabled()) {

        block->mr->align = MAX(block->mr->align, QEMU_VMALLOC_ALIGN);

    }

#endif



    file_size = get_file_size(fd);



    if (memory < block->page_size) {

        error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "

                   "or larger than page size 0x%zx",

                   memory, block->page_size);

        goto error;

    }



    if (file_size > 0 && file_size < memory) {

        error_setg(errp, "backing store %s size 0x%" PRIx64

                   " does not match 'size' option 0x" RAM_ADDR_FMT,

                   path, file_size, memory);

        goto error;

    }



    memory = ROUND_UP(memory, block->page_size);



    



























    if (!file_size && ftruncate(fd, memory)) {

        perror("ftruncate");

    }



    area = qemu_ram_mmap(fd, memory, block->mr->align,

                         block->flags & RAM_SHARED);

    if (area == MAP_FAILED) {

        error_setg_errno(errp, errno,

                         "unable to map backing store for guest RAM");

        goto error;

    }



    if (mem_prealloc) {

        os_mem_prealloc(fd, area, memory, errp);

        if (errp && *errp) {

            goto error;

        }

    }



    block->fd = fd;

    return area;



error:

    if (area != MAP_FAILED) {

        qemu_ram_munmap(area, memory);

    }

    if (unlink_on_error) {

        unlink(path);

    }

    if (fd != -1) {

        close(fd);

    }

    return NULL;

}

        os_mem_prealloc(fd, area, memory, errp);
static void *FUNC_0(RAMBlock *VAR_0,

                            ram_addr_t VAR_1,

                            const char *VAR_2,

                            Error **VAR_3)

{

    bool unlink_on_error = false;

    char *VAR_4;

    char *VAR_5;

    char *VAR_6;

    void *VAR_7 = MAP_FAILED;

    int VAR_8 = -1;

    int64_t file_size;



    if (kvm_enabled() && !kvm_has_sync_mmu()) {

        error_setg(VAR_3,

                   "host lacks kvm mmu notifiers, -mem-VAR_2 unsupported");

        return NULL;

    }



    for (;;) {

        VAR_8 = open(VAR_2, O_RDWR);

        if (VAR_8 >= 0) {

            

            break;

        }

        if (errno == ENOENT) {

            

            VAR_8 = open(VAR_2, O_RDWR | O_CREAT | O_EXCL, 0644);

            if (VAR_8 >= 0) {

                unlink_on_error = true;

                break;

            }

        } else if (errno == EISDIR) {

            

            

            VAR_5 = g_strdup(memory_region_name(VAR_0->mr));

            for (VAR_6 = VAR_5; *VAR_6 != '\0'; VAR_6++) {

                if (*VAR_6 == '/') {

                    *VAR_6 = '_';

                }

            }



            VAR_4 = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", VAR_2,

                                       VAR_5);

            g_free(VAR_5);



            VAR_8 = mkstemp(VAR_4);

            if (VAR_8 >= 0) {

                unlink(VAR_4);

                g_free(VAR_4);

                break;

            }

            g_free(VAR_4);

        }

        if (errno != EEXIST && errno != EINTR) {

            error_setg_errno(VAR_3, errno,

                             "can't open backing store %s for guest RAM",

                             VAR_2);

            goto error;

        }

        







    }



    VAR_0->page_size = qemu_fd_getpagesize(VAR_8);

    VAR_0->mr->align = VAR_0->page_size;

#if defined(__s390x__)

    if (kvm_enabled()) {

        VAR_0->mr->align = MAX(VAR_0->mr->align, QEMU_VMALLOC_ALIGN);

    }

#endif



    file_size = get_file_size(VAR_8);



    if (VAR_1 < VAR_0->page_size) {

        error_setg(VAR_3, "VAR_1 size 0x" RAM_ADDR_FMT " must be equal to "

                   "or larger than page size 0x%zx",

                   VAR_1, VAR_0->page_size);

        goto error;

    }



    if (file_size > 0 && file_size < VAR_1) {

        error_setg(VAR_3, "backing store %s size 0x%" PRIx64

                   " does not match 'size' option 0x" RAM_ADDR_FMT,

                   VAR_2, file_size, VAR_1);

        goto error;

    }



    VAR_1 = ROUND_UP(VAR_1, VAR_0->page_size);



    



























    if (!file_size && ftruncate(VAR_8, VAR_1)) {

        perror("ftruncate");

    }



    VAR_7 = qemu_ram_mmap(VAR_8, VAR_1, VAR_0->mr->align,

                         VAR_0->flags & RAM_SHARED);

    if (VAR_7 == MAP_FAILED) {

        error_setg_errno(VAR_3, errno,

                         "unable to map backing store for guest RAM");

        goto error;

    }



    if (mem_prealloc) {

        os_mem_prealloc(VAR_8, VAR_7, VAR_1, VAR_3);

        if (VAR_3 && *VAR_3) {

            goto error;

        }

    }



    VAR_0->VAR_8 = VAR_8;

    return VAR_7;



error:

    if (VAR_7 != MAP_FAILED) {

        qemu_ram_munmap(VAR_7, VAR_1);

    }

    if (unlink_on_error) {

        unlink(VAR_2);

    }

    if (VAR_8 != -1) {

        close(VAR_8);

    }

    return NULL;

}

static void *FUNC_0(RAMBlock *VAR_0,
ram_addr_t VAR_1,
const char *VAR_2,
Error **VAR_3)
{ bool unlink_on_error = false; char *VAR_4; char *VAR_5; char *VAR_6; void *VAR_7 = MAP_FAILED; int VAR_8 = -1; int64_t file_size; if (kvm_enabled() && !kvm_has_sync_mmu()) { error_setg(VAR_3,
"host lacks kvm mmu notifiers, -mem-VAR_2 unsupported"); return NULL; } for (;;) { VAR_8 = open(VAR_2, O_RDWR); if (VAR_8 >= 0) { break; } if (errno == ENOENT) { VAR_8 = open(VAR_2, O_RDWR | O_CREAT | O_EXCL, 0644); if (VAR_8 >= 0) { unlink_on_error = true; break; } } else if (errno == EISDIR) { VAR_5 = g_strdup(memory_region_name(VAR_0->mr)); for (VAR_6 = VAR_5; *VAR_6 != '\0'; VAR_6++) { if (*VAR_6 == '/') { *VAR_6 = '_'; } } VAR_4 = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", VAR_2,
VAR_5); g_free(VAR_5); VAR_8 = mkstemp(VAR_4); if (VAR_8 >= 0) { unlink(VAR_4); g_free(VAR_4); break; } g_free(VAR_4); } if (errno != EEXIST && errno != EINTR) { error_setg_errno(VAR_3, errno,
"can't open backing store %s for guest RAM",
VAR_2); goto error; } } VAR_0->page_size = qemu_fd_getpagesize(VAR_8); VAR_0->mr->align = VAR_0->page_size; #if defined(__s390x__)
if (kvm_enabled()) { VAR_0->mr->align = MAX(VAR_0->mr->align, QEMU_VMALLOC_ALIGN); } #endif
file_size = get_file_size(VAR_8); if (VAR_1 < VAR_0->page_size) { error_setg(VAR_3, "VAR_1 size 0x" RAM_ADDR_FMT " must be equal to "
"or larger than page size 0x%zx",
VAR_1, VAR_0->page_size); goto error; } if (file_size > 0 && file_size < VAR_1) { error_setg(VAR_3, "backing store %s size 0x%" PRIx64
" does not match 'size' option 0x" RAM_ADDR_FMT,
VAR_2, file_size, VAR_1); goto error; } VAR_1 = ROUND_UP(VAR_1, VAR_0->page_size); if (!file_size && ftruncate(VAR_8, VAR_1)) { perror("ftruncate"); } VAR_7 = qemu_ram_mmap(VAR_8, VAR_1, VAR_0->mr->align,
VAR_0->flags & RAM_SHARED); if (VAR_7 == MAP_FAILED) { error_setg_errno(VAR_3, errno,
"unable to map backing store for guest RAM"); goto error; } if (mem_prealloc) { os_mem_prealloc(VAR_8, VAR_7, VAR_1, VAR_3); if (VAR_3 && *VAR_3) { goto error; } } VAR_0->VAR_8 = VAR_8; return VAR_7; error:
if (VAR_7 != MAP_FAILED) { qemu_ram_munmap(VAR_7, VAR_1); } if (unlink_on_error) { unlink(VAR_2); } if (VAR_8 != -1) { close(VAR_8); } return NULL; }