]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - fs/sysfs/dir.c
sysfs: clean up sysfs_get_dirent()
[karo-tx-linux.git] / fs / sysfs / dir.c
index 99ec5b40e977730a3613ff295740446e651fba42..fee19d16e4a204fc043d54060e367327f27eff72 100644 (file)
@@ -28,7 +28,7 @@
 DEFINE_MUTEX(sysfs_mutex);
 DEFINE_SPINLOCK(sysfs_assoc_lock);
 
-#define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb);
+#define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb)
 
 static DEFINE_SPINLOCK(sysfs_ino_lock);
 static DEFINE_IDA(sysfs_ino_ida);
@@ -111,6 +111,11 @@ static int sysfs_link_sibling(struct sysfs_dirent *sd)
        /* add new node and rebalance the tree */
        rb_link_node(&sd->s_rb, parent, node);
        rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children);
+
+       /* if @sd has ns tag, mark the parent to enable ns filtering */
+       if (sd->s_ns)
+               sd->s_parent->s_flags |= SYSFS_FLAG_HAS_NS;
+
        return 0;
 }
 
@@ -130,6 +135,13 @@ static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
                sd->s_parent->s_dir.subdirs--;
 
        rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children);
+
+       /*
+        * Either all or none of the children have tags.  Clearing HAS_NS
+        * when there's no child left is enough to keep the flag synced.
+        */
+       if (RB_EMPTY_ROOT(&sd->s_parent->s_dir.children))
+               sd->s_parent->s_flags &= ~SYSFS_FLAG_HAS_NS;
 }
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
@@ -297,8 +309,6 @@ static int sysfs_dentry_delete(const struct dentry *dentry)
 static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
 {
        struct sysfs_dirent *sd;
-       int is_dir;
-       int type;
 
        if (flags & LOOKUP_RCU)
                return -ECHILD;
@@ -319,13 +329,8 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
                goto out_bad;
 
        /* The sysfs dirent has been moved to a different namespace */
-       type = KOBJ_NS_TYPE_NONE;
-       if (sd->s_parent) {
-               type = sysfs_ns_type(sd->s_parent);
-               if (type != KOBJ_NS_TYPE_NONE &&
-                               sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns)
-                       goto out_bad;
-       }
+       if (sd->s_ns && sd->s_ns != sysfs_info(dentry->d_sb)->ns)
+               goto out_bad;
 
        mutex_unlock(&sysfs_mutex);
 out_valid:
@@ -341,18 +346,15 @@ out_bad:
         * is performed at its new name the dentry will be readded
         * to the dcache hashes.
         */
-       is_dir = (sysfs_type(sd) == SYSFS_DIR);
        mutex_unlock(&sysfs_mutex);
-       if (is_dir) {
-               /* If we have submounts we must allow the vfs caches
-                * to lie about the state of the filesystem to prevent
-                * leaks and other nasty things.
-                */
-               if (have_submounts(dentry))
-                       goto out_valid;
-               shrink_dcache_parent(dentry);
-       }
-       d_drop(dentry);
+
+       /* If we have submounts we must allow the vfs caches
+        * to lie about the state of the filesystem to prevent
+        * leaks and other nasty things.
+        */
+       if (check_submounts_and_drop(dentry) != 0)
+               goto out_valid;
+
        return 0;
 }
 
@@ -449,13 +451,6 @@ int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
        struct sysfs_inode_attrs *ps_iattr;
        int ret;
 
-       if (!!sysfs_ns_type(acxt->parent_sd) != !!sd->s_ns) {
-               WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
-                       sysfs_ns_type(acxt->parent_sd) ? "required" : "invalid",
-                       acxt->parent_sd->s_name, sd->s_name);
-               return -EINVAL;
-       }
-
        sd->s_hash = sysfs_name_hash(sd->s_ns, sd->s_name);
        sd->s_parent = sysfs_get(acxt->parent_sd);
 
@@ -617,13 +612,6 @@ struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
        struct rb_node *node = parent_sd->s_dir.children.rb_node;
        unsigned int hash;
 
-       if (!!sysfs_ns_type(parent_sd) != !!ns) {
-               WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
-                       sysfs_ns_type(parent_sd) ? "required" : "invalid",
-                       parent_sd->s_name, name);
-               return NULL;
-       }
-
        hash = sysfs_name_hash(ns, name);
        while (node) {
                struct sysfs_dirent *sd;
@@ -642,9 +630,10 @@ struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
 }
 
 /**
- *     sysfs_get_dirent - find and get sysfs_dirent with the given name
+ *     sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
  *     @parent_sd: sysfs_dirent to search under
  *     @name: name to look for
+ *     @ns: the namespace tag to use
  *
  *     Look for sysfs_dirent with name @name under @parent_sd and get
  *     it if found.
@@ -655,9 +644,9 @@ struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  *     RETURNS:
  *     Pointer to sysfs_dirent if found, NULL if not.
  */
-struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
-                                     const void *ns,
-                                     const unsigned char *name)
+struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
+                                        const unsigned char *name,
+                                        const void *ns)
 {
        struct sysfs_dirent *sd;
 
@@ -668,11 +657,10 @@ struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
 
        return sd;
 }
