From nobody Mon Nov 25 02:31:56 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=1718293518; cv=none; d=zohomail.com; s=zohoarc; b=a6E9w5Xezb/oR3ru8AY+Ox7X/3QbrtqlmKizxH+SVWrEB6jamSJuICi2zWv3dDtTXPqAm0XTDwqjS+cJR+z51XLkzUJS97nnoftltNlZ8dM3zumoOooqepYpw+6gv2ocJiKkWqYi1kDKAZryqCWRViY0SVEeYT4pN3znRPeHJuI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1718293518; 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=/fh6OWzA0tCtf32dsesC220yACnTIpr6dXCwZv5/JP0=; b=XSVVyyfo1e4IzCk6+oDaRZSQnZfQcsnhGHX0DtYXcw0k1MtcH6BPDchwKhoQ0lZiKtq1SvRbBSKrM5H7BcT2scirS8zWNjtFhibTyz44iPI0J1OixAdIaBLuvB2vX5dEniYgZMdQqSCNYkRqHzVmFYktc2nhimxqGdoCyQcCPUU= 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 1718293518162719.4340448682638; Thu, 13 Jun 2024 08:45:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sHmcg-0005N4-KO; Thu, 13 Jun 2024 11:44:22 -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 1sHmce-0005ML-CV for qemu-devel@nongnu.org; Thu, 13 Jun 2024 11:44:20 -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 1sHmcb-0003aq-Ls for qemu-devel@nongnu.org; Thu, 13 Jun 2024 11:44:20 -0400 Received: from mx-prod-mc-03.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-411-TSwiTHGFOWuDXm-eQDesXA-1; Thu, 13 Jun 2024 11:44:13 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 06D1B1956051; Thu, 13 Jun 2024 15:44:12 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.52]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 629413000219; Thu, 13 Jun 2024 15:44:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718293456; 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=/fh6OWzA0tCtf32dsesC220yACnTIpr6dXCwZv5/JP0=; b=P1AmfREsfWcHaWjWcVKE7DcXOn/J1XUoswRESbuE9UBl/WM2YLm4adQFs5iFGTOlf+bjPh uaZLxI1H0l6BdXyMF2b97i7jCMobHSroSkgReD/v/UET/9frvwqXDA0EMkapCzIi0Z77hP WJnEnkunP6pWGolRbDiydOka3bGES3g= X-MC-Unique: TSwiTHGFOWuDXm-eQDesXA-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Konstantin Kostiuk , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Thomas Huth , Michael Roth , Paolo Bonzini Subject: [PATCH v2 06/22] qga: move linux memory block command impls to commands-linux.c Date: Thu, 13 Jun 2024 16:43:50 +0100 Message-ID: <20240613154406.1365469-1-berrange@redhat.com> In-Reply-To: <20240613150127.1361931-1-berrange@redhat.com> References: <20240613150127.1361931-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.4.1 on 10.30.177.4 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: 11 X-Spam_score: 1.1 X-Spam_bar: + X-Spam_report: (1.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.145, 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, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: 1718293518881100002 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__". Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Konstantin Kostiuk Reviewed-by: Philippe Mathieu-Daud=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