@@ -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)
291279static 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 );
318312out_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,
659667EXPORT_SYMBOL_GPL (sysfs_get_dirent_ns );
660668
661669static 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,
691701int 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 */
702738int 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 )) {
0 commit comments