Skip to content

Commit a1212d2

Browse files
committed
Revert "sysfs: drop kobj_ns_type handling"
This reverts commit cb26a31. It mysteriously causes NetworkManager to not find the wireless device for me. As far as I can tell, Tejun *meant* for this commit to not make any semantic changes, but there clearly are some. So revert it, taking into account some of the calling convention changes that happened in this area in subsequent commits. Cc: Tejun Heo <tj@kernel.org> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0b1e73e commit a1212d2

5 files changed

Lines changed: 122 additions & 47 deletions

File tree

‎fs/sysfs/dir.c‎

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ static int sysfs_link_sibling(struct sysfs_dirent *sd)
111111
/* add new node and rebalance the tree */
112112
rb_link_node(&sd->s_rb, parent, node);
113113
rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children);
114-
115-
/* if @sd has ns tag, mark the parent to enable ns filtering */
116-
if (sd->s_ns)
117-
sd->s_parent->s_flags |= SYSFS_FLAG_HAS_NS;
118-
119114
return 0;
120115
}
121116

@@ -135,13 +130,6 @@ static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
135130
sd->s_parent->s_dir.subdirs--;
136131

137132
rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children);
138-
139-
/*
140-
* Either all or none of the children have tags. Clearing HAS_NS
141-
* when there's no child left is enough to keep the flag synced.
142-
*/
143-
if (RB_EMPTY_ROOT(&sd->s_parent->s_dir.children))
144-
sd->s_parent->s_flags &= ~SYSFS_FLAG_HAS_NS;
145133
}
146134

147135
/**
@@ -291,6 +279,7 @@ static int sysfs_dentry_delete(const struct dentry *dentry)
291279
static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
292280
{
293281
struct sysfs_dirent *sd;
282+
int type;
294283

295284
if (flags & LOOKUP_RCU)
296285
return -ECHILD;
@@ -311,8 +300,13 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
311300
goto out_bad;
312301

313302
/* The sysfs dirent has been moved to a different namespace */
314-
if (sd->s_ns && sd->s_ns != sysfs_info(dentry->d_sb)->ns)
315-
goto out_bad;
303+
type = KOBJ_NS_TYPE_NONE;
304+
if (sd->s_parent) {
305+
type = sysfs_ns_type(sd->s_parent);
306+
if (type != KOBJ_NS_TYPE_NONE &&
307+
sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns)
308+
goto out_bad;
309+
}
316310

317311
mutex_unlock(&sysfs_mutex);
318312
out_valid:
@@ -432,6 +426,13 @@ int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
432426
struct sysfs_inode_attrs *ps_iattr;
433427
int ret;
434428

429+
if (!!sysfs_ns_type(parent_sd) != !!sd->s_ns) {
430+
WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
431+
sysfs_ns_type(parent_sd) ? "required" : "invalid",
432+
parent_sd->s_name, sd->s_name);
433+
return -EINVAL;
434+
}
435+
435436
sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
436437
sd->s_parent = sysfs_get(parent_sd);
437438

@@ -611,6 +612,13 @@ struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
611612
struct rb_node *node = parent_sd->s_dir.children.rb_node;
612613
unsigned int hash;
613614

