Commit 099fca3225b39f7a3ed853036038054172b55581

Authored by Li Zefan
Committed by Linus Torvalds
1 parent b6719ec1ad

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: Paul Menage <menage@google.com>
Reviewed-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 3 changed files with 40 additions and 4 deletions Side-by-side Diff

include/linux/cgroup.h
... ... @@ -258,6 +258,11 @@
258 258 */
259 259 char name[MAX_CFTYPE_NAME];
260 260 int private;
  261 + /*
  262 + * If not 0, file mode is set to this value, otherwise it will
  263 + * be figured out automatically
  264 + */
  265 + mode_t mode;
261 266  
262 267 /*
263 268 * If non-zero, defines the maximum length of string that can
... ... @@ -1686,7 +1686,7 @@
1686 1686 .rename = cgroup_rename,
1687 1687 };
1688 1688  
1689   -static int cgroup_create_file(struct dentry *dentry, int mode,
  1689 +static int cgroup_create_file(struct dentry *dentry, mode_t mode,
1690 1690 struct super_block *sb)
1691 1691 {
1692 1692 static const struct dentry_operations cgroup_dops = {
... ... @@ -1732,7 +1732,7 @@
1732 1732 * @mode: mode to set on new directory.
1733 1733 */
1734 1734 static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
1735   - int mode)
  1735 + mode_t mode)
1736 1736 {
1737 1737 struct dentry *parent;
1738 1738 int error = 0;
... ... @@ -1750,6 +1750,33 @@
1750 1750 return error;
1751 1751 }
1752 1752  
  1753 +/**
  1754 + * cgroup_file_mode - deduce file mode of a control file
  1755 + * @cft: the control file in question
  1756 + *
  1757 + * returns cft->mode if ->mode is not 0
  1758 + * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  1759 + * returns S_IRUGO if it has only a read handler
  1760 + * returns S_IWUSR if it has only a write hander
  1761 + */
  1762 +static mode_t cgroup_file_mode(const struct cftype *cft)
  1763 +{
  1764 + mode_t mode = 0;
  1765 +
  1766 + if (cft->mode)
  1767 + return cft->mode;
  1768 +
  1769 + if (cft->read || cft->read_u64 || cft->read_s64 ||
  1770 + cft->read_map || cft->read_seq_string)
  1771 + mode |= S_IRUGO;
  1772 +
  1773 + if (cft->write || cft->write_u64 || cft->write_s64 ||
  1774 + cft->write_string || cft->trigger)
  1775 + mode |= S_IWUSR;
  1776 +
  1777 + return mode;
  1778 +}
  1779 +
1753 1780 int cgroup_add_file(struct cgroup *cgrp,
1754 1781 struct cgroup_subsys *subsys,
1755 1782 const struct cftype *cft)
... ... @@ -1757,6 +1784,7 @@
1757 1784 struct dentry *dir = cgrp->dentry;
1758 1785 struct dentry *dentry;
1759 1786 int error;
  1787 + mode_t mode;
1760 1788  
1761 1789 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
1762 1790 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
... ... @@ -1767,7 +1795,8 @@
1767 1795 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1768 1796 dentry = lookup_one_len(name, dir, strlen(name));
1769 1797 if (!IS_ERR(dentry)) {
1770   - error = cgroup_create_file(dentry, 0644 | S_IFREG,
  1798 + mode = cgroup_file_mode(cft);
  1799 + error = cgroup_create_file(dentry, mode | S_IFREG,
1771 1800 cgrp->root->sb);
1772 1801 if (!error)
1773 1802 dentry->d_fsdata = (void *)cft;
... ... @@ -2349,6 +2378,7 @@
2349 2378 .write_u64 = cgroup_tasks_write,
2350 2379 .release = cgroup_tasks_release,
2351 2380 .private = FILE_TASKLIST,
  2381 + .mode = S_IRUGO | S_IWUSR,
2352 2382 },
2353 2383  
2354 2384 {
... ... @@ -2449,7 +2479,7 @@
2449 2479 * Must be called with the mutex on the parent inode held
2450 2480 */
2451 2481 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2452   - int mode)
  2482 + mode_t mode)
2453 2483 {
2454 2484 struct cgroup *cgrp;
2455 2485 struct cgroupfs_root *root = parent->root;
... ... @@ -1706,6 +1706,7 @@
1706 1706 .read_u64 = cpuset_read_u64,
1707 1707 .write_u64 = cpuset_write_u64,
1708 1708 .private = FILE_MEMORY_PRESSURE,
  1709 + .mode = S_IRUGO,
1709 1710 },
1710 1711  
1711 1712 {