util: Add virCgroupGetBlockDevString

This function translates device paths to "major:minor " string, and all
virCgroupSetBlkioDevice* functions are modified to use it.  It's a
cleanup with no functional change.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Martin Kletzander 2015-08-03 14:44:14 +02:00
parent 1fe69c4656
commit ea9db906fc

View File

@ -705,6 +705,35 @@ virCgroupDetect(virCgroupPtr group,
} }
static char *
virCgroupGetBlockDevString(const char *path)
{
char *ret = NULL;
struct stat sb;
if (stat(path, &sb) < 0) {
virReportSystemError(errno,
_("Path '%s' is not accessible"),
path);
return NULL;
}
if (!S_ISBLK(sb.st_mode)) {
virReportSystemError(EINVAL,
_("Path '%s' must be a block device"),
path);
return NULL;
}
/* Automatically append space after the string since all callers
* use it anyway */
if (virAsprintf(&ret, "%d:%d ", major(sb.st_rdev), minor(sb.st_rdev)) < 0)
return NULL;
return ret;
}
static int static int
virCgroupSetValueStr(virCgroupPtr group, virCgroupSetValueStr(virCgroupPtr group,
int controller, int controller,
@ -1966,7 +1995,6 @@ virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group,
long long *requests_write) long long *requests_write)
{ {
char *str1 = NULL, *str2 = NULL, *str3 = NULL, *p1, *p2; char *str1 = NULL, *str2 = NULL, *str3 = NULL, *p1, *p2;
struct stat sb;
size_t i; size_t i;
int ret = -1; int ret = -1;
@ -1983,20 +2011,6 @@ virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group,
requests_write requests_write
}; };
if (stat(path, &sb) < 0) {
virReportSystemError(errno,
_("Path '%s' is not accessible"),
path);
return -1;
}
if (!S_ISBLK(sb.st_mode)) {
virReportSystemError(EINVAL,
_("Path '%s' must be a block device"),
path);
return -1;
}
if (virCgroupGetValueStr(group, if (virCgroupGetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
"blkio.throttle.io_service_bytes", &str1) < 0) "blkio.throttle.io_service_bytes", &str1) < 0)
@ -2007,7 +2021,7 @@ virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group,
"blkio.throttle.io_serviced", &str2) < 0) "blkio.throttle.io_serviced", &str2) < 0)
goto cleanup; goto cleanup;
if (virAsprintf(&str3, "%d:%d ", major(sb.st_rdev), minor(sb.st_rdev)) < 0) if (!(str3 = virCgroupGetBlockDevString(path)))
goto cleanup; goto cleanup;
if (!(p1 = strstr(str1, str3))) { if (!(p1 = strstr(str1, str3))) {
@ -2116,33 +2130,22 @@ virCgroupSetBlkioDeviceReadIops(virCgroupPtr group,
const char *path, const char *path,
unsigned int riops) unsigned int riops)
{ {
char *str; char *str = NULL;
struct stat sb; char *blkstr = NULL;
int ret; int ret = -1;
if (stat(path, &sb) < 0) { if (!(blkstr = virCgroupGetBlockDevString(path)))
virReportSystemError(errno,
_("Path '%s' is not accessible"),
path);
return -1; return -1;
}
if (!S_ISBLK(sb.st_mode)) { if (virAsprintf(&str, "%s%u", blkstr, riops) < 0)
virReportSystemError(EINVAL, goto error;
_("Path '%s' must be a block device"),
path);
return -1;
}
if (virAsprintf(&str, "%d:%d %u", major(sb.st_rdev),
minor(sb.st_rdev), riops) < 0)
return -1;
ret = virCgroupSetValueStr(group, ret = virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
"blkio.throttle.read_iops_device", "blkio.throttle.read_iops_device",
str); str);
error:
VIR_FREE(blkstr);
VIR_FREE(str); VIR_FREE(str);
return ret; return ret;
} }
@ -2161,33 +2164,22 @@ virCgroupSetBlkioDeviceWriteIops(virCgroupPtr group,
const char *path, const char *path,
unsigned int wiops) unsigned int wiops)
{ {
char *str; char *str = NULL;
struct stat sb; char *blkstr = NULL;
int ret; int ret = -1;
if (stat(path, &sb) < 0) { if (!(blkstr = virCgroupGetBlockDevString(path)))
virReportSystemError(errno,
_("Path '%s' is not accessible"),
path);
return -1; return -1;
}
if (!S_ISBLK(sb.st_mode)) { if (virAsprintf(&str, "%s%u", blkstr, wiops) < 0)
virReportSystemError(EINVAL, goto error;
_("Path '%s' must be a block device"),
path);
return -1;
}
if (virAsprintf(&str, "%d:%d %u", major(sb.st_rdev),
minor(sb.st_rdev), wiops) < 0)
return -1;
ret = virCgroupSetValueStr(group, ret = virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
"blkio.throttle.write_iops_device", "blkio.throttle.write_iops_device",
str); str);
error:
VIR_FREE(blkstr);
VIR_FREE(str); VIR_FREE(str);
return ret; return ret;
} }
@ -2206,33 +2198,22 @@ virCgroupSetBlkioDeviceReadBps(virCgroupPtr group,
const char *path, const char *path,
unsigned long long rbps) unsigned long long rbps)
{ {
char *str; char *str = NULL;
struct stat sb; char *blkstr = NULL;
int ret; int ret = -1;
if (stat(path, &sb) < 0) { if (!(blkstr = virCgroupGetBlockDevString(path)))
virReportSystemError(errno,
_("Path '%s' is not accessible"),
path);
return -1; return -1;
}
if (!S_ISBLK(sb.st_mode)) { if (virAsprintf(&str, "%s%llu", blkstr, rbps) < 0)
virReportSystemError(EINVAL, goto error;
_("Path '%s' must be a block device"),
path);
return -1;
}
if (virAsprintf(&str, "%d:%d %llu", major(sb.st_rdev),
minor(sb.st_rdev), rbps) < 0)
return -1;
ret = virCgroupSetValueStr(group, ret = virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
"blkio.throttle.read_bps_device", "blkio.throttle.read_bps_device",
str); str);
error:
VIR_FREE(blkstr);
VIR_FREE(str); VIR_FREE(str);
return ret; return ret;
} }
@ -2250,33 +2231,22 @@ virCgroupSetBlkioDeviceWriteBps(virCgroupPtr group,
const char *path, const char *path,
unsigned long long wbps) unsigned long long wbps)
{ {
char *str; char *str = NULL;
struct stat sb; char *blkstr = NULL;
int ret; int ret = -1;
if (stat(path, &sb) < 0) { if (!(blkstr = virCgroupGetBlockDevString(path)))
virReportSystemError(errno,
_("Path '%s' is not accessible"),
path);
return -1; return -1;
}
if (!S_ISBLK(sb.st_mode)) { if (virAsprintf(&str, "%s%llu", blkstr, wbps) < 0)
virReportSystemError(EINVAL, goto error;
_("Path '%s' must be a block device"),
path);
return -1;
}
if (virAsprintf(&str, "%d:%d %llu", major(sb.st_rdev),
minor(sb.st_rdev), wbps) < 0)
return -1;
ret = virCgroupSetValueStr(group, ret = virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
"blkio.throttle.write_bps_device", "blkio.throttle.write_bps_device",
str); str);
error:
VIR_FREE(blkstr);
VIR_FREE(str); VIR_FREE(str);
return ret; return ret;
} }
@ -2299,32 +2269,22 @@ virCgroupSetBlkioDeviceWeight(virCgroupPtr group,
const char *path, const char *path,
unsigned int weight) unsigned int weight)
{ {
char *str; char *str = NULL;
struct stat sb; char *blkstr = NULL;
int ret; int ret = -1;
if (stat(path, &sb) < 0) { if (!(blkstr = virCgroupGetBlockDevString(path)))
virReportSystemError(errno,
_("Path '%s' is not accessible"),
path);
return -1; return -1;
}
if (!S_ISBLK(sb.st_mode)) { if (virAsprintf(&str, "%s%d", blkstr, weight) < 0)
virReportSystemError(EINVAL, goto error;
_("Path '%s' must be a block device"),
path);
return -1;
}
if (virAsprintf(&str, "%d:%d %d", major(sb.st_rdev), minor(sb.st_rdev),
weight) < 0)
return -1;
ret = virCgroupSetValueStr(group, ret = virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
"blkio.weight_device", "blkio.weight_device",
str); str);
error:
VIR_FREE(blkstr);
VIR_FREE(str); VIR_FREE(str);
return ret; return ret;
} }