blob: a0da2b05a7545580ab152cae49098438edb3ac28 [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>
Dave Young869512a2007-07-26 14:53:53 +000014#include <linux/mutex.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 Heo41fc1c22007-08-02 21:38:03 +090033static void 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 Heo41fc1c22007-08-02 21:38:03 +090052static void 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 Heo53e0ae92007-06-14 04:27:25 +090066 * sysfs_get_dentry - get dentry for the given sysfs_dirent
67 * @sd: sysfs_dirent of interest
68 *
69 * Get dentry for @sd. Dentry is looked up if currently not
70 * present. This function climbs sysfs_dirent tree till it
71 * reaches a sysfs_dirent with valid dentry attached and descends
72 * down from there looking up dentry for each step.
73 *
74 * LOCKING:
75 * Kernel thread context (may sleep)
76 *
77 * RETURNS:
78 * Pointer to found dentry on success, ERR_PTR() value on error.
79 */
80struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
81{
82 struct sysfs_dirent *cur;
83 struct dentry *parent_dentry, *dentry;
84 int i, depth;
85
86 /* Find the first parent which has valid s_dentry and get the
87 * dentry.
88 */
89 mutex_lock(&sysfs_mutex);
90 restart0:
91 spin_lock(&sysfs_assoc_lock);
92 restart1:
93 spin_lock(&dcache_lock);
94
95 dentry = NULL;
96 depth = 0;
97 cur = sd;
98 while (!cur->s_dentry || !cur->s_dentry->d_inode) {
99 if (cur->s_flags & SYSFS_FLAG_REMOVED) {
100 dentry = ERR_PTR(-ENOENT);
101 depth = 0;
102 break;
103 }
104 cur = cur->s_parent;
105 depth++;
106 }
107 if (!IS_ERR(dentry))
108 dentry = dget_locked(cur->s_dentry);
109
110 spin_unlock(&dcache_lock);
111 spin_unlock(&sysfs_assoc_lock);
112
113 /* from the found dentry, look up depth times */
114 while (depth--) {
115 /* find and get depth'th ancestor */
116 for (cur = sd, i = 0; cur && i < depth; i++)
117 cur = cur->s_parent;
118
119 /* This can happen if tree structure was modified due
120 * to move/rename. Restart.
121 */
122 if (i != depth) {
123 dput(dentry);
124 goto restart0;
125 }
126
127 sysfs_get(cur);
128
129 mutex_unlock(&sysfs_mutex);
130
131 /* look it up */
132 parent_dentry = dentry;
133 dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
134 strlen(cur->s_name));
135 dput(parent_dentry);
136
137 if (IS_ERR(dentry)) {
138 sysfs_put(cur);
139 return dentry;
140 }
141
142 mutex_lock(&sysfs_mutex);
143 spin_lock(&sysfs_assoc_lock);
144
145 /* This, again, can happen if tree structure has
146 * changed and we looked up the wrong thing. Restart.
147 */
148 if (cur->s_dentry != dentry) {
149 dput(dentry);
150 sysfs_put(cur);
151 goto restart1;
152 }
153
154 spin_unlock(&sysfs_assoc_lock);
155
156 sysfs_put(cur);
157 }
158
159 mutex_unlock(&sysfs_mutex);
160 return dentry;
161}
162
163/**
Tejun Heob6b4a432007-06-14 03:45:18 +0900164 * sysfs_get_active - get an active reference to sysfs_dirent
165 * @sd: sysfs_dirent to get an active reference to
166 *
167 * Get an active reference of @sd. This function is noop if @sd
168 * is NULL.
169 *
170 * RETURNS:
171 * Pointer to @sd on success, NULL on failure.
172 */
173struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
174{
Tejun Heo8619f972007-06-14 03:45:18 +0900175 if (unlikely(!sd))
176 return NULL;
177
178 while (1) {
179 int v, t;
180
181 v = atomic_read(&sd->s_active);
182 if (unlikely(v < 0))
183 return NULL;
184
185 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
186 if (likely(t == v))
187 return sd;
188 if (t < 0)
189 return NULL;
190
191 cpu_relax();
Tejun Heob6b4a432007-06-14 03:45:18 +0900192 }
Tejun Heob6b4a432007-06-14 03:45:18 +0900193}
194
195/**
196 * sysfs_put_active - put an active reference to sysfs_dirent
197 * @sd: sysfs_dirent to put an active reference to
198 *
199 * Put an active reference to @sd. This function is noop if @sd
200 * is NULL.
201 */
202void sysfs_put_active(struct sysfs_dirent *sd)
203{
Tejun Heo8619f972007-06-14 03:45:18 +0900204 struct completion *cmpl;
205 int v;
206
207 if (unlikely(!sd))
208 return;
209
210 v = atomic_dec_return(&sd->s_active);
211 if (likely(v != SD_DEACTIVATED_BIAS))
212 return;
213
214 /* atomic_dec_return() is a mb(), we'll always see the updated
Tejun Heo0c73f182007-06-14 03:45:18 +0900215 * sd->s_sibling.
Tejun Heo8619f972007-06-14 03:45:18 +0900216 */
Tejun Heo0c73f182007-06-14 03:45:18 +0900217 cmpl = (void *)sd->s_sibling;
Tejun Heo8619f972007-06-14 03:45:18 +0900218 complete(cmpl);
Tejun Heob6b4a432007-06-14 03:45:18 +0900219}
220
221/**
222 * sysfs_get_active_two - get active references to sysfs_dirent and parent
223 * @sd: sysfs_dirent of interest
224 *
225 * Get active reference to @sd and its parent. Parent's active
226 * reference is grabbed first. This function is noop if @sd is
227 * NULL.
228 *
229 * RETURNS:
230 * Pointer to @sd on success, NULL on failure.
231 */
232struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
233{
234 if (sd) {
235 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
236 return NULL;
237 if (unlikely(!sysfs_get_active(sd))) {
238 sysfs_put_active(sd->s_parent);
239 return NULL;
240 }
241 }
242 return sd;
243}
244
245/**
246 * sysfs_put_active_two - put active references to sysfs_dirent and parent
247 * @sd: sysfs_dirent of interest
248 *
249 * Put active references to @sd and its parent. This function is
250 * noop if @sd is NULL.
251 */
252void sysfs_put_active_two(struct sysfs_dirent *sd)
253{
254 if (sd) {
255 sysfs_put_active(sd);
256 sysfs_put_active(sd->s_parent);
257 }
258}
259
260/**
261 * sysfs_deactivate - deactivate sysfs_dirent
262 * @sd: sysfs_dirent to deactivate
263 *
Tejun Heo8619f972007-06-14 03:45:18 +0900264 * Deny new active references and drain existing ones.
Tejun Heob6b4a432007-06-14 03:45:18 +0900265 */
Tejun Heofb6896d2007-06-14 04:27:24 +0900266static void sysfs_deactivate(struct sysfs_dirent *sd)
Tejun Heob6b4a432007-06-14 03:45:18 +0900267{
Tejun Heo8619f972007-06-14 03:45:18 +0900268 DECLARE_COMPLETION_ONSTACK(wait);
269 int v;
Tejun Heob6b4a432007-06-14 03:45:18 +0900270
Tejun Heo380e6fb2007-06-14 04:27:22 +0900271 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
Tejun Heo0c73f182007-06-14 03:45:18 +0900272 sd->s_sibling = (void *)&wait;
Tejun Heo8619f972007-06-14 03:45:18 +0900273
274 /* atomic_add_return() is a mb(), put_active() will always see
Tejun Heo0c73f182007-06-14 03:45:18 +0900275 * the updated sd->s_sibling.
Tejun Heob6b4a432007-06-14 03:45:18 +0900276 */
Tejun Heo8619f972007-06-14 03:45:18 +0900277 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
278
279 if (v != SD_DEACTIVATED_BIAS)
280 wait_for_completion(&wait);
281
Tejun Heo0c73f182007-06-14 03:45:18 +0900282 sd->s_sibling = NULL;
Tejun Heob6b4a432007-06-14 03:45:18 +0900283}
284
Tejun Heo42b37df2007-06-14 03:45:17 +0900285static int sysfs_alloc_ino(ino_t *pino)
Tejun Heo2b611bb2007-06-14 03:45:13 +0900286{
287 int ino, rc;
288
289 retry:
290 spin_lock(&sysfs_ino_lock);
291 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
292 spin_unlock(&sysfs_ino_lock);
293
294 if (rc == -EAGAIN) {
295 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
296 goto retry;
297 rc = -ENOMEM;
298 }
299
300 *pino = ino;
301 return rc;
302}
303
304static void sysfs_free_ino(ino_t ino)
305{
306 spin_lock(&sysfs_ino_lock);
307 ida_remove(&sysfs_ino_ida, ino);
308 spin_unlock(&sysfs_ino_lock);
309}
310
Tejun Heofa7f9122007-06-14 03:45:13 +0900311void release_sysfs_dirent(struct sysfs_dirent * sd)
312{
Tejun Heo13b30862007-06-14 03:45:14 +0900313 struct sysfs_dirent *parent_sd;
314
315 repeat:
Tejun Heo3007e992007-06-14 04:27:23 +0900316 /* Moving/renaming is always done while holding reference.
317 * sd->s_parent won't change beneath us.
318 */
Tejun Heo13b30862007-06-14 03:45:14 +0900319 parent_sd = sd->s_parent;
320
Tejun Heob402d722007-06-14 04:27:21 +0900321 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
Tejun Heo2b29ac22007-06-14 03:45:15 +0900322 sysfs_put(sd->s_elem.symlink.target_sd);
Tejun Heob402d722007-06-14 04:27:21 +0900323 if (sysfs_type(sd) & SYSFS_COPY_NAME)
Tejun Heo0c096b52007-06-14 03:45:15 +0900324 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +0900325 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +0900326 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +0900327 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900328
329 sd = parent_sd;
330 if (sd && atomic_dec_and_test(&sd->s_count))
331 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +0900332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
335{
336 struct sysfs_dirent * sd = dentry->d_fsdata;
337
338 if (sd) {
Tejun Heo5f995322007-06-14 04:27:23 +0900339 /* sd->s_dentry is protected with sysfs_assoc_lock.
340 * This allows sysfs_drop_dentry() to dereference it.
Tejun Heodd14cbc2007-06-11 14:04:01 +0900341 */
Tejun Heo5f995322007-06-14 04:27:23 +0900342 spin_lock(&sysfs_assoc_lock);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900343
344 /* The dentry might have been deleted or another
345 * lookup could have happened updating sd->s_dentry to
346 * point the new dentry. Ignore if it isn't pointing
347 * to this dentry.
348 */
349 if (sd->s_dentry == dentry)
350 sd->s_dentry = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900351 spin_unlock(&sysfs_assoc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 sysfs_put(sd);
353 }
354 iput(inode);
355}
356
357static struct dentry_operations sysfs_dentry_ops = {
358 .d_iput = sysfs_d_iput,
359};
360
Tejun Heo3e519032007-06-14 03:45:15 +0900361struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Tejun Heo0c096b52007-06-14 03:45:15 +0900363 char *dup_name = NULL;
Akinobu Mita01da2422007-07-14 11:03:35 +0900364 struct sysfs_dirent *sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900365
366 if (type & SYSFS_COPY_NAME) {
367 name = dup_name = kstrdup(name, GFP_KERNEL);
368 if (!name)
Akinobu Mita01da2422007-07-14 11:03:35 +0900369 return NULL;
Tejun Heo0c096b52007-06-14 03:45:15 +0900370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800372 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (!sd)
Akinobu Mita01da2422007-07-14 11:03:35 +0900374 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Tejun Heo0c096b52007-06-14 03:45:15 +0900376 if (sysfs_alloc_ino(&sd->s_ino))
Akinobu Mita01da2422007-07-14 11:03:35 +0900377 goto err_out2;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 atomic_set(&sd->s_count, 1);
Tejun Heo8619f972007-06-14 03:45:18 +0900380 atomic_set(&sd->s_active, 0);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300381 atomic_set(&sd->s_event, 1);
Tejun Heoa26cd722007-06-14 03:45:14 +0900382
Tejun Heo0c096b52007-06-14 03:45:15 +0900383 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900384 sd->s_mode = mode;
Tejun Heob402d722007-06-14 04:27:21 +0900385 sd->s_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900388
Akinobu Mita01da2422007-07-14 11:03:35 +0900389 err_out2:
Tejun Heo0c096b52007-06-14 03:45:15 +0900390 kmem_cache_free(sysfs_dir_cachep, sd);
Akinobu Mita01da2422007-07-14 11:03:35 +0900391 err_out1:
392 kfree(dup_name);
Tejun Heo0c096b52007-06-14 03:45:15 +0900393 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Tejun Heo3007e992007-06-14 04:27:23 +0900396/**
397 * sysfs_attach_dentry - associate sysfs_dirent with dentry
398 * @sd: target sysfs_dirent
399 * @dentry: dentry to associate
400 *
401 * Associate @sd with @dentry. This is protected by
402 * sysfs_assoc_lock to avoid race with sysfs_d_iput().
403 *
404 * LOCKING:
405 * mutex_lock(sysfs_mutex)
406 */
Tejun Heo198a2a82007-06-14 03:45:16 +0900407static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
408{
409 dentry->d_op = &sysfs_dentry_ops;
410 dentry->d_fsdata = sysfs_get(sd);
411
412 /* protect sd->s_dentry against sysfs_d_iput */
Tejun Heo5f995322007-06-14 04:27:23 +0900413 spin_lock(&sysfs_assoc_lock);
Tejun Heo198a2a82007-06-14 03:45:16 +0900414 sd->s_dentry = dentry;
Tejun Heo5f995322007-06-14 04:27:23 +0900415 spin_unlock(&sysfs_assoc_lock);
Tejun Heo198a2a82007-06-14 03:45:16 +0900416
417 d_rehash(dentry);
418}
419
Tejun Heofb6896d2007-06-14 04:27:24 +0900420static int sysfs_ilookup_test(struct inode *inode, void *arg)
421{
422 struct sysfs_dirent *sd = arg;
423 return inode->i_ino == sd->s_ino;
424}
425
Tejun Heo3007e992007-06-14 04:27:23 +0900426/**
Tejun Heofb6896d2007-06-14 04:27:24 +0900427 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
428 * @acxt: pointer to sysfs_addrm_cxt to be used
429 * @parent_sd: parent sysfs_dirent
Tejun Heo3007e992007-06-14 04:27:23 +0900430 *
Tejun Heofb6896d2007-06-14 04:27:24 +0900431 * This function is called when the caller is about to add or
432 * remove sysfs_dirent under @parent_sd. This function acquires
433 * sysfs_mutex, grabs inode for @parent_sd if available and lock
434 * i_mutex of it. @acxt is used to keep and pass context to
435 * other addrm functions.
Tejun Heo3007e992007-06-14 04:27:23 +0900436 *
437 * LOCKING:
Tejun Heofb6896d2007-06-14 04:27:24 +0900438 * Kernel thread context (may sleep). sysfs_mutex is locked on
439 * return. i_mutex of parent inode is locked on return if
440 * available.
Tejun Heo3007e992007-06-14 04:27:23 +0900441 */
Tejun Heofb6896d2007-06-14 04:27:24 +0900442void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
443 struct sysfs_dirent *parent_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700444{
Tejun Heofb6896d2007-06-14 04:27:24 +0900445 struct inode *inode;
Tejun Heoa26cd722007-06-14 03:45:14 +0900446
Tejun Heofb6896d2007-06-14 04:27:24 +0900447 memset(acxt, 0, sizeof(*acxt));
448 acxt->parent_sd = parent_sd;
449
450 /* Lookup parent inode. inode initialization and I_NEW
451 * clearing are protected by sysfs_mutex. By grabbing it and
452 * looking up with _nowait variant, inode state can be
453 * determined reliably.
454 */
455 mutex_lock(&sysfs_mutex);
456
457 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
458 parent_sd);
459
460 if (inode && !(inode->i_state & I_NEW)) {
461 /* parent inode available */
462 acxt->parent_inode = inode;
463
464 /* sysfs_mutex is below i_mutex in lock hierarchy.
465 * First, trylock i_mutex. If fails, unlock
466 * sysfs_mutex and lock them in order.
467 */
468 if (!mutex_trylock(&inode->i_mutex)) {
469 mutex_unlock(&sysfs_mutex);
470 mutex_lock(&inode->i_mutex);
471 mutex_lock(&sysfs_mutex);
472 }
473 } else
474 iput(inode);
475}
476
477/**
478 * sysfs_add_one - add sysfs_dirent to parent
479 * @acxt: addrm context to use
480 * @sd: sysfs_dirent to be added
481 *
482 * Get @acxt->parent_sd and set sd->s_parent to it and increment
483 * nlink of parent inode if @sd is a directory. @sd is NOT
484 * linked into the children list of the parent. The caller
485 * should invoke sysfs_link_sibling() after this function
486 * completes if @sd needs to be on the children list.
487 *
488 * This function should be called between calls to
489 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
490 * passed the same @acxt as passed to sysfs_addrm_start().
491 *
492 * LOCKING:
493 * Determined by sysfs_addrm_start().
Tejun Heo23dc2792007-08-02 21:38:03 +0900494 *
495 * RETURNS:
496 * 0 on success, -EEXIST if entry with the given name already
497 * exists.
Tejun Heofb6896d2007-06-14 04:27:24 +0900498 */
Tejun Heo23dc2792007-08-02 21:38:03 +0900499int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
Tejun Heofb6896d2007-06-14 04:27:24 +0900500{
Tejun Heo23dc2792007-08-02 21:38:03 +0900501 if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
502 return -EEXIST;
503
Tejun Heofb6896d2007-06-14 04:27:24 +0900504 sd->s_parent = sysfs_get(acxt->parent_sd);
505
506 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
507 inc_nlink(acxt->parent_inode);
508
509 acxt->cnt++;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900510
511 sysfs_link_sibling(sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900512
513 return 0;
Tejun Heofb6896d2007-06-14 04:27:24 +0900514}
515
516/**
517 * sysfs_remove_one - remove sysfs_dirent from parent
518 * @acxt: addrm context to use
519 * @sd: sysfs_dirent to be added
520 *
521 * Mark @sd removed and drop nlink of parent inode if @sd is a
522 * directory. @sd is NOT unlinked from the children list of the
523 * parent. The caller is repsonsible for removing @sd from the
524 * children list before calling this function.
525 *
526 * This function should be called between calls to
527 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
528 * passed the same @acxt as passed to sysfs_addrm_start().
529 *
530 * LOCKING:
531 * Determined by sysfs_addrm_start().
532 */
533void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
534{
Tejun Heo41fc1c22007-08-02 21:38:03 +0900535 BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
536
537 sysfs_unlink_sibling(sd);
Tejun Heofb6896d2007-06-14 04:27:24 +0900538
539 sd->s_flags |= SYSFS_FLAG_REMOVED;
540 sd->s_sibling = acxt->removed;
541 acxt->removed = sd;
542
543 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
544 drop_nlink(acxt->parent_inode);
545
546 acxt->cnt++;
547}
548
549/**
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900550 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
551 * @sd: target sysfs_dirent
552 *
553 * Drop dentry for @sd. @sd must have been unlinked from its
554 * parent on entry to this function such that it can't be looked
555 * up anymore.
556 *
557 * @sd->s_dentry which is protected with sysfs_assoc_lock points
558 * to the currently associated dentry but we're not holding a
559 * reference to it and racing with dput(). Grab dcache_lock and
560 * verify dentry before dropping it. If @sd->s_dentry is NULL or
561 * dput() beats us, no need to bother.
562 */
563static void sysfs_drop_dentry(struct sysfs_dirent *sd)
564{
565 struct dentry *dentry = NULL;
566 struct inode *inode;
567
568 /* We're not holding a reference to ->s_dentry dentry but the
569 * field will stay valid as long as sysfs_assoc_lock is held.
570 */
571 spin_lock(&sysfs_assoc_lock);
572 spin_lock(&dcache_lock);
573
574 /* drop dentry if it's there and dput() didn't kill it yet */
575 if (sd->s_dentry && sd->s_dentry->d_inode) {
576 dentry = dget_locked(sd->s_dentry);
577 spin_lock(&dentry->d_lock);
578 __d_drop(dentry);
579 spin_unlock(&dentry->d_lock);
580 }
581
582 spin_unlock(&dcache_lock);
583 spin_unlock(&sysfs_assoc_lock);
584
Tejun Heo51225032007-06-14 04:27:25 +0900585 dput(dentry);
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900586
587 /* adjust nlink and update timestamp */
588 inode = ilookup(sysfs_sb, sd->s_ino);
589 if (inode) {
590 mutex_lock(&inode->i_mutex);
591
592 inode->i_ctime = CURRENT_TIME;
593 drop_nlink(inode);
594 if (sysfs_type(sd) == SYSFS_DIR)
595 drop_nlink(inode);
596
597 mutex_unlock(&inode->i_mutex);
598 iput(inode);
599 }
600}
601
602/**
Tejun Heofb6896d2007-06-14 04:27:24 +0900603 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
604 * @acxt: addrm context to finish up
605 *
606 * Finish up sysfs_dirent add/remove. Resources acquired by
607 * sysfs_addrm_start() are released and removed sysfs_dirents are
608 * cleaned up. Timestamps on the parent inode are updated.
609 *
610 * LOCKING:
611 * All mutexes acquired by sysfs_addrm_start() are released.
Tejun Heofb6896d2007-06-14 04:27:24 +0900612 */
Tejun Heo990e53f2007-08-02 21:38:03 +0900613void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
Tejun Heofb6896d2007-06-14 04:27:24 +0900614{
615 /* release resources acquired by sysfs_addrm_start() */
616 mutex_unlock(&sysfs_mutex);
617 if (acxt->parent_inode) {
618 struct inode *inode = acxt->parent_inode;
619
620 /* if added/removed, update timestamps on the parent */
621 if (acxt->cnt)
622 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
623
624 mutex_unlock(&inode->i_mutex);
625 iput(inode);
Tejun Heo13b30862007-06-14 03:45:14 +0900626 }
Tejun Heofb6896d2007-06-14 04:27:24 +0900627
628 /* kill removed sysfs_dirents */
629 while (acxt->removed) {
630 struct sysfs_dirent *sd = acxt->removed;
631
632 acxt->removed = sd->s_sibling;
633 sd->s_sibling = NULL;
634
635 sysfs_drop_dentry(sd);
636 sysfs_deactivate(sd);
637 sysfs_put(sd);
638 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700639}
640
Tejun Heof0b0af42007-06-14 04:27:22 +0900641/**
642 * sysfs_find_dirent - find sysfs_dirent with the given name
643 * @parent_sd: sysfs_dirent to search under
644 * @name: name to look for
Maneesh Sonic5168652006-03-09 19:40:14 +0530645 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900646 * Look for sysfs_dirent with name @name under @parent_sd.
Maneesh Sonic5168652006-03-09 19:40:14 +0530647 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900648 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900649 * mutex_lock(sysfs_mutex)
Tejun Heof0b0af42007-06-14 04:27:22 +0900650 *
651 * RETURNS:
652 * Pointer to sysfs_dirent if found, NULL if not.
Maneesh Sonic5168652006-03-09 19:40:14 +0530653 */
Tejun Heof0b0af42007-06-14 04:27:22 +0900654struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
655 const unsigned char *name)
Maneesh Sonic5168652006-03-09 19:40:14 +0530656{
Tejun Heof0b0af42007-06-14 04:27:22 +0900657 struct sysfs_dirent *sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530658
Tejun Heof0b0af42007-06-14 04:27:22 +0900659 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
660 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
661 return sd;
662 return NULL;
663}
Maneesh Sonic5168652006-03-09 19:40:14 +0530664
Tejun Heof0b0af42007-06-14 04:27:22 +0900665/**
666 * sysfs_get_dirent - find and get sysfs_dirent with the given name
667 * @parent_sd: sysfs_dirent to search under
668 * @name: name to look for
669 *
670 * Look for sysfs_dirent with name @name under @parent_sd and get
671 * it if found.
672 *
673 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900674 * Kernel thread context (may sleep). Grabs sysfs_mutex.
Tejun Heof0b0af42007-06-14 04:27:22 +0900675 *
676 * RETURNS:
677 * Pointer to sysfs_dirent if found, NULL if not.
678 */
679struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
680 const unsigned char *name)
681{
682 struct sysfs_dirent *sd;
683
Tejun Heo3007e992007-06-14 04:27:23 +0900684 mutex_lock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900685 sd = sysfs_find_dirent(parent_sd, name);
686 sysfs_get(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900687 mutex_unlock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900688
689 return sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530690}
691
Tejun Heo608e2662007-06-14 04:27:22 +0900692static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
693 const char *name, struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heo51225032007-06-14 04:27:25 +0900696 struct sysfs_addrm_cxt acxt;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900697 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900698 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Tejun Heofc9f54b2007-06-14 03:45:17 +0900700 /* allocate */
Tejun Heo3e519032007-06-14 03:45:15 +0900701 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900702 if (!sd)
Tejun Heo51225032007-06-14 04:27:25 +0900703 return -ENOMEM;
Tejun Heo3e519032007-06-14 03:45:15 +0900704 sd->s_elem.dir.kobj = kobj;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900705
Tejun Heo51225032007-06-14 04:27:25 +0900706 /* link in */
707 sysfs_addrm_start(&acxt, parent_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900708 rc = sysfs_add_one(&acxt, sd);
709 sysfs_addrm_finish(&acxt);
Tejun Heo967e35d2007-07-18 16:38:11 +0900710
Tejun Heo23dc2792007-08-02 21:38:03 +0900711 if (rc == 0)
712 *p_sd = sd;
713 else
Tejun Heo967e35d2007-07-18 16:38:11 +0900714 sysfs_put(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900715
Tejun Heo23dc2792007-08-02 21:38:03 +0900716 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Tejun Heo608e2662007-06-14 04:27:22 +0900719int sysfs_create_subdir(struct kobject *kobj, const char *name,
720 struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Tejun Heo608e2662007-06-14 04:27:22 +0900722 return create_dir(kobj, kobj->sd, name, p_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725/**
726 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * @kobj: object we're creating directory for.
728 */
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900729int sysfs_create_dir(struct kobject * kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Tejun Heo608e2662007-06-14 04:27:22 +0900731 struct sysfs_dirent *parent_sd, *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int error = 0;
733
734 BUG_ON(!kobj);
735
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900736 if (kobj->parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900737 parent_sd = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 else if (sysfs_mount && sysfs_mount->mnt_sb)
Tejun Heo608e2662007-06-14 04:27:22 +0900739 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 else
741 return -EFAULT;
742
Tejun Heo608e2662007-06-14 04:27:22 +0900743 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!error)
Tejun Heo608e2662007-06-14 04:27:22 +0900745 kobj->sd = sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return error;
747}
748
Tejun Heo51225032007-06-14 04:27:25 +0900749static int sysfs_count_nlink(struct sysfs_dirent *sd)
750{
751 struct sysfs_dirent *child;
752 int nr = 0;
753
754 for (child = sd->s_children; child; child = child->s_sibling)
755 if (sysfs_type(child) == SYSFS_DIR)
756 nr++;
757 return nr + 2;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
761 struct nameidata *nd)
762{
Tejun Heo6cb52142007-07-31 19:15:08 +0900763 struct dentry *ret = NULL;
Tejun Heoa7a04752007-08-02 21:38:02 +0900764 struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
765 struct sysfs_dirent *sd;
Tejun Heob402d722007-06-14 04:27:21 +0900766 struct bin_attribute *bin_attr;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900767 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Tejun Heo6cb52142007-07-31 19:15:08 +0900769 mutex_lock(&sysfs_mutex);
770
Tejun Heoa7a04752007-08-02 21:38:02 +0900771 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
772 if (sysfs_type(sd) && !strcmp(sd->s_name, dentry->d_name.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Tejun Heofc9f54b2007-06-14 03:45:17 +0900775 /* no such entry */
Tejun Heoa7a04752007-08-02 21:38:02 +0900776 if (!sd)
Tejun Heo6cb52142007-07-31 19:15:08 +0900777 goto out_unlock;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900778
779 /* attach dentry and inode */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900780 inode = sysfs_get_inode(sd);
Tejun Heo6cb52142007-07-31 19:15:08 +0900781 if (!inode) {
782 ret = ERR_PTR(-ENOMEM);
783 goto out_unlock;
784 }
Tejun Heo3007e992007-06-14 04:27:23 +0900785
Tejun Heo8312a8d2007-06-14 03:45:17 +0900786 if (inode->i_state & I_NEW) {
787 /* initialize inode according to type */
Tejun Heob402d722007-06-14 04:27:21 +0900788 switch (sysfs_type(sd)) {
Tejun Heo51225032007-06-14 04:27:25 +0900789 case SYSFS_DIR:
790 inode->i_op = &sysfs_dir_inode_operations;
791 inode->i_fop = &sysfs_dir_operations;
792 inode->i_nlink = sysfs_count_nlink(sd);
793 break;
Tejun Heob402d722007-06-14 04:27:21 +0900794 case SYSFS_KOBJ_ATTR:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900795 inode->i_size = PAGE_SIZE;
796 inode->i_fop = &sysfs_file_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900797 break;
798 case SYSFS_KOBJ_BIN_ATTR:
799 bin_attr = sd->s_elem.bin_attr.bin_attr;
Tejun Heo8312a8d2007-06-14 03:45:17 +0900800 inode->i_size = bin_attr->size;
801 inode->i_fop = &bin_fops;
Tejun Heob402d722007-06-14 04:27:21 +0900802 break;
803 case SYSFS_KOBJ_LINK:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900804 inode->i_op = &sysfs_symlink_inode_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900805 break;
806 default:
807 BUG();
808 }
Tejun Heo8312a8d2007-06-14 03:45:17 +0900809 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900810
811 sysfs_instantiate(dentry, inode);
812 sysfs_attach_dentry(sd, dentry);
813
Tejun Heo6cb52142007-07-31 19:15:08 +0900814 out_unlock:
Tejun Heo3007e992007-06-14 04:27:23 +0900815 mutex_unlock(&sysfs_mutex);
Tejun Heo6cb52142007-07-31 19:15:08 +0900816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800819const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530821 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822};
823
Tejun Heo608e2662007-06-14 04:27:22 +0900824static void remove_dir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Tejun Heofb6896d2007-06-14 04:27:24 +0900826 struct sysfs_addrm_cxt acxt;
827
828 sysfs_addrm_start(&acxt, sd->s_parent);
Tejun Heofb6896d2007-06-14 04:27:24 +0900829 sysfs_remove_one(&acxt, sd);
830 sysfs_addrm_finish(&acxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Tejun Heo608e2662007-06-14 04:27:22 +0900833void sysfs_remove_subdir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Tejun Heo608e2662007-06-14 04:27:22 +0900835 remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838
Tejun Heo608e2662007-06-14 04:27:22 +0900839static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Tejun Heofb6896d2007-06-14 04:27:24 +0900841 struct sysfs_addrm_cxt acxt;
Tejun Heo0c73f182007-06-14 03:45:18 +0900842 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Tejun Heo608e2662007-06-14 04:27:22 +0900844 if (!dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return;
846
Tejun Heo608e2662007-06-14 04:27:22 +0900847 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
Tejun Heofb6896d2007-06-14 04:27:24 +0900848 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900849 pos = &dir_sd->s_children;
Tejun Heo0c73f182007-06-14 03:45:18 +0900850 while (*pos) {
851 struct sysfs_dirent *sd = *pos;
852
Tejun Heo41fc1c22007-08-02 21:38:03 +0900853 if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR)
Tejun Heofb6896d2007-06-14 04:27:24 +0900854 sysfs_remove_one(&acxt, sd);
Tejun Heo41fc1c22007-08-02 21:38:03 +0900855 else
Tejun Heo0c73f182007-06-14 03:45:18 +0900856 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Tejun Heofb6896d2007-06-14 04:27:24 +0900858 sysfs_addrm_finish(&acxt);
Tejun Heo0ab66082007-06-14 03:45:16 +0900859
Tejun Heo608e2662007-06-14 04:27:22 +0900860 remove_dir(dir_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700861}
862
863/**
864 * sysfs_remove_dir - remove an object's directory.
865 * @kobj: object.
866 *
867 * The only thing special about this is that we remove any files in
868 * the directory before we remove the directory, and we've inlined
869 * what used to be sysfs_rmdir() below, instead of calling separately.
870 */
871
872void sysfs_remove_dir(struct kobject * kobj)
873{
Tejun Heo608e2662007-06-14 04:27:22 +0900874 struct sysfs_dirent *sd = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +0900875
Tejun Heo5f995322007-06-14 04:27:23 +0900876 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +0900877 kobj->sd = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900878 spin_unlock(&sysfs_assoc_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +0900879
Tejun Heo608e2662007-06-14 04:27:22 +0900880 __sysfs_remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900883int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900885 struct sysfs_dirent *sd;
886 struct dentry *parent = NULL;
Tejun Heo51225032007-06-14 04:27:25 +0900887 struct dentry *old_dentry = NULL, *new_dentry = NULL;
888 const char *dup_name = NULL;
Tejun Heo996b7372007-06-14 03:45:14 +0900889 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Tejun Heoff869de2007-08-02 21:38:02 +0900891 /* get the original dentry */
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900892 sd = kobj->sd;
Tejun Heo51225032007-06-14 04:27:25 +0900893 old_dentry = sysfs_get_dentry(sd);
894 if (IS_ERR(old_dentry)) {
895 error = PTR_ERR(old_dentry);
896 goto out_dput;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Tejun Heoff869de2007-08-02 21:38:02 +0900899 parent = old_dentry->d_parent;
Tejun Heo51225032007-06-14 04:27:25 +0900900
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900901 /* lock parent and get dentry for new name */
902 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900904 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900905 if (IS_ERR(new_dentry)) {
906 error = PTR_ERR(new_dentry);
907 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Tejun Heo996b7372007-06-14 03:45:14 +0900909
Tejun Heo996b7372007-06-14 03:45:14 +0900910 error = -EINVAL;
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900911 if (old_dentry == new_dentry)
Tejun Heo51225032007-06-14 04:27:25 +0900912 goto out_unlock;
Tejun Heo996b7372007-06-14 03:45:14 +0900913
914 error = -EEXIST;
915 if (new_dentry->d_inode)
Tejun Heo51225032007-06-14 04:27:25 +0900916 goto out_unlock;
Tejun Heo996b7372007-06-14 03:45:14 +0900917
Tejun Heo0c096b52007-06-14 03:45:15 +0900918 /* rename kobject and sysfs_dirent */
919 error = -ENOMEM;
920 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
921 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900922 goto out_drop;
923
Tejun Heo0c096b52007-06-14 03:45:15 +0900924 error = kobject_set_name(kobj, "%s", new_name);
925 if (error)
Tejun Heo51225032007-06-14 04:27:25 +0900926 goto out_drop;
Tejun Heo0c096b52007-06-14 03:45:15 +0900927
Tejun Heo6cb52142007-07-31 19:15:08 +0900928 mutex_lock(&sysfs_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900929 dup_name = sd->s_name;
Tejun Heo0c096b52007-06-14 03:45:15 +0900930 sd->s_name = new_name;
Tejun Heoff869de2007-08-02 21:38:02 +0900931 mutex_unlock(&sysfs_mutex);
Tejun Heo0c096b52007-06-14 03:45:15 +0900932
Tejun Heoff869de2007-08-02 21:38:02 +0900933 /* rename */
Tejun Heo996b7372007-06-14 03:45:14 +0900934 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900935 d_move(sd->s_dentry, new_dentry);
Tejun Heo996b7372007-06-14 03:45:14 +0900936
Tejun Heo996b7372007-06-14 03:45:14 +0900937 error = 0;
938 goto out_unlock;
939
940 out_drop:
941 d_drop(new_dentry);
Tejun Heo996b7372007-06-14 03:45:14 +0900942 out_unlock:
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900943 mutex_unlock(&parent->d_inode->i_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900944 out_dput:
945 kfree(dup_name);
Tejun Heo51225032007-06-14 04:27:25 +0900946 dput(old_dentry);
947 dput(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return error;
949}
950
Tejun Heo51225032007-06-14 04:27:25 +0900951int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
Cornelia Huck8a824722006-11-20 17:07:51 +0100952{
Tejun Heo51225032007-06-14 04:27:25 +0900953 struct sysfs_dirent *sd = kobj->sd;
954 struct sysfs_dirent *new_parent_sd;
955 struct dentry *old_parent, *new_parent = NULL;
956 struct dentry *old_dentry = NULL, *new_dentry = NULL;
Cornelia Huck8a824722006-11-20 17:07:51 +0100957 int error;
958
Tejun Heo51225032007-06-14 04:27:25 +0900959 BUG_ON(!sd->s_parent);
960 new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100961
Tejun Heo51225032007-06-14 04:27:25 +0900962 /* get dentries */
963 old_dentry = sysfs_get_dentry(sd);
964 if (IS_ERR(old_dentry)) {
965 error = PTR_ERR(old_dentry);
966 goto out_dput;
967 }
968 old_parent = sd->s_parent->s_dentry;
969
970 new_parent = sysfs_get_dentry(new_parent_sd);
971 if (IS_ERR(new_parent)) {
972 error = PTR_ERR(new_parent);
973 goto out_dput;
974 }
975
976 if (old_parent->d_inode == new_parent->d_inode) {
977 error = 0;
978 goto out_dput; /* nothing to move */
979 }
Cornelia Huck8a824722006-11-20 17:07:51 +0100980again:
Tejun Heo51225032007-06-14 04:27:25 +0900981 mutex_lock(&old_parent->d_inode->i_mutex);
982 if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
983 mutex_unlock(&old_parent->d_inode->i_mutex);
Cornelia Huck8a824722006-11-20 17:07:51 +0100984 goto again;
985 }
986
Greg Kroah-Hartman19c38de2007-09-12 15:06:57 -0700987 new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj)));
Cornelia Huck8a824722006-11-20 17:07:51 +0100988 if (IS_ERR(new_dentry)) {
989 error = PTR_ERR(new_dentry);
Tejun Heo51225032007-06-14 04:27:25 +0900990 goto out_unlock;
Cornelia Huck8a824722006-11-20 17:07:51 +0100991 } else
992 error = 0;
993 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900994 d_move(sd->s_dentry, new_dentry);
Cornelia Huck8a824722006-11-20 17:07:51 +0100995 dput(new_dentry);
996
997 /* Remove from old parent's list and insert into new parent's list. */
Tejun Heo3007e992007-06-14 04:27:23 +0900998 mutex_lock(&sysfs_mutex);
999
Tejun Heo0c73f182007-06-14 03:45:18 +09001000 sysfs_unlink_sibling(sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +09001001 sysfs_get(new_parent_sd);
1002 sysfs_put(sd->s_parent);
1003 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +09001004 sysfs_link_sibling(sd);
Cornelia Huck8a824722006-11-20 17:07:51 +01001005
Tejun Heo3007e992007-06-14 04:27:23 +09001006 mutex_unlock(&sysfs_mutex);
Cornelia Huck8a824722006-11-20 17:07:51 +01001007
Tejun Heo51225032007-06-14 04:27:25 +09001008 out_unlock:
1009 mutex_unlock(&new_parent->d_inode->i_mutex);
1010 mutex_unlock(&old_parent->d_inode->i_mutex);
1011 out_dput:
1012 dput(new_parent);
1013 dput(old_dentry);
1014 dput(new_dentry);
Cornelia Huck8a824722006-11-20 17:07:51 +01001015 return error;
1016}
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018static int sysfs_dir_open(struct inode *inode, struct file *file)
1019{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -08001020 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +09001022 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Tejun Heo3e519032007-06-14 03:45:15 +09001024 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heo3007e992007-06-14 04:27:23 +09001025 if (sd) {
1026 mutex_lock(&sysfs_mutex);
Tejun Heofb6896d2007-06-14 04:27:24 +09001027 sd->s_parent = sysfs_get(parent_sd);
1028 sysfs_link_sibling(sd);
Tejun Heo3007e992007-06-14 04:27:23 +09001029 mutex_unlock(&sysfs_mutex);
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Tejun Heoa26cd722007-06-14 03:45:14 +09001032 file->private_data = sd;
1033 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036static int sysfs_dir_close(struct inode *inode, struct file *file)
1037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct sysfs_dirent * cursor = file->private_data;
1039
Tejun Heo3007e992007-06-14 04:27:23 +09001040 mutex_lock(&sysfs_mutex);
Tejun Heo0c73f182007-06-14 03:45:18 +09001041 sysfs_unlink_sibling(cursor);
Tejun Heo3007e992007-06-14 04:27:23 +09001042 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 release_sysfs_dirent(cursor);
1045
1046 return 0;
1047}
1048
1049/* Relationship between s_mode and the DT_xxx types */
1050static inline unsigned char dt_type(struct sysfs_dirent *sd)
1051{
1052 return (sd->s_mode >> 12) & 15;
1053}
1054
1055static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1056{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -08001057 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
1059 struct sysfs_dirent *cursor = filp->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +09001060 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 ino_t ino;
1062 int i = filp->f_pos;
1063
1064 switch (i) {
1065 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +09001066 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1068 break;
1069 filp->f_pos++;
1070 i++;
1071 /* fallthrough */
1072 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +09001073 if (parent_sd->s_parent)
1074 ino = parent_sd->s_parent->s_ino;
1075 else
1076 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1078 break;
1079 filp->f_pos++;
1080 i++;
1081 /* fallthrough */
1082 default:
Tejun Heo3007e992007-06-14 04:27:23 +09001083 mutex_lock(&sysfs_mutex);
1084
Tejun Heo0c73f182007-06-14 03:45:18 +09001085 pos = &parent_sd->s_children;
1086 while (*pos != cursor)
1087 pos = &(*pos)->s_sibling;
Akinobu Mita1bfba4e2006-06-26 00:24:40 -07001088
Tejun Heo0c73f182007-06-14 03:45:18 +09001089 /* unlink cursor */
1090 *pos = cursor->s_sibling;
1091
1092 if (filp->f_pos == 2)
1093 pos = &parent_sd->s_children;
1094
1095 for ( ; *pos; pos = &(*pos)->s_sibling) {
1096 struct sysfs_dirent *next = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 const char * name;
1098 int len;
1099
Tejun Heob402d722007-06-14 04:27:21 +09001100 if (!sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 continue;
1102
Tejun Heo0c096b52007-06-14 03:45:15 +09001103 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +09001105 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (filldir(dirent, name, len, filp->f_pos, ino,
1108 dt_type(next)) < 0)
Tejun Heo0c73f182007-06-14 03:45:18 +09001109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 filp->f_pos++;
1112 }
Tejun Heo0c73f182007-06-14 03:45:18 +09001113
1114 /* put cursor back in */
1115 cursor->s_sibling = *pos;
1116 *pos = cursor;
Tejun Heo3007e992007-06-14 04:27:23 +09001117
1118 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 return 0;
1121}
1122
1123static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
1124{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -08001125 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 switch (origin) {
1128 case 1:
1129 offset += file->f_pos;
1130 case 0:
1131 if (offset >= 0)
1132 break;
1133 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return -EINVAL;
1135 }
1136 if (offset != file->f_pos) {
Tejun Heo3007e992007-06-14 04:27:23 +09001137 mutex_lock(&sysfs_mutex);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 file->f_pos = offset;
1140 if (file->f_pos >= 2) {
1141 struct sysfs_dirent *sd = dentry->d_fsdata;
1142 struct sysfs_dirent *cursor = file->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +09001143 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 loff_t n = file->f_pos - 2;
1145
Tejun Heo0c73f182007-06-14 03:45:18 +09001146 sysfs_unlink_sibling(cursor);
1147
1148 pos = &sd->s_children;
1149 while (n && *pos) {
1150 struct sysfs_dirent *next = *pos;
Tejun Heob402d722007-06-14 04:27:21 +09001151 if (sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 n--;
Tejun Heo0c73f182007-06-14 03:45:18 +09001153 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Tejun Heo0c73f182007-06-14 03:45:18 +09001155
1156 cursor->s_sibling = *pos;
1157 *pos = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Tejun Heo3007e992007-06-14 04:27:23 +09001159
1160 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Tejun Heo3007e992007-06-14 04:27:23 +09001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return offset;
1164}
1165
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001166const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 .open = sysfs_dir_open,
1168 .release = sysfs_dir_close,
1169 .llseek = sysfs_dir_lseek,
1170 .read = generic_read_dir,
1171 .readdir = sysfs_readdir,
1172};