From nobody Sun Nov 24 19:40:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1720790790; cv=none; d=zohomail.com; s=zohoarc; b=UMT0kU04SEVgWLCXiLqaRHm+6Y8OsRpkb+4eKvk5w3BBUjdm8P8rUtC+hW8s/o1UpMPHsZpGM/+lgPBLvwZ2JZ9KupSG1Pyz1EyJ6En3D8F/NZt1F9FZUeDtQq/Pm5/pstRUrctFgCkOwlocsLZlpMSTKvr0sgkHTq850ZT1loo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720790790; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4pLNMZgzEcZH+ABN0fSwaZBCtEDFrs9igcxkhnnQzco=; b=fEZVjfSwEKEBLWb43puUEwwS4gxHsswYMw+NBoYZ3OkiRQBfc28fHunKAoO5nIrhv1DYBdYLawUdj1cHTG1GdcFMuyhe8NPswJKNHfHkwF/P9l+LndHUiihEfDRWwmOMA7c6MH6ejkqtArjp2vLcngoUPbcghLQ0c6++JX+JVgQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17207907908971006.1822575398943; Fri, 12 Jul 2024 06:26:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sSGHk-0004gw-SD; Fri, 12 Jul 2024 09:26:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sSGHG-0002ZD-Op for qemu-devel@nongnu.org; Fri, 12 Jul 2024 09:25:34 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sSGHE-0003q3-0L for qemu-devel@nongnu.org; Fri, 12 Jul 2024 09:25:34 -0400 Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-223-hm4I8hclNr656ERGDDGGDw-1; Fri, 12 Jul 2024 09:25:27 -0400 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BE3461955F69; Fri, 12 Jul 2024 13:25:26 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.56]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C72AB1955F40; Fri, 12 Jul 2024 13:25:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720790731; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4pLNMZgzEcZH+ABN0fSwaZBCtEDFrs9igcxkhnnQzco=; b=JEGJrELHK/dxhHQj1iynbxtZqP8G//ecI0jcThiuDSn7NhNA98DkHC/q1ueBJvqomVFm57 3ATHyXS5+A++N6ZF9KtskLOKPYhLzRD3zuM5IsO33JfTg6jD8eDQVOZweCU77LTnpsKMms eQhi2J83a2FcNp8TxQUmPk66RcwdgyA= X-MC-Unique: hm4I8hclNr656ERGDDGGDw-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Thomas Huth , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Michael Roth , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Konstantin Kostiuk Subject: [PATCH v3 06/22] qga: move linux memory block command impls to commands-linux.c Date: Fri, 12 Jul 2024 14:24:43 +0100 Message-ID: <20240712132459.3974109-7-berrange@redhat.com> In-Reply-To: <20240712132459.3974109-1-berrange@redhat.com> References: <20240712132459.3974109-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.138, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1720790791926116600 The qmp_guest_{set,get}_{memory_blocks,block_info} command impls in commands-posix.c are surrounded by '#ifdef __linux__' so should instead live in commands-linux.c This also removes a "#ifdef CONFIG_LINUX" that was nested inside a "#ifdef __linux__". Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Konstantin Kostiuk Signed-off-by: Daniel P. Berrang=C3=A9 --- qga/commands-linux.c | 308 ++++++++++++++++++++++++++++++++++++++++++ qga/commands-posix.c | 311 +------------------------------------------ 2 files changed, 309 insertions(+), 310 deletions(-) diff --git a/qga/commands-linux.c b/qga/commands-linux.c index c0e8bd4062..73b13fbaf6 100644 --- a/qga/commands-linux.c +++ b/qga/commands-linux.c @@ -1595,6 +1595,314 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorLi= st *vcpus, Error **errp) return processed; } =20 + +static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf, + int size, Error **errp) +{ + int fd; + int res; + + errno =3D 0; + fd =3D openat(dirfd, pathname, O_RDONLY); + if (fd =3D=3D -1) { + error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname); + return; + } + + res =3D pread(fd, buf, size, 0); + if (res =3D=3D -1) { + error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathname); + } else if (res =3D=3D 0) { + error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", pathna= me); + } + close(fd); +} + +static void ga_write_sysfs_file(int dirfd, const char *pathname, + const char *buf, int size, Error **errp) +{ + int fd; + + errno =3D 0; + fd =3D openat(dirfd, pathname, O_WRONLY); + if (fd =3D=3D -1) { + error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname); + return; + } + + if (pwrite(fd, buf, size, 0) =3D=3D -1) { + error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname= ); + } + + close(fd); +} + +/* Transfer online/offline status between @mem_blk and the guest system. + * + * On input either @errp or *@errp must be NULL. + * + * In system-to-@mem_blk direction, the following @mem_blk fields are acce= ssed: + * - R: mem_blk->phys_index + * - W: mem_blk->online + * - W: mem_blk->can_offline + * + * In @mem_blk-to-system direction, the following @mem_blk fields are acce= ssed: + * - R: mem_blk->phys_index + * - R: mem_blk->online + *- R: mem_blk->can_offline + * Written members remain unmodified on error. + */ +static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memb= lk, + GuestMemoryBlockResponse *result, + Error **errp) +{ + char *dirpath; + int dirfd; + char *status; + Error *local_err =3D NULL; + + if (!sys2memblk) { + DIR *dp; + + if (!result) { + error_setg(errp, "Internal error, 'result' should not be NULL"= ); + return; + } + errno =3D 0; + dp =3D opendir("/sys/devices/system/memory/"); + /* if there is no 'memory' directory in sysfs, + * we think this VM does not support online/offline memory block, + * any other solution? + */ + if (!dp) { + if (errno =3D=3D ENOENT) { + result->response =3D + GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORT= ED; + } + goto out1; + } + closedir(dp); + } + + dirpath =3D g_strdup_printf("/sys/devices/system/memory/memory%" PRId6= 4 "/", + mem_blk->phys_index); + dirfd =3D open(dirpath, O_RDONLY | O_DIRECTORY); + if (dirfd =3D=3D -1) { + if (sys2memblk) { + error_setg_errno(errp, errno, "open(\"%s\")", dirpath); + } else { + if (errno =3D=3D ENOENT) { + result->response =3D GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_= FOUND; + } else { + result->response =3D + GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; + } + } + g_free(dirpath); + goto out1; + } + g_free(dirpath); + + status =3D g_malloc0(10); + ga_read_sysfs_file(dirfd, "state", status, 10, &local_err); + if (local_err) { + /* treat with sysfs file that not exist in old kernel */ + if (errno =3D=3D ENOENT) { + error_free(local_err); + if (sys2memblk) { + mem_blk->online =3D true; + mem_blk->can_offline =3D false; + } else if (!mem_blk->online) { + result->response =3D + GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORT= ED; + } + } else { + if (sys2memblk) { + error_propagate(errp, local_err); + } else { + error_free(local_err); + result->response =3D + GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; + } + } + goto out2; + } + + if (sys2memblk) { + char removable =3D '0'; + + mem_blk->online =3D (strncmp(status, "online", 6) =3D=3D 0); + + ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err); + if (local_err) { + /* if no 'removable' file, it doesn't support offline mem blk = */ + if (errno =3D=3D ENOENT) { + error_free(local_err); + mem_blk->can_offline =3D false; + } else { + error_propagate(errp, local_err); + } + } else { + mem_blk->can_offline =3D (removable !=3D '0'); + } + } else { + if (mem_blk->online !=3D (strncmp(status, "online", 6) =3D=3D 0)) { + const char *new_state =3D mem_blk->online ? "online" : "offlin= e"; + + ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_stat= e), + &local_err); + if (local_err) { + error_free(local_err); + result->response =3D + GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; + goto out2; + } + + result->response =3D GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS; + result->has_error_code =3D false; + } /* otherwise pretend successful re-(on|off)-lining */ + } + g_free(status); + close(dirfd); + return; + +out2: + g_free(status); + close(dirfd); +out1: + if (!sys2memblk) { + result->has_error_code =3D true; + result->error_code =3D errno; + } +} + +GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) +{ + GuestMemoryBlockList *head, **tail; + Error *local_err =3D NULL; + struct dirent *de; + DIR *dp; + + head =3D NULL; + tail =3D &head; + + dp =3D opendir("/sys/devices/system/memory/"); + if (!dp) { + /* it's ok if this happens to be a system that doesn't expose + * memory blocks via sysfs, but otherwise we should report + * an error + */ + if (errno !=3D ENOENT) { + error_setg_errno(errp, errno, "Can't open directory" + "\"/sys/devices/system/memory/\""); + } + return NULL; + } + + /* Note: the phys_index of memory block may be discontinuous, + * this is because a memblk is the unit of the Sparse Memory design, w= hich + * allows discontinuous memory ranges (ex. NUMA), so here we should + * traverse the memory block directory. + */ + while ((de =3D readdir(dp)) !=3D NULL) { + GuestMemoryBlock *mem_blk; + + if ((strncmp(de->d_name, "memory", 6) !=3D 0) || + !(de->d_type & DT_DIR)) { + continue; + } + + mem_blk =3D g_malloc0(sizeof *mem_blk); + /* The d_name is "memoryXXX", phys_index is block id, same as XXX= */ + mem_blk->phys_index =3D strtoul(&de->d_name[6], NULL, 10); + mem_blk->has_can_offline =3D true; /* lolspeak ftw */ + transfer_memory_block(mem_blk, true, NULL, &local_err); + if (local_err) { + break; + } + + QAPI_LIST_APPEND(tail, mem_blk); + } + + closedir(dp); + if (local_err =3D=3D NULL) { + /* there's no guest with zero memory blocks */ + if (head =3D=3D NULL) { + error_setg(errp, "guest reported zero memory blocks!"); + } + return head; + } + + qapi_free_GuestMemoryBlockList(head); + error_propagate(errp, local_err); + return NULL; +} + +GuestMemoryBlockResponseList * +qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) +{ + GuestMemoryBlockResponseList *head, **tail; + Error *local_err =3D NULL; + + head =3D NULL; + tail =3D &head; + + while (mem_blks !=3D NULL) { + GuestMemoryBlockResponse *result; + GuestMemoryBlock *current_mem_blk =3D mem_blks->value; + + result =3D g_malloc0(sizeof(*result)); + result->phys_index =3D current_mem_blk->phys_index; + transfer_memory_block(current_mem_blk, false, result, &local_err); + if (local_err) { /* should never happen */ + goto err; + } + + QAPI_LIST_APPEND(tail, result); + mem_blks =3D mem_blks->next; + } + + return head; +err: + qapi_free_GuestMemoryBlockResponseList(head); + error_propagate(errp, local_err); + return NULL; +} + +GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) +{ + Error *local_err =3D NULL; + char *dirpath; + int dirfd; + char *buf; + GuestMemoryBlockInfo *info; + + dirpath =3D g_strdup_printf("/sys/devices/system/memory/"); + dirfd =3D open(dirpath, O_RDONLY | O_DIRECTORY); + if (dirfd =3D=3D -1) { + error_setg_errno(errp, errno, "open(\"%s\")", dirpath); + g_free(dirpath); + return NULL; + } + g_free(dirpath); + + buf =3D g_malloc0(20); + ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err); + close(dirfd); + if (local_err) { + g_free(buf); + error_propagate(errp, local_err); + return NULL; + } + + info =3D g_new0(GuestMemoryBlockInfo, 1); + info->size =3D strtol(buf, NULL, 16); /* the unit is bytes */ + + g_free(buf); + + return info; +} + #define MAX_NAME_LEN 128 static GuestDiskStatsInfoList *guest_get_diskstats(Error **errp) { diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 5da60e65ab..2a3bef7445 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -887,316 +887,7 @@ void qmp_guest_set_user_password(const char *username, } #endif /* __linux__ || __FreeBSD__ */ =20 -#ifdef __linux__ -static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf, - int size, Error **errp) -{ - int fd; - int res; - - errno =3D 0; - fd =3D openat(dirfd, pathname, O_RDONLY); - if (fd =3D=3D -1) { - error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname); - return; - } - - res =3D pread(fd, buf, size, 0); - if (res =3D=3D -1) { - error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathname); - } else if (res =3D=3D 0) { - error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", pathna= me); - } - close(fd); -} - -static void ga_write_sysfs_file(int dirfd, const char *pathname, - const char *buf, int size, Error **errp) -{ - int fd; - - errno =3D 0; - fd =3D openat(dirfd, pathname, O_WRONLY); - if (fd =3D=3D -1) { - error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname); - return; - } - - if (pwrite(fd, buf, size, 0) =3D=3D -1) { - error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname= ); - } - - close(fd); -} - -/* Transfer online/offline status between @mem_blk and the guest system. - * - * On input either @errp or *@errp must be NULL. - * - * In system-to-@mem_blk direction, the following @mem_blk fields are acce= ssed: - * - R: mem_blk->phys_index - * - W: mem_blk->online - * - W: mem_blk->can_offline - * - * In @mem_blk-to-system direction, the following @mem_blk fields are acce= ssed: - * - R: mem_blk->phys_index - * - R: mem_blk->online - *- R: mem_blk->can_offline - * Written members remain unmodified on error. - */ -static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memb= lk, - GuestMemoryBlockResponse *result, - Error **errp) -{ - char *dirpath; - int dirfd; - char *status; - Error *local_err =3D NULL; - - if (!sys2memblk) { - DIR *dp; - - if (!result) { - error_setg(errp, "Internal error, 'result' should not be NULL"= ); - return; - } - errno =3D 0; - dp =3D opendir("/sys/devices/system/memory/"); - /* if there is no 'memory' directory in sysfs, - * we think this VM does not support online/offline memory block, - * any other solution? - */ - if (!dp) { - if (errno =3D=3D ENOENT) { - result->response =3D - GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORT= ED; - } - goto out1; - } - closedir(dp); - } - - dirpath =3D g_strdup_printf("/sys/devices/system/memory/memory%" PRId6= 4 "/", - mem_blk->phys_index); - dirfd =3D open(dirpath, O_RDONLY | O_DIRECTORY); - if (dirfd =3D=3D -1) { - if (sys2memblk) { - error_setg_errno(errp, errno, "open(\"%s\")", dirpath); - } else { - if (errno =3D=3D ENOENT) { - result->response =3D GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_= FOUND; - } else { - result->response =3D - GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; - } - } - g_free(dirpath); - goto out1; - } - g_free(dirpath); - - status =3D g_malloc0(10); - ga_read_sysfs_file(dirfd, "state", status, 10, &local_err); - if (local_err) { - /* treat with sysfs file that not exist in old kernel */ - if (errno =3D=3D ENOENT) { - error_free(local_err); - if (sys2memblk) { - mem_blk->online =3D true; - mem_blk->can_offline =3D false; - } else if (!mem_blk->online) { - result->response =3D - GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORT= ED; - } - } else { - if (sys2memblk) { - error_propagate(errp, local_err); - } else { - error_free(local_err); - result->response =3D - GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; - } - } - goto out2; - } - - if (sys2memblk) { - char removable =3D '0'; - - mem_blk->online =3D (strncmp(status, "online", 6) =3D=3D 0); - - ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err); - if (local_err) { - /* if no 'removable' file, it doesn't support offline mem blk = */ - if (errno =3D=3D ENOENT) { - error_free(local_err); - mem_blk->can_offline =3D false; - } else { - error_propagate(errp, local_err); - } - } else { - mem_blk->can_offline =3D (removable !=3D '0'); - } - } else { - if (mem_blk->online !=3D (strncmp(status, "online", 6) =3D=3D 0)) { - const char *new_state =3D mem_blk->online ? "online" : "offlin= e"; - - ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_stat= e), - &local_err); - if (local_err) { - error_free(local_err); - result->response =3D - GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED; - goto out2; - } - - result->response =3D GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS; - result->has_error_code =3D false; - } /* otherwise pretend successful re-(on|off)-lining */ - } - g_free(status); - close(dirfd); - return; - -out2: - g_free(status); - close(dirfd); -out1: - if (!sys2memblk) { - result->has_error_code =3D true; - result->error_code =3D errno; - } -} - -GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) -{ - GuestMemoryBlockList *head, **tail; - Error *local_err =3D NULL; - struct dirent *de; - DIR *dp; - - head =3D NULL; - tail =3D &head; - - dp =3D opendir("/sys/devices/system/memory/"); - if (!dp) { - /* it's ok if this happens to be a system that doesn't expose - * memory blocks via sysfs, but otherwise we should report - * an error - */ - if (errno !=3D ENOENT) { - error_setg_errno(errp, errno, "Can't open directory" - "\"/sys/devices/system/memory/\""); - } - return NULL; - } - - /* Note: the phys_index of memory block may be discontinuous, - * this is because a memblk is the unit of the Sparse Memory design, w= hich - * allows discontinuous memory ranges (ex. NUMA), so here we should - * traverse the memory block directory. - */ - while ((de =3D readdir(dp)) !=3D NULL) { - GuestMemoryBlock *mem_blk; - - if ((strncmp(de->d_name, "memory", 6) !=3D 0) || - !(de->d_type & DT_DIR)) { - continue; - } - - mem_blk =3D g_malloc0(sizeof *mem_blk); - /* The d_name is "memoryXXX", phys_index is block id, same as XXX= */ - mem_blk->phys_index =3D strtoul(&de->d_name[6], NULL, 10); - mem_blk->has_can_offline =3D true; /* lolspeak ftw */ - transfer_memory_block(mem_blk, true, NULL, &local_err); - if (local_err) { - break; - } - - QAPI_LIST_APPEND(tail, mem_blk); - } - - closedir(dp); - if (local_err =3D=3D NULL) { - /* there's no guest with zero memory blocks */ - if (head =3D=3D NULL) { - error_setg(errp, "guest reported zero memory blocks!"); - } - return head; - } - - qapi_free_GuestMemoryBlockList(head); - error_propagate(errp, local_err); - return NULL; -} - -GuestMemoryBlockResponseList * -qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp) -{ - GuestMemoryBlockResponseList *head, **tail; - Error *local_err =3D NULL; - - head =3D NULL; - tail =3D &head; - - while (mem_blks !=3D NULL) { - GuestMemoryBlockResponse *result; - GuestMemoryBlock *current_mem_blk =3D mem_blks->value; - - result =3D g_malloc0(sizeof(*result)); - result->phys_index =3D current_mem_blk->phys_index; - transfer_memory_block(current_mem_blk, false, result, &local_err); - if (local_err) { /* should never happen */ - goto err; - } - - QAPI_LIST_APPEND(tail, result); - mem_blks =3D mem_blks->next; - } - - return head; -err: - qapi_free_GuestMemoryBlockResponseList(head); - error_propagate(errp, local_err); - return NULL; -} - -GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp) -{ - Error *local_err =3D NULL; - char *dirpath; - int dirfd; - char *buf; - GuestMemoryBlockInfo *info; - - dirpath =3D g_strdup_printf("/sys/devices/system/memory/"); - dirfd =3D open(dirpath, O_RDONLY | O_DIRECTORY); - if (dirfd =3D=3D -1) { - error_setg_errno(errp, errno, "open(\"%s\")", dirpath); - g_free(dirpath); - return NULL; - } - g_free(dirpath); - - buf =3D g_malloc0(20); - ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err); - close(dirfd); - if (local_err) { - g_free(buf); - error_propagate(errp, local_err); - return NULL; - } - - info =3D g_new0(GuestMemoryBlockInfo, 1); - info->size =3D strtol(buf, NULL, 16); /* the unit is bytes */ - - g_free(buf); - - return info; -} - - -#else /* defined(__linux__) */ +#ifndef __linux__ =20 void qmp_guest_suspend_disk(Error **errp) { --=20 2.45.1