cgroup: let a symlink too be created with a cftype file
authorAngelo Ruocco <angeloruocco90@gmail.com>
Tue, 21 May 2019 08:01:54 +0000 (10:01 +0200)
committerJens Axboe <axboe@kernel.dk>
Fri, 7 Jun 2019 07:29:39 +0000 (01:29 -0600)
This commit enables a cftype to have a symlink (of any name) that
points to the file associated with the cftype.

Signed-off-by: Angelo Ruocco <angeloruocco90@gmail.com>
Signed-off-by: Paolo Valente <paolo.valente@linaro.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/linux/cgroup-defs.h
kernel/cgroup/cgroup.c

index 11e215d7937e61efbf6dd229275528bb75f4b70f..d71b079bb021a22621c2f5cbe484ab9b499db998 100644 (file)
@@ -106,6 +106,8 @@ enum {
        CFTYPE_WORLD_WRITABLE   = (1 << 4),     /* (DON'T USE FOR NEW FILES) S_IWUGO */
        CFTYPE_DEBUG            = (1 << 5),     /* create when cgroup_debug */
 
+       CFTYPE_SYMLINKED        = (1 << 6),     /* pointed to by symlink too */
+
        /* internal flags, do not use outside cgroup core proper */
        __CFTYPE_ONLY_ON_DFL    = (1 << 16),    /* only on default hierarchy */
        __CFTYPE_NOT_ON_DFL     = (1 << 17),    /* not on default hierarchy */
@@ -543,6 +545,7 @@ struct cftype {
         * end of cftype array.
         */
        char name[MAX_CFTYPE_NAME];
+       char link_name[MAX_CFTYPE_NAME];
        unsigned long private;
 
        /*
index 426a0026225c29085d8e09c7f4920c6edbd216be..155048b0eca2c34913bf009d9573ba53a10f8011 100644 (file)
@@ -1460,8 +1460,8 @@ struct cgroup *task_cgroup_from_root(struct task_struct *task,
 
 static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
 
-static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
-                             char *buf)
+static char *cgroup_fill_name(struct cgroup *cgrp, const struct cftype *cft,
+                             char *buf, bool write_link_name)
 {
        struct cgroup_subsys *ss = cft->ss;
 
@@ -1471,13 +1471,26 @@ static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
 
                snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
                         dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
-                        cft->name);
+                        write_link_name ? cft->link_name : cft->name);
        } else {
-               strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
+               strscpy(buf, write_link_name ? cft->link_name : cft->name,
+                       CGROUP_FILE_NAME_MAX);
        }
        return buf;
 }
 
+static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
+                             char *buf)
+{
+       return cgroup_fill_name(cgrp, cft, buf, false);
+}
+
+static char *cgroup_link_name(struct cgroup *cgrp, const struct cftype *cft,
+                             char *buf)
+{
+       return cgroup_fill_name(cgrp, cft, buf, true);
+}
+
 /**
  * cgroup_file_mode - deduce file mode of a control file
  * @cft: the control file in question
@@ -1636,6 +1649,9 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
        }
 
        kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
+       if (cft->flags & CFTYPE_SYMLINKED)
+               kernfs_remove_by_name(cgrp->kn,
+                                     cgroup_link_name(cgrp, cft, name));
 }
 
 /**
@@ -3821,6 +3837,7 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 {
        char name[CGROUP_FILE_NAME_MAX];
        struct kernfs_node *kn;
+       struct kernfs_node *kn_link;
        struct lock_class_key *key = NULL;
        int ret;
 
@@ -3851,6 +3868,14 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
                spin_unlock_irq(&cgroup_file_kn_lock);
        }
 
+       if (cft->flags & CFTYPE_SYMLINKED) {
+               kn_link = kernfs_create_link(cgrp->kn,
+                                            cgroup_link_name(cgrp, cft, name),
+                                            kn);
+               if (IS_ERR(kn_link))
+                       return PTR_ERR(kn_link);
+       }
+
        return 0;
 }