render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
7a3408
From 53d1fe4360d79d81864731234925d616a5646814 Mon Sep 17 00:00:00 2001
7a3408
Message-Id: <53d1fe4360d79d81864731234925d616a5646814@dist-git>
7a3408
From: Martin Kletzander <mkletzan@redhat.com>
7a3408
Date: Tue, 18 Aug 2015 17:27:59 -0700
7a3408
Subject: [PATCH] util: Add virCgroupGetBlockDevString
7a3408
7a3408
https://bugzilla.redhat.com/show_bug.cgi?id=1165580
7a3408
7a3408
This function translates device paths to "major:minor " string, and all
7a3408
virCgroupSetBlkioDevice* functions are modified to use it.  It's a
7a3408
cleanup with no functional change.
7a3408
7a3408
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
7a3408
(cherry picked from commit ea9db906fcf8425edad7ea49701c9b6495554b7f)
7a3408
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
7a3408
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
7a3408
---
7a3408
 src/util/vircgroup.c | 180 ++++++++++++++++++++-------------------------------
7a3408
 1 file changed, 70 insertions(+), 110 deletions(-)
7a3408
7a3408
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
7a3408
index 0599ba5..afa85de 100644
7a3408
--- a/src/util/vircgroup.c
7a3408
+++ b/src/util/vircgroup.c
7a3408
@@ -705,6 +705,35 @@ virCgroupDetect(virCgroupPtr group,
7a3408
 }
7a3408
 
7a3408
 
7a3408
+static char *
7a3408
+virCgroupGetBlockDevString(const char *path)
7a3408
+{
7a3408
+    char *ret = NULL;
7a3408
+    struct stat sb;
7a3408
+
7a3408
+    if (stat(path, &sb) < 0) {
7a3408
+        virReportSystemError(errno,
7a3408
+                             _("Path '%s' is not accessible"),
7a3408
+                             path);
7a3408
+        return NULL;
7a3408
+    }
7a3408
+
7a3408
+    if (!S_ISBLK(sb.st_mode)) {
7a3408
+        virReportSystemError(EINVAL,
7a3408
+                             _("Path '%s' must be a block device"),
7a3408
+                             path);
7a3408
+        return NULL;
7a3408
+    }
7a3408
+
7a3408
+    /* Automatically append space after the string since all callers
7a3408
+     * use it anyway */
7a3408
+    if (virAsprintf(&ret, "%d:%d ", major(sb.st_rdev), minor(sb.st_rdev)) < 0)
7a3408
+        return NULL;
7a3408
+
7a3408
+    return ret;
7a3408
+}
7a3408
+
7a3408
+
7a3408
 static int
7a3408
 virCgroupSetValueStr(virCgroupPtr group,
7a3408
                      int controller,
7a3408
@@ -1966,7 +1995,6 @@ virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group,
7a3408
                                   long long *requests_write)
7a3408
 {
7a3408
     char *str1 = NULL, *str2 = NULL, *str3 = NULL, *p1, *p2;
7a3408
-    struct stat sb;
7a3408
     size_t i;
7a3408
     int ret = -1;
7a3408
 
7a3408
@@ -1983,20 +2011,6 @@ virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group,
7a3408
         requests_write
7a3408
     };
7a3408
 
7a3408
-    if (stat(path, &sb) < 0) {
7a3408
-        virReportSystemError(errno,
7a3408
-                             _("Path '%s' is not accessible"),
7a3408
-                             path);
7a3408
-        return -1;
7a3408
-    }
7a3408
-
7a3408
-    if (!S_ISBLK(sb.st_mode)) {
7a3408
-        virReportSystemError(EINVAL,
7a3408
-                             _("Path '%s' must be a block device"),
7a3408
-                             path);
7a3408
-        return -1;
7a3408
-    }
7a3408
-
7a3408
     if (virCgroupGetValueStr(group,
7a3408
                              VIR_CGROUP_CONTROLLER_BLKIO,
7a3408
                              "blkio.throttle.io_service_bytes", &str1) < 0)
7a3408
@@ -2007,7 +2021,7 @@ virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group,
7a3408
                              "blkio.throttle.io_serviced", &str2) < 0)
7a3408
         goto cleanup;
7a3408
 
7a3408
-    if (virAsprintf(&str3, "%d:%d ", major(sb.st_rdev), minor(sb.st_rdev)) < 0)
7a3408
+    if (!(str3 = virCgroupGetBlockDevString(path)))
7a3408
         goto cleanup;
7a3408
 