615+
if (!!sysfs_ns_type(parent_sd) != !!ns) {
616+
WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
617+
sysfs_ns_type(parent_sd) ? "required" : "invalid",
618+
parent_sd->s_name, name);
619+
return NULL;
620+
}
621+
614622
hash = sysfs_name_hash(name, ns);
615623
while (node) {
616624
struct sysfs_dirent *sd;
@@ -659,6 +667,7 @@ struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
659667
EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
660668

661669
static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
670+
enum kobj_ns_type type,
662671
const char *name, const void *ns,
663672
struct sysfs_dirent **p_sd)
664673
{
@@ -672,6 +681,7 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
672681
if (!sd)
673682
return -ENOMEM;
674683

684+
sd->s_flags |= (type << SYSFS_NS_TYPE_SHIFT);
675685
sd->s_ns = ns;
676686
sd->s_dir.kobj = kobj;
677687

@@ -691,7 +701,33 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
691701
int sysfs_create_subdir(struct kobject *kobj, const char *name,
692702
struct sysfs_dirent **p_sd)
693703
{
694-
return create_dir(kobj, kobj->sd, name, NULL, p_sd);
704+
return create_dir(kobj, kobj->sd,
705+
KOBJ_NS_TYPE_NONE, name, NULL, p_sd);
706+
}
707+
708+
/**
709+
* sysfs_read_ns_type: return associated ns_type
710+
* @kobj: the kobject being queried
711+
*
712+
* Each kobject can be tagged with exactly one namespace type
713+
* (i.e. network or user). Return the ns_type associated with
714+
* this object if any
715+
*/
716+
static enum kobj_ns_type sysfs_read_ns_type(struct kobject *kobj)
717+
{
718+
const struct kobj_ns_type_operations *ops;
719+
enum kobj_ns_type type;
720+
721+
ops = kobj_child_ns_ops(kobj);
722+
if (!ops)
723+
return KOBJ_NS_TYPE_NONE;
724+
725+
type = ops->type;
726+
BUG_ON(type <= KOBJ_NS_TYPE_NONE);
727+
BUG_ON(type >= KOBJ_NS_TYPES);
728+
BUG_ON(!kobj_ns_type_registered(type));
729+
730+
return type;
695731
}
696732

697733
/**
@@ -701,6 +737,7 @@ int sysfs_create_subdir(struct kobject *kobj, const char *name,
701737
*/
702738
int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
703739
{
740+
enum kobj_ns_type type;
704741
struct sysfs_dirent *parent_sd, *sd;
705742
int error = 0;
706743

@@ -714,7 +751,9 @@ int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
714751
if (!parent_sd)
715752
return -ENOENT;
716753

717-
error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd);
754+
type = sysfs_read_ns_type(kobj);
755+
756+
error = create_dir(kobj, parent_sd, type, kobject_name(kobj), ns, &sd);
718757
if (!error)
719758
kobj->sd = sd;
720759
return error;
@@ -728,12 +767,13 @@ static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
728767
struct sysfs_dirent *parent_sd = parent->d_fsdata;
729768
struct sysfs_dirent *sd;
730769
struct inode *inode;
731-
const void *ns = NULL;
770+
enum kobj_ns_type type;
771+
const void *ns;
732772

733773
mutex_lock(&sysfs_mutex);
734774

735-
if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
736-
ns = sysfs_info(dir->i_sb)->ns;
775+
type = sysfs_ns_type(parent_sd);
776+
ns = sysfs_info(dir->i_sb)->ns[type];
737777

738778
sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
739779

@@ -1056,15 +1096,15 @@ static int sysfs_readdir(struct file *file, struct dir_context *ctx)
10561096
struct dentry *dentry = file->f_path.dentry;
10571097
struct sysfs_dirent *parent_sd = dentry->d_fsdata;
10581098
struct sysfs_dirent *pos = file->private_data;
1059-
const void *ns = NULL;
1099+
enum kobj_ns_type type;
1100+
const void *ns;
1101+
1102+
type = sysfs_ns_type(parent_sd);
1103+
ns = sysfs_info(dentry->d_sb)->ns[type];
10601104

10611105
if (!dir_emit_dots(file, ctx))
10621106
return 0;
10631107
mutex_lock(&sysfs_mutex);
1064-
1065-
if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
1066-
ns = sysfs_info(dentry->d_sb)->ns;
1067-
10681108
for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
10691109
pos;
10701110
pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {

‎fs/sysfs/mount.c‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static const struct super_operations sysfs_ops = {
3636
struct sysfs_dirent sysfs_root = {
3737
.s_name = "",
3838
.s_count = ATOMIC_INIT(1),
39-
.s_flags = SYSFS_DIR,
39+
.s_flags = SYSFS_DIR | (KOBJ_NS_TYPE_NONE << SYSFS_NS_TYPE_SHIFT),
4040
.s_mode = S_IFDIR | S_IRUGO | S_IXUGO,
4141
.s_ino = 1,
4242
};
@@ -77,8 +77,14 @@ static int sysfs_test_super(struct super_block *sb, void *data)
7777
{
7878
struct sysfs_super_info *sb_info = sysfs_info(sb);
7979
struct sysfs_super_info *info = data;
80+
enum kobj_ns_type type;
81+
int found = 1;
8082

81-
return sb_info->ns == info->ns;
83+
for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++) {
84+
if (sb_info->ns[type] != info->ns[type])
85+
found = 0;
86+
}
87+
return found;
8288
}
8389

8490
static int sysfs_set_super(struct super_block *sb, void *data)
@@ -92,30 +98,36 @@ static int sysfs_set_super(struct super_block *sb, void *data)
9298

9399
static void free_sysfs_super_info(struct sysfs_super_info *info)
94100
{
95-
kobj_ns_drop(KOBJ_NS_TYPE_NET, info->ns);
101+
int type;
102+
for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++)
103+
kobj_ns_drop(type, info->ns[type]);
96104
kfree(info);
97105
}
98106

99107
static struct dentry *sysfs_mount(struct file_system_type *fs_type,
100108
int flags, const char *dev_name, void *data)
101109
{
102110
struct sysfs_super_info *info;
111+
enum kobj_ns_type type;
103112
struct super_block *sb;
104113
int error;
105114

106115
if (!(flags & MS_KERNMOUNT)) {
107116
if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
108117
return ERR_PTR(-EPERM);
109118

110-
if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
111-
return ERR_PTR(-EPERM);
119+
for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++) {
120+
if (!kobj_ns_current_may_mount(type))
121+
return ERR_PTR(-EPERM);
122+
}
112123
}
113124

114125
info = kzalloc(sizeof(*info), GFP_KERNEL);
115126
if (!info)
116127
return ERR_PTR(-ENOMEM);
117128

118-
info->ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
129+
for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++)
130+
info->ns[type] = kobj_ns_grab_current(type);
119131

