sysfs: make kobj point to sysfs_dirent instead of dentry
[linux-block.git] / fs / sysfs / dir.c
CommitLineData
1da177e4
LT
1/*
2 * dir.c - Operations for sysfs directories.
3 */
4
5#undef DEBUG
6
7#include <linux/fs.h>
8#include <linux/mount.h>
9#include <linux/module.h>
10#include <linux/kobject.h>
5f45f1a7 11#include <linux/namei.h>
2b611bb7 12#include <linux/idr.h>
8619f979 13#include <linux/completion.h>
94bebf4d 14#include <asm/semaphore.h>
1da177e4
LT
15#include "sysfs.h"
16
17DECLARE_RWSEM(sysfs_rename_sem);
dd14cbc9 18spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
aecdceda 19spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
1da177e4 20
2b611bb7
TH
21static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
22static DEFINE_IDA(sysfs_ino_ida);
23
0c73f18b
TH
24/**
25 * sysfs_link_sibling - link sysfs_dirent into sibling list
26 * @sd: sysfs_dirent of interest
27 *
28 * Link @sd into its sibling list which starts from
29 * sd->s_parent->s_children.
30 *
31 * Locking:
32 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
33 */
34static void sysfs_link_sibling(struct sysfs_dirent *sd)
35{
36 struct sysfs_dirent *parent_sd = sd->s_parent;
37
38 BUG_ON(sd->s_sibling);
39 sd->s_sibling = parent_sd->s_children;
40 parent_sd->s_children = sd;
41}
42
43/**
44 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
45 * @sd: sysfs_dirent of interest
46 *
47 * Unlink @sd from its sibling list which starts from
48 * sd->s_parent->s_children.
49 *
50 * Locking:
51 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
52 */
53static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
54{
55 struct sysfs_dirent **pos;
56
57 for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
58 if (*pos == sd) {
59 *pos = sd->s_sibling;
60 sd->s_sibling = NULL;
61 break;
62 }
63 }
64}
65
b6b4a439
TH
66/**
67 * sysfs_get_active - get an active reference to sysfs_dirent
68 * @sd: sysfs_dirent to get an active reference to
69 *
70 * Get an active reference of @sd. This function is noop if @sd
71 * is NULL.
72 *
73 * RETURNS:
74 * Pointer to @sd on success, NULL on failure.
75 */
76struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
77{
8619f979
TH
78 if (unlikely(!sd))
79 return NULL;
80
81 while (1) {
82 int v, t;
83
84 v = atomic_read(&sd->s_active);
85 if (unlikely(v < 0))
86 return NULL;
87
88 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
89 if (likely(t == v))
90 return sd;
91 if (t < 0)
92 return NULL;
93
94 cpu_relax();
b6b4a439 95 }
b6b4a439
TH
96}
97
98/**
99 * sysfs_put_active - put an active reference to sysfs_dirent
100 * @sd: sysfs_dirent to put an active reference to
101 *
102 * Put an active reference to @sd. This function is noop if @sd
103 * is NULL.
104 */
105void sysfs_put_active(struct sysfs_dirent *sd)
106{
8619f979
TH
107 struct completion *cmpl;
108 int v;
109
110 if (unlikely(!sd))
111 return;
112
113 v = atomic_dec_return(&sd->s_active);
114 if (likely(v != SD_DEACTIVATED_BIAS))
115 return;
116
117 /* atomic_dec_return() is a mb(), we'll always see the updated
0c73f18b 118 * sd->s_sibling.
8619f979 119 */
0c73f18b 120 cmpl = (void *)sd->s_sibling;
8619f979 121 complete(cmpl);
b6b4a439
TH
122}
123
124/**
125 * sysfs_get_active_two - get active references to sysfs_dirent and parent
126 * @sd: sysfs_dirent of interest
127 *
128 * Get active reference to @sd and its parent. Parent's active
129 * reference is grabbed first. This function is noop if @sd is
130 * NULL.
131 *
132 * RETURNS:
133 * Pointer to @sd on success, NULL on failure.
134 */
135struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
136{
137 if (sd) {
138 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
139 return NULL;
140 if (unlikely(!sysfs_get_active(sd))) {
141 sysfs_put_active(sd->s_parent);
142 return NULL;
143 }
144 }
145 return sd;
146}
147
148/**
149 * sysfs_put_active_two - put active references to sysfs_dirent and parent
150 * @sd: sysfs_dirent of interest
151 *
152 * Put active references to @sd and its parent. This function is
153 * noop if @sd is NULL.
154 */
155void sysfs_put_active_two(struct sysfs_dirent *sd)
156{
157 if (sd) {
158 sysfs_put_active(sd);
159 sysfs_put_active(sd->s_parent);
160 }
161}
162
163/**
164 * sysfs_deactivate - deactivate sysfs_dirent
165 * @sd: sysfs_dirent to deactivate
166 *
8619f979 167 * Deny new active references and drain existing ones.
b6b4a439
TH
168 */
169void sysfs_deactivate(struct sysfs_dirent *sd)
170{
8619f979
TH
171 DECLARE_COMPLETION_ONSTACK(wait);
172 int v;
b6b4a439 173
380e6fbb 174 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
0c73f18b 175 sd->s_sibling = (void *)&wait;
8619f979
TH
176
177 /* atomic_add_return() is a mb(), put_active() will always see
0c73f18b 178 * the updated sd->s_sibling.
b6b4a439 179 */
8619f979
TH
180 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
181
182 if (v != SD_DEACTIVATED_BIAS)
183 wait_for_completion(&wait);
184
0c73f18b 185 sd->s_sibling = NULL;
b6b4a439
TH
186}
187
42b37df6 188static int sysfs_alloc_ino(ino_t *pino)
2b611bb7
TH
189{
190 int ino, rc;
191
192 retry:
193 spin_lock(&sysfs_ino_lock);
194 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
195 spin_unlock(&sysfs_ino_lock);
196
197 if (rc == -EAGAIN) {
198 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
199 goto retry;
200 rc = -ENOMEM;
201 }
202
203 *pino = ino;
204 return rc;
205}
206
207static void sysfs_free_ino(ino_t ino)
208{
209 spin_lock(&sysfs_ino_lock);
210 ida_remove(&sysfs_ino_ida, ino);
211 spin_unlock(&sysfs_ino_lock);
212}
213
fa7f912a
TH
214void release_sysfs_dirent(struct sysfs_dirent * sd)
215{
13b3086d
TH
216 struct sysfs_dirent *parent_sd;
217
218 repeat:
219 parent_sd = sd->s_parent;
220
b402d72c 221 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
2b29ac25 222 sysfs_put(sd->s_elem.symlink.target_sd);
b402d72c 223 if (sysfs_type(sd) & SYSFS_COPY_NAME)
0c096b50 224 kfree(sd->s_name);
fa7f912a 225 kfree(sd->s_iattr);
2b611bb7 226 sysfs_free_ino(sd->s_ino);
fa7f912a 227 kmem_cache_free(sysfs_dir_cachep, sd);
13b3086d
TH
228
229 sd = parent_sd;
230 if (sd && atomic_dec_and_test(&sd->s_count))
231 goto repeat;
fa7f912a
TH
232}
233
1da177e4
LT
234static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
235{
236 struct sysfs_dirent * sd = dentry->d_fsdata;
237
238 if (sd) {
dd14cbc9
TH
239 /* sd->s_dentry is protected with sysfs_lock. This
240 * allows sysfs_drop_dentry() to dereference it.
241 */
242 spin_lock(&sysfs_lock);
243
244 /* The dentry might have been deleted or another
245 * lookup could have happened updating sd->s_dentry to
246 * point the new dentry. Ignore if it isn't pointing
247 * to this dentry.
248 */
249 if (sd->s_dentry == dentry)
250 sd->s_dentry = NULL;
251 spin_unlock(&sysfs_lock);
1da177e4
LT
252 sysfs_put(sd);
253 }
254 iput(inode);
255}
256
257static struct dentry_operations sysfs_dentry_ops = {
258 .d_iput = sysfs_d_iput,
259};
260
3e519038 261struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
1da177e4 262{
0c096b50
TH
263 char *dup_name = NULL;
264 struct sysfs_dirent *sd = NULL;
265
266 if (type & SYSFS_COPY_NAME) {
267 name = dup_name = kstrdup(name, GFP_KERNEL);
268 if (!name)
269 goto err_out;
270 }
1da177e4 271
c3762229 272 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
1da177e4 273 if (!sd)
0c096b50 274 goto err_out;
1da177e4 275
0c096b50
TH
276 if (sysfs_alloc_ino(&sd->s_ino))
277 goto err_out;
2b611bb7 278
1da177e4 279 atomic_set(&sd->s_count, 1);
8619f979 280 atomic_set(&sd->s_active, 0);