2 * fs/sysfs/dir.c - sysfs core and dir operation implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
16 #include <linux/kobject.h>
17 #include <linux/slab.h>
20 DEFINE_SPINLOCK(sysfs_symlink_target_lock);
23 * sysfs_pathname - return full path to sysfs dirent
24 * @sd: sysfs_dirent whose path we want
25 * @path: caller allocated buffer of size PATH_MAX
27 * Gives the name "/" to the sysfs_root entry; any path returned
28 * is relative to wherever sysfs is mounted.
30 static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
33 sysfs_pathname(sd->s_parent, path);
34 strlcat(path, "/", PATH_MAX);
36 strlcat(path, sd->s_name, PATH_MAX);
40 void sysfs_warn_dup(struct sysfs_dirent *parent, const char *name)
44 path = kzalloc(PATH_MAX, GFP_KERNEL);
46 sysfs_pathname(parent, path);
47 strlcat(path, "/", PATH_MAX);
48 strlcat(path, name, PATH_MAX);
51 WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n",
58 * sysfs_create_dir_ns - create a directory for an object with a namespace tag
59 * @kobj: object we're creating directory for
60 * @ns: the namespace tag to use
62 int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
64 struct sysfs_dirent *parent_sd, *sd;
69 parent_sd = kobj->parent->sd;
71 parent_sd = sysfs_root_sd;
76 sd = kernfs_create_dir_ns(parent_sd, kobject_name(kobj), kobj, ns);
78 if (PTR_ERR(sd) == -EEXIST)
79 sysfs_warn_dup(parent_sd, kobject_name(kobj));
88 * sysfs_remove_dir - remove an object's directory.
91 * The only thing special about this is that we remove any files in
92 * the directory before we remove the directory, and we've inlined
93 * what used to be sysfs_rmdir() below, instead of calling separately.
95 void sysfs_remove_dir(struct kobject *kobj)
97 struct sysfs_dirent *sd = kobj->sd;
100 * In general, kboject owner is responsible for ensuring removal
101 * doesn't race with other operations and sysfs doesn't provide any
102 * protection; however, when @kobj is used as a symlink target, the
103 * symlinking entity usually doesn't own @kobj and thus has no
104 * control over removal. @kobj->sd may be removed anytime and
105 * symlink code may end up dereferencing an already freed sd.
107 * sysfs_symlink_target_lock synchronizes @kobj->sd disassociation
108 * against symlink operations so that symlink code can safely
109 * dereference @kobj->sd.
111 spin_lock(&sysfs_symlink_target_lock);
113 spin_unlock(&sysfs_symlink_target_lock);
116 WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
121 int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
124 struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
126 return kernfs_rename_ns(kobj->sd, parent_sd, new_name, new_ns);
129 int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
132 struct sysfs_dirent *sd = kobj->sd;
133 struct sysfs_dirent *new_parent_sd;
135 BUG_ON(!sd->s_parent);
136 new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
137 new_parent_kobj->sd : sysfs_root_sd;
139 return kernfs_rename_ns(sd, new_parent_sd, sd->s_name, new_ns);