120132
sb = sget(fs_type, sysfs_test_super, sysfs_set_super, flags, info);
121133
if (IS_ERR(sb) || sb->s_fs_info != info)

‎fs/sysfs/symlink.c‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
2828
struct sysfs_dirent *target_sd = NULL;
2929
struct sysfs_dirent *sd = NULL;
3030
struct sysfs_addrm_cxt acxt;
31+
enum kobj_ns_type ns_type;
3132
int error;
3233

3334
BUG_ON(!name || !parent_sd);
@@ -51,15 +52,29 @@ static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
5152
if (!sd)
5253
goto out_put;
5354

54-
sd->s_ns = target_sd->s_ns;
55+
ns_type = sysfs_ns_type(parent_sd);
56+
if (ns_type)
57+
sd->s_ns = target_sd->s_ns;
5558
sd->s_symlink.target_sd = target_sd;
5659
target_sd = NULL; /* reference is now owned by the symlink */
5760

5861
sysfs_addrm_start(&acxt);
59-
if (warn)
60-
error = sysfs_add_one(&acxt, sd, parent_sd);
61-
else
62-
error = __sysfs_add_one(&acxt, sd, parent_sd);
62+
/* Symlinks must be between directories with the same ns_type */
63+
if (!ns_type ||
64+
(ns_type == sysfs_ns_type(sd->s_symlink.target_sd->s_parent))) {
65+
if (warn)
66+
error = sysfs_add_one(&acxt, sd, parent_sd);
67+
else
68+
error = __sysfs_add_one(&acxt, sd, parent_sd);
69+
} else {
70+
error = -EINVAL;
71+
WARN(1, KERN_WARNING
72+
"sysfs: symlink across ns_types %s/%s -> %s/%s\n",
73+
parent_sd->s_name,
74+
sd->s_name,
75+
sd->s_symlink.target_sd->s_parent->s_name,
76+
sd->s_symlink.target_sd->s_name);
77+
}
6378
sysfs_addrm_finish(&acxt);
6479

