chiark / gitweb /
cgroup: beef up device lookup logic for block devices
authorLennart Poettering <lennart@poettering.net>
Mon, 11 Jun 2018 10:17:32 +0000 (12:17 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
Let's chase block devices through btrfs and LUKS like we do elsewhere.

src/core/cgroup.c

index 0a77d3ca82a1ad19887ddd99103e02dba6b1135b..1196b5257b4e0b7b7429ef7e9eb6e32e5aaa87b1 100644 (file)
@@ -11,6 +11,7 @@
 #include "alloc-util.h"
 //#include "blockdev-util.h"
 //#include "bpf-firewall.h"
+//#include "btrfs-util.h"
 //#include "bus-error.h"
 #include "cgroup-util.h"
 #include "cgroup.h"
@@ -307,30 +308,36 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
         }
 }
 
-static int lookup_block_device(const char *p, dev_t *dev) {
+static int lookup_block_device(const char *p, dev_t *ret) {
         struct stat st;
+        int r;
 
         assert(p);
-        assert(dev);
+        assert(ret);
 
         if (stat(p, &st) < 0)
-                return log_warning_errno(errno, "Couldn't stat device %s: %m", p);
+                return log_warning_errno(errno, "Couldn't stat device '%s': %m", p);
 
         if (S_ISBLK(st.st_mode))
-                *dev = 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 */
-                *dev = st.st_dev;
-
-                /* If this is a partition, try to get the originating
-                 * block device */
-                (void) block_get_whole_disk(*dev, dev);
-        } else {
-                log_warning("%s is not a block device and file system block device cannot be determined or is not local.", p);
-                return -ENODEV;
+                *ret = st.st_rdev;
+        else if (major(st.st_dev) != 0)
+                *ret = st.st_dev; /* If this is not a device node then use the block device this file is stored on */
+        else {
+                /* If this is btrfs, getting the backing block device is a bit harder */
+                r = btrfs_get_block_device(p, ret);
+                if (r < 0 && r != -ENOTTY)
+                        return log_warning_errno(r, "Failed to determine block device backing btrfs file system '%s': %m", p);
+                if (r == -ENOTTY) {
+                        log_warning("'%s' is not a block device node, and file system block device cannot be determined or is not local.", p);
+                        return -ENODEV;
+                }
         }
 
+        /* If this is a LUKS device, try to get the originating block device */
+        (void) block_get_originating(*ret, ret);
+
+        /* If this is a partition, try to get the originating block device */
+        (void) block_get_whole_disk(*ret, ret);
         return 0;
 }
 
@@ -1454,7 +1461,6 @@ static int unit_create_cgroup(
 
         CGroupContext *c;
         int r;
-        bool created;
 
         assert(u);
 
@@ -1471,20 +1477,14 @@ static int unit_create_cgroup(
         r = cg_create_everywhere(u->manager->cgroup_supported, target_mask, u->cgroup_path);
         if (r < 0)
                 return log_unit_error_errno(u, r, "Failed to create cgroup %s: %m", u->cgroup_path);
-        created = !!r;
 
         /* Start watching it */
         (void) unit_watch_cgroup(u);
 
-        /* Preserve enabled controllers in delegated units, adjust others. */
-        if (created || !unit_cgroup_delegate(u)) {
-
-                /* Enable all controllers we need */
-                r = cg_enable_everywhere(u->manager->cgroup_supported, enable_mask, u->cgroup_path);
-                if (r < 0)
-                        log_unit_warning_errno(u, r, "Failed to enable controllers on cgroup %s, ignoring: %m",
-                                               u->cgroup_path);
-        }
+        /* Enable all controllers we need */
+        r = cg_enable_everywhere(u->manager->cgroup_supported, enable_mask, u->cgroup_path);
+        if (r < 0)
+                log_unit_warning_errno(u, r, "Failed to enable controllers on cgroup %s, ignoring: %m", u->cgroup_path);
 
         /* Keep track that this is now realized */
         u->cgroup_realized = true;