sysfs: make sysfs_add_one() automatically check for duplicate entry
[deliverable/linux.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>
869512ab 14#include <linux/mutex.h>
1da177e4
LT
15#include "sysfs.h"
16
3007e997 17DEFINE_MUTEX(sysfs_mutex);
5f995323 18spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
1da177e4 19
2b611bb7
TH
20static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
21static DEFINE_IDA(sysfs_ino_ida);
22
0c73f18b
TH
23/**
24 * sysfs_link_sibling - link sysfs_dirent into sibling list
25 * @sd: sysfs_dirent of interest
26 *
27 * Link @sd into its sibling list which starts from
28 * sd->s_parent->s_children.
29 *
30 * Locking:
3007e997 31 * mutex_lock(sysfs_mutex)
0c73f18b 32 */
41fc1c27 33static void sysfs_link_sibling(struct sysfs_dirent *sd)
0c73f18b
TH
34{
35 struct sysfs_dirent *parent_sd = sd->s_parent;
36
37 BUG_ON(sd->s_sibling);
38 sd->s_sibling = parent_sd->s_children;
39 parent_sd->s_children = sd;
40}
41
42/**
43 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
44 * @sd: sysfs_dirent of interest
45 *
46 * Unlink @sd from its sibling list which starts from
47 * sd->s_parent->s_children.
48 *
49 * Locking:
3007e997 50 * mutex_lock(sysfs_mutex)
0c73f18b 51 */
41fc1c27 52static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
0c73f18b
TH
53{
54 struct sysfs_dirent **pos;
55
56 for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
57 if (*pos == sd) {
58 *pos = sd->s_sibling;
59 sd->s_sibling = NULL;
60 break;
61 }
62 }
63}
64
53e0ae92
TH
65/**
66 * sysfs_get_dentry - get dentry for the given sysfs_dirent
67 * @sd: sysfs_dirent of interest
68 *
69 * Get dentry for @sd. Dentry is looked up if currently not
70 * present. This function climbs sysfs_dirent tree till it
71 * reaches a sysfs_dirent with valid dentry attached and descends
72 * down from there looking up dentry for each step.
73 *
74 * LOCKING:
75 * Kernel thread context (may sleep)
76 *
77 * RETURNS:
78 * Pointer to found dentry on success, ERR_PTR() value on error.
79 */
80struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
81{
82 struct sysfs_dirent *cur;
83 struct dentry *parent_dentry, *dentry;
84 int i, depth;
85
86 /* Find the first parent which has valid s_dentry and get the
87 * dentry.
88 */
89 mutex_lock(&sysfs_mutex);
90 restart0:
91 spin_lock(&sysfs_assoc_lock);
92 restart1:
93 spin_lock(&dcache_lock);
94
95 dentry = NULL;
96 depth = 0;
97 cur = sd;
98 while (!cur->s_dentry || !cur->s_dentry->d_inode) {
99 if (cur->s_flags & SYSFS_FLAG_REMOVED) {
100 dentry = ERR_PTR(-ENOENT);
101 depth = 0;
102 break;
103 }
104 cur = cur->s_parent;
105 depth++;
106 }
107 if (!IS_ERR(dentry))
108 dentry = dget_locked(cur->s_dentry);
109
110 spin_unlock(&dcache_lock);
111 spin_unlock(&sysfs_assoc_lock);
112
113 /* from the found dentry, look up depth times */
114 while (depth--) {
115 /* find and get depth'th ancestor */
116 for (cur = sd, i = 0; cur && i < depth; i++)
117 cur = cur->s_parent;
118
119 /* This can happen if tree structure was modified due
120 * to move/rename. Restart.
121 */
122 if (i != depth) {
123 dput(dentry);
124 goto restart0;
125 }
126
127 sysfs_get(cur);
128
129 mutex_unlock(&sysfs_mutex);
130
131 /* look it up */
132 parent_dentry = dentry;
133 dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
134 strlen(cur->s_name));
135 dput(parent_dentry);
136
137 if (IS_ERR(dentry)) {
138 sysfs_put(cur);
139 return dentry;
140 }
141
142 mutex_lock(&sysfs_mutex);
143 spin_lock(&sysfs_assoc_lock);
144
145 /* This, again, can happen if tree structure has
146 * changed and we looked up the wrong thing. Restart.
147 */
148 if (cur->s_dentry != dentry) {
149 dput(dentry);
150 sysfs_put(cur);
151 goto restart1;
152 }
153
154 spin_unlock(&sysfs_assoc_lock);
155
156 sysfs_put(cur);
157 }
158
159 mutex_unlock(&sysfs_mutex);
160 return dentry;
161}
162
b6b4a439
TH
163/**
164 * sysfs_get_active - get an active reference to sysfs_dirent
165 * @sd: sysfs_dirent to get an active reference to
166 *
167 * Get an active reference of @sd. This function is noop if @sd
168 * is NULL.
169 *
170 * RETURNS:
171 * Pointer to @sd on success, NULL on failure.
172 */
173struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
174{
8619f979
TH
175 if (unlikely(!sd))
176 return NULL;
177
178 while (1) {
179 int v, t;
180
181 v = atomic_read(&sd->s_active);
182 if (unlikely(v < 0))
183 return NULL;
184
185 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
186 if (likely(t == v))
187 return sd;
188 if (t < 0)
189 return NULL;
190
191 cpu_relax();
b6b4a439 192 }
b6b4a439
TH
193}
194
195/**
196 * sysfs_put_active - put an active reference to sysfs_dirent
197 * @sd: sysfs_dirent to put an active reference to
198 *
199 * Put an active reference to @sd. This function is noop if @sd
200 * is NULL.
201 */
202void sysfs_put_active(struct sysfs_dirent *sd)
203{
8619f979
TH
204 struct completion *cmpl;
205 int v;
206
207 if (unlikely(!sd))
208 return;
209
210 v = atomic_dec_return(&sd->s_active);
211 if (likely(v != SD_DEACTIVATED_BIAS))
212 return;
213
214 /* atomic_dec_return() is a mb(), we'll always see the updated
0c73f18b 215 * sd->s_sibling.
8619f979 216 */
0c73f18b 217 cmpl = (void *)sd->s_sibling;
8619f979 218 complete(cmpl);
b6b4a439
TH
219}
220
221/**
222 * sysfs_get_active_two - get active references to sysfs_dirent and parent
223 * @sd: sysfs_dirent of interest
224 *
225 * Get active reference to @sd and its parent. Parent's active
226 * reference is grabbed first. This function is noop if @sd is
227 * NULL.
228 *
229 * RETURNS:
230 * Pointer to @sd on success, NULL on failure.
231 */
232struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
233{
234 if (sd) {
235 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
236 return NULL;
237 if (unlikely(!sysfs_get_active(sd))) {
238 sysfs_put_active(sd->s_parent);
239 return NULL;
240 }
241 }
242 return sd;
243}
244
245/**
246 * sysfs_put_active_two - put active references to sysfs_dirent and parent
247 * @sd: sysfs_dirent of interest
248 *
249 * Put active references to @sd and its parent. This function is
250 * noop if @sd is NULL.
251 */
252void sysfs_put_active_two(struct sysfs_dirent *sd)
253{
254 if (sd) {
255 sysfs_put_active(sd);
256 sysfs_put_active(sd->s_parent);
257 }
258}
259
260/**
261 * sysfs_deactivate - deactivate sysfs_dirent
262 * @sd: sysfs_dirent to deactivate
263 *
8619f979 264 * Deny new active references and drain existing ones.
b6b4a439 265 */
fb6896da 266static void sysfs_deactivate(struct sysfs_dirent *sd)
b6b4a439 267{
8619f979
TH
268 DECLARE_COMPLETION_ONSTACK(wait);
269 int v;
b6b4a439 270
380e6fbb 271 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
0c73f18b 272 sd->s_sibling = (void *)&wait;
8619f979
TH
273
274 /* atomic_add_return() is a mb(), put_active() will always see
0c73f18b 275 * the updated sd->s_sibling.
b6b4a439 276 */
8619f979
TH
277 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
278
279 if (v != SD_DEACTIVATED_BIAS)
280 wait_for_completion(&wait);
281
0c73f18b 282 sd->s_sibling = NULL;
b6b4a439
TH
283}
284
42b37df6 285static int sysfs_alloc_ino(ino_t *pino)
2b611bb7
TH
286{
287 int ino, rc;
288
289 retry:
290 spin_lock(&sysfs_ino_lock);
291 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
292 spin_unlock(&sysfs_ino_lock);
293
294 if (rc == -EAGAIN) {
295 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
296 goto retry;
297 rc = -ENOMEM;
298 }
299
300 *pino = ino;
301 return rc;
302}
303
304static void sysfs_free_ino(ino_t ino)
305{
306 spin_lock(&sysfs_ino_lock);
307 ida_remove(&sysfs_ino_ida, ino);
308 spin_unlock(&sysfs_ino_lock);
309}
310
fa7f912a
TH
311void release_sysfs_dirent(struct sysfs_dirent * sd)
312{
13b3086d
TH
313 struct sysfs_dirent *parent_sd;
314
315 repeat:
3007e997
TH
316 /* Moving/renaming is always done while holding reference.
317 * sd->s_parent won't change beneath us.
318 */
13b3086d
TH
319 parent_sd = sd->s_parent;
320
b402d72c 321 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
2b29ac25 322 sysfs_put(sd->s_elem.symlink.target_sd);
b402d72c 323 if (sysfs_type(sd) & SYSFS_COPY_NAME)
0c096b50 324 kfree(sd->s_name);
fa7f912a 325 kfree(sd->s_iattr);
2b611bb7 326 sysfs_free_ino(sd->s_ino);
fa7f912a 327 kmem_cache_free(sysfs_dir_cachep, sd);
13b3086d
TH
328
329 sd = parent_sd;
330 if (sd && atomic_dec_and_test(&sd->s_count))
331 goto repeat;
fa7f912a
TH
332}
333
1da177e4
LT
334static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
335{
336 struct sysfs_dirent * sd = dentry->d_fsdata;
337
338 if (sd) {
5f995323
TH
339 /* sd->s_dentry is protected with sysfs_assoc_lock.
340 * This allows sysfs_drop_dentry() to dereference it.
dd14cbc9 341 */
5f995323 342 spin_lock(&sysfs_assoc_lock);
dd14cbc9
TH
343
344 /* The dentry might have been deleted or another
345 * lookup could have happened updating sd->s_dentry to
346 * point the new dentry. Ignore if it isn't pointing
347 * to this dentry.
348 */
349 if (sd->s_dentry == dentry)
350 sd->s_dentry = NULL;
5f995323 351 spin_unlock(&sysfs_assoc_lock);
1da177e4
LT
352 sysfs_put(sd);
353 }
354 iput(inode);
355}
356
357static struct dentry_operations sysfs_dentry_ops = {
358 .d_iput = sysfs_d_iput,
359};
360
3e519038 361struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
1da177e4 362{
0c096b50 363 char *dup_name = NULL;
01da2425 364 struct sysfs_dirent *sd;
0c096b50
TH
365
366 if (type & SYSFS_COPY_NAME) {
367 name = dup_name = kstrdup(name, GFP_KERNEL);
368 if (!name)
01da2425 369 return NULL;
0c096b50 370 }
1da177e4 371
c3762229 372 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
1da177e4 373 if (!sd)
01da2425 374 goto err_out1;
1da177e4 375
0c096b50 376 if (sysfs_alloc_ino(&sd->s_ino))
01da2425 377 goto err_out2;
2b611bb7 378
1da177e4 379 atomic_set(&sd->s_count, 1);
8619f979 380 atomic_set(&sd->s_active, 0);