aboutsummaryrefslogtreecommitdiffstats
path: root/fs/sysfs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/sysfs')
-rw-r--r--fs/sysfs/dir.c58
-rw-r--r--fs/sysfs/mount.c4
2 files changed, 41 insertions, 21 deletions
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 2fbdff6be25c..e8e0e71b29d5 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -165,21 +165,8 @@ struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
165 if (unlikely(!sd)) 165 if (unlikely(!sd))
166 return NULL; 166 return NULL;
167 167
168 while (1) { 168 if (!atomic_inc_unless_negative(&sd->s_active))
169 int v, t; 169 return NULL;
170
171 v = atomic_read(&sd->s_active);
172 if (unlikely(v < 0))
173 return NULL;
174
175 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
176 if (likely(t == v))
177 break;
178 if (t < 0)
179 return NULL;
180
181 cpu_relax();
182 }
183 170
184 if (likely(!ignore_lockdep(sd))) 171 if (likely(!ignore_lockdep(sd)))
185 rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_); 172 rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
@@ -281,6 +268,10 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
281 */ 268 */
282 parent_sd = sd->s_parent; 269 parent_sd = sd->s_parent;
283 270
271 WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED),
272 "sysfs: free using entry: %s/%s\n",
273 parent_sd ? parent_sd->s_name : "", sd->s_name);
274
284 if (sysfs_type(sd) == SYSFS_KOBJ_LINK) 275 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
285 sysfs_put(sd->s_symlink.target_sd); 276 sysfs_put(sd->s_symlink.target_sd);
286 if (sysfs_type(sd) & SYSFS_COPY_NAME) 277 if (sysfs_type(sd) & SYSFS_COPY_NAME)
@@ -399,7 +390,7 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
399 390
400 sd->s_name = name; 391 sd->s_name = name;
401 sd->s_mode = mode; 392 sd->s_mode = mode;
402 sd->s_flags = type; 393 sd->s_flags = type | SYSFS_FLAG_REMOVED;
403 394
404 return sd; 395 return sd;
405 396
@@ -479,6 +470,9 @@ int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
479 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; 470 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
480 } 471 }
481 472
473 /* Mark the entry added into directory tree */
474 sd->s_flags &= ~SYSFS_FLAG_REMOVED;
475
482 return 0; 476 return 0;
483} 477}
484 478
@@ -1012,6 +1006,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1012 enum kobj_ns_type type; 1006 enum kobj_ns_type type;
1013 const void *ns; 1007 const void *ns;
1014 ino_t ino; 1008 ino_t ino;
1009 loff_t off;
1015 1010
1016 type = sysfs_ns_type(parent_sd); 1011 type = sysfs_ns_type(parent_sd);
1017 ns = sysfs_info(dentry->d_sb)->ns[type]; 1012 ns = sysfs_info(dentry->d_sb)->ns[type];
@@ -1020,6 +1015,8 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1020 ino = parent_sd->s_ino; 1015 ino = parent_sd->s_ino;
1021 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0) 1016 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
1022 filp->f_pos++; 1017 filp->f_pos++;
1018 else
1019 return 0;
1023 } 1020 }
1024 if (filp->f_pos == 1) { 1021 if (filp->f_pos == 1) {
1025 if (parent_sd->s_parent) 1022 if (parent_sd->s_parent)
@@ -1028,8 +1025,11 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1028 ino = parent_sd->s_ino; 1025 ino = parent_sd->s_ino;
1029 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0) 1026 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
1030 filp->f_pos++; 1027 filp->f_pos++;
1028 else
1029 return 0;
1031 } 1030 }
1032 mutex_lock(&sysfs_mutex); 1031 mutex_lock(&sysfs_mutex);
1032 off = filp->f_pos;
1033 for (pos = sysfs_dir_pos(ns, parent_sd, filp->f_pos, pos); 1033 for (pos = sysfs_dir_pos(ns, parent_sd, filp->f_pos, pos);
1034 pos; 1034 pos;
1035 pos = sysfs_dir_next_pos(ns, parent_sd, filp->f_pos, pos)) { 1035 pos = sysfs_dir_next_pos(ns, parent_sd, filp->f_pos, pos)) {
@@ -1041,27 +1041,43 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1041 len = strlen(name); 1041 len = strlen(name);
1042 ino = pos->s_ino; 1042 ino = pos->s_ino;
1043 type = dt_type(pos); 1043 type = dt_type(pos);
1044 filp->f_pos = pos->s_hash; 1044 off = filp->f_pos = pos->s_hash;
1045 filp->private_data = sysfs_get(pos); 1045 filp->private_data = sysfs_get(pos);
1046 1046
1047 mutex_unlock(&sysfs_mutex); 1047 mutex_unlock(&sysfs_mutex);
1048 ret = filldir(dirent, name, len, filp->f_pos, ino, type); 1048 ret = filldir(dirent, name, len, off, ino, type);
1049 mutex_lock(&sysfs_mutex); 1049 mutex_lock(&sysfs_mutex);
1050 if (ret < 0) 1050 if (ret < 0)
1051 break; 1051 break;
1052 } 1052 }
1053 mutex_unlock(&sysfs_mutex); 1053 mutex_unlock(&sysfs_mutex);
1054 if ((filp->f_pos > 1) && !pos) { /* EOF */ 1054
1055 filp->f_pos = INT_MAX; 1055 /* don't reference last entry if its refcount is dropped */
1056 if (!pos) {
1056 filp->private_data = NULL; 1057 filp->private_data = NULL;
1058
1059 /* EOF and not changed as 0 or 1 in read/write path */
1060 if (off == filp->f_pos && off > 1)
1061 filp->f_pos = INT_MAX;
1057 } 1062 }
1058 return 0; 1063 return 0;
1059} 1064}
1060 1065
1066static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
1067{
1068 struct inode *inode = file_inode(file);
1069 loff_t ret;
1070
1071 mutex_lock(&inode->i_mutex);
1072 ret = generic_file_llseek(file, offset, whence);
1073 mutex_unlock(&inode->i_mutex);
1074
1075 return ret;
1076}
1061 1077
1062const struct file_operations sysfs_dir_operations = { 1078const struct file_operations sysfs_dir_operations = {
1063 .read = generic_read_dir, 1079 .read = generic_read_dir,
1064 .readdir = sysfs_readdir, 1080 .readdir = sysfs_readdir,
1065 .release = sysfs_dir_release, 1081 .release = sysfs_dir_release,
1066 .llseek = generic_file_llseek, 1082 .llseek = sysfs_dir_llseek,
1067}; 1083};
diff --git a/fs/sysfs/mount.c b/fs/sysfs/mount.c
index 8d924b5ec733..afd83273e6ce 100644
--- a/fs/sysfs/mount.c
+++ b/fs/sysfs/mount.c
@@ -19,6 +19,7 @@
19#include <linux/module.h> 19#include <linux/module.h>
20#include <linux/magic.h> 20#include <linux/magic.h>
21#include <linux/slab.h> 21#include <linux/slab.h>
22#include <linux/user_namespace.h>
22 23
23#include "sysfs.h" 24#include "sysfs.h"
24 25
@@ -111,6 +112,9 @@ static struct dentry *sysfs_mount(struct file_system_type *fs_type,
111 struct super_block *sb; 112 struct super_block *sb;
112 int error; 113 int error;
113 114
115 if (!(flags & MS_KERNMOUNT) && !current_user_ns()->may_mount_sysfs)
116 return ERR_PTR(-EPERM);
117
114 info = kzalloc(sizeof(*info), GFP_KERNEL); 118 info = kzalloc(sizeof(*info), GFP_KERNEL);
115 if (!info) 119 if (!info)
116 return ERR_PTR(-ENOMEM); 120 return ERR_PTR(-ENOMEM);