提交 30d7030b 编写于 作者: L Linus Torvalds

Merge tag 'configfs-for-5.3' of git://git.infradead.org/users/hch/configfs

Pull configfs fixes from Christoph Hellwig:
 "Late configfs fixes from Al that fix pretty nasty removal vs attribute
  access races"

* tag 'configfs-for-5.3' of git://git.infradead.org/users/hch/configfs:
  configfs: provide exclusion between IO and removals
  configfs: new object reprsenting tree fragments
  configfs_register_group() shouldn't be (and isn't) called in rmdirable parts
  configfs: stash the data we need into configfs_buffer at open time
...@@ -20,6 +20,15 @@ ...@@ -20,6 +20,15 @@
#include <linux/list.h> #include <linux/list.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
struct configfs_fragment {
atomic_t frag_count;
struct rw_semaphore frag_sem;
bool frag_dead;
};
void put_fragment(struct configfs_fragment *);
struct configfs_fragment *get_fragment(struct configfs_fragment *);
struct configfs_dirent { struct configfs_dirent {
atomic_t s_count; atomic_t s_count;
int s_dependent_count; int s_dependent_count;
...@@ -34,6 +43,7 @@ struct configfs_dirent { ...@@ -34,6 +43,7 @@ struct configfs_dirent {
#ifdef CONFIG_LOCKDEP #ifdef CONFIG_LOCKDEP
int s_depth; int s_depth;
#endif #endif
struct configfs_fragment *s_frag;
}; };
#define CONFIGFS_ROOT 0x0001 #define CONFIGFS_ROOT 0x0001
...@@ -61,8 +71,8 @@ extern int configfs_create(struct dentry *, umode_t mode, void (*init)(struct in ...@@ -61,8 +71,8 @@ extern int configfs_create(struct dentry *, umode_t mode, void (*init)(struct in
extern int configfs_create_file(struct config_item *, const struct configfs_attribute *); extern int configfs_create_file(struct config_item *, const struct configfs_attribute *);
extern int configfs_create_bin_file(struct config_item *, extern int configfs_create_bin_file(struct config_item *,
const struct configfs_bin_attribute *); const struct configfs_bin_attribute *);
extern int configfs_make_dirent(struct configfs_dirent *, extern int configfs_make_dirent(struct configfs_dirent *, struct dentry *,
struct dentry *, void *, umode_t, int); void *, umode_t, int, struct configfs_fragment *);
extern int configfs_dirent_is_ready(struct configfs_dirent *); extern int configfs_dirent_is_ready(struct configfs_dirent *);
extern void configfs_hash_and_remove(struct dentry * dir, const char * name); extern void configfs_hash_and_remove(struct dentry * dir, const char * name);
...@@ -137,6 +147,7 @@ static inline void release_configfs_dirent(struct configfs_dirent * sd) ...@@ -137,6 +147,7 @@ static inline void release_configfs_dirent(struct configfs_dirent * sd)
{ {
if (!(sd->s_type & CONFIGFS_ROOT)) { if (!(sd->s_type & CONFIGFS_ROOT)) {
kfree(sd->s_iattr); kfree(sd->s_iattr);
put_fragment(sd->s_frag);
kmem_cache_free(configfs_dir_cachep, sd); kmem_cache_free(configfs_dir_cachep, sd);
} }
} }
......
...@@ -151,11 +151,38 @@ configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd) ...@@ -151,11 +151,38 @@ configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
#endif /* CONFIG_LOCKDEP */ #endif /* CONFIG_LOCKDEP */
static struct configfs_fragment *new_fragment(void)
{
struct configfs_fragment *p;
p = kmalloc(sizeof(struct configfs_fragment), GFP_KERNEL);
if (p) {
atomic_set(&p->frag_count, 1);
init_rwsem(&p->frag_sem);
p->frag_dead = false;
}
return p;
}
void put_fragment(struct configfs_fragment *frag)
{
if (frag && atomic_dec_and_test(&frag->frag_count))
kfree(frag);
}
struct configfs_fragment *get_fragment(struct configfs_fragment *frag)
{
if (likely(frag))
atomic_inc(&frag->frag_count);
return frag;
}
/* /*
* Allocates a new configfs_dirent and links it to the parent configfs_dirent * Allocates a new configfs_dirent and links it to the parent configfs_dirent
*/ */
static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd, static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
void *element, int type) void *element, int type,
struct configfs_fragment *frag)
{ {
struct configfs_dirent * sd; struct configfs_dirent * sd;
...@@ -175,6 +202,7 @@ static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *paren ...@@ -175,6 +202,7 @@ static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *paren
kmem_cache_free(configfs_dir_cachep, sd); kmem_cache_free(configfs_dir_cachep, sd);
return ERR_PTR(-ENOENT); return ERR_PTR(-ENOENT);
} }
sd->s_frag = get_fragment(frag);
list_add(&sd->s_sibling, &parent_sd->s_children); list_add(&sd->s_sibling, &parent_sd->s_children);
spin_unlock(&configfs_dirent_lock); spin_unlock(&configfs_dirent_lock);
...@@ -209,11 +237,11 @@ static int configfs_dirent_exists(struct configfs_dirent *parent_sd, ...@@ -209,11 +237,11 @@ static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
int configfs_make_dirent(struct configfs_dirent * parent_sd, int configfs_make_dirent(struct configfs_dirent * parent_sd,
struct dentry * dentry, void * element, struct dentry * dentry, void * element,
umode_t mode, int type) umode_t mode, int type, struct configfs_fragment *frag)
{ {
struct configfs_dirent * sd; struct configfs_dirent * sd;
sd = configfs_new_dirent(parent_sd, element, type); sd = configfs_new_dirent(parent_sd, element, type, frag);
if (IS_ERR(sd)) if (IS_ERR(sd))
return PTR_ERR(sd); return PTR_ERR(sd);
...@@ -260,7 +288,8 @@ static void init_symlink(struct inode * inode) ...@@ -260,7 +288,8 @@ static void init_symlink(struct inode * inode)
* until it is validated by configfs_dir_set_ready() * until it is validated by configfs_dir_set_ready()
*/ */
static int configfs_create_dir(struct config_item *item, struct dentry *dentry) static int configfs_create_dir(struct config_item *item, struct dentry *dentry,
struct configfs_fragment *frag)
{ {
int error; int error;
umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
...@@ -273,7 +302,8 @@ static int configfs_create_dir(struct config_item *item, struct dentry *dentry) ...@@ -273,7 +302,8 @@ static int configfs_create_dir(struct config_item *item, struct dentry *dentry)
return error; return error;
error = configfs_make_dirent(p->d_fsdata, dentry, item, mode, error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
CONFIGFS_DIR | CONFIGFS_USET_CREATING); CONFIGFS_DIR | CONFIGFS_USET_CREATING,
frag);
if (unlikely(error)) if (unlikely(error))
return error; return error;
...@@ -338,9 +368,10 @@ int configfs_create_link(struct configfs_symlink *sl, ...@@ -338,9 +368,10 @@ int configfs_create_link(struct configfs_symlink *sl,
{ {
int err = 0; int err = 0;
umode_t mode = S_IFLNK | S_IRWXUGO; umode_t mode = S_IFLNK | S_IRWXUGO;
struct configfs_dirent *p = parent->d_fsdata;
err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode, err = configfs_make_dirent(p, dentry, sl, mode,
CONFIGFS_ITEM_LINK); CONFIGFS_ITEM_LINK, p->s_frag);
if (!err) { if (!err) {
err = configfs_create(dentry, mode, init_symlink); err = configfs_create(dentry, mode, init_symlink);
if (err) { if (err) {
...@@ -599,7 +630,8 @@ static int populate_attrs(struct config_item *item) ...@@ -599,7 +630,8 @@ static int populate_attrs(struct config_item *item)
static int configfs_attach_group(struct config_item *parent_item, static int configfs_attach_group(struct config_item *parent_item,
struct config_item *item, struct config_item *item,
struct dentry *dentry); struct dentry *dentry,
struct configfs_fragment *frag);
static void configfs_detach_group(struct config_item *item); static void configfs_detach_group(struct config_item *item);
static void detach_groups(struct config_group *group) static void detach_groups(struct config_group *group)
...@@ -647,7 +679,8 @@ static void detach_groups(struct config_group *group) ...@@ -647,7 +679,8 @@ static void detach_groups(struct config_group *group)
* try using vfs_mkdir. Just a thought. * try using vfs_mkdir. Just a thought.
*/ */
static int create_default_group(struct config_group *parent_group, static int create_default_group(struct config_group *parent_group,
struct config_group *group) struct config_group *group,
struct configfs_fragment *frag)
{ {
int ret; int ret;
struct configfs_dirent *sd; struct configfs_dirent *sd;
...@@ -663,7 +696,7 @@ static int create_default_group(struct config_group *parent_group, ...@@ -663,7 +696,7 @@ static int create_default_group(struct config_group *parent_group,
d_add(child, NULL); d_add(child, NULL);
ret = configfs_attach_group(&parent_group->cg_item, ret = configfs_attach_group(&parent_group->cg_item,
&group->cg_item, child); &group->cg_item, child, frag);
if (!ret) { if (!ret) {
sd = child->d_fsdata; sd = child->d_fsdata;
sd->s_type |= CONFIGFS_USET_DEFAULT; sd->s_type |= CONFIGFS_USET_DEFAULT;
...@@ -677,13 +710,14 @@ static int create_default_group(struct config_group *parent_group, ...@@ -677,13 +710,14 @@ static int create_default_group(struct config_group *parent_group,
return ret; return ret;
} }
static int populate_groups(struct config_group *group) static int populate_groups(struct config_group *group,
struct configfs_fragment *frag)
{ {
struct config_group *new_group; struct config_group *new_group;
int ret = 0; int ret = 0;
list_for_each_entry(new_group, &group->default_groups, group_entry) { list_for_each_entry(new_group, &group->default_groups, group_entry) {
ret = create_default_group(group, new_group); ret = create_default_group(group, new_group, frag);
if (ret) { if (ret) {
detach_groups(group); detach_groups(group);
break; break;
...@@ -797,11 +831,12 @@ static void link_group(struct config_group *parent_group, struct config_group *g ...@@ -797,11 +831,12 @@ static void link_group(struct config_group *parent_group, struct config_group *g
*/ */
static int configfs_attach_item(struct config_item *parent_item, static int configfs_attach_item(struct config_item *parent_item,
struct config_item *item, struct config_item *item,
struct dentry *dentry) struct dentry *dentry,
struct configfs_fragment *frag)
{ {
int ret; int ret;
ret = configfs_create_dir(item, dentry); ret = configfs_create_dir(item, dentry, frag);
if (!ret) { if (!ret) {
ret = populate_attrs(item); ret = populate_attrs(item);
if (ret) { if (ret) {
...@@ -831,12 +866,13 @@ static void configfs_detach_item(struct config_item *item) ...@@ -831,12 +866,13 @@ static void configfs_detach_item(struct config_item *item)
static int configfs_attach_group(struct config_item *parent_item, static int configfs_attach_group(struct config_item *parent_item,
struct config_item *item, struct config_item *item,
struct dentry *dentry) struct dentry *dentry,
struct configfs_fragment *frag)
{ {
int ret; int ret;
struct configfs_dirent *sd; struct configfs_dirent *sd;
ret = configfs_attach_item(parent_item, item, dentry); ret = configfs_attach_item(parent_item, item, dentry, frag);
if (!ret) { if (!ret) {
sd = dentry->d_fsdata; sd = dentry->d_fsdata;
sd->s_type |= CONFIGFS_USET_DIR; sd->s_type |= CONFIGFS_USET_DIR;
...@@ -852,7 +888,7 @@ static int configfs_attach_group(struct config_item *parent_item, ...@@ -852,7 +888,7 @@ static int configfs_attach_group(struct config_item *parent_item,
*/ */
inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD); inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
configfs_adjust_dir_dirent_depth_before_populate(sd); configfs_adjust_dir_dirent_depth_before_populate(sd);
ret = populate_groups(to_config_group(item)); ret = populate_groups(to_config_group(item), frag);
if (ret) { if (ret) {
configfs_detach_item(item); configfs_detach_item(item);
d_inode(dentry)->i_flags |= S_DEAD; d_inode(dentry)->i_flags |= S_DEAD;
...@@ -1247,6 +1283,7 @@ static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode ...@@ -1247,6 +1283,7 @@ static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode
struct configfs_dirent *sd; struct configfs_dirent *sd;
const struct config_item_type *type; const struct config_item_type *type;
struct module *subsys_owner = NULL, *new_item_owner = NULL; struct module *subsys_owner = NULL, *new_item_owner = NULL;
struct configfs_fragment *frag;
char *name; char *name;
sd = dentry->d_parent->d_fsdata; sd = dentry->d_parent->d_fsdata;
...@@ -1265,6 +1302,12 @@ static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode ...@@ -1265,6 +1302,12 @@ static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode
goto out; goto out;
} }
frag = new_fragment();
if (!frag) {
ret = -ENOMEM;
goto out;
}
/* Get a working ref for the duration of this function */ /* Get a working ref for the duration of this function */
parent_item = configfs_get_config_item(dentry->d_parent); parent_item = configfs_get_config_item(dentry->d_parent);
type = parent_item->ci_type; type = parent_item->ci_type;
...@@ -1367,9 +1410,9 @@ static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode ...@@ -1367,9 +1410,9 @@ static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode
spin_unlock(&configfs_dirent_lock); spin_unlock(&configfs_dirent_lock);
if (group) if (group)
ret = configfs_attach_group(parent_item, item, dentry); ret = configfs_attach_group(parent_item, item, dentry, frag);
else else
ret = configfs_attach_item(parent_item, item, dentry); ret = configfs_attach_item(parent_item, item, dentry, frag);
spin_lock(&configfs_dirent_lock); spin_lock(&configfs_dirent_lock);
sd->s_type &= ~CONFIGFS_USET_IN_MKDIR; sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
...@@ -1406,6 +1449,7 @@ static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode ...@@ -1406,6 +1449,7 @@ static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode
* reference. * reference.
*/ */
config_item_put(parent_item); config_item_put(parent_item);
put_fragment(frag);
out: out:
return ret; return ret;
...@@ -1417,6 +1461,7 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry) ...@@ -1417,6 +1461,7 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
struct config_item *item; struct config_item *item;
struct configfs_subsystem *subsys; struct configfs_subsystem *subsys;
struct configfs_dirent *sd; struct configfs_dirent *sd;
struct configfs_fragment *frag;
struct module *subsys_owner = NULL, *dead_item_owner = NULL; struct module *subsys_owner = NULL, *dead_item_owner = NULL;
int ret; int ret;
...@@ -1474,6 +1519,16 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry) ...@@ -1474,6 +1519,16 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
} }
} while (ret == -EAGAIN); } while (ret == -EAGAIN);
frag = sd->s_frag;
if (down_write_killable(&frag->frag_sem)) {
spin_lock(&configfs_dirent_lock);
configfs_detach_rollback(dentry);
spin_unlock(&configfs_dirent_lock);
return -EINTR;
}
frag->frag_dead = true;
up_write(&frag->frag_sem);
/* Get a working ref for the duration of this function */ /* Get a working ref for the duration of this function */
item = configfs_get_config_item(dentry); item = configfs_get_config_item(dentry);
...@@ -1574,7 +1629,7 @@ static int configfs_dir_open(struct inode *inode, struct file *file) ...@@ -1574,7 +1629,7 @@ static int configfs_dir_open(struct inode *inode, struct file *file)
*/ */
err = -ENOENT; err = -ENOENT;
if (configfs_dirent_is_ready(parent_sd)) { if (configfs_dirent_is_ready(parent_sd)) {
file->private_data = configfs_new_dirent(parent_sd, NULL, 0); file->private_data = configfs_new_dirent(parent_sd, NULL, 0, NULL);
if (IS_ERR(file->private_data)) if (IS_ERR(file->private_data))
err = PTR_ERR(file->private_data); err = PTR_ERR(file->private_data);
else else
...@@ -1732,8 +1787,13 @@ int configfs_register_group(struct config_group *parent_group, ...@@ -1732,8 +1787,13 @@ int configfs_register_group(struct config_group *parent_group,
{ {
struct configfs_subsystem *subsys = parent_group->cg_subsys; struct configfs_subsystem *subsys = parent_group->cg_subsys;
struct dentry *parent; struct dentry *parent;
struct configfs_fragment *frag;
int ret; int ret;
frag = new_fragment();
if (!frag)
return -ENOMEM;
mutex_lock(&subsys->su_mutex); mutex_lock(&subsys->su_mutex);
link_group(parent_group, group); link_group(parent_group, group);
mutex_unlock(&subsys->su_mutex); mutex_unlock(&subsys->su_mutex);
...@@ -1741,7 +1801,7 @@ int configfs_register_group(struct config_group *parent_group, ...@@ -1741,7 +1801,7 @@ int configfs_register_group(struct config_group *parent_group,
parent = parent_group->cg_item.ci_dentry; parent = parent_group->cg_item.ci_dentry;
inode_lock_nested(d_inode(parent), I_MUTEX_PARENT); inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
ret = create_default_group(parent_group, group); ret = create_default_group(parent_group, group, frag);
if (ret) if (ret)
goto err_out; goto err_out;
...@@ -1749,12 +1809,14 @@ int configfs_register_group(struct config_group *parent_group, ...@@ -1749,12 +1809,14 @@ int configfs_register_group(struct config_group *parent_group,
configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata); configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
spin_unlock(&configfs_dirent_lock); spin_unlock(&configfs_dirent_lock);
inode_unlock(d_inode(parent)); inode_unlock(d_inode(parent));
put_fragment(frag);
return 0; return 0;
err_out: err_out:
inode_unlock(d_inode(parent)); inode_unlock(d_inode(parent));
mutex_lock(&subsys->su_mutex); mutex_lock(&subsys->su_mutex);
unlink_group(group); unlink_group(group);
mutex_unlock(&subsys->su_mutex); mutex_unlock(&subsys->su_mutex);
put_fragment(frag);
return ret; return ret;
} }
EXPORT_SYMBOL(configfs_register_group); EXPORT_SYMBOL(configfs_register_group);
...@@ -1770,16 +1832,12 @@ void configfs_unregister_group(struct config_group *group) ...@@ -1770,16 +1832,12 @@ void configfs_unregister_group(struct config_group *group)
struct configfs_subsystem *subsys = group->cg_subsys; struct configfs_subsystem *subsys = group->cg_subsys;
struct dentry *dentry = group->cg_item.ci_dentry; struct dentry *dentry = group->cg_item.ci_dentry;
struct dentry *parent = group->cg_item.ci_parent->ci_dentry; struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
struct configfs_dirent *sd = dentry->d_fsdata;
struct configfs_fragment *frag = sd->s_frag;
mutex_lock(&subsys->su_mutex); down_write(&frag->frag_sem);
if (!group->cg_item.ci_parent->ci_group) { frag->frag_dead = true;
/* up_write(&frag->frag_sem);
* The parent has already been unlinked and detached
* due to a rmdir.
*/
goto unlink_group;
}
mutex_unlock(&subsys->su_mutex);
inode_lock_nested(d_inode(parent), I_MUTEX_PARENT); inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
spin_lock(&configfs_dirent_lock); spin_lock(&configfs_dirent_lock);
...@@ -1796,7 +1854,6 @@ void configfs_unregister_group(struct config_group *group) ...@@ -1796,7 +1854,6 @@ void configfs_unregister_group(struct config_group *group)
dput(dentry); dput(dentry);
mutex_lock(&subsys->su_mutex); mutex_lock(&subsys->su_mutex);
unlink_group:
unlink_group(group); unlink_group(group);
mutex_unlock(&subsys->su_mutex); mutex_unlock(&subsys->su_mutex);
} }
...@@ -1853,10 +1910,17 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys) ...@@ -1853,10 +1910,17 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys)
struct dentry *dentry; struct dentry *dentry;
struct dentry *root; struct dentry *root;
struct configfs_dirent *sd; struct configfs_dirent *sd;
struct configfs_fragment *frag;
frag = new_fragment();
if (!frag)
return -ENOMEM;
root = configfs_pin_fs(); root = configfs_pin_fs();
if (IS_ERR(root)) if (IS_ERR(root)) {
put_fragment(frag);
return PTR_ERR(root); return PTR_ERR(root);
}
if (!group->cg_item.ci_name) if (!group->cg_item.ci_name)
group->cg_item.ci_name = group->cg_item.ci_namebuf; group->cg_item.ci_name = group->cg_item.ci_namebuf;
...@@ -1872,7 +1936,7 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys) ...@@ -1872,7 +1936,7 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys)
d_add(dentry, NULL); d_add(dentry, NULL);
err = configfs_attach_group(sd->s_element, &group->cg_item, err = configfs_attach_group(sd->s_element, &group->cg_item,
dentry); dentry, frag);
if (err) { if (err) {
BUG_ON(d_inode(dentry)); BUG_ON(d_inode(dentry));
d_drop(dentry); d_drop(dentry);
...@@ -1890,6 +1954,7 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys) ...@@ -1890,6 +1954,7 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys)
unlink_group(group); unlink_group(group);
configfs_release_fs(); configfs_release_fs();
} }
put_fragment(frag);
return err; return err;
} }
...@@ -1899,12 +1964,18 @@ void configfs_unregister_subsystem(struct configfs_subsystem *subsys) ...@@ -1899,12 +1964,18 @@ void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
struct config_group *group = &subsys->su_group; struct config_group *group = &subsys->su_group;
struct dentry *dentry = group->cg_item.ci_dentry; struct dentry *dentry = group->cg_item.ci_dentry;
struct dentry *root = dentry->d_sb->s_root; struct dentry *root = dentry->d_sb->s_root;
struct configfs_dirent *sd = dentry->d_fsdata;
struct configfs_fragment *frag = sd->s_frag;
if (dentry->d_parent != root) { if (dentry->d_parent != root) {
pr_err("Tried to unregister non-subsystem!\n"); pr_err("Tried to unregister non-subsystem!\n");
return; return;
} }
down_write(&frag->frag_sem);
frag->frag_dead = true;
up_write(&frag->frag_sem);
inode_lock_nested(d_inode(root), inode_lock_nested(d_inode(root),
I_MUTEX_PARENT); I_MUTEX_PARENT);
inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD); inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
......
...@@ -39,40 +39,44 @@ struct configfs_buffer { ...@@ -39,40 +39,44 @@ struct configfs_buffer {
bool write_in_progress; bool write_in_progress;
char *bin_buffer; char *bin_buffer;
int bin_buffer_size; int bin_buffer_size;
int cb_max_size;
struct config_item *item;
struct module *owner;
union {
struct configfs_attribute *attr;
struct configfs_bin_attribute *bin_attr;
};
}; };
static inline struct configfs_fragment *to_frag(struct file *file)
{
struct configfs_dirent *sd = file->f_path.dentry->d_fsdata;
/** return sd->s_frag;
* fill_read_buffer - allocate and fill buffer from item. }
* @dentry: dentry pointer.
* @buffer: data buffer for file. static int fill_read_buffer(struct file *file, struct configfs_buffer *buffer)
*
* Allocate @buffer->page, if it hasn't been already, then call the
* config_item's show() method to fill the buffer with this attribute's
* data.
* This is called only once, on the file's first read.
*/
static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)
{ {
struct configfs_attribute * attr = to_attr(dentry); struct configfs_fragment *frag = to_frag(file);
struct config_item * item = to_item(dentry->d_parent); ssize_t count = -ENOENT;
int ret = 0;
ssize_t count;
if (!buffer->page) if (!buffer->page)
buffer->page = (char *) get_zeroed_page(GFP_KERNEL); buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
if (!buffer->page) if (!buffer->page)
return -ENOMEM; return -ENOMEM;
count = attr->show(item, buffer->page); down_read(&frag->frag_sem);
if (!frag->frag_dead)
BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE); count = buffer->attr->show(buffer->item, buffer->page);
if (count >= 0) { up_read(&frag->frag_sem);
buffer->needs_read_fill = 0;
buffer->count = count; if (count < 0)
} else return count;
ret = count; if (WARN_ON_ONCE(count > (ssize_t)SIMPLE_ATTR_SIZE))
return ret; return -EIO;
buffer->needs_read_fill = 0;
buffer->count = count;
return 0;
} }
/** /**
...@@ -97,12 +101,13 @@ static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buf ...@@ -97,12 +101,13 @@ static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buf
static ssize_t static ssize_t
configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{ {
struct configfs_buffer * buffer = file->private_data; struct configfs_buffer *buffer = file->private_data;
ssize_t retval = 0; ssize_t retval = 0;
mutex_lock(&buffer->mutex); mutex_lock(&buffer->mutex);
if (buffer->needs_read_fill) { if (buffer->needs_read_fill) {
if ((retval = fill_read_buffer(file->f_path.dentry,buffer))) retval = fill_read_buffer(file, buffer);
if (retval)
goto out; goto out;
} }
pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n", pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
...@@ -138,10 +143,8 @@ static ssize_t ...@@ -138,10 +143,8 @@ static ssize_t
configfs_read_bin_file(struct file *file, char __user *buf, configfs_read_bin_file(struct file *file, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct configfs_fragment *frag = to_frag(file);
struct configfs_buffer *buffer = file->private_data; struct configfs_buffer *buffer = file->private_data;
struct dentry *dentry = file->f_path.dentry;
struct config_item *item = to_item(dentry->d_parent);
struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
ssize_t retval = 0; ssize_t retval = 0;
ssize_t len = min_t(size_t, count, PAGE_SIZE); ssize_t len = min_t(size_t, count, PAGE_SIZE);
...@@ -156,14 +159,19 @@ configfs_read_bin_file(struct file *file, char __user *buf, ...@@ -156,14 +159,19 @@ configfs_read_bin_file(struct file *file, char __user *buf,
if (buffer->needs_read_fill) { if (buffer->needs_read_fill) {
/* perform first read with buf == NULL to get extent */ /* perform first read with buf == NULL to get extent */
len = bin_attr->read(item, NULL, 0); down_read(&frag->frag_sem);
if (!frag->frag_dead)
len = buffer->bin_attr->read(buffer->item, NULL, 0);
else
len = -ENOENT;
up_read(&frag->frag_sem);
if (len <= 0) { if (len <= 0) {
retval = len; retval = len;
goto out; goto out;
} }
/* do not exceed the maximum value */ /* do not exceed the maximum value */
if (bin_attr->cb_max_size && len > bin_attr->cb_max_size) { if (buffer->cb_max_size && len > buffer->cb_max_size) {
retval = -EFBIG; retval = -EFBIG;
goto out; goto out;
} }
...@@ -176,7 +184,13 @@ configfs_read_bin_file(struct file *file, char __user *buf, ...@@ -176,7 +184,13 @@ configfs_read_bin_file(struct file *file, char __user *buf,
buffer->bin_buffer_size = len; buffer->bin_buffer_size = len;
/* perform second read to fill buffer */ /* perform second read to fill buffer */
len = bin_attr->read(item, buffer->bin_buffer, len); down_read(&frag->frag_sem);
if (!frag->frag_dead)
len = buffer->bin_attr->read(buffer->item,
buffer->bin_buffer, len);
else
len = -ENOENT;
up_read(&frag->frag_sem);
if (len < 0) { if (len < 0) {
retval = len; retval = len;
vfree(buffer->bin_buffer); vfree(buffer->bin_buffer);
...@@ -226,25 +240,17 @@ fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size ...@@ -226,25 +240,17 @@ fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size
return error ? -EFAULT : count; return error ? -EFAULT : count;
} }
/**
* flush_write_buffer - push buffer to config_item.
* @dentry: dentry to the attribute
* @buffer: data buffer for file.
* @count: number of bytes
*
* Get the correct pointers for the config_item and the attribute we're
* dealing with, then call the store() method for the attribute,
* passing the buffer that we acquired in fill_write_buffer().
*/
static int static int
flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count) flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
{ {
struct configfs_attribute * attr = to_attr(dentry); struct configfs_fragment *frag = to_frag(file);
struct config_item * item = to_item(dentry->d_parent); int res = -ENOENT;
return attr->store(item, buffer->page, count); down_read(&frag->frag_sem);
if (!frag->frag_dead)
res = buffer->attr->store(buffer->item, buffer->page, count);
up_read(&frag->frag_sem);
return res;
} }
...@@ -268,13 +274,13 @@ flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size ...@@ -268,13 +274,13 @@ flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size
static ssize_t static ssize_t
configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{ {
struct configfs_buffer * buffer = file->private_data; struct configfs_buffer *buffer = file->private_data;
ssize_t len; ssize_t len;
mutex_lock(&buffer->mutex); mutex_lock(&buffer->mutex);
len = fill_write_buffer(buffer, buf, count); len = fill_write_buffer(buffer, buf, count);
if (len > 0) if (len > 0)
len = flush_write_buffer(file->f_path.dentry, buffer, len); len = flush_write_buffer(file, buffer, len);
if (len > 0) if (len > 0)
*ppos += len; *ppos += len;
mutex_unlock(&buffer->mutex); mutex_unlock(&buffer->mutex);
...@@ -299,8 +305,6 @@ configfs_write_bin_file(struct file *file, const char __user *buf, ...@@ -299,8 +305,6 @@ configfs_write_bin_file(struct file *file, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct configfs_buffer *buffer = file->private_data; struct configfs_buffer *buffer = file->private_data;
struct dentry *dentry = file->f_path.dentry;
struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
void *tbuf = NULL; void *tbuf = NULL;
ssize_t len; ssize_t len;
...@@ -316,8 +320,8 @@ configfs_write_bin_file(struct file *file, const char __user *buf, ...@@ -316,8 +320,8 @@ configfs_write_bin_file(struct file *file, const char __user *buf,
/* buffer grows? */ /* buffer grows? */
if (*ppos + count > buffer->bin_buffer_size) { if (*ppos + count > buffer->bin_buffer_size) {
if (bin_attr->cb_max_size && if (buffer->cb_max_size &&
*ppos + count > bin_attr->cb_max_size) { *ppos + count > buffer->cb_max_size) {
len = -EFBIG; len = -EFBIG;
goto out; goto out;
} }
...@@ -349,31 +353,51 @@ configfs_write_bin_file(struct file *file, const char __user *buf, ...@@ -349,31 +353,51 @@ configfs_write_bin_file(struct file *file, const char __user *buf,
return len; return len;
} }
static int check_perm(struct inode * inode, struct file * file, int type) static int __configfs_open_file(struct inode *inode, struct file *file, int type)
{ {
struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent); struct dentry *dentry = file->f_path.dentry;
struct configfs_attribute * attr = to_attr(file->f_path.dentry); struct configfs_fragment *frag = to_frag(file);
struct configfs_bin_attribute *bin_attr = NULL; struct configfs_attribute *attr;
struct configfs_buffer * buffer; struct configfs_buffer *buffer;
struct configfs_item_operations * ops = NULL; int error;
int error = 0;
if (!item || !attr) error = -ENOMEM;
goto Einval; buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
if (!buffer)
goto out;
if (type & CONFIGFS_ITEM_BIN_ATTR) error = -ENOENT;
bin_attr = to_bin_attr(file->f_path.dentry); down_read(&frag->frag_sem);
if (unlikely(frag->frag_dead))
goto out_free_buffer;
/* Grab the module reference for this attribute if we have one */ error = -EINVAL;
if (!try_module_get(attr->ca_owner)) { buffer->item = to_item(dentry->d_parent);
error = -ENODEV; if (!buffer->item)
goto Done; goto out_free_buffer;
attr = to_attr(dentry);
if (!attr)
goto out_put_item;
if (type & CONFIGFS_ITEM_BIN_ATTR) {
buffer->bin_attr = to_bin_attr(dentry);
buffer->cb_max_size = buffer->bin_attr->cb_max_size;
} else {
buffer->attr = attr;
} }
if (item->ci_type) buffer->owner = attr->ca_owner;
ops = item->ci_type->ct_item_ops; /* Grab the module reference for this attribute if we have one */
else error = -ENODEV;
goto Eaccess; if (!try_module_get(buffer->owner))
goto out_put_item;
error = -EACCES;
if (!buffer->item->ci_type)
goto out_put_module;
buffer->ops = buffer->item->ci_type->ct_item_ops;
/* File needs write support. /* File needs write support.
* The inode's perms must say it's ok, * The inode's perms must say it's ok,
...@@ -381,13 +405,11 @@ static int check_perm(struct inode * inode, struct file * file, int type) ...@@ -381,13 +405,11 @@ static int check_perm(struct inode * inode, struct file * file, int type)
*/ */
if (file->f_mode & FMODE_WRITE) { if (file->f_mode & FMODE_WRITE) {
if (!(inode->i_mode & S_IWUGO)) if (!(inode->i_mode & S_IWUGO))
goto Eaccess; goto out_put_module;
if ((type & CONFIGFS_ITEM_ATTR) && !attr->store) if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
goto Eaccess; goto out_put_module;
if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write)
if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->write) goto out_put_module;
goto Eaccess;
} }
/* File needs read support. /* File needs read support.
...@@ -396,92 +418,72 @@ static int check_perm(struct inode * inode, struct file * file, int type) ...@@ -396,92 +418,72 @@ static int check_perm(struct inode * inode, struct file * file, int type)
*/ */
if (file->f_mode & FMODE_READ) { if (file->f_mode & FMODE_READ) {
if (!(inode->i_mode & S_IRUGO)) if (!(inode->i_mode & S_IRUGO))
goto Eaccess; goto out_put_module;
if ((type & CONFIGFS_ITEM_ATTR) && !attr->show) if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
goto Eaccess; goto out_put_module;
if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read)
if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->read) goto out_put_module;
goto Eaccess;
} }
/* No error? Great, allocate a buffer for the file, and store it
* it in file->private_data for easy access.
*/
buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
if (!buffer) {
error = -ENOMEM;
goto Enomem;
}
mutex_init(&buffer->mutex); mutex_init(&buffer->mutex);
buffer->needs_read_fill = 1; buffer->needs_read_fill = 1;
buffer->read_in_progress = false; buffer->read_in_progress = false;
buffer->write_in_progress = false; buffer->write_in_progress = false;
buffer->ops = ops;
file->private_data = buffer; file->private_data = buffer;
goto Done; up_read(&frag->frag_sem);
return 0;
Einval: out_put_module:
error = -EINVAL; module_put(buffer->owner);
goto Done; out_put_item:
Eaccess: config_item_put(buffer->item);
error = -EACCES; out_free_buffer:
Enomem: up_read(&frag->frag_sem);
module_put(attr->ca_owner); kfree(buffer);
Done: out:
if (error && item)
config_item_put(item);
return error; return error;
} }
static int configfs_release(struct inode *inode, struct file *filp) static int configfs_release(struct inode *inode, struct file *filp)
{ {
struct config_item * item = to_item(filp->f_path.dentry->d_parent); struct configfs_buffer *buffer = filp->private_data;
struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
struct module * owner = attr->ca_owner; module_put(buffer->owner);
struct configfs_buffer * buffer = filp->private_data; if (buffer->page)
free_page((unsigned long)buffer->page);
if (item) mutex_destroy(&buffer->mutex);
config_item_put(item); kfree(buffer);
/* After this point, attr should not be accessed. */
module_put(owner);
if (buffer) {
if (buffer->page)
free_page((unsigned long)buffer->page);
mutex_destroy(&buffer->mutex);
kfree(buffer);
}
return 0; return 0;
} }
static int configfs_open_file(struct inode *inode, struct file *filp) static int configfs_open_file(struct inode *inode, struct file *filp)
{ {
return check_perm(inode, filp, CONFIGFS_ITEM_ATTR); return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
} }
static int configfs_open_bin_file(struct inode *inode, struct file *filp) static int configfs_open_bin_file(struct inode *inode, struct file *filp)
{ {
return check_perm(inode, filp, CONFIGFS_ITEM_BIN_ATTR); return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
} }
static int configfs_release_bin_file(struct inode *inode, struct file *filp) static int configfs_release_bin_file(struct inode *inode, struct file *file)
{ {
struct configfs_buffer *buffer = filp->private_data; struct configfs_buffer *buffer = file->private_data;
struct dentry *dentry = filp->f_path.dentry;
struct config_item *item = to_item(dentry->d_parent);
struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
ssize_t len = 0;
int ret;
buffer->read_in_progress = false; buffer->read_in_progress = false;
if (buffer->write_in_progress) { if (buffer->write_in_progress) {
struct configfs_fragment *frag = to_frag(file);
buffer->write_in_progress = false; buffer->write_in_progress = false;
len = bin_attr->write(item, buffer->bin_buffer, down_read(&frag->frag_sem);
buffer->bin_buffer_size); if (!frag->frag_dead) {
/* result of ->release() is ignored */
buffer->bin_attr->write(buffer->item,
buffer->bin_buffer,
buffer->bin_buffer_size);
}
up_read(&frag->frag_sem);
/* vfree on NULL is safe */ /* vfree on NULL is safe */
vfree(buffer->bin_buffer); vfree(buffer->bin_buffer);
buffer->bin_buffer = NULL; buffer->bin_buffer = NULL;
...@@ -489,10 +491,8 @@ static int configfs_release_bin_file(struct inode *inode, struct file *filp) ...@@ -489,10 +491,8 @@ static int configfs_release_bin_file(struct inode *inode, struct file *filp)
buffer->needs_read_fill = 1; buffer->needs_read_fill = 1;
} }
ret = configfs_release(inode, filp); configfs_release(inode, file);
if (len < 0) return 0;
return len;
return ret;
} }
...@@ -527,7 +527,7 @@ int configfs_create_file(struct config_item * item, const struct configfs_attrib ...@@ -527,7 +527,7 @@ int configfs_create_file(struct config_item * item, const struct configfs_attrib
inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL); inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
CONFIGFS_ITEM_ATTR); CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
inode_unlock(d_inode(dir)); inode_unlock(d_inode(dir));
return error; return error;
...@@ -549,7 +549,7 @@ int configfs_create_bin_file(struct config_item *item, ...@@ -549,7 +549,7 @@ int configfs_create_bin_file(struct config_item *item,
inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL); inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode, error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
CONFIGFS_ITEM_BIN_ATTR); CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
inode_unlock(dir->d_inode); inode_unlock(dir->d_inode);
return error; return error;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册