blob: edb30621b82f3f75a4d39a87527b834b40c6308a [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 Heofb6896d2007-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 Heofb6896d2007-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 Heofb6896d2007-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 Heofb6896d2007-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 Heofb6896d2007-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 Heofb6896d2007-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 Heofb6896d2007-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 Heofb6896d2007-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 Heofb6896d2007-06-14 04:27:24 +0900346 struct inode *inode;
Tejun Heoa26cd722007-06-14 03:45:14 +0900347
Tejun Heofb6896d2007-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/**
438 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
439 * @acxt: addrm context to finish up
440 *
441 * Finish up sysfs_dirent add/remove. Resources acquired by
442 * sysfs_addrm_start() are released and removed sysfs_dirents are
443 * cleaned up. Timestamps on the parent inode are updated.
444 *
445 * LOCKING:
446 * All mutexes acquired by sysfs_addrm_start() are released.
447 *
448 * RETURNS:
449 * Number of added/removed sysfs_dirents since sysfs_addrm_start().
450 */
451int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
452{
453 /* release resources acquired by sysfs_addrm_start() */
454 mutex_unlock(&sysfs_mutex);
455 if (acxt->parent_inode) {
456 struct inode *inode = acxt->parent_inode;
457
458 /* if added/removed, update timestamps on the parent */
459 if (acxt->cnt)
460 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
461
462 mutex_unlock(&inode->i_mutex);
463 iput(inode);
Tejun Heo13b30862007-06-14 03:45:14 +0900464 }
Tejun Heofb6896d2007-06-14 04:27:24 +0900465
466 /* kill removed sysfs_dirents */
467 while (acxt->removed) {
468 struct sysfs_dirent *sd = acxt->removed;
469
470 acxt->removed = sd->s_sibling;
471 sd->s_sibling = NULL;
472
473 sysfs_drop_dentry(sd);
474 sysfs_deactivate(sd);
475 sysfs_put(sd);
476 }
477
478 return acxt->cnt;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700479}
480
Tejun Heof0b0af42007-06-14 04:27:22 +0900481/**
482 * sysfs_find_dirent - find sysfs_dirent with the given name
483 * @parent_sd: sysfs_dirent to search under
484 * @name: name to look for
Maneesh Sonic5168652006-03-09 19:40:14 +0530485 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900486 * Look for sysfs_dirent with name @name under @parent_sd.
Maneesh Sonic5168652006-03-09 19:40:14 +0530487 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900488 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900489 * mutex_lock(sysfs_mutex)
Tejun Heof0b0af42007-06-14 04:27:22 +0900490 *
491 * RETURNS:
492 * Pointer to sysfs_dirent if found, NULL if not.
Maneesh Sonic5168652006-03-09 19:40:14 +0530493 */
Tejun Heof0b0af42007-06-14 04:27:22 +0900494struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
495 const unsigned char *name)
Maneesh Sonic5168652006-03-09 19:40:14 +0530496{
Tejun Heof0b0af42007-06-14 04:27:22 +0900497 struct sysfs_dirent *sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530498
Tejun Heof0b0af42007-06-14 04:27:22 +0900499 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
500 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
501 return sd;
502 return NULL;
503}
Maneesh Sonic5168652006-03-09 19:40:14 +0530504
Tejun Heof0b0af42007-06-14 04:27:22 +0900505/**
506 * sysfs_get_dirent - find and get sysfs_dirent with the given name
507 * @parent_sd: sysfs_dirent to search under
508 * @name: name to look for
509 *
510 * Look for sysfs_dirent with name @name under @parent_sd and get
511 * it if found.
512 *
513 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900514 * Kernel thread context (may sleep). Grabs sysfs_mutex.
Tejun Heof0b0af42007-06-14 04:27:22 +0900515 *
516 * RETURNS:
517 * Pointer to sysfs_dirent if found, NULL if not.
518 */
519struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
520 const unsigned char *name)
521{
522 struct sysfs_dirent *sd;
523
Tejun Heo3007e992007-06-14 04:27:23 +0900524 mutex_lock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900525 sd = sysfs_find_dirent(parent_sd, name);
526 sysfs_get(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900527 mutex_unlock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900528
529 return sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530530}
531
Tejun Heo608e2662007-06-14 04:27:22 +0900532static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
533 const char *name, struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Tejun Heo608e2662007-06-14 04:27:22 +0900535 struct dentry *parent = parent_sd->s_dentry;
Tejun Heofb6896d2007-06-14 04:27:24 +0900536 struct sysfs_addrm_cxt acxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int error;
538 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900539 struct dentry *dentry;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900540 struct inode *inode;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900541 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Tejun Heofb6896d2007-06-14 04:27:24 +0900543 sysfs_addrm_start(&acxt, parent_sd);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900544
Tejun Heofc9f54b2007-06-14 03:45:17 +0900545 /* allocate */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900546 dentry = lookup_one_len(name, parent, strlen(name));
547 if (IS_ERR(dentry)) {
548 error = PTR_ERR(dentry);
Tejun Heofb6896d2007-06-14 04:27:24 +0900549 goto out_finish;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900550 }
551
552 error = -EEXIST;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900553 if (dentry->d_inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900554 goto out_dput;
555
Tejun Heoa26cd722007-06-14 03:45:14 +0900556 error = -ENOMEM;
Tejun Heo3e519032007-06-14 03:45:15 +0900557 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900558 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900559 goto out_drop;
Tejun Heo3e519032007-06-14 03:45:15 +0900560 sd->s_elem.dir.kobj = kobj;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900561
Tejun Heo8312a8d2007-06-14 03:45:17 +0900562 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900563 if (!inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900564 goto out_sput;
565
Tejun Heo8312a8d2007-06-14 03:45:17 +0900566 if (inode->i_state & I_NEW) {
567 inode->i_op = &sysfs_dir_inode_operations;
568 inode->i_fop = &sysfs_dir_operations;
569 /* directory inodes start off with i_nlink == 2 (for ".") */
570 inc_nlink(inode);
571 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900572
573 /* link in */
574 error = -EEXIST;
Tejun Heofb6896d2007-06-14 04:27:24 +0900575 if (sysfs_find_dirent(parent_sd, name))
Tejun Heofc9f54b2007-06-14 03:45:17 +0900576 goto out_iput;
577
Tejun Heofb6896d2007-06-14 04:27:24 +0900578 sysfs_add_one(&acxt, sd);
579 sysfs_link_sibling(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900580 sysfs_instantiate(dentry, inode);
Tejun Heofb6896d2007-06-14 04:27:24 +0900581 sysfs_attach_dentry(sd, dentry);
Tejun Heo3007e992007-06-14 04:27:23 +0900582
Tejun Heo608e2662007-06-14 04:27:22 +0900583 *p_sd = sd;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900584 error = 0;
Tejun Heofb6896d2007-06-14 04:27:24 +0900585 goto out_finish; /* pin directory dentry in core */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900586
Tejun Heofc9f54b2007-06-14 03:45:17 +0900587 out_iput:
588 iput(inode);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900589 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900590 sysfs_put(sd);
591 out_drop:
592 d_drop(dentry);
593 out_dput:
594 dput(dentry);
Tejun Heofb6896d2007-06-14 04:27:24 +0900595 out_finish:
596 sysfs_addrm_finish(&acxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return error;
598}
599
Tejun Heo608e2662007-06-14 04:27:22 +0900600int sysfs_create_subdir(struct kobject *kobj, const char *name,
601 struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Tejun Heo608e2662007-06-14 04:27:22 +0900603 return create_dir(kobj, kobj->sd, name, p_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/**
607 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * @kobj: object we're creating directory for.
Tejun Heo608e2662007-06-14 04:27:22 +0900609 * @shadow_parent: parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Tejun Heo608e2662007-06-14 04:27:22 +0900611int sysfs_create_dir(struct kobject *kobj,
612 struct sysfs_dirent *shadow_parent_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Tejun Heo608e2662007-06-14 04:27:22 +0900614 struct sysfs_dirent *parent_sd, *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int error = 0;
616
617 BUG_ON(!kobj);
618
Tejun Heo608e2662007-06-14 04:27:22 +0900619 if (shadow_parent_sd)
620 parent_sd = shadow_parent_sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700621 else if (kobj->parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900622 parent_sd = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 else if (sysfs_mount && sysfs_mount->mnt_sb)
Tejun Heo608e2662007-06-14 04:27:22 +0900624 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 else
626 return -EFAULT;
627
Tejun Heo608e2662007-06-14 04:27:22 +0900628 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!error)
Tejun Heo608e2662007-06-14 04:27:22 +0900630 kobj->sd = sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return error;
632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
635 struct nameidata *nd)
636{
637 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
638 struct sysfs_dirent * sd;
Tejun Heob402d722007-06-14 04:27:21 +0900639 struct bin_attribute *bin_attr;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900640 struct inode *inode;
641 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Tejun Heo0c73f182007-06-14 03:45:18 +0900643 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
Tejun Heob402d722007-06-14 04:27:21 +0900644 if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
Tejun Heofc9f54b2007-06-14 03:45:17 +0900645 !strcmp(sd->s_name, dentry->d_name.name)) {
646 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
648 }
649 }
650
Tejun Heofc9f54b2007-06-14 03:45:17 +0900651 /* no such entry */
652 if (!found)
653 return NULL;
654
655 /* attach dentry and inode */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900656 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900657 if (!inode)
658 return ERR_PTR(-ENOMEM);
659
Tejun Heo3007e992007-06-14 04:27:23 +0900660 mutex_lock(&sysfs_mutex);
661
Tejun Heo8312a8d2007-06-14 03:45:17 +0900662 if (inode->i_state & I_NEW) {
663 /* initialize inode according to type */
Tejun Heob402d722007-06-14 04:27:21 +0900664 switch (sysfs_type(sd)) {
665 case SYSFS_KOBJ_ATTR:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900666 inode->i_size = PAGE_SIZE;
667 inode->i_fop = &sysfs_file_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900668 break;
669 case SYSFS_KOBJ_BIN_ATTR:
670 bin_attr = sd->s_elem.bin_attr.bin_attr;
Tejun Heo8312a8d2007-06-14 03:45:17 +0900671 inode->i_size = bin_attr->size;
672 inode->i_fop = &bin_fops;
Tejun Heob402d722007-06-14 04:27:21 +0900673 break;
674 case SYSFS_KOBJ_LINK:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900675 inode->i_op = &sysfs_symlink_inode_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900676 break;
677 default:
678 BUG();
679 }
Tejun Heo8312a8d2007-06-14 03:45:17 +0900680 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900681
682 sysfs_instantiate(dentry, inode);
683 sysfs_attach_dentry(sd, dentry);
684
Tejun Heo3007e992007-06-14 04:27:23 +0900685 mutex_unlock(&sysfs_mutex);
686
Tejun Heofc9f54b2007-06-14 03:45:17 +0900687 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800690const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530692 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693};
694
Tejun Heo608e2662007-06-14 04:27:22 +0900695static void remove_dir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Tejun Heofb6896d2007-06-14 04:27:24 +0900697 struct sysfs_addrm_cxt acxt;
698
699 sysfs_addrm_start(&acxt, sd->s_parent);
Tejun Heo0c73f182007-06-14 03:45:18 +0900700 sysfs_unlink_sibling(sd);
Tejun Heofb6896d2007-06-14 04:27:24 +0900701 sysfs_remove_one(&acxt, sd);
702 sysfs_addrm_finish(&acxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Tejun Heo608e2662007-06-14 04:27:22 +0900705void sysfs_remove_subdir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Tejun Heo608e2662007-06-14 04:27:22 +0900707 remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710
Tejun Heo608e2662007-06-14 04:27:22 +0900711static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Tejun Heofb6896d2007-06-14 04:27:24 +0900713 struct sysfs_addrm_cxt acxt;
Tejun Heo0c73f182007-06-14 03:45:18 +0900714 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Tejun Heo608e2662007-06-14 04:27:22 +0900716 if (!dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return;
718
Tejun Heo608e2662007-06-14 04:27:22 +0900719 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
Tejun Heofb6896d2007-06-14 04:27:24 +0900720 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900721 pos = &dir_sd->s_children;
Tejun Heo0c73f182007-06-14 03:45:18 +0900722 while (*pos) {
723 struct sysfs_dirent *sd = *pos;
724
Tejun Heob402d722007-06-14 04:27:21 +0900725 if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
Tejun Heo0c73f182007-06-14 03:45:18 +0900726 *pos = sd->s_sibling;
Tejun Heofb6896d2007-06-14 04:27:24 +0900727 sd->s_sibling = NULL;
728 sysfs_remove_one(&acxt, sd);
Tejun Heo0c73f182007-06-14 03:45:18 +0900729 } else
730 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Tejun Heofb6896d2007-06-14 04:27:24 +0900732 sysfs_addrm_finish(&acxt);
Tejun Heo0ab66082007-06-14 03:45:16 +0900733
Tejun Heo608e2662007-06-14 04:27:22 +0900734 remove_dir(dir_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700735}
736
737/**
738 * sysfs_remove_dir - remove an object's directory.
739 * @kobj: object.
740 *
741 * The only thing special about this is that we remove any files in
742 * the directory before we remove the directory, and we've inlined
743 * what used to be sysfs_rmdir() below, instead of calling separately.
744 */
745
746void sysfs_remove_dir(struct kobject * kobj)
747{
Tejun Heo608e2662007-06-14 04:27:22 +0900748 struct sysfs_dirent *sd = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +0900749
Tejun Heo5f995322007-06-14 04:27:23 +0900750 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +0900751 kobj->sd = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900752 spin_unlock(&sysfs_assoc_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +0900753
Tejun Heo608e2662007-06-14 04:27:22 +0900754 __sysfs_remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Tejun Heo608e2662007-06-14 04:27:22 +0900757int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700758 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Tejun Heo608e2662007-06-14 04:27:22 +0900760 struct sysfs_dirent *sd = kobj->sd;
761 struct dentry *new_parent = new_parent_sd->s_dentry;
Tejun Heo0c096b52007-06-14 03:45:15 +0900762 struct dentry *new_dentry;
763 char *dup_name;
Tejun Heo996b7372007-06-14 03:45:14 +0900764 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Tejun Heo608e2662007-06-14 04:27:22 +0900766 if (!new_parent_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700767 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700769 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700771 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900772 if (IS_ERR(new_dentry)) {
773 error = PTR_ERR(new_dentry);
774 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Tejun Heo996b7372007-06-14 03:45:14 +0900776
777 /* By allowing two different directories with the same
778 * d_parent we allow this routine to move between different
779 * shadows of the same directory
780 */
781 error = -EINVAL;
Tejun Heo608e2662007-06-14 04:27:22 +0900782 if (sd->s_parent->s_dentry->d_inode != new_parent->d_inode ||
Tejun Heo996b7372007-06-14 03:45:14 +0900783 new_dentry->d_parent->d_inode != new_parent->d_inode ||
Tejun Heo608e2662007-06-14 04:27:22 +0900784 new_dentry == sd->s_dentry)
Tejun Heo996b7372007-06-14 03:45:14 +0900785 goto out_dput;
786
787 error = -EEXIST;
788 if (new_dentry->d_inode)
789 goto out_dput;
790
Tejun Heo0c096b52007-06-14 03:45:15 +0900791 /* rename kobject and sysfs_dirent */
792 error = -ENOMEM;
793 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
794 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900795 goto out_drop;
796
Tejun Heo0c096b52007-06-14 03:45:15 +0900797 error = kobject_set_name(kobj, "%s", new_name);
798 if (error)
799 goto out_free;
800
801 kfree(sd->s_name);
802 sd->s_name = new_name;
803
804 /* move under the new parent */
Tejun Heo996b7372007-06-14 03:45:14 +0900805 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900806 d_move(sd->s_dentry, new_dentry);
Tejun Heo996b7372007-06-14 03:45:14 +0900807
Tejun Heo3007e992007-06-14 04:27:23 +0900808 mutex_lock(&sysfs_mutex);
809
Tejun Heo0c73f182007-06-14 03:45:18 +0900810 sysfs_unlink_sibling(sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900811 sysfs_get(new_parent_sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900812 sysfs_put(sd->s_parent);
Tejun Heo608e2662007-06-14 04:27:22 +0900813 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900814 sysfs_link_sibling(sd);
Tejun Heo996b7372007-06-14 03:45:14 +0900815
Tejun Heo3007e992007-06-14 04:27:23 +0900816 mutex_unlock(&sysfs_mutex);
817
Tejun Heo996b7372007-06-14 03:45:14 +0900818 error = 0;
819 goto out_unlock;
820
Tejun Heo0c096b52007-06-14 03:45:15 +0900821 out_free:
822 kfree(dup_name);
Tejun Heo996b7372007-06-14 03:45:14 +0900823 out_drop:
824 d_drop(new_dentry);
825 out_dput:
826 dput(new_dentry);
827 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700828 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return error;
830}
831
Cornelia Huck8a824722006-11-20 17:07:51 +0100832int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
833{
834 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
835 struct sysfs_dirent *new_parent_sd, *sd;
836 int error;
837
Cornelia Huck8a824722006-11-20 17:07:51 +0100838 old_parent_dentry = kobj->parent ?
Tejun Heo608e2662007-06-14 04:27:22 +0900839 kobj->parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100840 new_parent_dentry = new_parent ?
Tejun Heo608e2662007-06-14 04:27:22 +0900841 new_parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100842
Mark Lord0de15172007-03-06 01:42:03 -0800843 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
844 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100845again:
846 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
847 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
848 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
849 goto again;
850 }
851
852 new_parent_sd = new_parent_dentry->d_fsdata;
Tejun Heo608e2662007-06-14 04:27:22 +0900853 sd = kobj->sd;
Cornelia Huck8a824722006-11-20 17:07:51 +0100854
855 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
856 strlen(kobj->name));
857 if (IS_ERR(new_dentry)) {
858 error = PTR_ERR(new_dentry);
859 goto out;
860 } else
861 error = 0;
862 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900863 d_move(sd->s_dentry, new_dentry);
Cornelia Huck8a824722006-11-20 17:07:51 +0100864 dput(new_dentry);
865
866 /* Remove from old parent's list and insert into new parent's list. */
Tejun Heo3007e992007-06-14 04:27:23 +0900867 mutex_lock(&sysfs_mutex);
868
Tejun Heo0c73f182007-06-14 03:45:18 +0900869 sysfs_unlink_sibling(sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900870 sysfs_get(new_parent_sd);
871 sysfs_put(sd->s_parent);
872 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900873 sysfs_link_sibling(sd);
Cornelia Huck8a824722006-11-20 17:07:51 +0100874
Tejun Heo3007e992007-06-14 04:27:23 +0900875 mutex_unlock(&sysfs_mutex);
Cornelia Huck8a824722006-11-20 17:07:51 +0100876out:
877 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
878 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
879
880 return error;
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static int sysfs_dir_open(struct inode *inode, struct file *file)
884{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800885 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900887 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Tejun Heo3e519032007-06-14 03:45:15 +0900889 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heo3007e992007-06-14 04:27:23 +0900890 if (sd) {
891 mutex_lock(&sysfs_mutex);
Tejun Heofb6896d2007-06-14 04:27:24 +0900892 sd->s_parent = sysfs_get(parent_sd);
893 sysfs_link_sibling(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900894 mutex_unlock(&sysfs_mutex);
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Tejun Heoa26cd722007-06-14 03:45:14 +0900897 file->private_data = sd;
898 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static int sysfs_dir_close(struct inode *inode, struct file *file)
902{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 struct sysfs_dirent * cursor = file->private_data;
904
Tejun Heo3007e992007-06-14 04:27:23 +0900905 mutex_lock(&sysfs_mutex);
Tejun Heo0c73f182007-06-14 03:45:18 +0900906 sysfs_unlink_sibling(cursor);
Tejun Heo3007e992007-06-14 04:27:23 +0900907 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 release_sysfs_dirent(cursor);
910
911 return 0;
912}
913
914/* Relationship between s_mode and the DT_xxx types */
915static inline unsigned char dt_type(struct sysfs_dirent *sd)
916{
917 return (sd->s_mode >> 12) & 15;
918}
919
920static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
921{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800922 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
924 struct sysfs_dirent *cursor = filp->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +0900925 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ino_t ino;
927 int i = filp->f_pos;
928
929 switch (i) {
930 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900931 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
933 break;
934 filp->f_pos++;
935 i++;
936 /* fallthrough */
937 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900938 if (parent_sd->s_parent)
939 ino = parent_sd->s_parent->s_ino;
940 else
941 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
943 break;
944 filp->f_pos++;
945 i++;
946 /* fallthrough */
947 default:
Tejun Heo3007e992007-06-14 04:27:23 +0900948 mutex_lock(&sysfs_mutex);
949
Tejun Heo0c73f182007-06-14 03:45:18 +0900950 pos = &parent_sd->s_children;
951 while (*pos != cursor)
952 pos = &(*pos)->s_sibling;
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700953
Tejun Heo0c73f182007-06-14 03:45:18 +0900954 /* unlink cursor */
955 *pos = cursor->s_sibling;
956
957 if (filp->f_pos == 2)
958 pos = &parent_sd->s_children;
959
960 for ( ; *pos; pos = &(*pos)->s_sibling) {
961 struct sysfs_dirent *next = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 const char * name;
963 int len;
964
Tejun Heob402d722007-06-14 04:27:21 +0900965 if (!sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 continue;
967
Tejun Heo0c096b52007-06-14 03:45:15 +0900968 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900970 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 if (filldir(dirent, name, len, filp->f_pos, ino,
973 dt_type(next)) < 0)
Tejun Heo0c73f182007-06-14 03:45:18 +0900974 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 filp->f_pos++;
977 }
Tejun Heo0c73f182007-06-14 03:45:18 +0900978
979 /* put cursor back in */
980 cursor->s_sibling = *pos;
981 *pos = cursor;
Tejun Heo3007e992007-06-14 04:27:23 +0900982
983 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 return 0;
986}
987
988static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
989{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800990 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 switch (origin) {
993 case 1:
994 offset += file->f_pos;
995 case 0:
996 if (offset >= 0)
997 break;
998 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return -EINVAL;
1000 }
1001 if (offset != file->f_pos) {
Tejun Heo3007e992007-06-14 04:27:23 +09001002 mutex_lock(&sysfs_mutex);
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 file->f_pos = offset;
1005 if (file->f_pos >= 2) {
1006 struct sysfs_dirent *sd = dentry->d_fsdata;
1007 struct sysfs_dirent *cursor = file->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +09001008 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 loff_t n = file->f_pos - 2;
1010
Tejun Heo0c73f182007-06-14 03:45:18 +09001011 sysfs_unlink_sibling(cursor);
1012
1013 pos = &sd->s_children;
1014 while (n && *pos) {
1015 struct sysfs_dirent *next = *pos;
Tejun Heob402d722007-06-14 04:27:21 +09001016 if (sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 n--;
Tejun Heo0c73f182007-06-14 03:45:18 +09001018 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Tejun Heo0c73f182007-06-14 03:45:18 +09001020
1021 cursor->s_sibling = *pos;
1022 *pos = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Tejun Heo3007e992007-06-14 04:27:23 +09001024
1025 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Tejun Heo3007e992007-06-14 04:27:23 +09001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return offset;
1029}
1030
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001031
1032/**
1033 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
1034 * @kobj: object we're creating shadow of.
1035 */
1036
1037int sysfs_make_shadowed_dir(struct kobject *kobj,
1038 void * (*follow_link)(struct dentry *, struct nameidata *))
1039{
1040 struct inode *inode;
1041 struct inode_operations *i_op;
1042
Tejun Heo608e2662007-06-14 04:27:22 +09001043 inode = kobj->sd->s_dentry->d_inode;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001044 if (inode->i_op != &sysfs_dir_inode_operations)
1045 return -EINVAL;
1046
1047 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
1048 if (!i_op)
1049 return -ENOMEM;
1050
1051 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
1052 i_op->follow_link = follow_link;
1053
1054 /* Locking of inode->i_op?
1055 * Since setting i_op is a single word write and they
1056 * are atomic we should be ok here.
1057 */
1058 inode->i_op = i_op;
1059 return 0;
1060}
1061
1062/**
1063 * sysfs_create_shadow_dir - create a shadow directory for an object.
1064 * @kobj: object we're creating directory for.
1065 *
1066 * sysfs_make_shadowed_dir must already have been called on this
1067 * directory.
1068 */
1069
Tejun Heo608e2662007-06-14 04:27:22 +09001070struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001071{
Tejun Heo608e2662007-06-14 04:27:22 +09001072 struct dentry *dir = kobj->sd->s_dentry;
Tejun Heo13b30862007-06-14 03:45:14 +09001073 struct inode *inode = dir->d_inode;
1074 struct dentry *parent = dir->d_parent;
1075 struct sysfs_dirent *parent_sd = parent->d_fsdata;
1076 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001077 struct sysfs_dirent *sd;
Tejun Heofb6896d2007-06-14 04:27:24 +09001078 struct sysfs_addrm_cxt acxt;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001079
Tejun Heo608e2662007-06-14 04:27:22 +09001080 sd = ERR_PTR(-EINVAL);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001081 if (!sysfs_is_shadowed_inode(inode))
1082 goto out;
1083
1084 shadow = d_alloc(parent, &dir->d_name);
1085 if (!shadow)
1086 goto nomem;
1087
Tejun Heo3e519032007-06-14 03:45:15 +09001088 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001089 if (!sd)
1090 goto nomem;
Tejun Heo3e519032007-06-14 03:45:15 +09001091 sd->s_elem.dir.kobj = kobj;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001092
Tejun Heofb6896d2007-06-14 04:27:24 +09001093 sysfs_addrm_start(&acxt, parent_sd);
1094
1095 /* add but don't link into children list */
1096 sysfs_add_one(&acxt, sd);
1097
1098 /* attach and instantiate dentry */
1099 sysfs_attach_dentry(sd, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001100 d_instantiate(shadow, igrab(inode));
Tejun Heofb6896d2007-06-14 04:27:24 +09001101 inc_nlink(inode); /* tj: synchronization? */
1102
1103 sysfs_addrm_finish(&acxt);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001104
1105 dget(shadow); /* Extra count - pin the dentry in core */
1106
1107out:
Tejun Heo608e2662007-06-14 04:27:22 +09001108 return sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001109nomem:
1110 dput(shadow);
Tejun Heo608e2662007-06-14 04:27:22 +09001111 sd = ERR_PTR(-ENOMEM);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001112 goto out;
1113}
1114
1115/**
1116 * sysfs_remove_shadow_dir - remove an object's directory.
Tejun Heo608e2662007-06-14 04:27:22 +09001117 * @shadow_sd: sysfs_dirent of shadow directory
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001118 *
1119 * The only thing special about this is that we remove any files in
1120 * the directory before we remove the directory, and we've inlined
1121 * what used to be sysfs_rmdir() below, instead of calling separately.
1122 */
1123
Tejun Heo608e2662007-06-14 04:27:22 +09001124void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001125{
Tejun Heo608e2662007-06-14 04:27:22 +09001126 __sysfs_remove_dir(shadow_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -07001127}
1128
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001129const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 .open = sysfs_dir_open,
1131 .release = sysfs_dir_close,
1132 .llseek = sysfs_dir_lseek,
1133 .read = generic_read_dir,
1134 .readdir = sysfs_readdir,
1135};