-EXPORT_SYMBOL_GPL(sysfs_get_dirent);
+EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
 
 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
-       enum kobj_ns_type type, const void *ns, const char *name,
-       struct sysfs_dirent **p_sd)
+       const void *ns, const char *name, struct sysfs_dirent **p_sd)
 {
        umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
        struct sysfs_addrm_cxt acxt;
@@ -684,7 +672,6 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
        if (!sd)
                return -ENOMEM;
 
-       sd->s_flags |= (type << SYSFS_NS_TYPE_SHIFT);
        sd->s_ns = ns;
        sd->s_dir.kobj = kobj;
 
@@ -704,44 +691,17 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
 int sysfs_create_subdir(struct kobject *kobj, const char *name,
                        struct sysfs_dirent **p_sd)
 {
-       return create_dir(kobj, kobj->sd,
-                         KOBJ_NS_TYPE_NONE, NULL, name, p_sd);
+       return create_dir(kobj, kobj->sd, NULL, name, p_sd);
 }
 
 /**
- *     sysfs_read_ns_type: return associated ns_type
- *     @kobj: the kobject being queried
- *
- *     Each kobject can be tagged with exactly one namespace type
- *     (i.e. network or user).  Return the ns_type associated with
- *     this object if any
- */
-static enum kobj_ns_type sysfs_read_ns_type(struct kobject *kobj)
-{
-       const struct kobj_ns_type_operations *ops;
-       enum kobj_ns_type type;
-
-       ops = kobj_child_ns_ops(kobj);
-       if (!ops)
-               return KOBJ_NS_TYPE_NONE;
-
-       type = ops->type;
-       BUG_ON(type <= KOBJ_NS_TYPE_NONE);
-       BUG_ON(type >= KOBJ_NS_TYPES);
-       BUG_ON(!kobj_ns_type_registered(type));
-
-       return type;
-}
-
-/**
- *     sysfs_create_dir - create a directory for an object.
- *     @kobj:          object we're creating directory for.
+ * sysfs_create_dir_ns - create a directory for an object with a namespace tag
+ * @kobj: object we're creating directory for
+ * @ns: the namespace tag to use
  */
-int sysfs_create_dir(struct kobject *kobj)
+int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
 {
-       enum kobj_ns_type type;
        struct sysfs_dirent *parent_sd, *sd;
-       const void *ns = NULL;
        int error = 0;
 
        BUG_ON(!kobj);
@@ -754,11 +714,7 @@ int sysfs_create_dir(struct kobject *kobj)
        if (!parent_sd)
                return -ENOENT;
 
-       if (sysfs_ns_type(parent_sd))
-               ns = kobj->ktype->namespace(kobj);
-       type = sysfs_read_ns_type(kobj);
-
-       error = create_dir(kobj, parent_sd, type, ns, kobject_name(kobj), &sd);
+       error = create_dir(kobj, parent_sd, ns, kobject_name(kobj), &sd);
        if (!error)
                kobj->sd = sd;
        return error;
@@ -772,13 +728,12 @@ static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
        struct sysfs_dirent *parent_sd = parent->d_fsdata;
        struct sysfs_dirent *sd;
        struct inode *inode;
-       enum kobj_ns_type type;
-       const void *ns;
+       const void *ns = NULL;
 
        mutex_lock(&sysfs_mutex);
 
-       type = sysfs_ns_type(parent_sd);
-       ns = sysfs_info(dir->i_sb)->ns[type];
+       if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
+               ns = sysfs_info(dir->i_sb)->ns;
 
        sd = sysfs_find_dirent(parent_sd, ns, dentry->d_name.name);
 
@@ -913,26 +868,21 @@ int sysfs_rename(struct sysfs_dirent *sd,
        return error;
 }
 
-int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
+int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
+                       const void *new_ns)
 {
        struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
-       const void *new_ns = NULL;
-
-       if (sysfs_ns_type(parent_sd))
-               new_ns = kobj->ktype->namespace(kobj);
 
        return sysfs_rename(kobj->sd, parent_sd, new_ns, new_name);
 }
 
-int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
+int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
+                     const void *new_ns)
 {
        struct sysfs_dirent *sd = kobj->sd;
        struct sysfs_dirent *new_parent_sd;
-       const void *new_ns = NULL;
 
        BUG_ON(!sd->s_parent);
-       if (sysfs_ns_type(sd->s_parent))
-               new_ns = kobj->ktype->namespace(kobj);
        new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
                new_parent_kobj->sd : &sysfs_root;
 
@@ -1006,15 +956,15 @@ static int sysfs_readdir(struct file *file, struct dir_context *ctx)
        struct dentry *dentry = file->f_path.dentry;
        struct sysfs_dirent *parent_sd = dentry->d_fsdata;
        struct sysfs_dirent *pos = file->private_data;
-       enum kobj_ns_type type;
-       const void *ns;
-
-       type = sysfs_ns_type(parent_sd);
-       ns = sysfs_info(dentry->d_sb)->ns[type];
+       const void *ns = NULL;
 
        if (!dir_emit_dots(file, ctx))
                return 0;
        mutex_lock(&sysfs_mutex);
+
+       if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
+               ns = sysfs_info(dentry->d_sb)->ns;
+
        for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
             pos;
             pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {