sysfs: Make sysfs_mount static
[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;
25328026 133 mutex_lock(&parent_dentry->d_inode->i_mutex);
53e0ae92
TH
134 dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
135 strlen(cur->s_name));
25328026 136 mutex_unlock(&parent_dentry->d_inode->i_mutex);
53e0ae92
TH
137 dput(parent_dentry);
138
139 if (IS_ERR(dentry)) {
140 sysfs_put(cur);
141 return dentry;
142 }
143
144 mutex_lock(&sysfs_mutex);
145 spin_lock(&sysfs_assoc_lock);
146
147 /* This, again, can happen if tree structure has
148 * changed and we looked up the wrong thing. Restart.
149 */
150 if (cur->s_dentry != dentry) {
151 dput(dentry);
152 sysfs_put(cur);
153 goto restart1;
154 }
155
156 spin_unlock(&sysfs_assoc_lock);
157
158 sysfs_put(cur);
159 }
160
161 mutex_unlock(&sysfs_mutex);
162 return dentry;
163}
164
b6b4a439
TH
165/**
166 * sysfs_get_active - get an active reference to sysfs_dirent
167 * @sd: sysfs_dirent to get an active reference to
168 *
169 * Get an active reference of @sd. This function is noop if @sd
170 * is NULL.
171 *
172 * RETURNS:
173 * Pointer to @sd on success, NULL on failure.
174 */
175struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
176{
8619f979
TH
177 if (unlikely(!sd))
178 return NULL;
179
180 while (1) {
181 int v, t;
182
183 v = atomic_read(&sd->s_active);
184 if (unlikely(v < 0))
185 return NULL;
186
187 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
188 if (likely(t == v))
189 return sd;
190 if (t < 0)
191 return NULL;
192
193 cpu_relax();
b6b4a439 194 }
b6b4a439
TH
195}
196
197/**
198 * sysfs_put_active - put an active reference to sysfs_dirent
199 * @sd: sysfs_dirent to put an active reference to
200 *
201 * Put an active reference to @sd. This function is noop if @sd
202 * is NULL.
203 */
204void sysfs_put_active(struct sysfs_dirent *sd)
205{
8619f979
TH
206 struct completion *cmpl;
207 int v;
208
209 if (unlikely(!sd))
210 return;
211
212 v = atomic_dec_return(&sd->s_active);
213 if (likely(v != SD_DEACTIVATED_BIAS))
214 return;
215
216 /* atomic_dec_return() is a mb(), we'll always see the updated
0c73f18b 217 * sd->s_sibling.
8619f979 218 */
0c73f18b 219 cmpl = (void *)sd->s_sibling;
8619f979 220 complete(cmpl);
b6b4a439
TH
221}
222
223/**
224 * sysfs_get_active_two - get active references to sysfs_dirent and parent
225 * @sd: sysfs_dirent of interest
226 *
227 * Get active reference to @sd and its parent. Parent's active
228 * reference is grabbed first. This function is noop if @sd is
229 * NULL.
230 *
231 * RETURNS:
232 * Pointer to @sd on success, NULL on failure.
233 */
234struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
235{
236 if (sd) {
237 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
238 return NULL;
239 if (unlikely(!sysfs_get_active(sd))) {
240 sysfs_put_active(sd->s_parent);
241 return NULL;
242 }
243 }
244 return sd;
245}
246
247/**
248 * sysfs_put_active_two - put active references to sysfs_dirent and parent
249 * @sd: sysfs_dirent of interest
250 *
251 * Put active references to @sd and its parent. This function is
252 * noop if @sd is NULL.
253 */
254void sysfs_put_active_two(struct sysfs_dirent *sd)
255{
256 if (sd) {
257 sysfs_put_active(sd);
258 sysfs_put_active(sd->s_parent);
259 }
260}
261
262/**
263 * sysfs_deactivate - deactivate sysfs_dirent
264 * @sd: sysfs_dirent to deactivate
265 *
8619f979 266 * Deny new active references and drain existing ones.
b6b4a439 267 */
fb6896da 268static void sysfs_deactivate(struct sysfs_dirent *sd)
b6b4a439 269{
8619f979
TH
270 DECLARE_COMPLETION_ONSTACK(wait);
271 int v;
b6b4a439 272
380e6fbb 273 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
0c73f18b 274 sd->s_sibling = (void *)&wait;
8619f979
TH
275
276 /* atomic_add_return() is a mb(), put_active() will always see
0c73f18b 277 * the updated sd->s_sibling.
b6b4a439 278 */
8619f979
TH
279 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
280
281 if (v != SD_DEACTIVATED_BIAS)
282 wait_for_completion(&wait);
283
0c73f18b 284 sd->s_sibling = NULL;
b6b4a439
TH
285}
286
42b37df6 287static int sysfs_alloc_ino(ino_t *pino)
2b611bb7
TH
288{
289 int ino, rc;
290
291 retry:
292 spin_lock(&sysfs_ino_lock);
293 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
294 spin_unlock(&sysfs_ino_lock);
295
296 if (rc == -EAGAIN) {
297 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
298 goto retry;
299 rc = -ENOMEM;
300 }
301
302 *pino = ino;
303 return rc;
304}
305
306static void sysfs_free_ino(ino_t ino)
307{
308 spin_lock(&sysfs_ino_lock);
309 ida_remove(&sysfs_ino_ida, ino);
310 spin_unlock(&sysfs_ino_lock);
311}
312
fa7f912a
TH
313void release_sysfs_dirent(struct sysfs_dirent * sd)
314{
13b3086d
TH
315 struct sysfs_dirent *parent_sd;
316
317 repeat:
3007e997
TH
318 /* Moving/renaming is always done while holding reference.
319 * sd->s_parent won't change beneath us.
320 */
13b3086d
TH
321 parent_sd = sd->s_parent;
322
b402d72c 323 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
2b29ac25 324 sysfs_put(sd->s_elem.symlink.target_sd);
b402d72c 325 if (sysfs_type(sd) & SYSFS_COPY_NAME)
0c096b50 326 kfree(sd->s_name);
fa7f912a 327 kfree(sd->s_iattr);
2b611bb7 328 sysfs_free_ino(sd->s_ino);
fa7f912a 329 kmem_cache_free(sysfs_dir_cachep, sd);
13b3086d
TH
330
331 sd = parent_sd;
332 if (sd && atomic_dec_and_test(&sd->s_count))
333 goto repeat;
fa7f912a
TH
334}
335
1da177e4
LT
336static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
337{
338 struct sysfs_dirent * sd = dentry->d_fsdata;
339
340 if (sd) {
5f995323
TH
341 /* sd->s_dentry is protected with sysfs_assoc_lock.
342 * This allows sysfs_drop_dentry() to dereference it.
dd14cbc9 343 */
5f995323 344 spin_lock(&sysfs_assoc_lock);
dd14cbc9
TH
345
346 /* The dentry might have been deleted or another
347 * lookup could have happened updating sd->s_dentry to
348 * point the new dentry. Ignore if it isn't pointing
349 * to this dentry.
350 */
351 if (sd->s_dentry == dentry)
352 sd->s_dentry = NULL;
5f995323 353 spin_unlock(&sysfs_assoc_lock);
1da177e4
LT
354 sysfs_put(sd);
355 }
356 iput(inode);
357}
358
359static struct dentry_operations sysfs_dentry_ops = {
360 .d_iput = sysfs_d_iput,
361};
362
3e519038 363struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
1da177e4 364{
0c096b50 365 char *dup_name = NULL;
01da2425 366 struct sysfs_dirent *sd;
0c096b50
TH
367
368 if (type & SYSFS_COPY_NAME) {
369 name = dup_name = kstrdup(name, GFP_KERNEL);
370 if (!name)
01da2425 371 return NULL;
0c096b50 372 }
1da177e4 373
c3762229 374 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
1da177e4 375 if (!sd)
01da2425 376 goto err_out1;
1da177e4 377
0c096b50 378 if (sysfs_alloc_ino(&sd->s_ino))
01da2425 379 goto err_out2;
2b611bb7 380
1da177e4 381 atomic_set(&sd->s_count, 1);
8619f979 382 atomic_set(&sd->s_active, 0);