xref: /openbmc/qemu/qga/commands-linux.c (revision adbe794a)
1 /*
2  * QEMU Guest Agent Linux-specific command implementations
3  *
4  * Copyright IBM Corp. 2011
5  *
6  * Authors:
7  *  Michael Roth      <mdroth@linux.vnet.ibm.com>
8  *  Michal Privoznik  <mprivozn@redhat.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11  * See the COPYING file in the top-level directory.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qga-qapi-commands.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qerror.h"
19 #include "commands-common.h"
20 #include "cutils.h"
21 #include <mntent.h>
22 #include <sys/ioctl.h>
23 #include <mntent.h>
24 #include <linux/nvme_ioctl.h>
25 #include "block/nvme.h"
26 
27 #ifdef CONFIG_LIBUDEV
28 #include <libudev.h>
29 #endif
30 
31 #include <sys/statvfs.h>
32 
33 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
34 static int dev_major_minor(const char *devpath,
35                            unsigned int *devmajor, unsigned int *devminor)
36 {
37     struct stat st;
38 
39     *devmajor = 0;
40     *devminor = 0;
41 
42     if (stat(devpath, &st) < 0) {
43         slog("failed to stat device file '%s': %s", devpath, strerror(errno));
44         return -1;
45     }
46     if (S_ISDIR(st.st_mode)) {
47         /* It is bind mount */
48         return -2;
49     }
50     if (S_ISBLK(st.st_mode)) {
51         *devmajor = major(st.st_rdev);
52         *devminor = minor(st.st_rdev);
53         return 0;
54     }
55     return -1;
56 }
57 
58 static bool build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
59 {
60     struct mntent *ment;
61     FsMount *mount;
62     char const *mtab = "/proc/self/mounts";
63     FILE *fp;
64     unsigned int devmajor, devminor;
65 
66     fp = setmntent(mtab, "r");
67     if (!fp) {
68         error_setg(errp, "failed to open mtab file: '%s'", mtab);
69         return false;
70     }
71 
72     while ((ment = getmntent(fp))) {
73         /*
74          * An entry which device name doesn't start with a '/' is
75          * either a dummy file system or a network file system.
76          * Add special handling for smbfs and cifs as is done by
77          * coreutils as well.
78          */
79         if ((ment->mnt_fsname[0] != '/') ||
80             (strcmp(ment->mnt_type, "smbfs") == 0) ||
81             (strcmp(ment->mnt_type, "cifs") == 0)) {
82             continue;
83         }
84         if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) == -2) {
85             /* Skip bind mounts */
86             continue;
87         }
88 
89         mount = g_new0(FsMount, 1);
90         mount->dirname = g_strdup(ment->mnt_dir);
91         mount->devtype = g_strdup(ment->mnt_type);
92         mount->devmajor = devmajor;
93         mount->devminor = devminor;
94 
95         QTAILQ_INSERT_TAIL(mounts, mount, next);
96     }
97 
98     endmntent(fp);
99     return true;
100 }
101 
102 static void decode_mntname(char *name, int len)
103 {
104     int i, j = 0;
105     for (i = 0; i <= len; i++) {
106         if (name[i] != '\\') {
107             name[j++] = name[i];
108         } else if (name[i + 1] == '\\') {
109             name[j++] = '\\';
110             i++;
111         } else if (name[i + 1] >= '0' && name[i + 1] <= '3' &&
112                    name[i + 2] >= '0' && name[i + 2] <= '7' &&
113                    name[i + 3] >= '0' && name[i + 3] <= '7') {
114             name[j++] = (name[i + 1] - '0') * 64 +
115                         (name[i + 2] - '0') * 8 +
116                         (name[i + 3] - '0');
117             i += 3;
118         } else {
119             name[j++] = name[i];
120         }
121     }
122 }
123 
124 /*
125  * Walk the mount table and build a list of local file systems
126  */
127 bool build_fs_mount_list(FsMountList *mounts, Error **errp)
128 {
129     FsMount *mount;
130     char const *mountinfo = "/proc/self/mountinfo";
131     FILE *fp;
132     char *line = NULL, *dash;
133     size_t n;
134     char check;
135     unsigned int devmajor, devminor;
136     int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e;
137 
138     fp = fopen(mountinfo, "r");
139     if (!fp) {
140         return build_fs_mount_list_from_mtab(mounts, errp);
141     }
142 
143     while (getline(&line, &n, fp) != -1) {
144         ret = sscanf(line, "%*u %*u %u:%u %*s %n%*s%n%c",
145                      &devmajor, &devminor, &dir_s, &dir_e, &check);
146         if (ret < 3) {
147             continue;
148         }
149         dash = strstr(line + dir_e, " - ");
150         if (!dash) {
151             continue;
152         }
153         ret = sscanf(dash, " - %n%*s%n %n%*s%n%c",
154                      &type_s, &type_e, &dev_s, &dev_e, &check);
155         if (ret < 1) {
156             continue;
157         }
158         line[dir_e] = 0;
159         dash[type_e] = 0;
160         dash[dev_e] = 0;
161         decode_mntname(line + dir_s, dir_e - dir_s);
162         decode_mntname(dash + dev_s, dev_e - dev_s);
163         if (devmajor == 0) {
164             /* btrfs reports major number = 0 */
165             if (strcmp("btrfs", dash + type_s) != 0 ||
166                 dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) {
167                 continue;
168             }
169         }
170 
171         mount = g_new0(FsMount, 1);
172         mount->dirname = g_strdup(line + dir_s);
173         mount->devtype = g_strdup(dash + type_s);
174         mount->devmajor = devmajor;
175         mount->devminor = devminor;
176 
177         QTAILQ_INSERT_TAIL(mounts, mount, next);
178     }
179     free(line);
180 
181     fclose(fp);
182     return true;
183 }
184 #endif /* CONFIG_FSFREEZE || CONFIG_FSTRIM */
185 
186 #ifdef CONFIG_FSFREEZE
187 /*
188  * Walk list of mounted file systems in the guest, and freeze the ones which
189  * are real local file systems.
190  */
191 int64_t qmp_guest_fsfreeze_do_freeze_list(bool has_mountpoints,
192                                           strList *mountpoints,
193                                           FsMountList mounts,
194                                           Error **errp)
195 {
196     struct FsMount *mount;
197     strList *list;
198     int fd, ret, i = 0;
199 
200     QTAILQ_FOREACH_REVERSE(mount, &mounts, next) {
201         /* To issue fsfreeze in the reverse order of mounts, check if the
202          * mount is listed in the list here */
203         if (has_mountpoints) {
204             for (list = mountpoints; list; list = list->next) {
205                 if (strcmp(list->value, mount->dirname) == 0) {
206                     break;
207                 }
208             }
209             if (!list) {
210                 continue;
211             }
212         }
213 
214         fd = qga_open_cloexec(mount->dirname, O_RDONLY, 0);
215         if (fd == -1) {
216             error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
217             return -1;
218         }
219 
220         /* we try to cull filesystems we know won't work in advance, but other
221          * filesystems may not implement fsfreeze for less obvious reasons.
222          * these will report EOPNOTSUPP. we simply ignore these when tallying
223          * the number of frozen filesystems.
224          * if a filesystem is mounted more than once (aka bind mount) a
225          * consecutive attempt to freeze an already frozen filesystem will
226          * return EBUSY.
227          *
228          * any other error means a failure to freeze a filesystem we
229          * expect to be freezable, so return an error in those cases
230          * and return system to thawed state.
231          */
232         ret = ioctl(fd, FIFREEZE);
233         if (ret == -1) {
234             if (errno != EOPNOTSUPP && errno != EBUSY) {
235                 error_setg_errno(errp, errno, "failed to freeze %s",
236                                  mount->dirname);
237                 close(fd);
238                 return -1;
239             }
240         } else {
241             i++;
242         }
243         close(fd);
244     }
245     return i;
246 }
247 
248 int qmp_guest_fsfreeze_do_thaw(Error **errp)
249 {
250     int ret;
251     FsMountList mounts;
252     FsMount *mount;
253     int fd, i = 0, logged;
254     Error *local_err = NULL;
255 
256     QTAILQ_INIT(&mounts);
257     if (!build_fs_mount_list(&mounts, &local_err)) {
258         error_propagate(errp, local_err);
259         return -1;
260     }
261 
262     QTAILQ_FOREACH(mount, &mounts, next) {
263         logged = false;
264         fd = qga_open_cloexec(mount->dirname, O_RDONLY, 0);
265         if (fd == -1) {
266             continue;
267         }
268         /* we have no way of knowing whether a filesystem was actually unfrozen
269          * as a result of a successful call to FITHAW, only that if an error
270          * was returned the filesystem was *not* unfrozen by that particular
271          * call.
272          *
273          * since multiple preceding FIFREEZEs require multiple calls to FITHAW
274          * to unfreeze, continuing issuing FITHAW until an error is returned,
275          * in which case either the filesystem is in an unfreezable state, or,
276          * more likely, it was thawed previously (and remains so afterward).
277          *
278          * also, since the most recent successful call is the one that did
279          * the actual unfreeze, we can use this to provide an accurate count
280          * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
281          * may * be useful for determining whether a filesystem was unfrozen
282          * during the freeze/thaw phase by a process other than qemu-ga.
283          */
284         do {
285             ret = ioctl(fd, FITHAW);
286             if (ret == 0 && !logged) {
287                 i++;
288                 logged = true;
289             }
290         } while (ret == 0);
291         close(fd);
292     }
293 
294     free_fs_mount_list(&mounts);
295 
296     return i;
297 }
298 #endif /* CONFIG_FSFREEZE */
299 
300 #if defined(CONFIG_FSFREEZE)
301 
302 static char *get_pci_driver(char const *syspath, int pathlen, Error **errp)
303 {
304     char *path;
305     char *dpath;
306     char *driver = NULL;
307     char buf[PATH_MAX];
308     ssize_t len;
309 
310     path = g_strndup(syspath, pathlen);
311     dpath = g_strdup_printf("%s/driver", path);
312     len = readlink(dpath, buf, sizeof(buf) - 1);
313     if (len != -1) {
314         buf[len] = 0;
315         driver = g_path_get_basename(buf);
316     }
317     g_free(dpath);
318     g_free(path);
319     return driver;
320 }
321 
322 static int compare_uint(const void *_a, const void *_b)
323 {
324     unsigned int a = *(unsigned int *)_a;
325     unsigned int b = *(unsigned int *)_b;
326 
327     return a < b ? -1 : a > b ? 1 : 0;
328 }
329 
330 /* Walk the specified sysfs and build a sorted list of host or ata numbers */
331 static int build_hosts(char const *syspath, char const *host, bool ata,
332                        unsigned int *hosts, int hosts_max, Error **errp)
333 {
334     char *path;
335     DIR *dir;
336     struct dirent *entry;
337     int i = 0;
338 
339     path = g_strndup(syspath, host - syspath);
340     dir = opendir(path);
341     if (!dir) {
342         error_setg_errno(errp, errno, "opendir(\"%s\")", path);
343         g_free(path);
344         return -1;
345     }
346 
347     while (i < hosts_max) {
348         entry = readdir(dir);
349         if (!entry) {
350             break;
351         }
352         if (ata && sscanf(entry->d_name, "ata%d", hosts + i) == 1) {
353             ++i;
354         } else if (!ata && sscanf(entry->d_name, "host%d", hosts + i) == 1) {
355             ++i;
356         }
357     }
358 
359     qsort(hosts, i, sizeof(hosts[0]), compare_uint);
360 
361     g_free(path);
362     closedir(dir);
363     return i;
364 }
365 
366 /*
367  * Store disk device info for devices on the PCI bus.
368  * Returns true if information has been stored, or false for failure.
369  */
370 static bool build_guest_fsinfo_for_pci_dev(char const *syspath,
371                                            GuestDiskAddress *disk,
372                                            Error **errp)
373 {
374     unsigned int pci[4], host, hosts[8], tgt[3];
375     int i, nhosts = 0, pcilen;
376     GuestPCIAddress *pciaddr = disk->pci_controller;
377     bool has_ata = false, has_host = false, has_tgt = false;
378     char *p, *q, *driver = NULL;
379     bool ret = false;
380 
381     p = strstr(syspath, "/devices/pci");
382     if (!p || sscanf(p + 12, "%*x:%*x/%x:%x:%x.%x%n",
383                      pci, pci + 1, pci + 2, pci + 3, &pcilen) < 4) {
384         g_debug("only pci device is supported: sysfs path '%s'", syspath);
385         return false;
386     }
387 
388     p += 12 + pcilen;
389     while (true) {
390         driver = get_pci_driver(syspath, p - syspath, errp);
391         if (driver && (g_str_equal(driver, "ata_piix") ||
392                        g_str_equal(driver, "sym53c8xx") ||
393                        g_str_equal(driver, "virtio-pci") ||
394                        g_str_equal(driver, "ahci") ||
395                        g_str_equal(driver, "nvme") ||
396                        g_str_equal(driver, "xhci_hcd") ||
397                        g_str_equal(driver, "ehci-pci"))) {
398             break;
399         }
400 
401         g_free(driver);
402         if (sscanf(p, "/%x:%x:%x.%x%n",
403                           pci, pci + 1, pci + 2, pci + 3, &pcilen) == 4) {
404             p += pcilen;
405             continue;
406         }
407 
408         g_debug("unsupported driver or sysfs path '%s'", syspath);
409         return false;
410     }
411 
412     p = strstr(syspath, "/target");
413     if (p && sscanf(p + 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
414                     tgt, tgt + 1, tgt + 2) == 3) {
415         has_tgt = true;
416     }
417 
418     p = strstr(syspath, "/ata");
419     if (p) {
420         q = p + 4;
421         has_ata = true;
422     } else {
423         p = strstr(syspath, "/host");
424         q = p + 5;
425     }
426     if (p && sscanf(q, "%u", &host) == 1) {
427         has_host = true;
428         nhosts = build_hosts(syspath, p, has_ata, hosts,
429                              ARRAY_SIZE(hosts), errp);
430         if (nhosts < 0) {
431             goto cleanup;
432         }
433     }
434 
435     pciaddr->domain = pci[0];
436     pciaddr->bus = pci[1];
437     pciaddr->slot = pci[2];
438     pciaddr->function = pci[3];
439 
440     if (strcmp(driver, "ata_piix") == 0) {
441         /* a host per ide bus, target*:0:<unit>:0 */
442         if (!has_host || !has_tgt) {
443             g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
444             goto cleanup;
445         }
446         for (i = 0; i < nhosts; i++) {
447             if (host == hosts[i]) {
448                 disk->bus_type = GUEST_DISK_BUS_TYPE_IDE;
449                 disk->bus = i;
450                 disk->unit = tgt[1];
451                 break;
452             }
453         }
454         if (i >= nhosts) {
455             g_debug("no host for '%s' (driver '%s')", syspath, driver);
456             goto cleanup;
457         }
458     } else if (strcmp(driver, "sym53c8xx") == 0) {
459         /* scsi(LSI Logic): target*:0:<unit>:0 */
460         if (!has_tgt) {
461             g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
462             goto cleanup;
463         }
464         disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
465         disk->unit = tgt[1];
466     } else if (strcmp(driver, "virtio-pci") == 0) {
467         if (has_tgt) {
468             /* virtio-scsi: target*:0:0:<unit> */
469             disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
470             disk->unit = tgt[2];
471         } else {
472             /* virtio-blk: 1 disk per 1 device */
473             disk->bus_type = GUEST_DISK_BUS_TYPE_VIRTIO;
474         }
475     } else if (strcmp(driver, "ahci") == 0) {
476         /* ahci: 1 host per 1 unit */
477         if (!has_host || !has_tgt) {
478             g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
479             goto cleanup;
480         }
481         for (i = 0; i < nhosts; i++) {
482             if (host == hosts[i]) {
483                 disk->unit = i;
484                 disk->bus_type = GUEST_DISK_BUS_TYPE_SATA;
485                 break;
486             }
487         }
488         if (i >= nhosts) {
489             g_debug("no host for '%s' (driver '%s')", syspath, driver);
490             goto cleanup;
491         }
492     } else if (strcmp(driver, "nvme") == 0) {
493         disk->bus_type = GUEST_DISK_BUS_TYPE_NVME;
494     } else if (strcmp(driver, "ehci-pci") == 0 || strcmp(driver, "xhci_hcd") == 0) {
495         disk->bus_type = GUEST_DISK_BUS_TYPE_USB;
496     } else {
497         g_debug("unknown driver '%s' (sysfs path '%s')", driver, syspath);
498         goto cleanup;
499     }
500 
501     ret = true;
502 
503 cleanup:
504     g_free(driver);
505     return ret;
506 }
507 
508 /*
509  * Store disk device info for non-PCI virtio devices (for example s390x
510  * channel I/O devices). Returns true if information has been stored, or
511  * false for failure.
512  */
513 static bool build_guest_fsinfo_for_nonpci_virtio(char const *syspath,
514                                                  GuestDiskAddress *disk,
515                                                  Error **errp)
516 {
517     unsigned int tgt[3];
518     char *p;
519 
520     if (!strstr(syspath, "/virtio") || !strstr(syspath, "/block")) {
521         g_debug("Unsupported virtio device '%s'", syspath);
522         return false;
523     }
524 
525     p = strstr(syspath, "/target");
526     if (p && sscanf(p + 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
527                     &tgt[0], &tgt[1], &tgt[2]) == 3) {
528         /* virtio-scsi: target*:0:<target>:<unit> */
529         disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
530         disk->bus = tgt[0];
531         disk->target = tgt[1];
532         disk->unit = tgt[2];
533     } else {
534         /* virtio-blk: 1 disk per 1 device */
535         disk->bus_type = GUEST_DISK_BUS_TYPE_VIRTIO;
536     }
537 
538     return true;
539 }
540 
541 /*
542  * Store disk device info for CCW devices (s390x channel I/O devices).
543  * Returns true if information has been stored, or false for failure.
544  */
545 static bool build_guest_fsinfo_for_ccw_dev(char const *syspath,
546                                            GuestDiskAddress *disk,
547                                            Error **errp)
548 {
549     unsigned int cssid, ssid, subchno, devno;
550     char *p;
551 
552     p = strstr(syspath, "/devices/css");
553     if (!p || sscanf(p + 12, "%*x/%x.%x.%x/%*x.%*x.%x/",
554                      &cssid, &ssid, &subchno, &devno) < 4) {
555         g_debug("could not parse ccw device sysfs path: %s", syspath);
556         return false;
557     }
558 
559     disk->ccw_address = g_new0(GuestCCWAddress, 1);
560     disk->ccw_address->cssid = cssid;
561     disk->ccw_address->ssid = ssid;
562     disk->ccw_address->subchno = subchno;
563     disk->ccw_address->devno = devno;
564 
565     if (strstr(p, "/virtio")) {
566         build_guest_fsinfo_for_nonpci_virtio(syspath, disk, errp);
567     }
568 
569     return true;
570 }
571 
572 /* Store disk device info specified by @sysfs into @fs */
573 static void build_guest_fsinfo_for_real_device(char const *syspath,
574                                                GuestFilesystemInfo *fs,
575                                                Error **errp)
576 {
577     GuestDiskAddress *disk;
578     GuestPCIAddress *pciaddr;
579     bool has_hwinf;
580 #ifdef CONFIG_LIBUDEV
581     struct udev *udev = NULL;
582     struct udev_device *udevice = NULL;
583 #endif
584 
585     pciaddr = g_new0(GuestPCIAddress, 1);
586     pciaddr->domain = -1;                       /* -1 means field is invalid */
587     pciaddr->bus = -1;
588     pciaddr->slot = -1;
589     pciaddr->function = -1;
590 
591     disk = g_new0(GuestDiskAddress, 1);
592     disk->pci_controller = pciaddr;
593     disk->bus_type = GUEST_DISK_BUS_TYPE_UNKNOWN;
594 
595 #ifdef CONFIG_LIBUDEV
596     udev = udev_new();
597     udevice = udev_device_new_from_syspath(udev, syspath);
598     if (udev == NULL || udevice == NULL) {
599         g_debug("failed to query udev");
600     } else {
601         const char *devnode, *serial;
602         devnode = udev_device_get_devnode(udevice);
603         if (devnode != NULL) {
604             disk->dev = g_strdup(devnode);
605         }
606         serial = udev_device_get_property_value(udevice, "ID_SERIAL");
607         if (serial != NULL && *serial != 0) {
608             disk->serial = g_strdup(serial);
609         }
610     }
611 
612     udev_unref(udev);
613     udev_device_unref(udevice);
614 #endif
615 
616     if (strstr(syspath, "/devices/pci")) {
617         has_hwinf = build_guest_fsinfo_for_pci_dev(syspath, disk, errp);
618     } else if (strstr(syspath, "/devices/css")) {
619         has_hwinf = build_guest_fsinfo_for_ccw_dev(syspath, disk, errp);
620     } else if (strstr(syspath, "/virtio")) {
621         has_hwinf = build_guest_fsinfo_for_nonpci_virtio(syspath, disk, errp);
622     } else {
623         g_debug("Unsupported device type for '%s'", syspath);
624         has_hwinf = false;
625     }
626 
627     if (has_hwinf || disk->dev || disk->serial) {
628         QAPI_LIST_PREPEND(fs->disk, disk);
629     } else {
630         qapi_free_GuestDiskAddress(disk);
631     }
632 }
633 
634 static void build_guest_fsinfo_for_device(char const *devpath,
635                                           GuestFilesystemInfo *fs,
636                                           Error **errp);
637 
638 /* Store a list of slave devices of virtual volume specified by @syspath into
639  * @fs */
640 static void build_guest_fsinfo_for_virtual_device(char const *syspath,
641                                                   GuestFilesystemInfo *fs,
642                                                   Error **errp)
643 {
644     Error *err = NULL;
645     DIR *dir;
646     char *dirpath;
647     struct dirent *entry;
648 
649     dirpath = g_strdup_printf("%s/slaves", syspath);
650     dir = opendir(dirpath);
651     if (!dir) {
652         if (errno != ENOENT) {
653             error_setg_errno(errp, errno, "opendir(\"%s\")", dirpath);
654         }
655         g_free(dirpath);
656         return;
657     }
658 
659     for (;;) {
660         errno = 0;
661         entry = readdir(dir);
662         if (entry == NULL) {
663             if (errno) {
664                 error_setg_errno(errp, errno, "readdir(\"%s\")", dirpath);
665             }
666             break;
667         }
668 
669         if (entry->d_type == DT_LNK) {
670             char *path;
671 
672             g_debug(" slave device '%s'", entry->d_name);
673             path = g_strdup_printf("%s/slaves/%s", syspath, entry->d_name);
674             build_guest_fsinfo_for_device(path, fs, &err);
675             g_free(path);
676 
677             if (err) {
678                 error_propagate(errp, err);
679                 break;
680             }
681         }
682     }
683 
684     g_free(dirpath);
685     closedir(dir);
686 }
687 
688 static bool is_disk_virtual(const char *devpath, Error **errp)
689 {
690     g_autofree char *syspath = realpath(devpath, NULL);
691 
692     if (!syspath) {
693         error_setg_errno(errp, errno, "realpath(\"%s\")", devpath);
694         return false;
695     }
696     return strstr(syspath, "/devices/virtual/block/") != NULL;
697 }
698 
699 /* Dispatch to functions for virtual/real device */
700 static void build_guest_fsinfo_for_device(char const *devpath,
701                                           GuestFilesystemInfo *fs,
702                                           Error **errp)
703 {
704     ERRP_GUARD();
705     g_autofree char *syspath = NULL;
706     bool is_virtual = false;
707 
708     syspath = realpath(devpath, NULL);
709     if (!syspath) {
710         if (errno != ENOENT) {
711             error_setg_errno(errp, errno, "realpath(\"%s\")", devpath);
712             return;
713         }
714 
715         /* ENOENT: This devpath may not exist because of container config */
716         if (!fs->name) {
717             fs->name = g_path_get_basename(devpath);
718         }
719         return;
720     }
721 
722     if (!fs->name) {
723         fs->name = g_path_get_basename(syspath);
724     }
725 
726     g_debug("  parse sysfs path '%s'", syspath);
727     is_virtual = is_disk_virtual(syspath, errp);
728     if (*errp != NULL) {
729         return;
730     }
731     if (is_virtual) {
732         build_guest_fsinfo_for_virtual_device(syspath, fs, errp);
733     } else {
734         build_guest_fsinfo_for_real_device(syspath, fs, errp);
735     }
736 }
737 
738 #ifdef CONFIG_LIBUDEV
739 
740 /*
741  * Wrapper around build_guest_fsinfo_for_device() for getting just
742  * the disk address.
743  */
744 static GuestDiskAddress *get_disk_address(const char *syspath, Error **errp)
745 {
746     g_autoptr(GuestFilesystemInfo) fs = NULL;
747 
748     fs = g_new0(GuestFilesystemInfo, 1);
749     build_guest_fsinfo_for_device(syspath, fs, errp);
750     if (fs->disk != NULL) {
751         return g_steal_pointer(&fs->disk->value);
752     }
753     return NULL;
754 }
755 
756 static char *get_alias_for_syspath(const char *syspath)
757 {
758     struct udev *udev = NULL;
759     struct udev_device *udevice = NULL;
760     char *ret = NULL;
761 
762     udev = udev_new();
763     if (udev == NULL) {
764         g_debug("failed to query udev");
765         goto out;
766     }
767     udevice = udev_device_new_from_syspath(udev, syspath);
768     if (udevice == NULL) {
769         g_debug("failed to query udev for path: %s", syspath);
770         goto out;
771     } else {
772         const char *alias = udev_device_get_property_value(
773             udevice, "DM_NAME");
774         /*
775          * NULL means there was an error and empty string means there is no
776          * alias. In case of no alias we return NULL instead of empty string.
777          */
778         if (alias == NULL) {
779             g_debug("failed to query udev for device alias for: %s",
780                 syspath);
781         } else if (*alias != 0) {
782             ret = g_strdup(alias);
783         }
784     }
785 
786 out:
787     udev_unref(udev);
788     udev_device_unref(udevice);
789     return ret;
790 }
791 
792 static char *get_device_for_syspath(const char *syspath)
793 {
794     struct udev *udev = NULL;
795     struct udev_device *udevice = NULL;
796     char *ret = NULL;
797 
798     udev = udev_new();
799     if (udev == NULL) {
800         g_debug("failed to query udev");
801         goto out;
802     }
803     udevice = udev_device_new_from_syspath(udev, syspath);
804     if (udevice == NULL) {
805         g_debug("failed to query udev for path: %s", syspath);
806         goto out;
807     } else {
808         ret = g_strdup(udev_device_get_devnode(udevice));
809     }
810 
811 out:
812     udev_unref(udev);
813     udev_device_unref(udevice);
814     return ret;
815 }
816 
817 static void get_disk_deps(const char *disk_dir, GuestDiskInfo *disk)
818 {
819     g_autofree char *deps_dir = NULL;
820     const gchar *dep;
821     GDir *dp_deps = NULL;
822 
823     /* List dependent disks */
824     deps_dir = g_strdup_printf("%s/slaves", disk_dir);
825     g_debug("  listing entries in: %s", deps_dir);
826     dp_deps = g_dir_open(deps_dir, 0, NULL);
827     if (dp_deps == NULL) {
828         g_debug("failed to list entries in %s", deps_dir);
829         return;
830     }
831     disk->has_dependencies = true;
832     while ((dep = g_dir_read_name(dp_deps)) != NULL) {
833         g_autofree char *dep_dir = NULL;
834         char *dev_name;
835 
836         /* Add dependent disks */
837         dep_dir = g_strdup_printf("%s/%s", deps_dir, dep);
838         dev_name = get_device_for_syspath(dep_dir);
839         if (dev_name != NULL) {
840             g_debug("  adding dependent device: %s", dev_name);
841             QAPI_LIST_PREPEND(disk->dependencies, dev_name);
842         }
843     }
844     g_dir_close(dp_deps);
845 }
846 
847 /*
848  * Detect partitions subdirectory, name is "<disk_name><number>" or
849  * "<disk_name>p<number>"
850  *
851  * @disk_name -- last component of /sys path (e.g. sda)
852  * @disk_dir -- sys path of the disk (e.g. /sys/block/sda)
853  * @disk_dev -- device node of the disk (e.g. /dev/sda)
854  */
855 static GuestDiskInfoList *get_disk_partitions(
856     GuestDiskInfoList *list,
857     const char *disk_name, const char *disk_dir,
858     const char *disk_dev)
859 {
860     GuestDiskInfoList *ret = list;
861     struct dirent *de_disk;
862     DIR *dp_disk = NULL;
863     size_t len = strlen(disk_name);
864 
865     dp_disk = opendir(disk_dir);
866     while ((de_disk = readdir(dp_disk)) != NULL) {
867         g_autofree char *partition_dir = NULL;
868         char *dev_name;
869         GuestDiskInfo *partition;
870 
871         if (!(de_disk->d_type & DT_DIR)) {
872             continue;
873         }
874 
875         if (!(strncmp(disk_name, de_disk->d_name, len) == 0 &&
876             ((*(de_disk->d_name + len) == 'p' &&
877             isdigit(*(de_disk->d_name + len + 1))) ||
878                 isdigit(*(de_disk->d_name + len))))) {
879             continue;
880         }
881 
882         partition_dir = g_strdup_printf("%s/%s",
883             disk_dir, de_disk->d_name);
884         dev_name = get_device_for_syspath(partition_dir);
885         if (dev_name == NULL) {
886             g_debug("Failed to get device name for syspath: %s",
887                 disk_dir);
888             continue;
889         }
890         partition = g_new0(GuestDiskInfo, 1);
891         partition->name = dev_name;
892         partition->partition = true;
893         partition->has_dependencies = true;
894         /* Add parent disk as dependent for easier tracking of hierarchy */
895         QAPI_LIST_PREPEND(partition->dependencies, g_strdup(disk_dev));
896 
897         QAPI_LIST_PREPEND(ret, partition);
898     }
899     closedir(dp_disk);
900 
901     return ret;
902 }
903 
904 static void get_nvme_smart(GuestDiskInfo *disk)
905 {
906     int fd;
907     GuestNVMeSmart *smart;
908     NvmeSmartLog log = {0};
909     struct nvme_admin_cmd cmd = {
910         .opcode = NVME_ADM_CMD_GET_LOG_PAGE,
911         .nsid = NVME_NSID_BROADCAST,
912         .addr = (uintptr_t)&log,
913         .data_len = sizeof(log),
914         .cdw10 = NVME_LOG_SMART_INFO | (1 << 15) /* RAE bit */
915                  | (((sizeof(log) >> 2) - 1) << 16)
916     };
917 
918     fd = qga_open_cloexec(disk->name, O_RDONLY, 0);
919     if (fd == -1) {
920         g_debug("Failed to open device: %s: %s", disk->name, g_strerror(errno));
921         return;
922     }
923 
924     if (ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd)) {
925         g_debug("Failed to get smart: %s: %s", disk->name, g_strerror(errno));
926         close(fd);
927         return;
928     }
929 
930     disk->smart = g_new0(GuestDiskSmart, 1);
931     disk->smart->type = GUEST_DISK_BUS_TYPE_NVME;
932 
933     smart = &disk->smart->u.nvme;
934     smart->critical_warning = log.critical_warning;
935     smart->temperature = lduw_le_p(&log.temperature); /* unaligned field */
936     smart->available_spare = log.available_spare;
937     smart->available_spare_threshold = log.available_spare_threshold;
938     smart->percentage_used = log.percentage_used;
939     smart->data_units_read_lo = le64_to_cpu(log.data_units_read[0]);
940     smart->data_units_read_hi = le64_to_cpu(log.data_units_read[1]);
941     smart->data_units_written_lo = le64_to_cpu(log.data_units_written[0]);
942     smart->data_units_written_hi = le64_to_cpu(log.data_units_written[1]);
943     smart->host_read_commands_lo = le64_to_cpu(log.host_read_commands[0]);
944     smart->host_read_commands_hi = le64_to_cpu(log.host_read_commands[1]);
945     smart->host_write_commands_lo = le64_to_cpu(log.host_write_commands[0]);
946     smart->host_write_commands_hi = le64_to_cpu(log.host_write_commands[1]);
947     smart->controller_busy_time_lo = le64_to_cpu(log.controller_busy_time[0]);
948     smart->controller_busy_time_hi = le64_to_cpu(log.controller_busy_time[1]);
949     smart->power_cycles_lo = le64_to_cpu(log.power_cycles[0]);
950     smart->power_cycles_hi = le64_to_cpu(log.power_cycles[1]);
951     smart->power_on_hours_lo = le64_to_cpu(log.power_on_hours[0]);
952     smart->power_on_hours_hi = le64_to_cpu(log.power_on_hours[1]);
953     smart->unsafe_shutdowns_lo = le64_to_cpu(log.unsafe_shutdowns[0]);
954     smart->unsafe_shutdowns_hi = le64_to_cpu(log.unsafe_shutdowns[1]);
955     smart->media_errors_lo = le64_to_cpu(log.media_errors[0]);
956     smart->media_errors_hi = le64_to_cpu(log.media_errors[1]);
957     smart->number_of_error_log_entries_lo =
958         le64_to_cpu(log.number_of_error_log_entries[0]);
959     smart->number_of_error_log_entries_hi =
960         le64_to_cpu(log.number_of_error_log_entries[1]);
961 
962     close(fd);
963 }
964 
965 static void get_disk_smart(GuestDiskInfo *disk)
966 {
967     if (disk->address
968         && (disk->address->bus_type == GUEST_DISK_BUS_TYPE_NVME)) {
969         get_nvme_smart(disk);
970     }
971 }
972 
973 GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
974 {
975     GuestDiskInfoList *ret = NULL;
976     GuestDiskInfo *disk;
977     DIR *dp = NULL;
978     struct dirent *de = NULL;
979 
980     g_debug("listing /sys/block directory");
981     dp = opendir("/sys/block");
982     if (dp == NULL) {
983         error_setg_errno(errp, errno, "Can't open directory \"/sys/block\"");
984         return NULL;
985     }
986     while ((de = readdir(dp)) != NULL) {
987         g_autofree char *disk_dir = NULL, *line = NULL,
988             *size_path = NULL;
989         char *dev_name;
990         Error *local_err = NULL;
991         if (de->d_type != DT_LNK) {
992             g_debug("  skipping entry: %s", de->d_name);
993             continue;
994         }
995 
996         /* Check size and skip zero-sized disks */
997         g_debug("  checking disk size");
998         size_path = g_strdup_printf("/sys/block/%s/size", de->d_name);
999         if (!g_file_get_contents(size_path, &line, NULL, NULL)) {
1000             g_debug("  failed to read disk size");
1001             continue;
1002         }
1003         if (g_strcmp0(line, "0\n") == 0) {
1004             g_debug("  skipping zero-sized disk");
1005             continue;
1006         }
1007 
1008         g_debug("  adding %s", de->d_name);
1009         disk_dir = g_strdup_printf("/sys/block/%s", de->d_name);
1010         dev_name = get_device_for_syspath(disk_dir);
1011         if (dev_name == NULL) {
1012             g_debug("Failed to get device name for syspath: %s",
1013                 disk_dir);
1014             continue;
1015         }
1016         disk = g_new0(GuestDiskInfo, 1);
1017         disk->name = dev_name;
1018         disk->partition = false;
1019         disk->alias = get_alias_for_syspath(disk_dir);
1020         QAPI_LIST_PREPEND(ret, disk);
1021 
1022         /* Get address for non-virtual devices */
1023         bool is_virtual = is_disk_virtual(disk_dir, &local_err);
1024         if (local_err != NULL) {
1025             g_debug("  failed to check disk path, ignoring error: %s",
1026                 error_get_pretty(local_err));
1027             error_free(local_err);
1028             local_err = NULL;
1029             /* Don't try to get the address */
1030             is_virtual = true;
1031         }
1032         if (!is_virtual) {
1033             disk->address = get_disk_address(disk_dir, &local_err);
1034             if (local_err != NULL) {
1035                 g_debug("  failed to get device info, ignoring error: %s",
1036                     error_get_pretty(local_err));
1037                 error_free(local_err);
1038                 local_err = NULL;
1039             }
1040         }
1041 
1042         get_disk_deps(disk_dir, disk);
1043         get_disk_smart(disk);
1044         ret = get_disk_partitions(ret, de->d_name, disk_dir, dev_name);
1045     }
1046 
1047     closedir(dp);
1048 
1049     return ret;
1050 }
1051 
1052 #endif
1053 
1054 /* Return a list of the disk device(s)' info which @mount lies on */
1055 static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
1056                                                Error **errp)
1057 {
1058     GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
1059     struct statvfs buf;
1060     unsigned long used, nonroot_total, fr_size;
1061     char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
1062                                     mount->devmajor, mount->devminor);
1063 
1064     fs->mountpoint = g_strdup(mount->dirname);
1065     fs->type = g_strdup(mount->devtype);
1066     build_guest_fsinfo_for_device(devpath, fs, errp);
1067 
1068     if (statvfs(fs->mountpoint, &buf) == 0) {
1069         fr_size = buf.f_frsize;
1070         used = buf.f_blocks - buf.f_bfree;
1071         nonroot_total = used + buf.f_bavail;
1072         fs->used_bytes = used * fr_size;
1073         fs->total_bytes = nonroot_total * fr_size;
1074         fs->total_bytes_privileged = buf.f_blocks * fr_size;
1075 
1076         fs->has_total_bytes = true;
1077         fs->has_total_bytes_privileged = true;
1078         fs->has_used_bytes = true;
1079     }
1080 
1081     g_free(devpath);
1082 
1083     return fs;
1084 }
1085 
1086 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
1087 {
1088     FsMountList mounts;
1089     struct FsMount *mount;
1090     GuestFilesystemInfoList *ret = NULL;
1091     Error *local_err = NULL;
1092 
1093     QTAILQ_INIT(&mounts);
1094     if (!build_fs_mount_list(&mounts, &local_err)) {
1095         error_propagate(errp, local_err);
1096         return NULL;
1097     }
1098 
1099     QTAILQ_FOREACH(mount, &mounts, next) {
1100         g_debug("Building guest fsinfo for '%s'", mount->dirname);
1101 
1102         QAPI_LIST_PREPEND(ret, build_guest_fsinfo(mount, &local_err));
1103         if (local_err) {
1104             error_propagate(errp, local_err);
1105             qapi_free_GuestFilesystemInfoList(ret);
1106             ret = NULL;
1107             break;
1108         }
1109     }
1110 
1111     free_fs_mount_list(&mounts);
1112     return ret;
1113 }
1114 #endif /* CONFIG_FSFREEZE */
1115 
1116 #if defined(CONFIG_FSTRIM)
1117 /*
1118  * Walk list of mounted file systems in the guest, and trim them.
1119  */
1120 GuestFilesystemTrimResponse *
1121 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
1122 {
1123     GuestFilesystemTrimResponse *response;
1124     GuestFilesystemTrimResult *result;
1125     int ret = 0;
1126     FsMountList mounts;
1127     struct FsMount *mount;
1128     int fd;
1129     struct fstrim_range r;
1130 
1131     slog("guest-fstrim called");
1132 
1133     QTAILQ_INIT(&mounts);
1134     if (!build_fs_mount_list(&mounts, errp)) {
1135         return NULL;
1136     }
1137 
1138     response = g_malloc0(sizeof(*response));
1139 
1140     QTAILQ_FOREACH(mount, &mounts, next) {
1141         result = g_malloc0(sizeof(*result));
1142         result->path = g_strdup(mount->dirname);
1143 
1144         QAPI_LIST_PREPEND(response->paths, result);
1145 
1146         fd = qga_open_cloexec(mount->dirname, O_RDONLY, 0);
1147         if (fd == -1) {
1148             result->error = g_strdup_printf("failed to open: %s",
1149                                             strerror(errno));
1150             continue;
1151         }
1152 
1153         /* We try to cull filesystems we know won't work in advance, but other
1154          * filesystems may not implement fstrim for less obvious reasons.
1155          * These will report EOPNOTSUPP; while in some other cases ENOTTY
1156          * will be reported (e.g. CD-ROMs).
1157          * Any other error means an unexpected error.
1158          */
1159         r.start = 0;
1160         r.len = -1;
1161         r.minlen = has_minimum ? minimum : 0;
1162         ret = ioctl(fd, FITRIM, &r);
1163         if (ret == -1) {
1164             if (errno == ENOTTY || errno == EOPNOTSUPP) {
1165                 result->error = g_strdup("trim not supported");
1166             } else {
1167                 result->error = g_strdup_printf("failed to trim: %s",
1168                                                 strerror(errno));
1169             }
1170             close(fd);
1171             continue;
1172         }
1173 
1174         result->has_minimum = true;
1175         result->minimum = r.minlen;
1176         result->has_trimmed = true;
1177         result->trimmed = r.len;
1178         close(fd);
1179     }
1180 
1181     free_fs_mount_list(&mounts);
1182     return response;
1183 }
1184 #endif /* CONFIG_FSTRIM */
1185 
1186 #define LINUX_SYS_STATE_FILE "/sys/power/state"
1187 #define SUSPEND_SUPPORTED 0
1188 #define SUSPEND_NOT_SUPPORTED 1
1189 
1190 typedef enum {
1191     SUSPEND_MODE_DISK = 0,
1192     SUSPEND_MODE_RAM = 1,
1193     SUSPEND_MODE_HYBRID = 2,
1194 } SuspendMode;
1195 
1196 /*
1197  * Executes a command in a child process using g_spawn_sync,
1198  * returning an int >= 0 representing the exit status of the
1199  * process.
1200  *
1201  * If the program wasn't found in path, returns -1.
1202  *
1203  * If a problem happened when creating the child process,
1204  * returns -1 and errp is set.
1205  */
1206 static int run_process_child(const char *command[], Error **errp)
1207 {
1208     int exit_status, spawn_flag;
1209     GError *g_err = NULL;
1210     bool success;
1211 
1212     spawn_flag = G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL |
1213                  G_SPAWN_STDERR_TO_DEV_NULL;
1214 
1215     success =  g_spawn_sync(NULL, (char **)command, NULL, spawn_flag,
1216                             NULL, NULL, NULL, NULL,
1217                             &exit_status, &g_err);
1218 
1219     if (success) {
1220         return WEXITSTATUS(exit_status);
1221     }
1222 
1223     if (g_err && (g_err->code != G_SPAWN_ERROR_NOENT)) {
1224         error_setg(errp, "failed to create child process, error '%s'",
1225                    g_err->message);
1226     }
1227 
1228     g_error_free(g_err);
1229     return -1;
1230 }
1231 
1232 static bool systemd_supports_mode(SuspendMode mode, Error **errp)
1233 {
1234     const char *systemctl_args[3] = {"systemd-hibernate", "systemd-suspend",
1235                                      "systemd-hybrid-sleep"};
1236     const char *cmd[4] = {"systemctl", "status", systemctl_args[mode], NULL};
1237     int status;
1238 
1239     status = run_process_child(cmd, errp);
1240 
1241     /*
1242      * systemctl status uses LSB return codes so we can expect
1243      * status > 0 and be ok. To assert if the guest has support
1244      * for the selected suspend mode, status should be < 4. 4 is
1245      * the code for unknown service status, the return value when
1246      * the service does not exist. A common value is status = 3
1247      * (program is not running).
1248      */
1249     if (status > 0 && status < 4) {
1250         return true;
1251     }
1252 
1253     return false;
1254 }
1255 
1256 static void systemd_suspend(SuspendMode mode, Error **errp)
1257 {
1258     Error *local_err = NULL;
1259     const char *systemctl_args[3] = {"hibernate", "suspend", "hybrid-sleep"};
1260     const char *cmd[3] = {"systemctl", systemctl_args[mode], NULL};
1261     int status;
1262 
1263     status = run_process_child(cmd, &local_err);
1264 
1265     if (status == 0) {
1266         return;
1267     }
1268 
1269     if ((status == -1) && !local_err) {
1270         error_setg(errp, "the helper program 'systemctl %s' was not found",
1271                    systemctl_args[mode]);
1272         return;
1273     }
1274 
1275     if (local_err) {
1276         error_propagate(errp, local_err);
1277     } else {
1278         error_setg(errp, "the helper program 'systemctl %s' returned an "
1279                    "unexpected exit status code (%d)",
1280                    systemctl_args[mode], status);
1281     }
1282 }
1283 
1284 static bool pmutils_supports_mode(SuspendMode mode, Error **errp)
1285 {
1286     Error *local_err = NULL;
1287     const char *pmutils_args[3] = {"--hibernate", "--suspend",
1288                                    "--suspend-hybrid"};
1289     const char *cmd[3] = {"pm-is-supported", pmutils_args[mode], NULL};
1290     int status;
1291 
1292     status = run_process_child(cmd, &local_err);
1293 
1294     if (status == SUSPEND_SUPPORTED) {
1295         return true;
1296     }
1297 
1298     if ((status == -1) && !local_err) {
1299         return false;
1300     }
1301 
1302     if (local_err) {
1303         error_propagate(errp, local_err);
1304     } else {
1305         error_setg(errp,
1306                    "the helper program '%s' returned an unexpected exit"
1307                    " status code (%d)", "pm-is-supported", status);
1308     }
1309 
1310     return false;
1311 }
1312 
1313 static void pmutils_suspend(SuspendMode mode, Error **errp)
1314 {
1315     Error *local_err = NULL;
1316     const char *pmutils_binaries[3] = {"pm-hibernate", "pm-suspend",
1317                                        "pm-suspend-hybrid"};
1318     const char *cmd[2] = {pmutils_binaries[mode], NULL};
1319     int status;
1320 
1321     status = run_process_child(cmd, &local_err);
1322 
1323     if (status == 0) {
1324         return;
1325     }
1326 
1327     if ((status == -1) && !local_err) {
1328         error_setg(errp, "the helper program '%s' was not found",
1329                    pmutils_binaries[mode]);
1330         return;
1331     }
1332 
1333     if (local_err) {
1334         error_propagate(errp, local_err);
1335     } else {
1336         error_setg(errp,
1337                    "the helper program '%s' returned an unexpected exit"
1338                    " status code (%d)", pmutils_binaries[mode], status);
1339     }
1340 }
1341 
1342 static bool linux_sys_state_supports_mode(SuspendMode mode, Error **errp)
1343 {
1344     const char *sysfile_strs[3] = {"disk", "mem", NULL};
1345     const char *sysfile_str = sysfile_strs[mode];
1346     char buf[32]; /* hopefully big enough */
1347     int fd;
1348     ssize_t ret;
1349 
1350     if (!sysfile_str) {
1351         error_setg(errp, "unknown guest suspend mode");
1352         return false;
1353     }
1354 
1355     fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
1356     if (fd < 0) {
1357         return false;
1358     }
1359 
1360     ret = read(fd, buf, sizeof(buf) - 1);
1361     close(fd);
1362     if (ret <= 0) {
1363         return false;
1364     }
1365     buf[ret] = '\0';
1366 
1367     if (strstr(buf, sysfile_str)) {
1368         return true;
1369     }
1370     return false;
1371 }
1372 
1373 static void linux_sys_state_suspend(SuspendMode mode, Error **errp)
1374 {
1375     g_autoptr(GError) local_gerr = NULL;
1376     const char *sysfile_strs[3] = {"disk", "mem", NULL};
1377     const char *sysfile_str = sysfile_strs[mode];
1378 
1379     if (!sysfile_str) {
1380         error_setg(errp, "unknown guest suspend mode");
1381         return;
1382     }
1383 
1384     if (!g_file_set_contents(LINUX_SYS_STATE_FILE, sysfile_str,
1385                              -1, &local_gerr)) {
1386         error_setg(errp, "suspend: cannot write to '%s': %s",
1387                    LINUX_SYS_STATE_FILE, local_gerr->message);
1388         return;
1389     }
1390 }
1391 
1392 static void guest_suspend(SuspendMode mode, Error **errp)
1393 {
1394     Error *local_err = NULL;
1395     bool mode_supported = false;
1396 
1397     if (systemd_supports_mode(mode, &local_err)) {
1398         mode_supported = true;
1399         systemd_suspend(mode, &local_err);
1400 
1401         if (!local_err) {
1402             return;
1403         }
1404     }
1405 
1406     error_free(local_err);
1407     local_err = NULL;
1408 
1409     if (pmutils_supports_mode(mode, &local_err)) {
1410         mode_supported = true;
1411         pmutils_suspend(mode, &local_err);
1412 
1413         if (!local_err) {
1414             return;
1415         }
1416     }
1417 
1418     error_free(local_err);
1419     local_err = NULL;
1420 
1421     if (linux_sys_state_supports_mode(mode, &local_err)) {
1422         mode_supported = true;
1423         linux_sys_state_suspend(mode, &local_err);
1424     }
1425 
1426     if (!mode_supported) {
1427         error_free(local_err);
1428         error_setg(errp,
1429                    "the requested suspend mode is not supported by the guest");
1430     } else {
1431         error_propagate(errp, local_err);
1432     }
1433 }
1434 
1435 void qmp_guest_suspend_disk(Error **errp)
1436 {
1437     guest_suspend(SUSPEND_MODE_DISK, errp);
1438 }
1439 
1440 void qmp_guest_suspend_ram(Error **errp)
1441 {
1442     guest_suspend(SUSPEND_MODE_RAM, errp);
1443 }
1444 
1445 void qmp_guest_suspend_hybrid(Error **errp)
1446 {
1447     guest_suspend(SUSPEND_MODE_HYBRID, errp);
1448 }
1449 
1450 /* Transfer online/offline status between @vcpu and the guest system.
1451  *
1452  * On input either @errp or *@errp must be NULL.
1453  *
1454  * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1455  * - R: vcpu->logical_id
1456  * - W: vcpu->online
1457  * - W: vcpu->can_offline
1458  *
1459  * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1460  * - R: vcpu->logical_id
1461  * - R: vcpu->online
1462  *
1463  * Written members remain unmodified on error.
1464  */
1465 static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1466                           char *dirpath, Error **errp)
1467 {
1468     int fd;
1469     int res;
1470     int dirfd;
1471     static const char fn[] = "online";
1472 
1473     dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1474     if (dirfd == -1) {
1475         error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1476         return;
1477     }
1478 
1479     fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1480     if (fd == -1) {
1481         if (errno != ENOENT) {
1482             error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
1483         } else if (sys2vcpu) {
1484             vcpu->online = true;
1485             vcpu->can_offline = false;
1486         } else if (!vcpu->online) {
1487             error_setg(errp, "logical processor #%" PRId64 " can't be "
1488                        "offlined", vcpu->logical_id);
1489         } /* otherwise pretend successful re-onlining */
1490     } else {
1491         unsigned char status;
1492 
1493         res = pread(fd, &status, 1, 0);
1494         if (res == -1) {
1495             error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
1496         } else if (res == 0) {
1497             error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
1498                        fn);
1499         } else if (sys2vcpu) {
1500             vcpu->online = (status != '0');
1501             vcpu->can_offline = true;
1502         } else if (vcpu->online != (status != '0')) {
1503             status = '0' + vcpu->online;
1504             if (pwrite(fd, &status, 1, 0) == -1) {
1505                 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
1506                                  fn);
1507             }
1508         } /* otherwise pretend successful re-(on|off)-lining */
1509 
1510         res = close(fd);
1511         g_assert(res == 0);
1512     }
1513 
1514     res = close(dirfd);
1515     g_assert(res == 0);
1516 }
1517 
1518 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1519 {
1520     GuestLogicalProcessorList *head, **tail;
1521     const char *cpu_dir = "/sys/devices/system/cpu";
1522     const gchar *line;
1523     g_autoptr(GDir) cpu_gdir = NULL;
1524     Error *local_err = NULL;
1525 
1526     head = NULL;
1527     tail = &head;
1528     cpu_gdir = g_dir_open(cpu_dir, 0, NULL);
1529 
1530     if (cpu_gdir == NULL) {
1531         error_setg_errno(errp, errno, "failed to list entries: %s", cpu_dir);
1532         return NULL;
1533     }
1534 
1535     while (local_err == NULL && (line = g_dir_read_name(cpu_gdir)) != NULL) {
1536         GuestLogicalProcessor *vcpu;
1537         int64_t id;
1538         if (sscanf(line, "cpu%" PRId64, &id)) {
1539             g_autofree char *path = g_strdup_printf("/sys/devices/system/cpu/"
1540                                                     "cpu%" PRId64 "/", id);
1541             vcpu = g_malloc0(sizeof *vcpu);
1542             vcpu->logical_id = id;
1543             vcpu->has_can_offline = true; /* lolspeak ftw */
1544             transfer_vcpu(vcpu, true, path, &local_err);
1545             QAPI_LIST_APPEND(tail, vcpu);
1546         }
1547     }
1548 
1549     if (local_err == NULL) {
1550         /* there's no guest with zero VCPUs */
1551         g_assert(head != NULL);
1552         return head;
1553     }
1554 
1555     qapi_free_GuestLogicalProcessorList(head);
1556     error_propagate(errp, local_err);
1557     return NULL;
1558 }
1559 
1560 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1561 {
1562     int64_t processed;
1563     Error *local_err = NULL;
1564 
1565     processed = 0;
1566     while (vcpus != NULL) {
1567         char *path = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1568                                      vcpus->value->logical_id);
1569 
1570         transfer_vcpu(vcpus->value, false, path, &local_err);
1571         g_free(path);
1572         if (local_err != NULL) {
1573             break;
1574         }
1575         ++processed;
1576         vcpus = vcpus->next;
1577     }
1578 
1579     if (local_err != NULL) {
1580         if (processed == 0) {
1581             error_propagate(errp, local_err);
1582         } else {
1583             error_free(local_err);
1584         }
1585     }
1586 
1587     return processed;
1588 }
1589 
1590 
1591 static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf,
1592                                int size, Error **errp)
1593 {
1594     int fd;
1595     int res;
1596 
1597     errno = 0;
1598     fd = openat(dirfd, pathname, O_RDONLY);
1599     if (fd == -1) {
1600         error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
1601         return;
1602     }
1603 
1604     res = pread(fd, buf, size, 0);
1605     if (res == -1) {
1606         error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathname);
1607     } else if (res == 0) {
1608         error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", pathname);
1609     }
1610     close(fd);
1611 }
1612 
1613 static void ga_write_sysfs_file(int dirfd, const char *pathname,
1614                                 const char *buf, int size, Error **errp)
1615 {
1616     int fd;
1617 
1618     errno = 0;
1619     fd = openat(dirfd, pathname, O_WRONLY);
1620     if (fd == -1) {
1621         error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
1622         return;
1623     }
1624 
1625     if (pwrite(fd, buf, size, 0) == -1) {
1626         error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname);
1627     }
1628 
1629     close(fd);
1630 }
1631 
1632 /* Transfer online/offline status between @mem_blk and the guest system.
1633  *
1634  * On input either @errp or *@errp must be NULL.
1635  *
1636  * In system-to-@mem_blk direction, the following @mem_blk fields are accessed:
1637  * - R: mem_blk->phys_index
1638  * - W: mem_blk->online
1639  * - W: mem_blk->can_offline
1640  *
1641  * In @mem_blk-to-system direction, the following @mem_blk fields are accessed:
1642  * - R: mem_blk->phys_index
1643  * - R: mem_blk->online
1644  *-  R: mem_blk->can_offline
1645  * Written members remain unmodified on error.
1646  */
1647 static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
1648                                   GuestMemoryBlockResponse *result,
1649                                   Error **errp)
1650 {
1651     char *dirpath;
1652     int dirfd;
1653     char *status;
1654     Error *local_err = NULL;
1655 
1656     if (!sys2memblk) {
1657         DIR *dp;
1658 
1659         if (!result) {
1660             error_setg(errp, "Internal error, 'result' should not be NULL");
1661             return;
1662         }
1663         errno = 0;
1664         dp = opendir("/sys/devices/system/memory/");
1665          /* if there is no 'memory' directory in sysfs,
1666          * we think this VM does not support online/offline memory block,
1667          * any other solution?
1668          */
1669         if (!dp) {
1670             if (errno == ENOENT) {
1671                 result->response =
1672                     GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
1673             }
1674             goto out1;
1675         }
1676         closedir(dp);
1677     }
1678 
1679     dirpath = g_strdup_printf("/sys/devices/system/memory/memory%" PRId64 "/",
1680                               mem_blk->phys_index);
1681     dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1682     if (dirfd == -1) {
1683         if (sys2memblk) {
1684             error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1685         } else {
1686             if (errno == ENOENT) {
1687                 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND;
1688             } else {
1689                 result->response =
1690                     GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
1691             }
1692         }
1693         g_free(dirpath);
1694         goto out1;
1695     }
1696     g_free(dirpath);
1697 
1698     status = g_malloc0(10);
1699     ga_read_sysfs_file(dirfd, "state", status, 10, &local_err);
1700     if (local_err) {
1701         /* treat with sysfs file that not exist in old kernel */
1702         if (errno == ENOENT) {
1703             error_free(local_err);
1704             if (sys2memblk) {
1705                 mem_blk->online = true;
1706                 mem_blk->can_offline = false;
1707             } else if (!mem_blk->online) {
1708                 result->response =
1709                     GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
1710             }
1711         } else {
1712             if (sys2memblk) {
1713                 error_propagate(errp, local_err);
1714             } else {
1715                 error_free(local_err);
1716                 result->response =
1717                     GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
1718             }
1719         }
1720         goto out2;
1721     }
1722 
1723     if (sys2memblk) {
1724         char removable = '0';
1725 
1726         mem_blk->online = (strncmp(status, "online", 6) == 0);
1727 
1728         ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err);
1729         if (local_err) {
1730             /* if no 'removable' file, it doesn't support offline mem blk */
1731             if (errno == ENOENT) {
1732                 error_free(local_err);
1733                 mem_blk->can_offline = false;
1734             } else {
1735                 error_propagate(errp, local_err);
1736             }
1737         } else {
1738             mem_blk->can_offline = (removable != '0');
1739         }
1740     } else {
1741         if (mem_blk->online != (strncmp(status, "online", 6) == 0)) {
1742             const char *new_state = mem_blk->online ? "online" : "offline";
1743 
1744             ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_state),
1745                                 &local_err);
1746             if (local_err) {
1747                 error_free(local_err);
1748                 result->response =
1749                     GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
1750                 goto out2;
1751             }
1752 
1753             result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS;
1754             result->has_error_code = false;
1755         } /* otherwise pretend successful re-(on|off)-lining */
1756     }
1757     g_free(status);
1758     close(dirfd);
1759     return;
1760 
1761 out2:
1762     g_free(status);
1763     close(dirfd);
1764 out1:
1765     if (!sys2memblk) {
1766         result->has_error_code = true;
1767         result->error_code = errno;
1768     }
1769 }
1770 
1771 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
1772 {
1773     GuestMemoryBlockList *head, **tail;
1774     Error *local_err = NULL;
1775     struct dirent *de;
1776     DIR *dp;
1777 
1778     head = NULL;
1779     tail = &head;
1780 
1781     dp = opendir("/sys/devices/system/memory/");
1782     if (!dp) {
1783         /* it's ok if this happens to be a system that doesn't expose
1784          * memory blocks via sysfs, but otherwise we should report
1785          * an error
1786          */
1787         if (errno != ENOENT) {
1788             error_setg_errno(errp, errno, "Can't open directory"
1789                              "\"/sys/devices/system/memory/\"");
1790         }
1791         return NULL;
1792     }
1793 
1794     /* Note: the phys_index of memory block may be discontinuous,
1795      * this is because a memblk is the unit of the Sparse Memory design, which
1796      * allows discontinuous memory ranges (ex. NUMA), so here we should
1797      * traverse the memory block directory.
1798      */
1799     while ((de = readdir(dp)) != NULL) {
1800         GuestMemoryBlock *mem_blk;
1801 
1802         if ((strncmp(de->d_name, "memory", 6) != 0) ||
1803             !(de->d_type & DT_DIR)) {
1804             continue;
1805         }
1806 
1807         mem_blk = g_malloc0(sizeof *mem_blk);
1808         /* The d_name is "memoryXXX",  phys_index is block id, same as XXX */
1809         mem_blk->phys_index = strtoul(&de->d_name[6], NULL, 10);
1810         mem_blk->has_can_offline = true; /* lolspeak ftw */
1811         transfer_memory_block(mem_blk, true, NULL, &local_err);
1812         if (local_err) {
1813             break;
1814         }
1815 
1816         QAPI_LIST_APPEND(tail, mem_blk);
1817     }
1818 
1819     closedir(dp);
1820     if (local_err == NULL) {
1821         /* there's no guest with zero memory blocks */
1822         if (head == NULL) {
1823             error_setg(errp, "guest reported zero memory blocks!");
1824         }
1825         return head;
1826     }
1827 
1828     qapi_free_GuestMemoryBlockList(head);
1829     error_propagate(errp, local_err);
1830     return NULL;
1831 }
1832 
1833 GuestMemoryBlockResponseList *
1834 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
1835 {
1836     GuestMemoryBlockResponseList *head, **tail;
1837     Error *local_err = NULL;
1838 
1839     head = NULL;
1840     tail = &head;
1841 
1842     while (mem_blks != NULL) {
1843         GuestMemoryBlockResponse *result;
1844         GuestMemoryBlock *current_mem_blk = mem_blks->value;
1845 
1846         result = g_malloc0(sizeof(*result));
1847         result->phys_index = current_mem_blk->phys_index;
1848         transfer_memory_block(current_mem_blk, false, result, &local_err);
1849         if (local_err) { /* should never happen */
1850             goto err;
1851         }
1852 
1853         QAPI_LIST_APPEND(tail, result);
1854         mem_blks = mem_blks->next;
1855     }
1856 
1857     return head;
1858 err:
1859     qapi_free_GuestMemoryBlockResponseList(head);
1860     error_propagate(errp, local_err);
1861     return NULL;
1862 }
1863 
1864 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
1865 {
1866     Error *local_err = NULL;
1867     char *dirpath;
1868     int dirfd;
1869     char *buf;
1870     GuestMemoryBlockInfo *info;
1871 
1872     dirpath = g_strdup_printf("/sys/devices/system/memory/");
1873     dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1874     if (dirfd == -1) {
1875         error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1876         g_free(dirpath);
1877         return NULL;
1878     }
1879     g_free(dirpath);
1880 
1881     buf = g_malloc0(20);
1882     ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err);
1883     close(dirfd);
1884     if (local_err) {
1885         g_free(buf);
1886         error_propagate(errp, local_err);
1887         return NULL;
1888     }
1889 
1890     info = g_new0(GuestMemoryBlockInfo, 1);
1891     info->size = strtol(buf, NULL, 16); /* the unit is bytes */
1892 
1893     g_free(buf);
1894 
1895     return info;
1896 }
1897 
1898 #define MAX_NAME_LEN 128
1899 static GuestDiskStatsInfoList *guest_get_diskstats(Error **errp)
1900 {
1901     GuestDiskStatsInfoList *head = NULL, **tail = &head;
1902     const char *diskstats = "/proc/diskstats";
1903     FILE *fp;
1904     size_t n;
1905     char *line = NULL;
1906 
1907     fp = fopen(diskstats, "r");
1908     if (fp  == NULL) {
1909         error_setg_errno(errp, errno, "open(\"%s\")", diskstats);
1910         return NULL;
1911     }
1912 
1913     while (getline(&line, &n, fp) != -1) {
1914         g_autofree GuestDiskStatsInfo *diskstatinfo = NULL;
1915         g_autofree GuestDiskStats *diskstat = NULL;
1916         char dev_name[MAX_NAME_LEN];
1917         unsigned int ios_pgr, tot_ticks, rq_ticks, wr_ticks, dc_ticks, fl_ticks;
1918         unsigned long rd_ios, rd_merges_or_rd_sec, rd_ticks_or_wr_sec, wr_ios;
1919         unsigned long wr_merges, rd_sec_or_wr_ios, wr_sec;
1920         unsigned long dc_ios, dc_merges, dc_sec, fl_ios;
1921         unsigned int major, minor;
1922         int i;
1923 
1924         i = sscanf(line, "%u %u %s %lu %lu %lu"
1925                    "%lu %lu %lu %lu %u %u %u %u"
1926                    "%lu %lu %lu %u %lu %u",
1927                    &major, &minor, dev_name,
1928                    &rd_ios, &rd_merges_or_rd_sec, &rd_sec_or_wr_ios,
1929                    &rd_ticks_or_wr_sec, &wr_ios, &wr_merges, &wr_sec,
1930                    &wr_ticks, &ios_pgr, &tot_ticks, &rq_ticks,
1931                    &dc_ios, &dc_merges, &dc_sec, &dc_ticks,
1932                    &fl_ios, &fl_ticks);
1933 
1934         if (i < 7) {
1935             continue;
1936         }
1937 
1938         diskstatinfo = g_new0(GuestDiskStatsInfo, 1);
1939         diskstatinfo->name = g_strdup(dev_name);
1940         diskstatinfo->major = major;
1941         diskstatinfo->minor = minor;
1942 
1943         diskstat = g_new0(GuestDiskStats, 1);
1944         if (i == 7) {
1945             diskstat->has_read_ios = true;
1946             diskstat->read_ios = rd_ios;
1947             diskstat->has_read_sectors = true;
1948             diskstat->read_sectors = rd_merges_or_rd_sec;
1949             diskstat->has_write_ios = true;
1950             diskstat->write_ios = rd_sec_or_wr_ios;
1951             diskstat->has_write_sectors = true;
1952             diskstat->write_sectors = rd_ticks_or_wr_sec;
1953         }
1954         if (i >= 14) {
1955             diskstat->has_read_ios = true;
1956             diskstat->read_ios = rd_ios;
1957             diskstat->has_read_sectors = true;
1958             diskstat->read_sectors = rd_sec_or_wr_ios;
1959             diskstat->has_read_merges = true;
1960             diskstat->read_merges = rd_merges_or_rd_sec;
1961             diskstat->has_read_ticks = true;
1962             diskstat->read_ticks = rd_ticks_or_wr_sec;
1963             diskstat->has_write_ios = true;
1964             diskstat->write_ios = wr_ios;
1965             diskstat->has_write_sectors = true;
1966             diskstat->write_sectors = wr_sec;
1967             diskstat->has_write_merges = true;
1968             diskstat->write_merges = wr_merges;
1969             diskstat->has_write_ticks = true;
1970             diskstat->write_ticks = wr_ticks;
1971             diskstat->has_ios_pgr = true;
1972             diskstat->ios_pgr = ios_pgr;
1973             diskstat->has_total_ticks = true;
1974             diskstat->total_ticks = tot_ticks;
1975             diskstat->has_weight_ticks = true;
1976             diskstat->weight_ticks = rq_ticks;
1977         }
1978         if (i >= 18) {
1979             diskstat->has_discard_ios = true;
1980             diskstat->discard_ios = dc_ios;
1981             diskstat->has_discard_merges = true;
1982             diskstat->discard_merges = dc_merges;
1983             diskstat->has_discard_sectors = true;
1984             diskstat->discard_sectors = dc_sec;
1985             diskstat->has_discard_ticks = true;
1986             diskstat->discard_ticks = dc_ticks;
1987         }
1988         if (i >= 20) {
1989             diskstat->has_flush_ios = true;
1990             diskstat->flush_ios = fl_ios;
1991             diskstat->has_flush_ticks = true;
1992             diskstat->flush_ticks = fl_ticks;
1993         }
1994 
1995         diskstatinfo->stats = g_steal_pointer(&diskstat);
1996         QAPI_LIST_APPEND(tail, diskstatinfo);
1997         diskstatinfo = NULL;
1998     }
1999     free(line);
2000     fclose(fp);
2001     return head;
2002 }
2003 
2004 GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error **errp)
2005 {
2006     return guest_get_diskstats(errp);
2007 }
2008 
2009 GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp)
2010 {
2011     GuestCpuStatsList *head = NULL, **tail = &head;
2012     const char *cpustats = "/proc/stat";
2013     int clk_tck = sysconf(_SC_CLK_TCK);
2014     FILE *fp;
2015     size_t n;
2016     char *line = NULL;
2017 
2018     fp = fopen(cpustats, "r");
2019     if (fp  == NULL) {
2020         error_setg_errno(errp, errno, "open(\"%s\")", cpustats);
2021         return NULL;
2022     }
2023 
2024     while (getline(&line, &n, fp) != -1) {
2025         GuestCpuStats *cpustat = NULL;
2026         GuestLinuxCpuStats *linuxcpustat;
2027         int i;
2028         unsigned long user, system, idle, iowait, irq, softirq, steal, guest;
2029         unsigned long nice, guest_nice;
2030         char name[64];
2031 
2032         i = sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
2033                    name, &user, &nice, &system, &idle, &iowait, &irq, &softirq,
2034                    &steal, &guest, &guest_nice);
2035 
2036         /* drop "cpu 1 2 3 ...", get "cpuX 1 2 3 ..." only */
2037         if ((i == EOF) || strncmp(name, "cpu", 3) || (name[3] == '\0')) {
2038             continue;
2039         }
2040 
2041         if (i < 5) {
2042             slog("Parsing cpu stat from %s failed, see \"man proc\"", cpustats);
2043             break;
2044         }
2045 
2046         cpustat = g_new0(GuestCpuStats, 1);
2047         cpustat->type = GUEST_CPU_STATS_TYPE_LINUX;
2048 
2049         linuxcpustat = &cpustat->u.q_linux;
2050         linuxcpustat->cpu = atoi(&name[3]);
2051         linuxcpustat->user = user * 1000 / clk_tck;
2052         linuxcpustat->nice = nice * 1000 / clk_tck;
2053         linuxcpustat->system = system * 1000 / clk_tck;
2054         linuxcpustat->idle = idle * 1000 / clk_tck;
2055 
2056         if (i > 5) {
2057             linuxcpustat->has_iowait = true;
2058             linuxcpustat->iowait = iowait * 1000 / clk_tck;
2059         }
2060 
2061         if (i > 6) {
2062             linuxcpustat->has_irq = true;
2063             linuxcpustat->irq = irq * 1000 / clk_tck;
2064             linuxcpustat->has_softirq = true;
2065             linuxcpustat->softirq = softirq * 1000 / clk_tck;
2066         }
2067 
2068         if (i > 8) {
2069             linuxcpustat->has_steal = true;
2070             linuxcpustat->steal = steal * 1000 / clk_tck;
2071         }
2072 
2073         if (i > 9) {
2074             linuxcpustat->has_guest = true;
2075             linuxcpustat->guest = guest * 1000 / clk_tck;
2076         }
2077 
2078         if (i > 10) {
2079             linuxcpustat->has_guest = true;
2080             linuxcpustat->guest = guest * 1000 / clk_tck;
2081             linuxcpustat->has_guestnice = true;
2082             linuxcpustat->guestnice = guest_nice * 1000 / clk_tck;
2083         }
2084 
2085         QAPI_LIST_APPEND(tail, cpustat);
2086     }
2087 
2088     free(line);
2089     fclose(fp);
2090     return head;
2091 }
2092