Commit 099fca32 authored by Li Zefan's avatar Li Zefan Committed by Linus Torvalds

cgroups: show correct file mode

We have some read-only files and write-only files, but currently they are
all set to 0644, which is counter-intuitive and cause trouble for some
cgroup tools like libcgroup.

This patch adds 'mode' to struct cftype to allow cgroup subsys to set it's
own files' file mode, and for the most cases cft->mode can be default to 0
and cgroup will figure out proper mode.
Acked-by: default avatarPaul Menage <menage@google.com>
Reviewed-by: default avatarKAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Signed-off-by: default avatarLi Zefan <lizf@cn.fujitsu.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent b6719ec1
...@@ -258,6 +258,11 @@ struct cftype { ...@@ -258,6 +258,11 @@ struct cftype {
*/ */
char name[MAX_CFTYPE_NAME]; char name[MAX_CFTYPE_NAME];
int private; int private;
/*
* If not 0, file mode is set to this value, otherwise it will
* be figured out automatically
*/
mode_t mode;
/* /*
* If non-zero, defines the maximum length of string that can * If non-zero, defines the maximum length of string that can
......
...@@ -1686,7 +1686,7 @@ static struct inode_operations cgroup_dir_inode_operations = { ...@@ -1686,7 +1686,7 @@ static struct inode_operations cgroup_dir_inode_operations = {
.rename = cgroup_rename, .rename = cgroup_rename,
}; };
static int cgroup_create_file(struct dentry *dentry, int mode, static int cgroup_create_file(struct dentry *dentry, mode_t mode,
struct super_block *sb) struct super_block *sb)
{ {
static const struct dentry_operations cgroup_dops = { static const struct dentry_operations cgroup_dops = {
...@@ -1732,7 +1732,7 @@ static int cgroup_create_file(struct dentry *dentry, int mode, ...@@ -1732,7 +1732,7 @@ static int cgroup_create_file(struct dentry *dentry, int mode,
* @mode: mode to set on new directory. * @mode: mode to set on new directory.
*/ */
static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry, static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
int mode) mode_t mode)
{ {
struct dentry *parent; struct dentry *parent;
int error = 0; int error = 0;
...@@ -1750,6 +1750,33 @@ static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry, ...@@ -1750,6 +1750,33 @@ static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
return error; return error;
} }
/**
* cgroup_file_mode - deduce file mode of a control file
* @cft: the control file in question
*
* returns cft->mode if ->mode is not 0
* returns S_IRUGO|S_IWUSR if it has both a read and a write handler
* returns S_IRUGO if it has only a read handler
* returns S_IWUSR if it has only a write hander
*/
static mode_t cgroup_file_mode(const struct cftype *cft)
{
mode_t mode = 0;
if (cft->mode)
return cft->mode;
if (cft->read || cft->read_u64 || cft->read_s64 ||
cft->read_map || cft->read_seq_string)
mode |= S_IRUGO;
if (cft->write || cft->write_u64 || cft->write_s64 ||
cft->write_string || cft->trigger)
mode |= S_IWUSR;
return mode;
}
int cgroup_add_file(struct cgroup *cgrp, int cgroup_add_file(struct cgroup *cgrp,
struct cgroup_subsys *subsys, struct cgroup_subsys *subsys,
const struct cftype *cft) const struct cftype *cft)
...@@ -1757,6 +1784,7 @@ int cgroup_add_file(struct cgroup *cgrp, ...@@ -1757,6 +1784,7 @@ int cgroup_add_file(struct cgroup *cgrp,
struct dentry *dir = cgrp->dentry; struct dentry *dir = cgrp->dentry;
struct dentry *dentry; struct dentry *dentry;
int error; int error;
mode_t mode;
char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) { if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
...@@ -1767,7 +1795,8 @@ int cgroup_add_file(struct cgroup *cgrp, ...@@ -1767,7 +1795,8 @@ int cgroup_add_file(struct cgroup *cgrp,
BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
dentry = lookup_one_len(name, dir, strlen(name)); dentry = lookup_one_len(name, dir, strlen(name));
if (!IS_ERR(dentry)) { if (!IS_ERR(dentry)) {
error = cgroup_create_file(dentry, 0644 | S_IFREG, mode = cgroup_file_mode(cft);
error = cgroup_create_file(dentry, mode | S_IFREG,
cgrp->root->sb); cgrp->root->sb);
if (!error) if (!error)
dentry->d_fsdata = (void *)cft; dentry->d_fsdata = (void *)cft;
...@@ -2349,6 +2378,7 @@ static struct cftype files[] = { ...@@ -2349,6 +2378,7 @@ static struct cftype files[] = {
.write_u64 = cgroup_tasks_write, .write_u64 = cgroup_tasks_write,
.release = cgroup_tasks_release, .release = cgroup_tasks_release,
.private = FILE_TASKLIST, .private = FILE_TASKLIST,
.mode = S_IRUGO | S_IWUSR,
}, },
{ {
...@@ -2449,7 +2479,7 @@ static void cgroup_unlock_hierarchy(struct cgroupfs_root *root) ...@@ -2449,7 +2479,7 @@ static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
* Must be called with the mutex on the parent inode held * Must be called with the mutex on the parent inode held
*/ */
static long cgroup_create(struct cgroup *parent, struct dentry *dentry, static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
int mode) mode_t mode)
{ {
struct cgroup *cgrp; struct cgroup *cgrp;
struct cgroupfs_root *root = parent->root; struct cgroupfs_root *root = parent->root;
......
...@@ -1706,6 +1706,7 @@ static struct cftype files[] = { ...@@ -1706,6 +1706,7 @@ static struct cftype files[] = {
.read_u64 = cpuset_read_u64, .read_u64 = cpuset_read_u64,
.write_u64 = cpuset_write_u64, .write_u64 = cpuset_write_u64,
.private = FILE_MEMORY_PRESSURE, .private = FILE_MEMORY_PRESSURE,
.mode = S_IRUGO,
}, },
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment