blob: c6f3b697064cb7a7fa3d084366a92362f06ded0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070011#include <linux/namei.h>
Tejun Heo2b611bb2007-06-14 03:45:13 +090012#include <linux/idr.h>
Tejun Heo8619f972007-06-14 03:45:18 +090013#include <linux/completion.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010014#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "sysfs.h"
16
Tejun Heo3007e992007-06-14 04:27:23 +090017DEFINE_MUTEX(sysfs_mutex);
Tejun Heo5f995322007-06-14 04:27:23 +090018spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Tejun Heo2b611bb2007-06-14 03:45:13 +090020static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
21static DEFINE_IDA(sysfs_ino_ida);
22
Tejun Heob6b4a432007-06-14 03:45:18 +090023/**
Tejun Heo0c73f182007-06-14 03:45:18 +090024 * 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:
Tejun Heo3007e992007-06-14 04:27:23 +090031 * mutex_lock(sysfs_mutex)
Tejun Heo0c73f182007-06-14 03:45:18 +090032 */
Tejun Heofb6896da2007-06-14 04:27:24 +090033void sysfs_link_sibling(struct sysfs_dirent *sd)
Tejun Heo0c73f182007-06-14 03:45:18 +090034{
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:
Tejun Heo3007e992007-06-14 04:27:23 +090050 * mutex_lock(sysfs_mutex)
Tejun Heo0c73f182007-06-14 03:45:18 +090051 */
Tejun Heofb6896da2007-06-14 04:27:24 +090052void sysfs_unlink_sibling(struct sysfs_dirent *sd)
Tejun Heo0c73f182007-06-14 03:45:18 +090053{
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
65/**
Tejun Heob6b4a432007-06-14 03:45:18 +090066 * sysfs_get_active - get an active reference to sysfs_dirent
67 * @sd: sysfs_dirent to get an active reference to
68 *
69 * Get an active reference of @sd. This function is noop if @sd
70 * is NULL.
71 *
72 * RETURNS:
73 * Pointer to @sd on success, NULL on failure.
74 */
75struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
76{
Tejun Heo8619f972007-06-14 03:45:18 +090077 if (unlikely(!sd))
78 return NULL;
79
80 while (1) {
81 int v, t;
82
83 v = atomic_read(&sd->s_active);
84 if (unlikely(v < 0))
85 return NULL;
86
87 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
88 if (likely(t == v))
89 return sd;
90 if (t < 0)
91 return NULL;
92
93 cpu_relax();
Tejun Heob6b4a432007-06-14 03:45:18 +090094 }
Tejun Heob6b4a432007-06-14 03:45:18 +090095}
96
97/**
98 * sysfs_put_active - put an active reference to sysfs_dirent
99 * @sd: sysfs_dirent to put an active reference to
100 *
101 * Put an active reference to @sd. This function is noop if @sd
102 * is NULL.
103 */
104void sysfs_put_active(struct sysfs_dirent *sd)
105{
Tejun Heo8619f972007-06-14 03:45:18 +0900106 struct completion *cmpl;
107 int v;
108
109 if (unlikely(!sd))
110 return;
111
112 v = atomic_dec_return(&sd->s_active);
113 if (likely(v != SD_DEACTIVATED_BIAS))
114 return;
115
116 /* atomic_dec_return() is a mb(), we'll always see the updated
Tejun Heo0c73f182007-06-14 03:45:18 +0900117 * sd->s_sibling.
Tejun Heo8619f972007-06-14 03:45:18 +0900118 */
Tejun Heo0c73f182007-06-14 03:45:18 +0900119 cmpl = (void *)sd->s_sibling;
Tejun Heo8619f972007-06-14 03:45:18 +0900120 complete(cmpl);
Tejun Heob6b4a432007-06-14 03:45:18 +0900121}
122
123/**
124 * sysfs_get_active_two - get active references to sysfs_dirent and parent
125 * @sd: sysfs_dirent of interest
126 *
127 * Get active reference to @sd and its parent. Parent's active
128 * reference is grabbed first. This function is noop if @sd is
129 * NULL.
130 *
131 * RETURNS:
132 * Pointer to @sd on success, NULL on failure.
133 */
134struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
135{
136 if (sd) {
137 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
138 return NULL;
139 if (unlikely(!sysfs_get_active(sd))) {
140 sysfs_put_active(sd->s_parent);
141 return NULL;
142 }
143 }
144 return sd;
145}
146
147/**
148 * sysfs_put_active_two - put active references to sysfs_dirent and parent
149 * @sd: sysfs_dirent of interest
150 *
151 * Put active references to @sd and its parent. This function is
152 * noop if @sd is NULL.
153 */
154void sysfs_put_active_two(struct sysfs_dirent *sd)
155{
156 if (sd) {
157 sysfs_put_active(sd);
158 sysfs_put_active(sd->s_parent);
159 }
160}
161
162/**
163 * sysfs_deactivate - deactivate sysfs_dirent
164 * @sd: sysfs_dirent to deactivate
165 *
Tejun Heo8619f972007-06-14 03:45:18 +0900166 * Deny new active references and drain existing ones.
Tejun Heob6b4a432007-06-14 03:45:18 +0900167 */
Tejun Heofb6896da2007-06-14 04:27:24 +0900168static void sysfs_deactivate(struct sysfs_dirent *sd)
Tejun Heob6b4a432007-06-14 03:45:18 +0900169{
Tejun Heo8619f972007-06-14 03:45:18 +0900170 DECLARE_COMPLETION_ONSTACK(wait);
171 int v;
Tejun Heob6b4a432007-06-14 03:45:18 +0900172
Tejun Heo380e6fb2007-06-14 04:27:22 +0900173 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
Tejun Heo0c73f182007-06-14 03:45:18 +0900174 sd->s_sibling = (void *)&wait;
Tejun Heo8619f972007-06-14 03:45:18 +0900175
176 /* atomic_add_return() is a mb(), put_active() will always see
Tejun Heo0c73f182007-06-14 03:45:18 +0900177 * the updated sd->s_sibling.
Tejun Heob6b4a432007-06-14 03:45:18 +0900178 */
Tejun Heo8619f972007-06-14 03:45:18 +0900179 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
180
181 if (v != SD_DEACTIVATED_BIAS)
182 wait_for_completion(&wait);
183
Tejun Heo0c73f182007-06-14 03:45:18 +0900184 sd->s_sibling = NULL;
Tejun Heob6b4a432007-06-14 03:45:18 +0900185}
186
Tejun Heo42b37df2007-06-14 03:45:17 +0900187static int sysfs_alloc_ino(ino_t *pino)
Tejun Heo2b611bb2007-06-14 03:45:13 +0900188{
189 int ino, rc;
190
191 retry:
192 spin_lock(&sysfs_ino_lock);
193 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
194 spin_unlock(&sysfs_ino_lock);
195
196 if (rc == -EAGAIN) {
197 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
198 goto retry;
199 rc = -ENOMEM;
200 }
201
202 *pino = ino;
203 return rc;
204}
205
206static void sysfs_free_ino(ino_t ino)
207{
208 spin_lock(&sysfs_ino_lock);
209 ida_remove(&sysfs_ino_ida, ino);
210 spin_unlock(&sysfs_ino_lock);
211}
212
Tejun Heofa7f9122007-06-14 03:45:13 +0900213void release_sysfs_dirent(struct sysfs_dirent * sd)
214{
Tejun Heo13b30862007-06-14 03:45:14 +0900215 struct sysfs_dirent *parent_sd;
216
217 repeat:
Tejun Heo3007e992007-06-14 04:27:23 +0900218 /* Moving/renaming is always done while holding reference.
219 * sd->s_parent won't change beneath us.
220 */
Tejun Heo13b30862007-06-14 03:45:14 +0900221 parent_sd = sd->s_parent;
222
Tejun Heob402d722007-06-14 04:27:21 +0900223 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
Tejun Heo2b29ac22007-06-14 03:45:15 +0900224 sysfs_put(sd->s_elem.symlink.target_sd);
Tejun Heob402d722007-06-14 04:27:21 +0900225 if (sysfs_type(sd) & SYSFS_COPY_NAME)
Tejun Heo0c096b52007-06-14 03:45:15 +0900226 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +0900227 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +0900228 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +0900229 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900230
231 sd = parent_sd;
232 if (sd && atomic_dec_and_test(&sd->s_count))
233 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +0900234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
237{
238 struct sysfs_dirent * sd = dentry->d_fsdata;
239
240 if (sd) {
Tejun Heo5f995322007-06-14 04:27:23 +0900241 /* sd->s_dentry is protected with sysfs_assoc_lock.
242 * This allows sysfs_drop_dentry() to dereference it.
Tejun Heodd14cbc2007-06-11 14:04:01 +0900243 */
Tejun Heo5f995322007-06-14 04:27:23 +0900244 spin_lock(&sysfs_assoc_lock);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900245
246 /* The dentry might have been deleted or another
247 * lookup could have happened updating sd->s_dentry to
248 * point the new dentry. Ignore if it isn't pointing
249 * to this dentry.
250 */
251 if (sd->s_dentry == dentry)
252 sd->s_dentry = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900253 spin_unlock(&sysfs_assoc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 sysfs_put(sd);
255 }
256 iput(inode);
257}
258
259static struct dentry_operations sysfs_dentry_ops = {
260 .d_iput = sysfs_d_iput,
261};
262
Tejun Heo3e519032007-06-14 03:45:15 +0900263struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Tejun Heo0c096b52007-06-14 03:45:15 +0900265 char *dup_name = NULL;
266 struct sysfs_dirent *sd = NULL;
267
268 if (type & SYSFS_COPY_NAME) {
269 name = dup_name = kstrdup(name, GFP_KERNEL);
270 if (!name)
271 goto err_out;
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800274 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!sd)
Tejun Heo0c096b52007-06-14 03:45:15 +0900276 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Tejun Heo0c096b52007-06-14 03:45:15 +0900278 if (sysfs_alloc_ino(&sd->s_ino))
279 goto err_out;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 atomic_set(&sd->s_count, 1);
Tejun Heo8619f972007-06-14 03:45:18 +0900282 atomic_set(&sd->s_active, 0);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300283 atomic_set(&sd->s_event, 1);
Tejun Heoa26cd722007-06-14 03:45:14 +0900284
Tejun Heo0c096b52007-06-14 03:45:15 +0900285 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900286 sd->s_mode = mode;
Tejun Heob402d722007-06-14 04:27:21 +0900287 sd->s_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900290
291 err_out:
292 kfree(dup_name);
293 kmem_cache_free(sysfs_dir_cachep, sd);
294 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Tejun Heo3007e992007-06-14 04:27:23 +0900297/**
298 * sysfs_attach_dentry - associate sysfs_dirent with dentry
299 * @sd: target sysfs_dirent
300 * @dentry: dentry to associate
301 *
302 * Associate @sd with @dentry. This is protected by
303 * sysfs_assoc_lock to avoid race with sysfs_d_iput().
304 *
305 * LOCKING:
306 * mutex_lock(sysfs_mutex)
307 */
Tejun Heo198a2a82007-06-14 03:45:16 +0900308static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
309{
310 dentry->d_op = &sysfs_dentry_ops;
311 dentry->d_fsdata = sysfs_get(sd);
312
313 /* protect sd->s_dentry against sysfs_d_iput */
Tejun Heo5f995322007-06-14 04:27:23 +0900314 spin_lock(&sysfs_assoc_lock);
Tejun Heo198a2a82007-06-14 03:45:16 +0900315 sd->s_dentry = dentry;
Tejun Heo5f995322007-06-14 04:27:23 +0900316 spin_unlock(&sysfs_assoc_lock);
Tejun Heo198a2a82007-06-14 03:45:16 +0900317
318 d_rehash(dentry);
319}
320
Tejun Heofb6896da2007-06-14 04:27:24 +0900321static int sysfs_ilookup_test(struct inode *inode, void *arg)
322{
323 struct sysfs_dirent *sd = arg;
324 return inode->i_ino == sd->s_ino;
325}
326
Tejun Heo3007e992007-06-14 04:27:23 +0900327/**
Tejun Heofb6896da2007-06-14 04:27:24 +0900328 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
329 * @acxt: pointer to sysfs_addrm_cxt to be used
330 * @parent_sd: parent sysfs_dirent
Tejun Heo3007e992007-06-14 04:27:23 +0900331 *
Tejun Heofb6896da2007-06-14 04:27:24 +0900332 * This function is called when the caller is about to add or
333 * remove sysfs_dirent under @parent_sd. This function acquires
334 * sysfs_mutex, grabs inode for @parent_sd if available and lock
335 * i_mutex of it. @acxt is used to keep and pass context to
336 * other addrm functions.
Tejun Heo3007e992007-06-14 04:27:23 +0900337 *
338 * LOCKING:
Tejun Heofb6896da2007-06-14 04:27:24 +0900339 * Kernel thread context (may sleep). sysfs_mutex is locked on
340 * return. i_mutex of parent inode is locked on return if
341 * available.
Tejun Heo3007e992007-06-14 04:27:23 +0900342 */
Tejun Heofb6896da2007-06-14 04:27:24 +0900343void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
344 struct sysfs_dirent *parent_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700345{
Tejun Heofb6896da2007-06-14 04:27:24 +0900346 struct inode *inode;
Tejun Heoa26cd722007-06-14 03:45:14 +0900347
Tejun Heofb6896da2007-06-14 04:27:24 +0900348 memset(acxt, 0, sizeof(*acxt));
349 acxt->parent_sd = parent_sd;
350
351 /* Lookup parent inode. inode initialization and I_NEW
352 * clearing are protected by sysfs_mutex. By grabbing it and
353 * looking up with _nowait variant, inode state can be
354 * determined reliably.
355 */
356 mutex_lock(&sysfs_mutex);
357
358 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
359 parent_sd);
360
361 if (inode && !(inode->i_state & I_NEW)) {
362 /* parent inode available */
363 acxt->parent_inode = inode;
364
365 /* sysfs_mutex is below i_mutex in lock hierarchy.
366 * First, trylock i_mutex. If fails, unlock
367 * sysfs_mutex and lock them in order.
368 */
369 if (!mutex_trylock(&inode->i_mutex)) {
370 mutex_unlock(&sysfs_mutex);
371 mutex_lock(&inode->i_mutex);
372 mutex_lock(&sysfs_mutex);
373 }
374 } else
375 iput(inode);
376}
377
378/**
379 * sysfs_add_one - add sysfs_dirent to parent
380 * @acxt: addrm context to use
381 * @sd: sysfs_dirent to be added
382 *
383 * Get @acxt->parent_sd and set sd->s_parent to it and increment
384 * nlink of parent inode if @sd is a directory. @sd is NOT
385 * linked into the children list of the parent. The caller
386 * should invoke sysfs_link_sibling() after this function
387 * completes if @sd needs to be on the children list.
388 *
389 * This function should be called between calls to
390 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
391 * passed the same @acxt as passed to sysfs_addrm_start().
392 *
393 * LOCKING:
394 * Determined by sysfs_addrm_start().
395 */
396void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
397{
398 sd->s_parent = sysfs_get(acxt->parent_sd);
399
400 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
401 inc_nlink(acxt->parent_inode);
402
403 acxt->cnt++;
404}
405
406/**
407 * sysfs_remove_one - remove sysfs_dirent from parent
408 * @acxt: addrm context to use
409 * @sd: sysfs_dirent to be added
410 *
411 * Mark @sd removed and drop nlink of parent inode if @sd is a
412 * directory. @sd is NOT unlinked from the children list of the
413 * parent. The caller is repsonsible for removing @sd from the
414 * children list before calling this function.
415 *
416 * This function should be called between calls to
417 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
418 * passed the same @acxt as passed to sysfs_addrm_start().
419 *
420 * LOCKING:
421 * Determined by sysfs_addrm_start().
422 */
423void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
424{
425 BUG_ON(sd->s_sibling || (sd->s_flags & SYSFS_FLAG_REMOVED));
426
427 sd->s_flags |= SYSFS_FLAG_REMOVED;
428 sd->s_sibling = acxt->removed;
429 acxt->removed = sd;
430
431 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
432 drop_nlink(acxt->parent_inode);
433
434 acxt->cnt++;
435}
436
437/**
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900438 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
439 * @sd: target sysfs_dirent
440 *
441 * Drop dentry for @sd. @sd must have been unlinked from its
442 * parent on entry to this function such that it can't be looked
443 * up anymore.
444 *
445 * @sd->s_dentry which is protected with sysfs_assoc_lock points
446 * to the currently associated dentry but we're not holding a
447 * reference to it and racing with dput(). Grab dcache_lock and
448 * verify dentry before dropping it. If @sd->s_dentry is NULL or
449 * dput() beats us, no need to bother.
450 */
451static void sysfs_drop_dentry(struct sysfs_dirent *sd)
452{
453 struct dentry *dentry = NULL;
454 struct inode *inode;
455
456 /* We're not holding a reference to ->s_dentry dentry but the
457 * field will stay valid as long as sysfs_assoc_lock is held.
458 */
459 spin_lock(&sysfs_assoc_lock);
460 spin_lock(&dcache_lock);
461
462 /* drop dentry if it's there and dput() didn't kill it yet */
463 if (sd->s_dentry && sd->s_dentry->d_inode) {
464 dentry = dget_locked(sd->s_dentry);
465 spin_lock(&dentry->d_lock);
466 __d_drop(dentry);
467 spin_unlock(&dentry->d_lock);
468 }
469
470 spin_unlock(&dcache_lock);
471 spin_unlock(&sysfs_assoc_lock);
472
473 dput(dentry);
474 /* XXX: unpin if directory, this will go away soon */
475 if (sysfs_type(sd) == SYSFS_DIR)
476 dput(dentry);
477
478 /* adjust nlink and update timestamp */
479 inode = ilookup(sysfs_sb, sd->s_ino);
480 if (inode) {
481 mutex_lock(&inode->i_mutex);
482
483 inode->i_ctime = CURRENT_TIME;
484 drop_nlink(inode);
485 if (sysfs_type(sd) == SYSFS_DIR)
486 drop_nlink(inode);
487
488 mutex_unlock(&inode->i_mutex);
489 iput(inode);
490 }
491}
492
493/**
Tejun Heofb6896da2007-06-14 04:27:24 +0900494 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
495 * @acxt: addrm context to finish up
496 *
497 * Finish up sysfs_dirent add/remove. Resources acquired by
498 * sysfs_addrm_start() are released and removed sysfs_dirents are
499 * cleaned up. Timestamps on the parent inode are updated.
500 *
501 * LOCKING:
502 * All mutexes acquired by sysfs_addrm_start() are released.
503 *
504 * RETURNS:
505 * Number of added/removed sysfs_dirents since sysfs_addrm_start().
506 */
507int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
508{
509 /* release resources acquired by sysfs_addrm_start() */
510 mutex_unlock(&sysfs_mutex);
511 if (acxt->parent_inode) {
512 struct inode *inode = acxt->parent_inode;
513
514 /* if added/removed, update timestamps on the parent */
515 if (acxt->cnt)
516 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
517
518 mutex_unlock(&inode->i_mutex);
519 iput(inode);
Tejun Heo13b30862007-06-14 03:45:14 +0900520 }
Tejun Heofb6896da2007-06-14 04:27:24 +0900521
522 /* kill removed sysfs_dirents */
523 while (acxt->removed) {
524 struct sysfs_dirent *sd = acxt->removed;
525
526 acxt->removed = sd->s_sibling;
527 sd->s_sibling = NULL;
528
529 sysfs_drop_dentry(sd);
530 sysfs_deactivate(sd);
531 sysfs_put(sd);
532 }
533
534 return acxt->cnt;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700535}
536
Tejun Heof0b0af42007-06-14 04:27:22 +0900537/**
538 * sysfs_find_dirent - find sysfs_dirent with the given name
539 * @parent_sd: sysfs_dirent to search under
540 * @name: name to look for
Maneesh Sonic5168652006-03-09 19:40:14 +0530541 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900542 * Look for sysfs_dirent with name @name under @parent_sd.
Maneesh Sonic5168652006-03-09 19:40:14 +0530543 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900544 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900545 * mutex_lock(sysfs_mutex)
Tejun Heof0b0af42007-06-14 04:27:22 +0900546 *
547 * RETURNS:
548 * Pointer to sysfs_dirent if found, NULL if not.
Maneesh Sonic5168652006-03-09 19:40:14 +0530549 */
Tejun Heof0b0af42007-06-14 04:27:22 +0900550struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
551 const unsigned char *name)
Maneesh Sonic5168652006-03-09 19:40:14 +0530552{
Tejun Heof0b0af42007-06-14 04:27:22 +0900553 struct sysfs_dirent *sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530554
Tejun Heof0b0af42007-06-14 04:27:22 +0900555 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
556 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
557 return sd;
558 return NULL;
559}
Maneesh Sonic5168652006-03-09 19:40:14 +0530560
Tejun Heof0b0af42007-06-14 04:27:22 +0900561/**
562 * sysfs_get_dirent - find and get sysfs_dirent with the given name
563 * @parent_sd: sysfs_dirent to search under
564 * @name: name to look for
565 *
566 * Look for sysfs_dirent with name @name under @parent_sd and get
567 * it if found.
568 *
569 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900570 * Kernel thread context (may sleep). Grabs sysfs_mutex.
Tejun Heof0b0af42007-06-14 04:27:22 +0900571 *
572 * RETURNS:
573 * Pointer to sysfs_dirent if found, NULL if not.
574 */
575struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
576 const unsigned char *name)
577{
578 struct sysfs_dirent *sd;
579
Tejun Heo3007e992007-06-14 04:27:23 +0900580 mutex_lock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900581 sd = sysfs_find_dirent(parent_sd, name);
582 sysfs_get(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900583 mutex_unlock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900584
585 return sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530586}
587
Tejun Heo608e2662007-06-14 04:27:22 +0900588static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
589 const char *name, struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Tejun Heo608e2662007-06-14 04:27:22 +0900591 struct dentry *parent = parent_sd->s_dentry;
Tejun Heofb6896da2007-06-14 04:27:24 +0900592 struct sysfs_addrm_cxt acxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int error;
594 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900595 struct dentry *dentry;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900596 struct inode *inode;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900597 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Tejun Heofb6896da2007-06-14 04:27:24 +0900599 sysfs_addrm_start(&acxt, parent_sd);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900600
Tejun Heofc9f54b2007-06-14 03:45:17 +0900601 /* allocate */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900602 dentry = lookup_one_len(name, parent, strlen(name));
603 if (IS_ERR(dentry)) {
604 error = PTR_ERR(dentry);
Tejun Heofb6896da2007-06-14 04:27:24 +0900605 goto out_finish;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900606 }
607
608 error = -EEXIST;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900609 if (dentry->d_inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900610 goto out_dput;
611
Tejun Heoa26cd722007-06-14 03:45:14 +0900612 error = -ENOMEM;
Tejun Heo3e519032007-06-14 03:45:15 +0900613 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900614 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900615 goto out_drop;
Tejun Heo3e519032007-06-14 03:45:15 +0900616 sd->s_elem.dir.kobj = kobj;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900617
Tejun Heo8312a8d2007-06-14 03:45:17 +0900618 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900619 if (!inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900620 goto out_sput;
621
Tejun Heo8312a8d2007-06-14 03:45:17 +0900622 if (inode->i_state & I_NEW) {
623 inode->i_op = &sysfs_dir_inode_operations;
624 inode->i_fop = &sysfs_dir_operations;
625 /* directory inodes start off with i_nlink == 2 (for ".") */
626 inc_nlink(inode);
627 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900628
629 /* link in */
630 error = -EEXIST;
Tejun Heofb6896da2007-06-14 04:27:24 +0900631 if (sysfs_find_dirent(parent_sd, name))
Tejun Heofc9f54b2007-06-14 03:45:17 +0900632 goto out_iput;
633
Tejun Heofb6896da2007-06-14 04:27:24 +0900634 sysfs_add_one(&acxt, sd);
635 sysfs_link_sibling(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900636 sysfs_instantiate(dentry, inode);
Tejun Heofb6896da2007-06-14 04:27:24 +0900637 sysfs_attach_dentry(sd, dentry);
Tejun Heo3007e992007-06-14 04:27:23 +0900638
Tejun Heo608e2662007-06-14 04:27:22 +0900639 *p_sd = sd;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900640 error = 0;
Tejun Heofb6896da2007-06-14 04:27:24 +0900641 goto out_finish; /* pin directory dentry in core */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900642
Tejun Heofc9f54b2007-06-14 03:45:17 +0900643 out_iput:
644 iput(inode);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900645 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900646 sysfs_put(sd);
647 out_drop:
648 d_drop(dentry);
649 out_dput:
650 dput(dentry);
Tejun Heofb6896da2007-06-14 04:27:24 +0900651 out_finish:
652 sysfs_addrm_finish(&acxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return error;
654}
655
Tejun Heo608e2662007-06-14 04:27:22 +0900656int sysfs_create_subdir(struct kobject *kobj, const char *name,
657 struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Tejun Heo608e2662007-06-14 04:27:22 +0900659 return create_dir(kobj, kobj->sd, name, p_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/**
663 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 * @kobj: object we're creating directory for.
Tejun Heo608e2662007-06-14 04:27:22 +0900665 * @shadow_parent: parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Tejun Heo608e2662007-06-14 04:27:22 +0900667int sysfs_create_dir(struct kobject *kobj,
668 struct sysfs_dirent *shadow_parent_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Tejun Heo608e2662007-06-14 04:27:22 +0900670 struct sysfs_dirent *parent_sd, *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int error = 0;
672
673 BUG_ON(!kobj);
674
Tejun Heo608e2662007-06-14 04:27:22 +0900675 if (shadow_parent_sd)
676 parent_sd = shadow_parent_sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700677 else if (kobj->parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900678 parent_sd = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 else if (sysfs_mount && sysfs_mount->mnt_sb)
Tejun Heo608e2662007-06-14 04:27:22 +0900680 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 else
682 return -EFAULT;
683
Tejun Heo608e2662007-06-14 04:27:22 +0900684 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!error)
Tejun Heo608e2662007-06-14 04:27:22 +0900686 kobj->sd = sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return error;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
691 struct nameidata *nd)
692{
693 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
694 struct sysfs_dirent * sd;
Tejun Heob402d722007-06-14 04:27:21 +0900695 struct bin_attribute *bin_attr;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900696 struct inode *inode;
697 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Tejun Heo0c73f182007-06-14 03:45:18 +0900699 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
Tejun Heob402d722007-06-14 04:27:21 +0900700 if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
Tejun Heofc9f54b2007-06-14 03:45:17 +0900701 !strcmp(sd->s_name, dentry->d_name.name)) {
702 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
704 }
705 }
706
Tejun Heofc9f54b2007-06-14 03:45:17 +0900707 /* no such entry */
708 if (!found)
709 return NULL;
710
711 /* attach dentry and inode */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900712 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900713 if (!inode)
714 return ERR_PTR(-ENOMEM);
715
Tejun Heo3007e992007-06-14 04:27:23 +0900716 mutex_lock(&sysfs_mutex);
717
Tejun Heo8312a8d2007-06-14 03:45:17 +0900718 if (inode->i_state & I_NEW) {
719 /* initialize inode according to type */
Tejun Heob402d722007-06-14 04:27:21 +0900720 switch (sysfs_type(sd)) {
721 case SYSFS_KOBJ_ATTR:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900722 inode->i_size = PAGE_SIZE;
723 inode->i_fop = &sysfs_file_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900724 break;
725 case SYSFS_KOBJ_BIN_ATTR:
726 bin_attr = sd->s_elem.bin_attr.bin_attr;
Tejun Heo8312a8d2007-06-14 03:45:17 +0900727 inode->i_size = bin_attr->size;
728 inode->i_fop = &bin_fops;
Tejun Heob402d722007-06-14 04:27:21 +0900729 break;
730 case SYSFS_KOBJ_LINK:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900731 inode->i_op = &sysfs_symlink_inode_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900732 break;
733 default:
734 BUG();
735 }
Tejun Heo8312a8d2007-06-14 03:45:17 +0900736 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900737
738 sysfs_instantiate(dentry, inode);
739 sysfs_attach_dentry(sd, dentry);
740
Tejun Heo3007e992007-06-14 04:27:23 +0900741 mutex_unlock(&sysfs_mutex);
742
Tejun Heofc9f54b2007-06-14 03:45:17 +0900743 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800746const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530748 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749};
750
Tejun Heo608e2662007-06-14 04:27:22 +0900751static void remove_dir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Tejun Heofb6896da2007-06-14 04:27:24 +0900753 struct sysfs_addrm_cxt acxt;
754
755 sysfs_addrm_start(&acxt, sd->s_parent);
Tejun Heo0c73f182007-06-14 03:45:18 +0900756 sysfs_unlink_sibling(sd);
Tejun Heofb6896da2007-06-14 04:27:24 +0900757 sysfs_remove_one(&acxt, sd);
758 sysfs_addrm_finish(&acxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Tejun Heo608e2662007-06-14 04:27:22 +0900761void sysfs_remove_subdir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Tejun Heo608e2662007-06-14 04:27:22 +0900763 remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766
Tejun Heo608e2662007-06-14 04:27:22 +0900767static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Tejun Heofb6896da2007-06-14 04:27:24 +0900769 struct sysfs_addrm_cxt acxt;
Tejun Heo0c73f182007-06-14 03:45:18 +0900770 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Tejun Heo608e2662007-06-14 04:27:22 +0900772 if (!dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return;
774
Tejun Heo608e2662007-06-14 04:27:22 +0900775 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
Tejun Heofb6896da2007-06-14 04:27:24 +0900776 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900777 pos = &dir_sd->s_children;
Tejun Heo0c73f182007-06-14 03:45:18 +0900778 while (*pos) {
779 struct sysfs_dirent *sd = *pos;
780
Tejun Heob402d722007-06-14 04:27:21 +0900781 if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
Tejun Heo0c73f182007-06-14 03:45:18 +0900782 *pos = sd->s_sibling;
Tejun Heofb6896da2007-06-14 04:27:24 +0900783 sd->s_sibling = NULL;
784 sysfs_remove_one(&acxt, sd);
Tejun Heo0c73f182007-06-14 03:45:18 +0900785 } else
786 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Tejun Heofb6896da2007-06-14 04:27:24 +0900788 sysfs_addrm_finish(&acxt);
Tejun Heo0ab66082007-06-14 03:45:16 +0900789
Tejun Heo608e2662007-06-14 04:27:22 +0900790 remove_dir(dir_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700791}
792
793/**
794 * sysfs_remove_dir - remove an object's directory.
795 * @kobj: object.
796 *
797 * The only thing special about this is that we remove any files in
798 * the directory before we remove the directory, and we've inlined
799 * what used to be sysfs_rmdir() below, instead of calling separately.
800 */
801
802void sysfs_remove_dir(struct kobject * kobj)
803{
Tejun Heo608e2662007-06-14 04:27:22 +0900804 struct sysfs_dirent *sd = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +0900805
Tejun Heo5f995322007-06-14 04:27:23 +0900806 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +0900807 kobj->sd = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900808 spin_unlock(&sysfs_assoc_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +0900809
Tejun Heo608e2662007-06-14 04:27:22 +0900810 __sysfs_remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Tejun Heo608e2662007-06-14 04:27:22 +0900813int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700814 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Tejun Heo608e2662007-06-14 04:27:22 +0900816 struct sysfs_dirent *sd = kobj->sd;
817 struct dentry *new_parent = new_parent_sd->s_dentry;
Tejun Heo0c096b52007-06-14 03:45:15 +0900818 struct dentry *new_dentry;
819 char *dup_name;
Tejun Heo996b7372007-06-14 03:45:14 +0900820 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Tejun Heo608e2662007-06-14 04:27:22 +0900822 if (!new_parent_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700823 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700825 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700827 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900828 if (IS_ERR(new_dentry)) {
829 error = PTR_ERR(new_dentry);
830 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Tejun Heo996b7372007-06-14 03:45:14 +0900832
833 /* By allowing two different directories with the same
834 * d_parent we allow this routine to move between different
835 * shadows of the same directory
836 */
837 error = -EINVAL;
Tejun Heo608e2662007-06-14 04:27:22 +0900838 if (sd->s_parent->s_dentry->d_inode != new_parent->d_inode ||
Tejun Heo996b7372007-06-14 03:45:14 +0900839 new_dentry->d_parent->d_inode != new_parent->d_inode ||
Tejun Heo608e2662007-06-14 04:27:22 +0900840 new_dentry == sd->s_dentry)
Tejun Heo996b7372007-06-14 03:45:14 +0900841 goto out_dput;
842
843 error = -EEXIST;
844 if (new_dentry->d_inode)
845 goto out_dput;
846
Tejun Heo0c096b52007-06-14 03:45:15 +0900847 /* rename kobject and sysfs_dirent */
848 error = -ENOMEM;
849 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
850 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900851 goto out_drop;
852
Tejun Heo0c096b52007-06-14 03:45:15 +0900853 error = kobject_set_name(kobj, "%s", new_name);
854 if (error)
855 goto out_free;
856
857 kfree(sd->s_name);
858 sd->s_name = new_name;
859
860 /* move under the new parent */
Tejun Heo996b7372007-06-14 03:45:14 +0900861 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900862 d_move(sd->s_dentry, new_dentry);
Tejun Heo996b7372007-06-14 03:45:14 +0900863
Tejun Heo3007e992007-06-14 04:27:23 +0900864 mutex_lock(&sysfs_mutex);
865
Tejun Heo0c73f182007-06-14 03:45:18 +0900866 sysfs_unlink_sibling(sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900867 sysfs_get(new_parent_sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900868 sysfs_put(sd->s_parent);
Tejun Heo608e2662007-06-14 04:27:22 +0900869 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900870 sysfs_link_sibling(sd);
Tejun Heo996b7372007-06-14 03:45:14 +0900871
Tejun Heo3007e992007-06-14 04:27:23 +0900872 mutex_unlock(&sysfs_mutex);
873
Tejun Heo996b7372007-06-14 03:45:14 +0900874 error = 0;
875 goto out_unlock;
876
Tejun Heo0c096b52007-06-14 03:45:15 +0900877 out_free:
878 kfree(dup_name);
Tejun Heo996b7372007-06-14 03:45:14 +0900879 out_drop:
880 d_drop(new_dentry);
881 out_dput:
882 dput(new_dentry);
883 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700884 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return error;
886}
887
Cornelia Huck8a824722006-11-20 17:07:51 +0100888int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
889{
890 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
891 struct sysfs_dirent *new_parent_sd, *sd;
892 int error;
893
Cornelia Huck8a824722006-11-20 17:07:51 +0100894 old_parent_dentry = kobj->parent ?
Tejun Heo608e2662007-06-14 04:27:22 +0900895 kobj->parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100896 new_parent_dentry = new_parent ?
Tejun Heo608e2662007-06-14 04:27:22 +0900897 new_parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100898
Mark Lord0de15172007-03-06 01:42:03 -0800899 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
900 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100901again:
902 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
903 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
904 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
905 goto again;
906 }
907
908 new_parent_sd = new_parent_dentry->d_fsdata;
Tejun Heo608e2662007-06-14 04:27:22 +0900909 sd = kobj->sd;
Cornelia Huck8a824722006-11-20 17:07:51 +0100910
911 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
912 strlen(kobj->name));
913 if (IS_ERR(new_dentry)) {
914 error = PTR_ERR(new_dentry);
915 goto out;
916 } else
917 error = 0;
918 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900919 d_move(sd->s_dentry, new_dentry);
Cornelia Huck8a824722006-11-20 17:07:51 +0100920 dput(new_dentry);
921
922 /* Remove from old parent's list and insert into new parent's list. */
Tejun Heo3007e992007-06-14 04:27:23 +0900923 mutex_lock(&sysfs_mutex);
924
Tejun Heo0c73f182007-06-14 03:45:18 +0900925 sysfs_unlink_sibling(sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900926 sysfs_get(new_parent_sd);
927 sysfs_put(sd->s_parent);
928 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900929 sysfs_link_sibling(sd);
Cornelia Huck8a824722006-11-20 17:07:51 +0100930
Tejun Heo3007e992007-06-14 04:27:23 +0900931 mutex_unlock(&sysfs_mutex);
Cornelia Huck8a824722006-11-20 17:07:51 +0100932out:
933 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
934 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
935
936 return error;
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static int sysfs_dir_open(struct inode *inode, struct file *file)
940{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800941 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900943 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Tejun Heo3e519032007-06-14 03:45:15 +0900945 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heo3007e992007-06-14 04:27:23 +0900946 if (sd) {
947 mutex_lock(&sysfs_mutex);
Tejun Heofb6896da2007-06-14 04:27:24 +0900948 sd->s_parent = sysfs_get(parent_sd);
949 sysfs_link_sibling(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900950 mutex_unlock(&sysfs_mutex);
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Tejun Heoa26cd722007-06-14 03:45:14 +0900953 file->private_data = sd;
954 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957static int sysfs_dir_close(struct inode *inode, struct file *file)
958{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct sysfs_dirent * cursor = file->private_data;
960
Tejun Heo3007e992007-06-14 04:27:23 +0900961 mutex_lock(&sysfs_mutex);
Tejun Heo0c73f182007-06-14 03:45:18 +0900962 sysfs_unlink_sibling(cursor);
Tejun Heo3007e992007-06-14 04:27:23 +0900963 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 release_sysfs_dirent(cursor);
966
967 return 0;
968}
969
970/* Relationship between s_mode and the DT_xxx types */
971static inline unsigned char dt_type(struct sysfs_dirent *sd)
972{
973 return (sd->s_mode >> 12) & 15;
974}
975
976static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
977{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800978 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
980 struct sysfs_dirent *cursor = filp->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +0900981 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ino_t ino;
983 int i = filp->f_pos;
984
985 switch (i) {
986 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900987 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
989 break;
990 filp->f_pos++;
991 i++;
992 /* fallthrough */
993 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900994 if (parent_sd->s_parent)
995 ino = parent_sd->s_parent->s_ino;
996 else
997 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
999 break;
1000 filp->f_pos++;
1001 i++;
1002 /* fallthrough */
1003 default:
Tejun Heo3007e992007-06-14 04:27:23 +09001004 mutex_lock(&sysfs_mutex);
1005
Tejun Heo0c73f182007-06-14 03:45:18 +09001006 pos = &parent_sd->s_children;
1007 while (*pos != cursor)
1008 pos = &(*pos)->s_sibling;
Akinobu Mita1bfba4e2006-06-26 00:24:40 -07001009
Tejun Heo0c73f182007-06-14 03:45:18 +09001010 /* unlink cursor */
1011 *pos = cursor->s_sibling;
1012
1013 if (filp->f_pos == 2)
1014 pos = &parent_sd->s_children;
1015
1016 for ( ; *pos; pos = &(*pos)->s_sibling) {
1017 struct sysfs_dirent *next = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 const char * name;
1019 int len;
1020
Tejun Heob402d722007-06-14 04:27:21 +09001021 if (!sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 continue;
1023
Tejun Heo0c096b52007-06-14 03:45:15 +09001024 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +09001026 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (filldir(dirent, name, len, filp->f_pos, ino,
1029 dt_type(next)) < 0)
Tejun Heo0c73f182007-06-14 03:45:18 +09001030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 filp->f_pos++;
1033 }
Tejun Heo0c73f182007-06-14 03:45:18 +09001034
1035 /* put cursor back in */
1036 cursor->s_sibling = *pos;
1037 *pos = cursor;
Tejun Heo3007e992007-06-14 04:27:23 +09001038
1039 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 return 0;
1042}
1043
1044static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
1045{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -08001046 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 switch (origin) {
1049 case 1:
1050 offset += file->f_pos;
1051 case 0:
1052 if (offset >= 0)
1053 break;
1054 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return -EINVAL;
1056 }
1057 if (offset != file->f_pos) {
Tejun Heo3007e992007-06-14 04:27:23 +09001058 mutex_lock(&sysfs_mutex);
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 file->f_pos = offset;
1061 if (file->f_pos >= 2) {
1062 struct sysfs_dirent *sd = dentry->d_fsdata;
1063 struct sysfs_dirent *cursor = file->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +09001064 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 loff_t n = file->f_pos - 2;
1066
Tejun Heo0c73f182007-06-14 03:45:18 +09001067 sysfs_unlink_sibling(cursor);
1068
1069 pos = &sd->s_children;
1070 while (n && *pos) {
1071 struct sysfs_dirent *next = *pos;
Tejun Heob402d722007-06-14 04:27:21 +09001072 if (sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 n--;
Tejun Heo0c73f182007-06-14 03:45:18 +09001074 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Tejun Heo0c73f182007-06-14 03:45:18 +09001076
1077 cursor->s_sibling = *pos;
1078 *pos = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Tejun Heo3007e992007-06-14 04:27:23 +09001080
1081 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Tejun Heo3007e992007-06-14 04:27:23 +09001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return offset;
1085}
1086
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001087
1088/**
1089 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
1090 * @kobj: object we're creating shadow of.
1091 */
1092
1093int sysfs_make_shadowed_dir(struct kobject *kobj,
1094 void * (*follow_link)(struct dentry *, struct nameidata *))
1095{
1096 struct inode *inode;
1097 struct inode_operations *i_op;
1098
Tejun Heo608e2662007-06-14 04:27:22 +09001099 inode = kobj->sd->s_dentry->d_inode;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001100 if (inode->i_op != &sysfs_dir_inode_operations)
1101 return -EINVAL;
1102
1103 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
1104 if (!i_op)
1105 return -ENOMEM;
1106
1107 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
1108 i_op->follow_link = follow_link;
1109
1110 /* Locking of inode->i_op?
1111 * Since setting i_op is a single word write and they
1112 * are atomic we should be ok here.
1113 */
1114 inode->i_op = i_op;
1115 return 0;
1116}
1117
1118/**
1119 * sysfs_create_shadow_dir - create a shadow directory for an object.
1120 * @kobj: object we're creating directory for.
1121 *
1122 * sysfs_make_shadowed_dir must already have been called on this
1123 * directory.
1124 */
1125
Tejun Heo608e2662007-06-14 04:27:22 +09001126struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001127{
Tejun Heo608e2662007-06-14 04:27:22 +09001128 struct dentry *dir = kobj->sd->s_dentry;
Tejun Heo13b30862007-06-14 03:45:14 +09001129 struct inode *inode = dir->d_inode;
1130 struct dentry *parent = dir->d_parent;
1131 struct sysfs_dirent *parent_sd = parent->d_fsdata;
1132 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001133 struct sysfs_dirent *sd;
Tejun Heofb6896da2007-06-14 04:27:24 +09001134 struct sysfs_addrm_cxt acxt;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001135
Tejun Heo608e2662007-06-14 04:27:22 +09001136 sd = ERR_PTR(-EINVAL);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001137 if (!sysfs_is_shadowed_inode(inode))
1138 goto out;
1139
1140 shadow = d_alloc(parent, &dir->d_name);
1141 if (!shadow)
1142 goto nomem;
1143
Tejun Heo3e519032007-06-14 03:45:15 +09001144 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001145 if (!sd)
1146 goto nomem;
Tejun Heo3e519032007-06-14 03:45:15 +09001147 sd->s_elem.dir.kobj = kobj;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001148
Tejun Heofb6896da2007-06-14 04:27:24 +09001149 sysfs_addrm_start(&acxt, parent_sd);
1150
1151 /* add but don't link into children list */
1152 sysfs_add_one(&acxt, sd);
1153
1154 /* attach and instantiate dentry */
1155 sysfs_attach_dentry(sd, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001156 d_instantiate(shadow, igrab(inode));
Tejun Heofb6896da2007-06-14 04:27:24 +09001157 inc_nlink(inode); /* tj: synchronization? */
1158
1159 sysfs_addrm_finish(&acxt);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001160
1161 dget(shadow); /* Extra count - pin the dentry in core */
1162
1163out:
Tejun Heo608e2662007-06-14 04:27:22 +09001164 return sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001165nomem:
1166 dput(shadow);
Tejun Heo608e2662007-06-14 04:27:22 +09001167 sd = ERR_PTR(-ENOMEM);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001168 goto out;
1169}
1170
1171/**
1172 * sysfs_remove_shadow_dir - remove an object's directory.
Tejun Heo608e2662007-06-14 04:27:22 +09001173 * @shadow_sd: sysfs_dirent of shadow directory
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001174 *
1175 * The only thing special about this is that we remove any files in
1176 * the directory before we remove the directory, and we've inlined
1177 * what used to be sysfs_rmdir() below, instead of calling separately.
1178 */
1179
Tejun Heo608e2662007-06-14 04:27:22 +09001180void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001181{
Tejun Heo608e2662007-06-14 04:27:22 +09001182 __sysfs_remove_dir(shadow_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001183}
1184
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001185const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 .open = sysfs_dir_open,
1187 .release = sysfs_dir_close,
1188 .llseek = sysfs_dir_lseek,
1189 .read = generic_read_dir,
1190 .readdir = sysfs_readdir,
1191};