6580
if (error)
@@ -149,7 +164,7 @@ void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
149164
* sysfs_remove_dir() for details.
150165
*/
151166
spin_lock(&sysfs_symlink_target_lock);
152-
if (targ->sd)
167+
if (targ->sd && sysfs_ns_type(kobj->sd))
153168
ns = targ->sd->s_ns;
154169
spin_unlock(&sysfs_symlink_target_lock);
155170
sysfs_hash_and_remove(kobj->sd, name, ns);

‎fs/sysfs/sysfs.h‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,27 @@ struct sysfs_dirent {
9090
#define SYSFS_COPY_NAME (SYSFS_DIR | SYSFS_KOBJ_LINK)
9191
#define SYSFS_ACTIVE_REF (SYSFS_KOBJ_ATTR | SYSFS_KOBJ_BIN_ATTR)
9292

93-
#define SYSFS_FLAG_MASK ~SYSFS_TYPE_MASK
94-
#define SYSFS_FLAG_HAS_NS 0x01000
93+
/* identify any namespace tag on sysfs_dirents */
94+
#define SYSFS_NS_TYPE_MASK 0xf00
95+
#define SYSFS_NS_TYPE_SHIFT 8
96+
97+
#define SYSFS_FLAG_MASK ~(SYSFS_NS_TYPE_MASK|SYSFS_TYPE_MASK)
9598
#define SYSFS_FLAG_REMOVED 0x02000
9699

97100
static inline unsigned int sysfs_type(struct sysfs_dirent *sd)
98101
{
99102
return sd->s_flags & SYSFS_TYPE_MASK;
100103
}
101104

105+
/*
106+
* Return any namespace tags on this dirent.
107+
* enum kobj_ns_type is defined in linux/kobject.h
108+
*/
109+
static inline enum kobj_ns_type sysfs_ns_type(struct sysfs_dirent *sd)
110+
{
111+
return (sd->s_flags & SYSFS_NS_TYPE_MASK) >> SYSFS_NS_TYPE_SHIFT;
112+
}
113+
102114
#ifdef CONFIG_DEBUG_LOCK_ALLOC
103115

104116
#define sysfs_dirent_init_lockdep(sd) \
@@ -143,13 +155,12 @@ struct sysfs_addrm_cxt {
143155
*/
144156

145157
/*
146-
* Each sb is associated with one namespace tag, currently the network
147-
* namespace of the task which mounted this sysfs instance. If multiple
148-
* tags become necessary, make the following an array and compare
149-
* sysfs_dirent tag against every entry.
158+
* Each sb is associated with a set of namespace tags (i.e.
159+
* the network namespace of the task which mounted this sysfs
160+
* instance).
150161
*/
151162
struct sysfs_super_info {
152-
void *ns;
163+
void *ns[KOBJ_NS_TYPES];
153164
};
154165
#define sysfs_info(SB) ((struct sysfs_super_info *)(SB->s_fs_info))
155166
extern struct sysfs_dirent sysfs_root;

‎lib/kobject.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@
3030
const void *kobject_namespace(struct kobject *kobj)
3131
{
3232
const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
33-
const void *ns;
3433

3534
if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
3635
return NULL;
3736

38-
ns = kobj->ktype->namespace(kobj);
39-
WARN_ON(!ns); /* @kobj in a namespace is required to have !NULL tag */
40-
return ns;
37+
return kobj->ktype->namespace(kobj);
4138
}
4239

4340
/*

0 commit comments

Comments
 (0)