2018-01-22 15:18:13 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2007-09-20 08:31:38 +00:00
|
|
|
* fs/sysfs/file.c - sysfs regular (text) file implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001-3 Patrick Mochel
|
|
|
|
* Copyright (c) 2007 SUSE Linux Products GmbH
|
|
|
|
* Copyright (c) 2007 Tejun Heo <teheo@suse.de>
|
|
|
|
*
|
2020-04-14 16:48:37 +00:00
|
|
|
* Please see Documentation/filesystems/sysfs.rst for more information.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kobject.h>
|
2008-03-14 02:41:52 +00:00
|
|
|
#include <linux/slab.h>
|
2006-12-20 09:52:44 +00:00
|
|
|
#include <linux/list.h>
|
2007-07-26 11:03:54 +00:00
|
|
|
#include <linux/mutex.h>
|
2013-10-01 21:42:02 +00:00
|
|
|
#include <linux/seq_file.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#include "sysfs.h"
|
2013-11-28 19:54:21 +00:00
|
|
|
|
2013-10-01 21:41:57 +00:00
|
|
|
/*
|
2013-12-11 19:11:53 +00:00
|
|
|
* Determine ktype->sysfs_ops for the given kernfs_node. This function
|
2013-10-01 21:41:57 +00:00
|
|
|
* must be called while holding an active reference.
|
|
|
|
*/
|
2013-12-11 19:11:53 +00:00
|
|
|
static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
|
2013-10-01 21:41:57 +00:00
|
|
|
{
|
2013-12-11 19:11:54 +00:00
|
|
|
struct kobject *kobj = kn->parent->priv;
|
2013-10-01 21:41:57 +00:00
|
|
|
|
2013-12-11 19:11:56 +00:00
|
|
|
if (kn->flags & KERNFS_LOCKDEP)
|
2013-12-11 19:11:53 +00:00
|
|
|
lockdep_assert_held(kn);
|
2013-10-01 21:41:57 +00:00
|
|
|
return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-01 21:42:02 +00:00
|
|
|
/*
|
|
|
|
* Reads on sysfs are handled through seq_file, which takes care of hairy
|
|
|
|
* details like buffering and seeking. The following function pipes
|
|
|
|
* sysfs_ops->show() result through seq_file.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2013-11-28 19:54:16 +00:00
|
|
|
static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-12-11 19:11:55 +00:00
|
|
|
struct kernfs_open_file *of = sf->private;
|
2013-12-11 19:11:54 +00:00
|
|
|
struct kobject *kobj = of->kn->parent->priv;
|
2013-12-11 19:11:53 +00:00
|
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
|
2005-04-16 22:20:36 +00:00
|
|
|
ssize_t count;
|
2013-11-28 19:54:16 +00:00
|
|
|
char *buf;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2014-05-19 19:52:10 +00:00
|
|
|
/* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
|
2013-10-01 21:42:02 +00:00
|
|
|
count = seq_get_buf(sf, &buf);
|
|
|
|
if (count < PAGE_SIZE) {
|
|
|
|
seq_commit(sf, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-19 19:52:10 +00:00
|
|
|
memset(buf, 0, PAGE_SIZE);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-10-01 21:42:02 +00:00
|
|
|
/*
|
2013-11-28 19:54:16 +00:00
|
|
|
* Invoke show(). Control may reach here via seq file lseek even
|
|
|
|
* if @ops->show() isn't implemented.
|
2013-10-01 21:42:02 +00:00
|
|
|
*/
|
2013-11-28 19:54:16 +00:00
|
|
|
if (ops->show) {
|
2013-12-11 19:11:53 +00:00
|
|
|
count = ops->show(kobj, of->kn->priv, buf);
|
2013-11-28 19:54:16 +00:00
|
|
|
if (count < 0)
|
|
|
|
return count;
|
|
|
|
}
|
2007-06-13 18:45:16 +00:00
|
|
|
|
2007-11-21 22:55:19 +00:00
|
|
|
/*
|
|
|
|
* The code works fine with PAGE_SIZE return but it's likely to
|
|
|
|
* indicate truncated result or overflow in normal use cases.
|
|
|
|
*/
|
2008-03-04 23:09:07 +00:00
|
|
|
if (count >= (ssize_t)PAGE_SIZE) {
|
2017-12-11 12:50:22 +00:00
|
|
|
printk("fill_read_buffer: %pS returned bad count\n",
|
|
|
|
ops->show);
|
2008-03-04 23:09:07 +00:00
|
|
|
/* Try to struggle along */
|
|
|
|
count = PAGE_SIZE - 1;
|
|
|
|
}
|
2013-10-01 21:42:02 +00:00
|
|
|
seq_commit(sf, count);
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 19:11:55 +00:00
|
|
|
static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
|
2013-11-28 19:54:16 +00:00
|
|
|
size_t count, loff_t pos)
|
2013-10-01 21:42:06 +00:00
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
struct bin_attribute *battr = of->kn->priv;
|
2013-12-11 19:11:54 +00:00
|
|
|
struct kobject *kobj = of->kn->parent->priv;
|
2013-11-28 19:54:16 +00:00
|
|
|
loff_t size = file_inode(of->file)->i_size;
|
2013-10-01 21:42:06 +00:00
|
|
|
|
2013-11-28 19:54:16 +00:00
|
|
|
if (!count)
|
2013-10-01 21:42:06 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (size) {
|
2015-05-21 21:21:16 +00:00
|
|
|
if (pos >= size)
|
2013-10-01 21:42:06 +00:00
|
|
|
return 0;
|
2013-11-28 19:54:16 +00:00
|
|
|
if (pos + count > size)
|
|
|
|
count = size - pos;
|
2013-10-01 21:42:06 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 19:54:16 +00:00
|
|
|
if (!battr->read)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
return battr->read(of->file, kobj, battr, buf, pos, count);
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:57:26 +00:00
|
|
|
/* kernfs read callback for regular sysfs files with pre-alloc */
|
|
|
|
static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
|
|
|
|
size_t count, loff_t pos)
|
|
|
|
{
|
|
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
|
|
|
|
struct kobject *kobj = of->kn->parent->priv;
|
sysfs: be careful of error returns from ops->show()
ops->show() can return a negative error code.
Commit 65da3484d9be ("sysfs: correctly handle short reads on PREALLOC attrs.")
(in v4.4) caused this to be stored in an unsigned 'size_t' variable, so errors
would look like large numbers.
As a result, if an error is returned, sysfs_kf_read() will return the
value of 'count', typically 4096.
Commit 17d0774f8068 ("sysfs: correctly handle read offset on PREALLOC attrs")
(in v4.8) extended this error to use the unsigned large 'len' as a size for
memmove().
Consequently, if ->show returns an error, then the first read() on the
sysfs file will return 4096 and could return uninitialized memory to
user-space.
If the application performs a subsequent read, this will trigger a memmove()
with extremely large count, and is likely to crash the machine is bizarre ways.
This bug can currently only be triggered by reading from an md
sysfs attribute declared with __ATTR_PREALLOC() during the
brief period between when mddev_put() deletes an mddev from
the ->all_mddevs list, and when mddev_delayed_delete() - which is
scheduled on a workqueue - completes.
Before this, an error won't be returned by the ->show()
After this, the ->show() won't be called.
I can reproduce it reliably only by putting delay like
usleep_range(500000,700000);
early in mddev_delayed_delete(). Then after creating an
md device md0 run
echo clear > /sys/block/md0/md/array_state; cat /sys/block/md0/md/array_state
The bug can be triggered without the usleep.
Fixes: 65da3484d9be ("sysfs: correctly handle short reads on PREALLOC attrs.")
Fixes: 17d0774f8068 ("sysfs: correctly handle read offset on PREALLOC attrs")
Cc: stable@vger.kernel.org
Signed-off-by: NeilBrown <neilb@suse.com>
Acked-by: Tejun Heo <tj@kernel.org>
Reported-and-tested-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-04-03 01:30:34 +00:00
|
|
|
ssize_t len;
|
2014-10-14 05:57:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If buf != of->prealloc_buf, we don't know how
|
|
|
|
* large it is, so cannot safely pass it to ->show
|
|
|
|
*/
|
2016-06-22 18:42:16 +00:00
|
|
|
if (WARN_ON_ONCE(buf != of->prealloc_buf))
|
2014-10-14 05:57:26 +00:00
|
|
|
return 0;
|
2015-08-05 22:27:55 +00:00
|
|
|
len = ops->show(kobj, of->kn->priv, buf);
|
sysfs: be careful of error returns from ops->show()
ops->show() can return a negative error code.
Commit 65da3484d9be ("sysfs: correctly handle short reads on PREALLOC attrs.")
(in v4.4) caused this to be stored in an unsigned 'size_t' variable, so errors
would look like large numbers.
As a result, if an error is returned, sysfs_kf_read() will return the
value of 'count', typically 4096.
Commit 17d0774f8068 ("sysfs: correctly handle read offset on PREALLOC attrs")
(in v4.8) extended this error to use the unsigned large 'len' as a size for
memmove().
Consequently, if ->show returns an error, then the first read() on the
sysfs file will return 4096 and could return uninitialized memory to
user-space.
If the application performs a subsequent read, this will trigger a memmove()
with extremely large count, and is likely to crash the machine is bizarre ways.
This bug can currently only be triggered by reading from an md
sysfs attribute declared with __ATTR_PREALLOC() during the
brief period between when mddev_put() deletes an mddev from
the ->all_mddevs list, and when mddev_delayed_delete() - which is
scheduled on a workqueue - completes.
Before this, an error won't be returned by the ->show()
After this, the ->show() won't be called.
I can reproduce it reliably only by putting delay like
usleep_range(500000,700000);
early in mddev_delayed_delete(). Then after creating an
md device md0 run
echo clear > /sys/block/md0/md/array_state; cat /sys/block/md0/md/array_state
The bug can be triggered without the usleep.
Fixes: 65da3484d9be ("sysfs: correctly handle short reads on PREALLOC attrs.")
Fixes: 17d0774f8068 ("sysfs: correctly handle read offset on PREALLOC attrs")
Cc: stable@vger.kernel.org
Signed-off-by: NeilBrown <neilb@suse.com>
Acked-by: Tejun Heo <tj@kernel.org>
Reported-and-tested-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-04-03 01:30:34 +00:00
|
|
|
if (len < 0)
|
|
|
|
return len;
|
2016-06-22 18:42:16 +00:00
|
|
|
if (pos) {
|
|
|
|
if (len <= pos)
|
|
|
|
return 0;
|
|
|
|
len -= pos;
|
|
|
|
memmove(buf, buf + pos, len);
|
|
|
|
}
|
sysfs: be careful of error returns from ops->show()
ops->show() can return a negative error code.
Commit 65da3484d9be ("sysfs: correctly handle short reads on PREALLOC attrs.")
(in v4.4) caused this to be stored in an unsigned 'size_t' variable, so errors
would look like large numbers.
As a result, if an error is returned, sysfs_kf_read() will return the
value of 'count', typically 4096.
Commit 17d0774f8068 ("sysfs: correctly handle read offset on PREALLOC attrs")
(in v4.8) extended this error to use the unsigned large 'len' as a size for
memmove().
Consequently, if ->show returns an error, then the first read() on the
sysfs file will return 4096 and could return uninitialized memory to
user-space.
If the application performs a subsequent read, this will trigger a memmove()
with extremely large count, and is likely to crash the machine is bizarre ways.
This bug can currently only be triggered by reading from an md
sysfs attribute declared with __ATTR_PREALLOC() during the
brief period between when mddev_put() deletes an mddev from
the ->all_mddevs list, and when mddev_delayed_delete() - which is
scheduled on a workqueue - completes.
Before this, an error won't be returned by the ->show()
After this, the ->show() won't be called.
I can reproduce it reliably only by putting delay like
usleep_range(500000,700000);
early in mddev_delayed_delete(). Then after creating an
md device md0 run
echo clear > /sys/block/md0/md/array_state; cat /sys/block/md0/md/array_state
The bug can be triggered without the usleep.
Fixes: 65da3484d9be ("sysfs: correctly handle short reads on PREALLOC attrs.")
Fixes: 17d0774f8068 ("sysfs: correctly handle read offset on PREALLOC attrs")
Cc: stable@vger.kernel.org
Signed-off-by: NeilBrown <neilb@suse.com>
Acked-by: Tejun Heo <tj@kernel.org>
Reported-and-tested-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-04-03 01:30:34 +00:00
|
|
|
return min_t(ssize_t, count, len);
|
2014-10-14 05:57:26 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 19:54:17 +00:00
|
|
|
/* kernfs write callback for regular sysfs files */
|
2013-12-11 19:11:55 +00:00
|
|
|
static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
|
2013-11-28 19:54:17 +00:00
|
|
|
size_t count, loff_t pos)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
|
2013-12-11 19:11:54 +00:00
|
|
|
struct kobject *kobj = of->kn->parent->priv;
|
2007-06-13 18:45:16 +00:00
|
|
|
|
2013-11-28 19:54:17 +00:00
|
|
|
if (!count)
|
|
|
|
return 0;
|
2007-06-13 18:45:16 +00:00
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
return ops->store(kobj, of->kn->priv, buf, count);
|
2013-11-28 19:54:17 +00:00
|
|
|
}
|
2013-10-01 21:42:05 +00:00
|
|
|
|
2013-11-28 19:54:17 +00:00
|
|
|
/* kernfs write callback for bin sysfs files */
|
2013-12-11 19:11:55 +00:00
|
|
|
static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
|
2013-11-28 19:54:17 +00:00
|
|
|
size_t count, loff_t pos)
|
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
struct bin_attribute *battr = of->kn->priv;
|
2013-12-11 19:11:54 +00:00
|
|
|
struct kobject *kobj = of->kn->parent->priv;
|
2013-11-28 19:54:17 +00:00
|
|
|
loff_t size = file_inode(of->file)->i_size;
|
2013-10-01 21:42:05 +00:00
|
|
|
|
2013-11-28 19:54:17 +00:00
|
|
|
if (size) {
|
|
|
|
if (size <= pos)
|
2014-09-24 15:21:04 +00:00
|
|
|
return -EFBIG;
|
2013-11-28 19:54:17 +00:00
|
|
|
count = min_t(ssize_t, count, size - pos);
|
2013-10-01 21:42:05 +00:00
|
|
|
}
|
2013-11-28 19:54:17 +00:00
|
|
|
if (!count)
|
|
|
|
return 0;
|
2007-06-13 18:45:16 +00:00
|
|
|
|
2013-11-28 19:54:17 +00:00
|
|
|
if (!battr->write)
|
|
|
|
return -EIO;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-11-28 19:54:17 +00:00
|
|
|
return battr->write(of->file, kobj, battr, buf, pos, count);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 19:11:55 +00:00
|
|
|
static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
|
2013-11-28 19:54:18 +00:00
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
struct bin_attribute *battr = of->kn->priv;
|
2013-12-11 19:11:54 +00:00
|
|
|
struct kobject *kobj = of->kn->parent->priv;
|
2013-11-28 19:54:18 +00:00
|
|
|
|
|
|
|
return battr->mmap(of->file, kobj, battr, vma);
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
|
2006-03-20 06:53:53 +00:00
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
struct kernfs_node *kn = kobj->sd, *tmp;
|
2007-06-13 19:27:25 +00:00
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
if (kn && dir)
|
|
|
|
kn = kernfs_find_and_get(kn, dir);
|
2013-11-28 19:54:27 +00:00
|
|
|
else
|
2013-12-11 19:11:53 +00:00
|
|
|
kernfs_get(kn);
|
2013-11-28 19:54:27 +00:00
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
if (kn && attr) {
|
|
|
|
tmp = kernfs_find_and_get(kn, attr);
|
|
|
|
kernfs_put(kn);
|
|
|
|
kn = tmp;
|
2013-11-28 19:54:27 +00:00
|
|
|
}
|
2007-06-13 19:27:25 +00:00
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
if (kn) {
|
|
|
|
kernfs_notify(kn);
|
|
|
|
kernfs_put(kn);
|
2013-11-28 19:54:27 +00:00
|
|
|
}
|
2006-03-20 06:53:53 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_notify);
|
|
|
|
|
2013-11-28 19:54:21 +00:00
|
|
|
static const struct kernfs_ops sysfs_file_kfops_empty = {
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_ro = {
|
|
|
|
.seq_show = sysfs_kf_seq_show,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_wo = {
|
|
|
|
.write = sysfs_kf_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_rw = {
|
|
|
|
.seq_show = sysfs_kf_seq_show,
|
|
|
|
.write = sysfs_kf_write,
|
|
|
|
};
|
|
|
|
|
2014-10-14 05:57:26 +00:00
|
|
|
static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
|
|
|
|
.read = sysfs_kf_read,
|
|
|
|
.prealloc = true,
|
|
|
|
};
|
|
|
|
|
2014-10-13 05:41:28 +00:00
|
|
|
static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
|
|
|
|
.write = sysfs_kf_write,
|
|
|
|
.prealloc = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
|
2014-10-14 05:57:26 +00:00
|
|
|
.read = sysfs_kf_read,
|
2014-10-13 05:41:28 +00:00
|
|
|
.write = sysfs_kf_write,
|
|
|
|
.prealloc = true,
|
|
|
|
};
|
|
|
|
|
2013-11-28 19:54:21 +00:00
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_ro = {
|
|
|
|
.read = sysfs_kf_bin_read,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_wo = {
|
|
|
|
.write = sysfs_kf_bin_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_rw = {
|
|
|
|
.read = sysfs_kf_bin_read,
|
|
|
|
.write = sysfs_kf_bin_write,
|
2013-12-10 14:29:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_mmap = {
|
|
|
|
.read = sysfs_kf_bin_read,
|
|
|
|
.write = sysfs_kf_bin_write,
|
2013-11-28 19:54:21 +00:00
|
|
|
.mmap = sysfs_kf_bin_mmap,
|
|
|
|
};
|
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
int sysfs_add_file_mode_ns(struct kernfs_node *parent,
|
2013-11-28 19:54:23 +00:00
|
|
|
const struct attribute *attr, bool is_bin,
|
2018-07-20 21:56:48 +00:00
|
|
|
umode_t mode, kuid_t uid, kgid_t gid, const void *ns)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-11-28 19:54:29 +00:00
|
|
|
struct lock_class_key *key = NULL;
|
2013-11-28 19:54:21 +00:00
|
|
|
const struct kernfs_ops *ops;
|
2013-12-11 19:11:53 +00:00
|
|
|
struct kernfs_node *kn;
|
2013-11-28 19:54:22 +00:00
|
|
|
loff_t size;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-11-28 19:54:23 +00:00
|
|
|
if (!is_bin) {
|
2013-12-11 19:11:53 +00:00
|
|
|
struct kobject *kobj = parent->priv;
|
2013-11-28 19:54:21 +00:00
|
|
|
const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
|
|
|
|
|
|
|
|
/* every kobject with an attribute needs a ktype assigned */
|
|
|
|
if (WARN(!sysfs_ops, KERN_ERR
|
|
|
|
"missing sysfs attribute operations for kobject: %s\n",
|
|
|
|
kobject_name(kobj)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-10-13 05:41:28 +00:00
|
|
|
if (sysfs_ops->show && sysfs_ops->store) {
|
|
|
|
if (mode & SYSFS_PREALLOC)
|
|
|
|
ops = &sysfs_prealloc_kfops_rw;
|
|
|
|
else
|
|
|
|
ops = &sysfs_file_kfops_rw;
|
2014-10-14 05:57:26 +00:00
|
|
|
} else if (sysfs_ops->show) {
|
|
|
|
if (mode & SYSFS_PREALLOC)
|
|
|
|
ops = &sysfs_prealloc_kfops_ro;
|
|
|
|
else
|
|
|
|
ops = &sysfs_file_kfops_ro;
|
|
|
|
} else if (sysfs_ops->store) {
|
2014-10-13 05:41:28 +00:00
|
|
|
if (mode & SYSFS_PREALLOC)
|
|
|
|
ops = &sysfs_prealloc_kfops_wo;
|
|
|
|
else
|
|
|
|
ops = &sysfs_file_kfops_wo;
|
|
|
|
} else
|
2013-11-28 19:54:21 +00:00
|
|
|
ops = &sysfs_file_kfops_empty;
|
2013-11-28 19:54:22 +00:00
|
|
|
|
|
|
|
size = PAGE_SIZE;
|
2013-11-28 19:54:21 +00:00
|
|
|
} else {
|
|
|
|
struct bin_attribute *battr = (void *)attr;
|
|
|
|
|
2013-12-10 14:29:17 +00:00
|
|
|
if (battr->mmap)
|
|
|
|
ops = &sysfs_bin_kfops_mmap;
|
|
|
|
else if (battr->read && battr->write)
|
2013-11-28 19:54:21 +00:00
|
|
|
ops = &sysfs_bin_kfops_rw;
|
|
|
|
else if (battr->read)
|
|
|
|
ops = &sysfs_bin_kfops_ro;
|
|
|
|
else if (battr->write)
|
|
|
|
ops = &sysfs_bin_kfops_wo;
|
|
|
|
else
|
|
|
|
ops = &sysfs_file_kfops_empty;
|
2013-11-28 19:54:22 +00:00
|
|
|
|
|
|
|
size = battr->size;
|
2013-11-28 19:54:21 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 19:54:29 +00:00
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
|
|
if (!attr->ignore_lockdep)
|
|
|
|
key = attr->key ?: (struct lock_class_key *)&attr->skey;
|
|
|
|
#endif
|
2018-07-20 21:56:48 +00:00
|
|
|
|
|
|
|
kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
|
2018-07-20 21:56:47 +00:00
|
|
|
size, ops, (void *)attr, ns, key);
|
2013-12-11 19:11:53 +00:00
|
|
|
if (IS_ERR(kn)) {
|
|
|
|
if (PTR_ERR(kn) == -EEXIST)
|
|
|
|
sysfs_warn_dup(parent, attr->name);
|
|
|
|
return PTR_ERR(kn);
|
2013-11-28 19:54:24 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2013-09-12 02:29:04 +00:00
|
|
|
* sysfs_create_file_ns - create an attribute file for an object with custom ns
|
|
|
|
* @kobj: object we're creating for
|
|
|
|
* @attr: attribute descriptor
|
|
|
|
* @ns: namespace the new file should belong to
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2013-09-12 02:29:04 +00:00
|
|
|
int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
|
|
|
|
const void *ns)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2018-07-20 21:56:48 +00:00
|
|
|
kuid_t uid;
|
|
|
|
kgid_t gid;
|
|
|
|
|
2019-01-03 09:23:47 +00:00
|
|
|
if (WARN_ON(!kobj || !kobj->sd || !attr))
|
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2018-07-20 21:56:48 +00:00
|
|
|
kobject_get_ownership(kobj, &uid, &gid);
|
|
|
|
return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode,
|
|
|
|
uid, gid, ns);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
}
|
2013-09-12 02:29:04 +00:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2018-10-04 14:37:49 +00:00
|
|
|
int sysfs_create_files(struct kobject *kobj, const struct attribute * const *ptr)
|
2010-01-05 11:48:01 +00:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; ptr[i] && !err; i++)
|
|
|
|
err = sysfs_create_file(kobj, ptr[i]);
|
|
|
|
if (err)
|
|
|
|
while (--i >= 0)
|
|
|
|
sysfs_remove_file(kobj, ptr[i]);
|
|
|
|
return err;
|
|
|
|
}
|
2013-08-21 23:17:47 +00:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_create_files);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-02-20 20:02:44 +00:00
|
|
|
/**
|
|
|
|
* sysfs_add_file_to_group - add an attribute file to a pre-existing group.
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
* @group: group name.
|
|
|
|
*/
|
|
|
|
int sysfs_add_file_to_group(struct kobject *kobj,
|
|
|
|
const struct attribute *attr, const char *group)
|
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
struct kernfs_node *parent;
|
2018-07-20 21:56:48 +00:00
|
|
|
kuid_t uid;
|
|
|
|
kgid_t gid;
|
2007-02-20 20:02:44 +00:00
|
|
|
int error;
|
|
|
|
|
2013-11-28 19:54:30 +00:00
|
|
|
if (group) {
|
2013-12-11 19:11:53 +00:00
|
|
|
parent = kernfs_find_and_get(kobj->sd, group);
|
2013-11-28 19:54:30 +00:00
|
|
|
} else {
|
2013-12-11 19:11:53 +00:00
|
|
|
parent = kobj->sd;
|
|
|
|
kernfs_get(parent);
|
2013-11-28 19:54:30 +00:00
|
|
|
}
|
2008-01-03 00:44:05 +00:00
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
if (!parent)
|
2007-06-13 19:27:22 +00:00
|
|
|
return -ENOENT;
|
|
|
|
|
2018-07-20 21:56:48 +00:00
|
|
|
kobject_get_ownership(kobj, &uid, &gid);
|
2018-07-27 21:33:27 +00:00
|
|
|
error = sysfs_add_file_mode_ns(parent, attr, false,
|
2018-07-20 21:56:48 +00:00
|
|
|
attr->mode, uid, gid, NULL);
|
2013-12-11 19:11:53 +00:00
|
|
|
kernfs_put(parent);
|
2007-06-13 19:27:22 +00:00
|
|
|
|
2007-02-20 20:02:44 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
|
|
|
|
|
2005-04-19 04:57:32 +00:00
|
|
|
/**
|
|
|
|
* sysfs_chmod_file - update the modified mode value on an object attribute.
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
* @mode: file permissions.
|
|
|
|
*
|
|
|
|
*/
|
2010-07-02 14:54:05 +00:00
|
|
|
int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
|
2011-07-24 07:40:40 +00:00
|
|
|
umode_t mode)
|
2005-04-19 04:57:32 +00:00
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
struct kernfs_node *kn;
|
2005-07-29 19:13:35 +00:00
|
|
|
struct iattr newattrs;
|
2007-06-13 19:27:25 +00:00
|
|
|
int rc;
|
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
kn = kernfs_find_and_get(kobj->sd, attr->name);
|
|
|
|
if (!kn)
|
2013-11-23 22:21:52 +00:00
|
|
|
return -ENOENT;
|
2007-09-20 07:05:10 +00:00
|
|
|
|
2013-12-11 19:11:54 +00:00
|
|
|
newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
|
2009-11-08 07:27:02 +00:00
|
|
|
newattrs.ia_valid = ATTR_MODE;
|
2007-09-20 07:05:10 +00:00
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
rc = kernfs_setattr(kn, &newattrs);
|
2013-11-23 22:21:52 +00:00
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
kernfs_put(kn);
|
2007-06-13 19:27:25 +00:00
|
|
|
return rc;
|
2005-04-19 04:57:32 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_chmod_file);
|
|
|
|
|
2018-08-02 17:51:40 +00:00
|
|
|
/**
|
|
|
|
* sysfs_break_active_protection - break "active" protection
|
|
|
|
* @kobj: The kernel object @attr is associated with.
|
|
|
|
* @attr: The attribute to break the "active" protection for.
|
|
|
|
*
|
|
|
|
* With sysfs, just like kernfs, deletion of an attribute is postponed until
|
|
|
|
* all active .show() and .store() callbacks have finished unless this function
|
|
|
|
* is called. Hence this function is useful in methods that implement self
|
|
|
|
* deletion.
|
|
|
|
*/
|
|
|
|
struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
|
|
|
|
const struct attribute *attr)
|
|
|
|
{
|
|
|
|
struct kernfs_node *kn;
|
|
|
|
|
|
|
|
kobject_get(kobj);
|
|
|
|
kn = kernfs_find_and_get(kobj->sd, attr->name);
|
|
|
|
if (kn)
|
|
|
|
kernfs_break_active_protection(kn);
|
|
|
|
return kn;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sysfs_unbreak_active_protection - restore "active" protection
|
|
|
|
* @kn: Pointer returned by sysfs_break_active_protection().
|
|
|
|
*
|
|
|
|
* Undo the effects of sysfs_break_active_protection(). Since this function
|
|
|
|
* calls kernfs_put() on the kernfs node that corresponds to the 'attr'
|
|
|
|
* argument passed to sysfs_break_active_protection() that attribute may have
|
|
|
|
* been removed between the sysfs_break_active_protection() and
|
|
|
|
* sysfs_unbreak_active_protection() calls, it is not safe to access @kn after
|
|
|
|
* this function has returned.
|
|
|
|
*/
|
|
|
|
void sysfs_unbreak_active_protection(struct kernfs_node *kn)
|
|
|
|
{
|
|
|
|
struct kobject *kobj = kn->parent->priv;
|
|
|
|
|
|
|
|
kernfs_unbreak_active_protection(kn);
|
|
|
|
kernfs_put(kn);
|
|
|
|
kobject_put(kobj);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2013-09-12 02:29:04 +00:00
|
|
|
* sysfs_remove_file_ns - remove an object attribute with a custom ns tag
|
|
|
|
* @kobj: object we're acting for
|
|
|
|
* @attr: attribute descriptor
|
|
|
|
* @ns: namespace tag of the file to remove
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2013-09-12 02:29:04 +00:00
|
|
|
* Hash the attribute name and namespace tag and kill the victim.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2013-09-12 02:29:04 +00:00
|
|
|
void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
|
|
|
|
const void *ns)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
struct kernfs_node *parent = kobj->sd;
|
2011-10-12 21:53:38 +00:00
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
kernfs_remove_by_name_ns(parent, attr->name, ns);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2013-09-12 02:29:04 +00:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
kernfs, sysfs, driver-core: implement kernfs_remove_self() and its wrappers
Sometimes it's necessary to implement a node which wants to delete
nodes including itself. This isn't straightforward because of kernfs
active reference. While a file operation is in progress, an active
reference is held and kernfs_remove() waits for all such references to
drain before completing. For a self-deleting node, this is a deadlock
as kernfs_remove() ends up waiting for an active reference that itself
is sitting on top of.
This currently is worked around in the sysfs layer using
sysfs_schedule_callback() which makes such removals asynchronous.
While it works, it's rather cumbersome and inherently breaks
synchronicity of the operation - the file operation which triggered
the operation may complete before the removal is finished (or even
started) and the removal may fail asynchronously. If a removal
operation is immmediately followed by another operation which expects
the specific name to be available (e.g. removal followed by rename
onto the same name), there's no way to make the latter operation
reliable.
The thing is there's no inherent reason for this to be asynchrnous.
All that's necessary to do this synchronous is a dedicated operation
which drops its own active ref and deactivates self. This patch
implements kernfs_remove_self() and its wrappers in sysfs and driver
core. kernfs_remove_self() is to be called from one of the file
operations, drops the active ref the task is holding, removes the self
node, and restores active ref to the dead node so that the ref is
balanced afterwards. __kernfs_remove() is updated so that it takes an
early exit if the target node is already fully removed so that the
active ref restored by kernfs_remove_self() after removal doesn't
confuse the deactivation path.
This makes implementing self-deleting nodes very easy. The normal
removal path doesn't even need to be changed to use
kernfs_remove_self() for the self-deleting node. The method can
invoke kernfs_remove_self() on itself before proceeding the normal
removal path. kernfs_remove() invoked on the node by the normal
deletion path will simply be ignored.
This will replace sysfs_schedule_callback(). A subtle feature of
sysfs_schedule_callback() is that it collapses multiple invocations -
even if multiple removals are triggered, the removal callback is run
only once. An equivalent effect can be achieved by testing the return
value of kernfs_remove_self() - only the one which gets %true return
value should proceed with actual deletion. All other instances of
kernfs_remove_self() will wait till the enclosing kernfs operation
which invoked the winning instance of kernfs_remove_self() finishes
and then return %false. This trivially makes all users of
kernfs_remove_self() automatically show correct synchronous behavior
even when there are multiple concurrent operations - all "echo 1 >
delete" instances will finish only after the whole operation is
completed by one of the instances.
Note that manipulation of active ref is implemented in separate public
functions - kernfs_[un]break_active_protection().
kernfs_remove_self() is the only user at the moment but this will be
used to cater to more complex cases.
v2: For !CONFIG_SYSFS, dummy version kernfs_remove_self() was missing
and sysfs_remove_file_self() had incorrect return type. Fix it.
Reported by kbuild test bot.
v3: kernfs_[un]break_active_protection() separated out from
kernfs_remove_self() and exposed as public API.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: kbuild test robot <fengguang.wu@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-02-03 19:03:01 +00:00
|
|
|
/**
|
|
|
|
* sysfs_remove_file_self - remove an object attribute from its own method
|
|
|
|
* @kobj: object we're acting for
|
|
|
|
* @attr: attribute descriptor
|
|
|
|
*
|
|
|
|
* See kernfs_remove_self() for details.
|
|
|
|
*/
|
|
|
|
bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
|
|
|
|
{
|
|
|
|
struct kernfs_node *parent = kobj->sd;
|
|
|
|
struct kernfs_node *kn;
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
kn = kernfs_find_and_get(parent, attr->name);
|
|
|
|
if (WARN_ON_ONCE(!kn))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ret = kernfs_remove_self(kn);
|
|
|
|
|
|
|
|
kernfs_put(kn);
|
|
|
|
return ret;
|
|
|
|
}
|
2020-05-11 13:51:07 +00:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_file_self);
|
kernfs, sysfs, driver-core: implement kernfs_remove_self() and its wrappers
Sometimes it's necessary to implement a node which wants to delete
nodes including itself. This isn't straightforward because of kernfs
active reference. While a file operation is in progress, an active
reference is held and kernfs_remove() waits for all such references to
drain before completing. For a self-deleting node, this is a deadlock
as kernfs_remove() ends up waiting for an active reference that itself
is sitting on top of.
This currently is worked around in the sysfs layer using
sysfs_schedule_callback() which makes such removals asynchronous.
While it works, it's rather cumbersome and inherently breaks
synchronicity of the operation - the file operation which triggered
the operation may complete before the removal is finished (or even
started) and the removal may fail asynchronously. If a removal
operation is immmediately followed by another operation which expects
the specific name to be available (e.g. removal followed by rename
onto the same name), there's no way to make the latter operation
reliable.
The thing is there's no inherent reason for this to be asynchrnous.
All that's necessary to do this synchronous is a dedicated operation
which drops its own active ref and deactivates self. This patch
implements kernfs_remove_self() and its wrappers in sysfs and driver
core. kernfs_remove_self() is to be called from one of the file
operations, drops the active ref the task is holding, removes the self
node, and restores active ref to the dead node so that the ref is
balanced afterwards. __kernfs_remove() is updated so that it takes an
early exit if the target node is already fully removed so that the
active ref restored by kernfs_remove_self() after removal doesn't
confuse the deactivation path.
This makes implementing self-deleting nodes very easy. The normal
removal path doesn't even need to be changed to use
kernfs_remove_self() for the self-deleting node. The method can
invoke kernfs_remove_self() on itself before proceeding the normal
removal path. kernfs_remove() invoked on the node by the normal
deletion path will simply be ignored.
This will replace sysfs_schedule_callback(). A subtle feature of
sysfs_schedule_callback() is that it collapses multiple invocations -
even if multiple removals are triggered, the removal callback is run
only once. An equivalent effect can be achieved by testing the return
value of kernfs_remove_self() - only the one which gets %true return
value should proceed with actual deletion. All other instances of
kernfs_remove_self() will wait till the enclosing kernfs operation
which invoked the winning instance of kernfs_remove_self() finishes
and then return %false. This trivially makes all users of
kernfs_remove_self() automatically show correct synchronous behavior
even when there are multiple concurrent operations - all "echo 1 >
delete" instances will finish only after the whole operation is
completed by one of the instances.
Note that manipulation of active ref is implemented in separate public
functions - kernfs_[un]break_active_protection().
kernfs_remove_self() is the only user at the moment but this will be
used to cater to more complex cases.
v2: For !CONFIG_SYSFS, dummy version kernfs_remove_self() was missing
and sysfs_remove_file_self() had incorrect return type. Fix it.
Reported by kbuild test bot.
v3: kernfs_[un]break_active_protection() separated out from
kernfs_remove_self() and exposed as public API.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: kbuild test robot <fengguang.wu@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-02-03 19:03:01 +00:00
|
|
|
|
2018-10-04 14:37:49 +00:00
|
|
|
void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *ptr)
|
2010-01-05 11:48:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
2018-12-20 21:50:28 +00:00
|
|
|
|
2010-01-05 11:48:01 +00:00
|
|
|
for (i = 0; ptr[i]; i++)
|
|
|
|
sysfs_remove_file(kobj, ptr[i]);
|
|
|
|
}
|
2013-08-21 23:17:47 +00:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_files);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-02-20 20:02:44 +00:00
|
|
|
/**
|
|
|
|
* sysfs_remove_file_from_group - remove an attribute file from a group.
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
* @group: group name.
|
|
|
|
*/
|
|
|
|
void sysfs_remove_file_from_group(struct kobject *kobj,
|
|
|
|
const struct attribute *attr, const char *group)
|
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
struct kernfs_node *parent;
|
2007-02-20 20:02:44 +00:00
|
|
|
|
2013-11-28 19:54:30 +00:00
|
|
|
if (group) {
|
2013-12-11 19:11:53 +00:00
|
|
|
parent = kernfs_find_and_get(kobj->sd, group);
|
2013-11-28 19:54:30 +00:00
|
|
|
} else {
|
2013-12-11 19:11:53 +00:00
|
|
|
parent = kobj->sd;
|
|
|
|
kernfs_get(parent);
|
2013-11-28 19:54:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 19:11:53 +00:00
|
|
|
if (parent) {
|
|
|
|
kernfs_remove_by_name(parent, attr->name);
|
|
|
|
kernfs_put(parent);
|
2007-02-20 20:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
|
|
|
|
|
2013-10-01 21:42:09 +00:00
|
|
|
/**
|
|
|
|
* sysfs_create_bin_file - create binary file for object.
|
|
|
|
* @kobj: object.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
*/
|
|
|
|
int sysfs_create_bin_file(struct kobject *kobj,
|
|
|
|
const struct bin_attribute *attr)
|
|
|
|
{
|
2018-07-20 21:56:48 +00:00
|
|
|
kuid_t uid;
|
|
|
|
kgid_t gid;
|
|
|
|
|
2019-01-03 09:23:47 +00:00
|
|
|
if (WARN_ON(!kobj || !kobj->sd || !attr))
|
|
|
|
return -EINVAL;
|
2013-10-01 21:42:09 +00:00
|
|
|
|
2018-07-20 21:56:48 +00:00
|
|
|
kobject_get_ownership(kobj, &uid, &gid);
|
|
|
|
return sysfs_add_file_mode_ns(kobj->sd, &attr->attr, true,
|
|
|
|
attr->attr.mode, uid, gid, NULL);
|
2013-10-01 21:42:09 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sysfs_remove_bin_file - remove binary file for object.
|
|
|
|
* @kobj: object.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
*/
|
|
|
|
void sysfs_remove_bin_file(struct kobject *kobj,
|
|
|
|
const struct bin_attribute *attr)
|
|
|
|
{
|
2013-11-23 22:21:49 +00:00
|
|
|
kernfs_remove_by_name(kobj->sd, attr->attr.name);
|
2013-10-01 21:42:09 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
|
2020-02-27 03:37:11 +00:00
|
|
|
|
|
|
|
static int internal_change_owner(struct kernfs_node *kn, kuid_t kuid,
|
|
|
|
kgid_t kgid)
|
|
|
|
{
|
|
|
|
struct iattr newattrs = {
|
|
|
|
.ia_valid = ATTR_UID | ATTR_GID,
|
|
|
|
.ia_uid = kuid,
|
|
|
|
.ia_gid = kgid,
|
|
|
|
};
|
|
|
|
return kernfs_setattr(kn, &newattrs);
|
|
|
|
}
|
|
|
|
|
2020-02-27 03:37:12 +00:00
|
|
|
/**
|
|
|
|
* sysfs_link_change_owner - change owner of a sysfs file.
|
|
|
|
* @kobj: object of the kernfs_node the symlink is located in.
|
|
|
|
* @targ: object of the kernfs_node the symlink points to.
|
|
|
|
* @name: name of the link.
|
|
|
|
* @kuid: new owner's kuid
|
|
|
|
* @kgid: new owner's kgid
|
|
|
|
*
|
|
|
|
* This function looks up the sysfs symlink entry @name under @kobj and changes
|
|
|
|
* the ownership to @kuid/@kgid. The symlink is looked up in the namespace of
|
|
|
|
* @targ.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or error code on failure.
|
|
|
|
*/
|
|
|
|
int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ,
|
|
|
|
const char *name, kuid_t kuid, kgid_t kgid)
|
|
|
|
{
|
|
|
|
struct kernfs_node *kn = NULL;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (!name || !kobj->state_in_sysfs || !targ->state_in_sysfs)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
error = -ENOENT;
|
|
|
|
kn = kernfs_find_and_get_ns(kobj->sd, name, targ->sd->ns);
|
|
|
|
if (!kn)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
error = -EINVAL;
|
|
|
|
if (kernfs_type(kn) != KERNFS_LINK)
|
|
|
|
goto out;
|
|
|
|
if (kn->symlink.target_kn->priv != targ)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
error = internal_change_owner(kn, kuid, kgid);
|
|
|
|
|
|
|
|
out:
|
|
|
|
kernfs_put(kn);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2020-02-27 03:37:11 +00:00
|
|
|
/**
|
|
|
|
* sysfs_file_change_owner - change owner of a sysfs file.
|
|
|
|
* @kobj: object.
|
|
|
|
* @name: name of the file to change.
|
|
|
|
* @kuid: new owner's kuid
|
|
|
|
* @kgid: new owner's kgid
|
|
|
|
*
|
|
|
|
* This function looks up the sysfs entry @name under @kobj and changes the
|
|
|
|
* ownership to @kuid/@kgid.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or error code on failure.
|
|
|
|
*/
|
|
|
|
int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid,
|
|
|
|
kgid_t kgid)
|
|
|
|
{
|
|
|
|
struct kernfs_node *kn;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!kobj->state_in_sysfs)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
kn = kernfs_find_and_get(kobj->sd, name);
|
|
|
|
if (!kn)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
error = internal_change_owner(kn, kuid, kgid);
|
|
|
|
|
|
|
|
kernfs_put(kn);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_file_change_owner);
|
2020-02-27 03:37:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sysfs_change_owner - change owner of the given object.
|
|
|
|
* @kobj: object.
|
|
|
|
* @kuid: new owner's kuid
|
|
|
|
* @kgid: new owner's kgid
|
|
|
|
*
|
|
|
|
* Change the owner of the default directory, files, groups, and attributes of
|
|
|
|
* @kobj to @kuid/@kgid. Note that sysfs_change_owner mirrors how the sysfs
|
|
|
|
* entries for a kobject are added by driver core. In summary,
|
|
|
|
* sysfs_change_owner() takes care of the default directory entry for @kobj,
|
|
|
|
* the default attributes associated with the ktype of @kobj and the default
|
|
|
|
* attributes associated with the ktype of @kobj.
|
|
|
|
* Additional properties not added by driver core have to be changed by the
|
|
|
|
* driver or subsystem which created them. This is similar to how
|
|
|
|
* driver/subsystem specific entries are removed.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or error code on failure.
|
|
|
|
*/
|
|
|
|
int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
const struct kobj_type *ktype;
|
|
|
|
|
|
|
|
if (!kobj->state_in_sysfs)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Change the owner of the kobject itself. */
|
|
|
|
error = internal_change_owner(kobj->sd, kuid, kgid);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
ktype = get_ktype(kobj);
|
|
|
|
if (ktype) {
|
|
|
|
struct attribute **kattr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change owner of the default attributes associated with the
|
|
|
|
* ktype of @kobj.
|
|
|
|
*/
|
|
|
|
for (kattr = ktype->default_attrs; kattr && *kattr; kattr++) {
|
|
|
|
error = sysfs_file_change_owner(kobj, (*kattr)->name,
|
|
|
|
kuid, kgid);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change owner of the default groups associated with the
|
|
|
|
* ktype of @kobj.
|
|
|
|
*/
|
|
|
|
error = sysfs_groups_change_owner(kobj, ktype->default_groups,
|
|
|
|
kuid, kgid);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_change_owner);
|