]> Pileus Git - ~andy/linux/blob - fs/sysfs/dir.c
sysfs, kernfs: introduce sysfs_root_sd
[~andy/linux] / fs / sysfs / dir.c
1 /*
2  * fs/sysfs/dir.c - sysfs core and dir operation implementation
3  *
4  * Copyright (c) 2001-3 Patrick Mochel
5  * Copyright (c) 2007 SUSE Linux Products GmbH
6  * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7  *
8  * This file is released under the GPLv2.
9  *
10  * Please see Documentation/filesystems/sysfs.txt for more information.
11  */
12
13 #undef DEBUG
14
15 #include <linux/fs.h>
16 #include <linux/kobject.h>
17 #include <linux/slab.h>
18 #include "sysfs.h"
19
20 DEFINE_SPINLOCK(sysfs_symlink_target_lock);
21
22 /**
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
26  *
27  *      Gives the name "/" to the sysfs_root entry; any path returned
28  *      is relative to wherever sysfs is mounted.
29  */
30 static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
31 {
32         if (sd->s_parent) {
33                 sysfs_pathname(sd->s_parent, path);
34                 strlcat(path, "/", PATH_MAX);
35         }
36         strlcat(path, sd->s_name, PATH_MAX);
37         return path;
38 }
39
40 void sysfs_warn_dup(struct sysfs_dirent *parent, const char *name)
41 {
42         char *path;
43
44         path = kzalloc(PATH_MAX, GFP_KERNEL);
45         if (path) {
46                 sysfs_pathname(parent, path);
47                 strlcat(path, "/", PATH_MAX);
48                 strlcat(path, name, PATH_MAX);
49         }
50
51         WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n",
52              path ? path : name);
53
54         kfree(path);
55 }
56
57 /**
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
61  */
62 int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
63 {
64         struct sysfs_dirent *parent_sd, *sd;
65
66         BUG_ON(!kobj);
67
68         if (kobj->parent)
69                 parent_sd = kobj->parent->sd;
70         else
71                 parent_sd = sysfs_root_sd;
72
73         if (!parent_sd)
74                 return -ENOENT;
75
76         sd = kernfs_create_dir_ns(parent_sd, kobject_name(kobj), kobj, ns);
77         if (IS_ERR(sd)) {
78                 if (PTR_ERR(sd) == -EEXIST)
79                         sysfs_warn_dup(parent_sd, kobject_name(kobj));
80                 return PTR_ERR(sd);
81         }
82
83         kobj->sd = sd;
84         return 0;
85 }
86
87 /**
88  *      sysfs_remove_dir - remove an object's directory.
89  *      @kobj:  object.
90  *
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.
94  */
95 void sysfs_remove_dir(struct kobject *kobj)
96 {
97         struct sysfs_dirent *sd = kobj->sd;
98
99         /*
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.
106          *
107          * sysfs_symlink_target_lock synchronizes @kobj->sd disassociation
108          * against symlink operations so that symlink code can safely
109          * dereference @kobj->sd.
110          */
111         spin_lock(&sysfs_symlink_target_lock);
112         kobj->sd = NULL;
113         spin_unlock(&sysfs_symlink_target_lock);
114
115         if (sd) {
116                 WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
117                 kernfs_remove(sd);
118         }
119 }
120
121 int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
122                         const void *new_ns)
123 {
124         struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
125
126         return kernfs_rename_ns(kobj->sd, parent_sd, new_name, new_ns);
127 }
128
129 int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
130                       const void *new_ns)
131 {
132         struct sysfs_dirent *sd = kobj->sd;
133         struct sysfs_dirent *new_parent_sd;
134
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;
138
139         return kernfs_rename_ns(sd, new_parent_sd, sd->s_name, new_ns);
140 }