chiark / gitweb /
exec: allow passing arbitrary path names to blkio cgroup attributes
authorLennart Poettering <lennart@poettering.net>
Sat, 20 Aug 2011 22:28:30 +0000 (00:28 +0200)
committerLennart Poettering <lennart@poettering.net>
Sun, 21 Aug 2011 18:07:45 +0000 (20:07 +0200)
If a device node is specified, then adjust the bandwidth/weight of it,
otherwise find the backing block device of the file system the path
refers to and adjust its bandwidth/weight.

man/systemd.exec.xml
src/load-fragment.c
src/util.c
src/util.h

index 6bc8bf3e7991a3c649f15f766442f4434273ae8c..ce6833b8438d6b97710c3ab2ca119f97dc03d31b 100644 (file)
                                 processes. Takes either a single
                                 weight value (between 10 and 1000) to
                                 set the default block IO weight, or a
-                                space separated pair of a device node
-                                path and a weight value to specify the
+                                space separated pair of a file path
+                                and a weight value to specify the
                                 device specific weight value (Example:
-                                "/dev/sda 500"). This controls the
+                                "/dev/sda 500"). The file path may be
+                                specified as path to a block device
+                                node or as any other file in which
+                                case the backing block device of the
+                                file system of the file is
+                                determined. This controls the
                                 <literal>blkio.weight</literal> and
                                 <literal>blkio.weight_device</literal>
                                 control group attributes, which
                                 <term><varname>BlockIOWriteBandwidth=</varname></term>
 
                                 <listitem><para>Set the per-device
-                                overall block IO bandwith limit for the
-                                executed processes. Takes a space
-                                separated pair of a device node path
-                                and a bandwith value (in bytes per
-                                second) to specify the device specific
-                                bandwidth. If the bandwith is suffixed
-                                with K, M, G, or T the specified
-                                bandwith is parsed as Kilobytes,
-                                Megabytes, Gigabytes, resp. Terabytes
-                                (Example: "/dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0 5M"). This
-                                controls the
+                                overall block IO bandwith limit for
+                                the executed processes. Takes a space
+                                separated pair of a file path and a
+                                bandwith value (in bytes per second)
+                                to specify the device specific
+                                bandwidth. The file path may be
+                                specified as path to a block device
+                                node or as any other file in which
+                                case the backing block device of the
+                                file system of the file is determined.
+                                If the bandwith is suffixed with K, M,
+                                G, or T the specified bandwith is
+                                parsed as Kilobytes, Megabytes,
+                                Gigabytes, resp. Terabytes (Example:
+                                "/dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0
+                                5M"). This controls the
                                 <literal>blkio.read_bps_device</literal>
                                 and
                                 <literal>blkio.write_bps_device</literal>
index b122ea419cfe907235cb930ac26ffea54caba5d8..c8b4b5a9c6f552024ac23febcab29ed9ff1b4c0b 100644 (file)
@@ -1843,6 +1843,7 @@ int config_parse_unit_device_allow(const char *filename, unsigned line, const ch
 static int blkio_map(const char *controller, const char *name, const char *value, char **ret) {
         struct stat st;
         char **l;
+        dev_t d;
 
         assert(controller);
         assert(name);
@@ -1861,13 +1862,23 @@ static int blkio_map(const char *controller, const char *name, const char *value
                 return -errno;
         }
 
-        if (!S_ISBLK(st.st_mode)) {
-                log_warning("%s is not a block device.", l[0]);
+        if (S_ISBLK(st.st_mode))
+                d = st.st_rdev;
+        else if (major(st.st_dev) != 0) {
+                /* If this is not a device node then find the block
+                 * device this file is stored on */
+                d = st.st_dev;
+
+                /* If this is a partition, try to get the originating
+                 * block device */
+                block_get_whole_disk(d, &d);
+        } else {
+                log_warning("%s is not a block device and file system block device cannot be determined or is not local.", l[0]);
                 strv_free(l);
                 return -ENODEV;
         }
 
-        if (asprintf(ret, "%u:%u %s", major(st.st_rdev), minor(st.st_rdev), l[1]) < 0) {
+        if (asprintf(ret, "%u:%u %s", major(d), minor(d), l[1]) < 0) {
                 strv_free(l);
                 return -ENOMEM;
         }
@@ -1907,7 +1918,7 @@ int config_parse_unit_blkio_weight(const char *filename, unsigned line, const ch
                 weight = l[1];
         }
 
-        if (device && !path_startswith(device, "/dev/")) {
+        if (device && !path_is_absolute(device)) {
                 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
                 strv_free(l);
                 return 0;
@@ -1965,7 +1976,7 @@ int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const
                 return 0;
         }
 
-        if (!path_startswith(l[0], "/dev/")) {
+        if (!path_is_absolute(l[0])) {
                 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
                 strv_free(l);
                 return 0;
index c24c749a5a6696996cb1da4fa9f6fcb9ccd4e804..017b99589735de53a459b9c06c30cc8c7b4e109e 100644 (file)
@@ -5614,6 +5614,67 @@ bool is_main_thread(void) {
         return cached > 0;
 }
 
+int block_get_whole_disk(dev_t d, dev_t *ret) {
+        char *p, *s;
+        int r;
+        unsigned n, m;
+
+        assert(ret);
+
+        /* If it has a queue this is good enough for us */
+        if (asprintf(&p, "/sys/dev/block/%u:%u/queue", major(d), minor(d)) < 0)
+                return -ENOMEM;
+
+        r = access(p, F_OK);
+        free(p);
+
+        if (r >= 0) {
+                *ret = d;
+                return 0;
+        }
+
+        /* If it is a partition find the originating device */
+        if (asprintf(&p, "/sys/dev/block/%u:%u/partition", major(d), minor(d)) < 0)
+                return -ENOMEM;
+
+        r = access(p, F_OK);
+        free(p);
+
+        if (r < 0)
+                return -ENOENT;
+
+        /* Get parent dev_t */
+        if (asprintf(&p, "/sys/dev/block/%u:%u/../dev", major(d), minor(d)) < 0)
+                return -ENOMEM;
+
+        r = read_one_line_file(p, &s);
+        free(p);
+
+        if (r < 0)
+                return r;
+
+        r = sscanf(s, "%u:%u", &m, &n);
+        free(s);
+
+        if (r != 2)
+                return -EINVAL;
+
+        /* Only return this if it is really good enough for us. */
+        if (asprintf(&p, "/sys/dev/block/%u:%u/queue", m, n) < 0)
+                return -ENOMEM;
+
+        r = access(p, F_OK);
+        free(p);
+
+        if (r >= 0) {
+                *ret = makedev(m, n);
+                return 0;
+        }
+
+        return -ENOENT;
+}
+
+
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",
index 54873e648e1006949abbba3a9c27af300198c630..e23f309d3d499e51bc2a9c1e40f605e2fd70f990 100644 (file)
@@ -465,6 +465,8 @@ bool is_main_thread(void);
 
 bool in_charset(const char *s, const char* charset);
 
+int block_get_whole_disk(dev_t d, dev_t *ret);
+
 #define NULSTR_FOREACH(i, l)                                    \
         for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)