7a3408
     if (!(p1 = strstr(str1, str3))) {
7a3408
@@ -2116,33 +2130,22 @@ virCgroupSetBlkioDeviceReadIops(virCgroupPtr group,
7a3408
                                 const char *path,
7a3408
                                 unsigned int riops)
7a3408
 {
7a3408
-    char *str;
7a3408
-    struct stat sb;
7a3408
-    int ret;
7a3408
+    char *str = NULL;
7a3408
+    char *blkstr = NULL;
7a3408
+    int ret = -1;
7a3408
 
7a3408
-    if (stat(path, &sb) < 0) {
7a3408
-        virReportSystemError(errno,
7a3408
-                             _("Path '%s' is not accessible"),
7a3408
-                             path);
7a3408
+    if (!(blkstr = virCgroupGetBlockDevString(path)))
7a3408
         return -1;
7a3408
-    }
7a3408
 
7a3408
-    if (!S_ISBLK(sb.st_mode)) {
7a3408
-        virReportSystemError(EINVAL,
7a3408
-                             _("Path '%s' must be a block device"),
7a3408
-                             path);
7a3408
-        return -1;
7a3408
-    }
7a3408
-
7a3408
-    if (virAsprintf(&str, "%d:%d %u", major(sb.st_rdev),
7a3408
-                    minor(sb.st_rdev), riops) < 0)
7a3408
-        return -1;
7a3408
+    if (virAsprintf(&str, "%s%u", blkstr, riops) < 0)
7a3408
+        goto error;
7a3408
 
7a3408
     ret = virCgroupSetValueStr(group,
7a3408
                                VIR_CGROUP_CONTROLLER_BLKIO,
7a3408
                                "blkio.throttle.read_iops_device",
7a3408
                                str);
7a3408
-
7a3408
+ error:
7a3408
+    VIR_FREE(blkstr);
7a3408
     VIR_FREE(str);
7a3408
     return ret;
7a3408
 }
7a3408
@@ -2161,33 +2164,22 @@ virCgroupSetBlkioDeviceWriteIops(virCgroupPtr group,
7a3408
                                  const char *path,
7a3408
                                  unsigned int wiops)
7a3408
 {
7a3408
-    char *str;
7a3408
-    struct stat sb;
7a3408
-    int ret;
7a3408
+    char *str = NULL;
7a3408
+    char *blkstr = NULL;
7a3408
+    int ret = -1;
7a3408
 
7a3408
-    if (stat(path, &sb) < 0) {
7a3408
-        virReportSystemError(errno,
7a3408
-                             _("Path '%s' is not accessible"),
7a3408
-                             path);
7a3408
+    if (!(blkstr = virCgroupGetBlockDevString(path)))
7a3408
         return -1;
7a3408
-    }
7a3408
 
7a3408
-    if (!S_ISBLK(sb.st_mode)) {
7a3408
-        virReportSystemError(EINVAL,
7a3408
-                             _("Path '%s' must be a block device"),
7a3408
-                             path);
7a3408
-        return -1;
7a3408
-    }
7a3408
-
7a3408
-    if (virAsprintf(&str, "%d:%d %u", major(sb.st_rdev),
7a3408
-                    minor(sb.st_rdev), wiops) < 0)
7a3408
-        return -1;
7a3408
+    if (virAsprintf(&str, "%s%u", blkstr, wiops) < 0)
7a3408
+        goto error;
7a3408
 
7a3408
     ret = virCgroupSetValueStr(group,
7a3408
                                VIR_CGROUP_CONTROLLER_BLKIO,
7a3408
                                "blkio.throttle.write_iops_device",
7a3408
                                str);
7a3408
-
7a3408
+ error:
7a3408
+    VIR_FREE(blkstr);
7a3408
     VIR_FREE(str);
7a3408
     return ret;
7a3408
 }
7a3408
@@ -2206,33 +2198,22 @@ virCgroupSetBlkioDeviceReadBps(virCgroupPtr group,
7a3408
                                const char *path,
7a3408
                                unsigned long long rbps)
7a3408
 {
7a3408
-    char *str;
7a3408
-    struct stat sb;
7a3408
-    int ret;
7a3408
+    char *str = NULL;
7a3408
+    char *blkstr = NULL;
7a3408
+    int ret = -1;
7a3408
 
7a3408
-    if (stat(path, &sb) < 0) {
7a3408
-        virReportSystemError(errno,
7a3408
-                             _("Path '%s' is not accessible"),
7a3408
-                             path);
7a3408
+    if (!(blkstr = virCgroupGetBlockDevString(path)))
7a3408
         return -1;
7a3408
-    }
7a3408
 
7a3408
-    if (!S_ISBLK(sb.st_mode)) {
7a3408
-        virReportSystemError(EINVAL,
7a3408
-                             _("Path '%s' must be a block device"),
7a3408
-                             path);
7a3408
-        return -1;
7a3408
-    }
7a3408
-
7a3408
-    if (virAsprintf(&str, "%d:%d %llu", major(sb.st_rdev),
7a3408
-                    minor(sb.st_rdev), rbps) < 0)
7a3408
-        return -1;
7a3408
+    if (virAsprintf(&str, "%s%llu", blkstr, rbps) < 0)
7a3408
+        goto error;
7a3408
 
7a3408
     ret = virCgroupSetValueStr(group,
7a3408
                                VIR_CGROUP_CONTROLLER_BLKIO,
7a3408
                                "blkio.throttle.read_bps_device",
7a3408
                                str);
7a3408
-
7a3408
+ error:
7a3408
+    VIR_FREE(blkstr);
7a3408
     VIR_FREE(str);
7a3408
     return ret;
7a3408
 }
7a3408
@@ -2250,33 +2231,22 @@ virCgroupSetBlkioDeviceWriteBps(virCgroupPtr group,
7a3408
                                 const char *path,
7a3408
                                 unsigned long long wbps)
7a3408
 {
7a3408
-    char *str;
7a3408
-    struct stat sb;
7a3408
-    int ret;
7a3408
+    char *str = NULL;
7a3408
+    char *blkstr = NULL;
7a3408
+    int ret = -1;
7a3408
 
7a3408
-    if (stat(path, &sb) < 0) {
7a3408
-        virReportSystemError(errno,
7a3408
-                             _("Path '%s' is not accessible"),
7a3408
-                             path);
7a3408
+    if (!(blkstr = virCgroupGetBlockDevString(path)))
7a3408
         return -1;
7a3408
-    }
7a3408
 
7a3408
-    if (!S_ISBLK(sb.st_mode)) {
7a3408
-        virReportSystemError(EINVAL,
7a3408
-                             _("Path '%s' must be a block device"),
7a3408
-                             path);
7a3408
-        return -1;
7a3408
-    }
7a3408
-
7a3408
-    if (virAsprintf(&str, "%d:%d %llu", major(sb.st_rdev),
7a3408
-                    minor(sb.st_rdev), wbps) < 0)
7a3408
-        return -1;
7a3408
+    if (virAsprintf(&str, "%s%llu", blkstr, wbps) < 0)
7a3408
+        goto error;
7a3408
 
7a3408
     ret = virCgroupSetValueStr(group,
7a3408
                                VIR_CGROUP_CONTROLLER_BLKIO,
7a3408
                                "blkio.throttle.write_bps_device",
7a3408
                                str);
7a3408
-
7a3408
+ error:
7a3408
+    VIR_FREE(blkstr);
7a3408
     VIR_FREE(str);
7a3408
     return ret;
7a3408
 }
7a3408
@@ -2299,32 +2269,22 @@ virCgroupSetBlkioDeviceWeight(virCgroupPtr group,
7a3408
                               const char *path,
7a3408
                               unsigned int weight)
7a3408
 {
7a3408
-    char *str;
7a3408
-    struct stat sb;
7a3408
-    int ret;
7a3408
+    char *str = NULL;
7a3408
+    char *blkstr = NULL;
7a3408
+    int ret = -1;
7a3408
 
7a3408
-    if (stat(path, &sb) < 0) {
7a3408
-        virReportSystemError(errno,
7a3408
-                             _("Path '%s' is not accessible"),
7a3408
-                             path);
7a3408
+    if (!(blkstr = virCgroupGetBlockDevString(path)))
7a3408
         return -1;
7a3408
-    }
7a3408
 
7a3408
-    if (!S_ISBLK(sb.st_mode)) {
7a3408
-        virReportSystemError(EINVAL,
7a3408
-                             _("Path '%s' must be a block device"),
7a3408
-                             path);
7a3408
-        return -1;
7a3408
-    }
7a3408
-
7a3408
-    if (virAsprintf(&str, "%d:%d %d", major(sb.st_rdev), minor(sb.st_rdev),
7a3408
-                    weight) < 0)
7a3408
-        return -1;
7a3408
+    if (virAsprintf(&str, "%s%d", blkstr, weight) < 0)
7a3408
+        goto error;
7a3408
 
7a3408
     ret = virCgroupSetValueStr(group,
7a3408
                                VIR_CGROUP_CONTROLLER_BLKIO,
7a3408
                                "blkio.weight_device",
7a3408
                                str);
7a3408
+ error:
7a3408
+    VIR_FREE(blkstr);
7a3408
     VIR_FREE(str);
7a3408
     return ret;
7a3408
 }
7a3408
-- 
7a3408
2.5.1
7a3408