blob: 7f4abe17670116bcf62b8e9dcba4f0c9dd37c82c [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;
Tejun Heo25328022007-08-20 21:36:29 +0900133 mutex_lock(&parent_dentry->d_inode->i_mutex);
Tejun Heo53e0ae92007-06-14 04:27:25 +0900134 dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
135 strlen(cur->s_name));
Tejun Heo25328022007-08-20 21:36:29 +0900136 mutex_unlock(&parent_dentry->d_inode->i_mutex);
Tejun Heo53e0ae92007-06-14 04:27:25 +0900137 dput(parent_dentry);
138
139 if (IS_ERR(dentry)) {
140 sysfs_put(cur);
141 return dentry;
142 }
143
144 mutex_lock(&sysfs_mutex);
145 spin_lock(&sysfs_assoc_lock);
146
147 /* This, again, can happen if tree structure has
148 * changed and we looked up the wrong thing. Restart.
149 */
150 if (cur->s_dentry != dentry) {
151 dput(dentry);
152 sysfs_put(cur);
153 goto restart1;
154 }
155
156 spin_unlock(&sysfs_assoc_lock);
157
158 sysfs_put(cur);
159 }
160
161 mutex_unlock(&sysfs_mutex);
162 return dentry;
163}
164
165/**
Tejun Heob6b4a432007-06-14 03:45:18 +0900166 * sysfs_get_active - get an active reference to sysfs_dirent
167 * @sd: sysfs_dirent to get an active reference to
168 *
169 * Get an active reference of @sd. This function is noop if @sd
170 * is NULL.
171 *
172 * RETURNS:
173 * Pointer to @sd on success, NULL on failure.
174 */
175struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
176{
Tejun Heo8619f972007-06-14 03:45:18 +0900177 if (unlikely(!sd))
178 return NULL;
179
180 while (1) {
181 int v, t;
182
183 v = atomic_read(&sd->s_active);
184 if (unlikely(v < 0))
185 return NULL;
186
187 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
188 if (likely(t == v))
189 return sd;
190 if (t < 0)
191 return NULL;
192
193 cpu_relax();
Tejun Heob6b4a432007-06-14 03:45:18 +0900194 }
Tejun Heob6b4a432007-06-14 03:45:18 +0900195}
196
197/**
198 * sysfs_put_active - put an active reference to sysfs_dirent
199 * @sd: sysfs_dirent to put an active reference to
200 *
201 * Put an active reference to @sd. This function is noop if @sd
202 * is NULL.
203 */
204void sysfs_put_active(struct sysfs_dirent *sd)
205{
Tejun Heo8619f972007-06-14 03:45:18 +0900206 struct completion *cmpl;
207 int v;
208
209 if (unlikely(!sd))
210 return;
211
212 v = atomic_dec_return(&sd->s_active);
213 if (likely(v != SD_DEACTIVATED_BIAS))
214 return;
215
216 /* atomic_dec_return() is a mb(), we'll always see the updated
Tejun Heo0c73f182007-06-14 03:45:18 +0900217 * sd->s_sibling.
Tejun Heo8619f972007-06-14 03:45:18 +0900218 */
Tejun Heo0c73f182007-06-14 03:45:18 +0900219 cmpl = (void *)sd->s_sibling;
Tejun Heo8619f972007-06-14 03:45:18 +0900220 complete(cmpl);
Tejun Heob6b4a432007-06-14 03:45:18 +0900221}
222
223/**
224 * sysfs_get_active_two - get active references to sysfs_dirent and parent
225 * @sd: sysfs_dirent of interest
226 *
227 * Get active reference to @sd and its parent. Parent's active
228 * reference is grabbed first. This function is noop if @sd is
229 * NULL.
230 *
231 * RETURNS:
232 * Pointer to @sd on success, NULL on failure.
233 */
234struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
235{
236 if (sd) {
237 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
238 return NULL;
239 if (unlikely(!sysfs_get_active(sd))) {
240 sysfs_put_active(sd->s_parent);
241 return NULL;
242 }
243 }
244 return sd;
245}
246
247/**
248 * sysfs_put_active_two - put active references to sysfs_dirent and parent
249 * @sd: sysfs_dirent of interest
250 *
251 * Put active references to @sd and its parent. This function is
252 * noop if @sd is NULL.
253 */
254void sysfs_put_active_two(struct sysfs_dirent *sd)
255{
256 if (sd) {
257 sysfs_put_active(sd);
258 sysfs_put_active(sd->s_parent);
259 }
260}
261
262/**
263 * sysfs_deactivate - deactivate sysfs_dirent
264 * @sd: sysfs_dirent to deactivate
265 *
Tejun Heo8619f972007-06-14 03:45:18 +0900266 * Deny new active references and drain existing ones.
Tejun Heob6b4a432007-06-14 03:45:18 +0900267 */
Tejun Heofb6896d2007-06-14 04:27:24 +0900268static void sysfs_deactivate(struct sysfs_dirent *sd)
Tejun Heob6b4a432007-06-14 03:45:18 +0900269{
Tejun Heo8619f972007-06-14 03:45:18 +0900270 DECLARE_COMPLETION_ONSTACK(wait);
271 int v;
Tejun Heob6b4a432007-06-14 03:45:18 +0900272
Tejun Heo380e6fb2007-06-14 04:27:22 +0900273 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
Tejun Heo0c73f182007-06-14 03:45:18 +0900274 sd->s_sibling = (void *)&wait;
Tejun Heo8619f972007-06-14 03:45:18 +0900275
276 /* atomic_add_return() is a mb(), put_active() will always see
Tejun Heo0c73f182007-06-14 03:45:18 +0900277 * the updated sd->s_sibling.
Tejun Heob6b4a432007-06-14 03:45:18 +0900278 */
Tejun Heo8619f972007-06-14 03:45:18 +0900279 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
280
281 if (v != SD_DEACTIVATED_BIAS)
282 wait_for_completion(&wait);
283
Tejun Heo0c73f182007-06-14 03:45:18 +0900284 sd->s_sibling = NULL;
Tejun Heob6b4a432007-06-14 03:45:18 +0900285}
286
Tejun Heo42b37df2007-06-14 03:45:17 +0900287static int sysfs_alloc_ino(ino_t *pino)
Tejun Heo2b611bb2007-06-14 03:45:13 +0900288{
289 int ino, rc;
290
291 retry:
292 spin_lock(&sysfs_ino_lock);
293 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
294 spin_unlock(&sysfs_ino_lock);
295
296 if (rc == -EAGAIN) {
297 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
298 goto retry;
299 rc = -ENOMEM;
300 }
301
302 *pino = ino;
303 return rc;
304}
305
306static void sysfs_free_ino(ino_t ino)
307{
308 spin_lock(&sysfs_ino_lock);
309 ida_remove(&sysfs_ino_ida, ino);
310 spin_unlock(&sysfs_ino_lock);
311}
312
Tejun Heofa7f9122007-06-14 03:45:13 +0900313void release_sysfs_dirent(struct sysfs_dirent * sd)
314{
Tejun Heo13b30862007-06-14 03:45:14 +0900315 struct sysfs_dirent *parent_sd;
316
317 repeat:
Tejun Heo3007e992007-06-14 04:27:23 +0900318 /* Moving/renaming is always done while holding reference.
319 * sd->s_parent won't change beneath us.
320 */
Tejun Heo13b30862007-06-14 03:45:14 +0900321 parent_sd = sd->s_parent;
322
Tejun Heob402d722007-06-14 04:27:21 +0900323 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
Tejun Heo2b29ac22007-06-14 03:45:15 +0900324 sysfs_put(sd->s_elem.symlink.target_sd);
Tejun Heob402d722007-06-14 04:27:21 +0900325 if (sysfs_type(sd) & SYSFS_COPY_NAME)
Tejun Heo0c096b52007-06-14 03:45:15 +0900326 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +0900327 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +0900328 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +0900329 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900330
331 sd = parent_sd;
332 if (sd && atomic_dec_and_test(&sd->s_count))
333 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +0900334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
337{
338 struct sysfs_dirent * sd = dentry->d_fsdata;
339
340 if (sd) {
Tejun Heo5f995322007-06-14 04:27:23 +0900341 /* sd->s_dentry is protected with sysfs_assoc_lock.
342 * This allows sysfs_drop_dentry() to dereference it.
Tejun Heodd14cbc2007-06-11 14:04:01 +0900343 */
Tejun Heo5f995322007-06-14 04:27:23 +0900344 spin_lock(&sysfs_assoc_lock);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900345
346 /* The dentry might have been deleted or another
347 * lookup could have happened updating sd->s_dentry to
348 * point the new dentry. Ignore if it isn't pointing
349 * to this dentry.
350 */
351 if (sd->s_dentry == dentry)
352 sd->s_dentry = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900353 spin_unlock(&sysfs_assoc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 sysfs_put(sd);
355 }
356 iput(inode);
357}
358
359static struct dentry_operations sysfs_dentry_ops = {
360 .d_iput = sysfs_d_iput,
361};
362
Tejun Heo3e519032007-06-14 03:45:15 +0900363struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Tejun Heo0c096b52007-06-14 03:45:15 +0900365 char *dup_name = NULL;
Akinobu Mita01da2422007-07-14 11:03:35 +0900366 struct sysfs_dirent *sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900367
368 if (type & SYSFS_COPY_NAME) {
369 name = dup_name = kstrdup(name, GFP_KERNEL);
370 if (!name)
Akinobu Mita01da2422007-07-14 11:03:35 +0900371 return NULL;
Tejun Heo0c096b52007-06-14 03:45:15 +0900372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800374 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (!sd)
Akinobu Mita01da2422007-07-14 11:03:35 +0900376 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Tejun Heo0c096b52007-06-14 03:45:15 +0900378 if (sysfs_alloc_ino(&sd->s_ino))
Akinobu Mita01da2422007-07-14 11:03:35 +0900379 goto err_out2;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 atomic_set(&sd->s_count, 1);
Tejun Heo8619f972007-06-14 03:45:18 +0900382 atomic_set(&sd->s_active, 0);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300383 atomic_set(&sd->s_event, 1);
Tejun Heoa26cd722007-06-14 03:45:14 +0900384
Tejun Heo0c096b52007-06-14 03:45:15 +0900385 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900386 sd->s_mode = mode;
Tejun Heob402d722007-06-14 04:27:21 +0900387 sd->s_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900390
Akinobu Mita01da2422007-07-14 11:03:35 +0900391 err_out2:
Tejun Heo0c096b52007-06-14 03:45:15 +0900392 kmem_cache_free(sysfs_dir_cachep, sd);
Akinobu Mita01da2422007-07-14 11:03:35 +0900393 err_out1:
394 kfree(dup_name);
Tejun Heo0c096b52007-06-14 03:45:15 +0900395 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Tejun Heo3007e992007-06-14 04:27:23 +0900398/**
399 * sysfs_attach_dentry - associate sysfs_dirent with dentry
400 * @sd: target sysfs_dirent
401 * @dentry: dentry to associate
402 *
403 * Associate @sd with @dentry. This is protected by
404 * sysfs_assoc_lock to avoid race with sysfs_d_iput().
405 *
406 * LOCKING:
407 * mutex_lock(sysfs_mutex)
408 */
Tejun Heo198a2a82007-06-14 03:45:16 +0900409static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
410{
411 dentry->d_op = &sysfs_dentry_ops;
412 dentry->d_fsdata = sysfs_get(sd);
413
414 /* protect sd->s_dentry against sysfs_d_iput */
Tejun Heo5f995322007-06-14 04:27:23 +0900415 spin_lock(&sysfs_assoc_lock);
Tejun Heo198a2a82007-06-14 03:45:16 +0900416 sd->s_dentry = dentry;
Tejun Heo5f995322007-06-14 04:27:23 +0900417 spin_unlock(&sysfs_assoc_lock);
Tejun Heo198a2a82007-06-14 03:45:16 +0900418
419 d_rehash(dentry);
420}
421
Tejun Heofb6896d2007-06-14 04:27:24 +0900422static int sysfs_ilookup_test(struct inode *inode, void *arg)
423{
424 struct sysfs_dirent *sd = arg;
425 return inode->i_ino == sd->s_ino;
426}
427
Tejun Heo3007e992007-06-14 04:27:23 +0900428/**
Tejun Heofb6896d2007-06-14 04:27:24 +0900429 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
430 * @acxt: pointer to sysfs_addrm_cxt to be used
431 * @parent_sd: parent sysfs_dirent
Tejun Heo3007e992007-06-14 04:27:23 +0900432 *
Tejun Heofb6896d2007-06-14 04:27:24 +0900433 * This function is called when the caller is about to add or
434 * remove sysfs_dirent under @parent_sd. This function acquires
435 * sysfs_mutex, grabs inode for @parent_sd if available and lock
436 * i_mutex of it. @acxt is used to keep and pass context to
437 * other addrm functions.
Tejun Heo3007e992007-06-14 04:27:23 +0900438 *
439 * LOCKING:
Tejun Heofb6896d2007-06-14 04:27:24 +0900440 * Kernel thread context (may sleep). sysfs_mutex is locked on
441 * return. i_mutex of parent inode is locked on return if
442 * available.
Tejun Heo3007e992007-06-14 04:27:23 +0900443 */
Tejun Heofb6896d2007-06-14 04:27:24 +0900444void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
445 struct sysfs_dirent *parent_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700446{
Tejun Heofb6896d2007-06-14 04:27:24 +0900447 struct inode *inode;
Tejun Heoa26cd722007-06-14 03:45:14 +0900448
Tejun Heofb6896d2007-06-14 04:27:24 +0900449 memset(acxt, 0, sizeof(*acxt));
450 acxt->parent_sd = parent_sd;
451
452 /* Lookup parent inode. inode initialization and I_NEW
453 * clearing are protected by sysfs_mutex. By grabbing it and
454 * looking up with _nowait variant, inode state can be
455 * determined reliably.
456 */
457 mutex_lock(&sysfs_mutex);
458
459 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
460 parent_sd);
461
462 if (inode && !(inode->i_state & I_NEW)) {
463 /* parent inode available */
464 acxt->parent_inode = inode;
465
466 /* sysfs_mutex is below i_mutex in lock hierarchy.
467 * First, trylock i_mutex. If fails, unlock
468 * sysfs_mutex and lock them in order.
469 */
470 if (!mutex_trylock(&inode->i_mutex)) {
471 mutex_unlock(&sysfs_mutex);
472 mutex_lock(&inode->i_mutex);
473 mutex_lock(&sysfs_mutex);
474 }
475 } else
476 iput(inode);
477}
478
479/**
480 * sysfs_add_one - add sysfs_dirent to parent
481 * @acxt: addrm context to use
482 * @sd: sysfs_dirent to be added
483 *
484 * Get @acxt->parent_sd and set sd->s_parent to it and increment
485 * nlink of parent inode if @sd is a directory. @sd is NOT
486 * linked into the children list of the parent. The caller
487 * should invoke sysfs_link_sibling() after this function
488 * completes if @sd needs to be on the children list.
489 *
490 * This function should be called between calls to
491 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
492 * passed the same @acxt as passed to sysfs_addrm_start().
493 *
494 * LOCKING:
495 * Determined by sysfs_addrm_start().
Tejun Heo23dc2792007-08-02 21:38:03 +0900496 *
497 * RETURNS:
498 * 0 on success, -EEXIST if entry with the given name already
499 * exists.
Tejun Heofb6896d2007-06-14 04:27:24 +0900500 */
Tejun Heo23dc2792007-08-02 21:38:03 +0900501int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
Tejun Heofb6896d2007-06-14 04:27:24 +0900502{
Tejun Heo23dc2792007-08-02 21:38:03 +0900503 if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
504 return -EEXIST;
505
Tejun Heofb6896d2007-06-14 04:27:24 +0900506 sd->s_parent = sysfs_get(acxt->parent_sd);
507
508 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
509 inc_nlink(acxt->parent_inode);
510
511 acxt->cnt++;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900512
513 sysfs_link_sibling(sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900514
515 return 0;
Tejun Heofb6896d2007-06-14 04:27:24 +0900516}
517
518/**
519 * sysfs_remove_one - remove sysfs_dirent from parent
520 * @acxt: addrm context to use
521 * @sd: sysfs_dirent to be added
522 *
523 * Mark @sd removed and drop nlink of parent inode if @sd is a
524 * directory. @sd is NOT unlinked from the children list of the
525 * parent. The caller is repsonsible for removing @sd from the
526 * children list before calling this function.
527 *
528 * This function should be called between calls to
529 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
530 * passed the same @acxt as passed to sysfs_addrm_start().
531 *
532 * LOCKING:
533 * Determined by sysfs_addrm_start().
534 */
535void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
536{
Tejun Heo41fc1c22007-08-02 21:38:03 +0900537 BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
538
539 sysfs_unlink_sibling(sd);
Tejun Heofb6896d2007-06-14 04:27:24 +0900540
541 sd->s_flags |= SYSFS_FLAG_REMOVED;
542 sd->s_sibling = acxt->removed;
543 acxt->removed = sd;
544
545 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
546 drop_nlink(acxt->parent_inode);
547
548 acxt->cnt++;
549}
550
551/**
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900552 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
553 * @sd: target sysfs_dirent
554 *
555 * Drop dentry for @sd. @sd must have been unlinked from its
556 * parent on entry to this function such that it can't be looked
557 * up anymore.
558 *
559 * @sd->s_dentry which is protected with sysfs_assoc_lock points
560 * to the currently associated dentry but we're not holding a
561 * reference to it and racing with dput(). Grab dcache_lock and
562 * verify dentry before dropping it. If @sd->s_dentry is NULL or
563 * dput() beats us, no need to bother.
564 */
565static void sysfs_drop_dentry(struct sysfs_dirent *sd)
566{
567 struct dentry *dentry = NULL;
568 struct inode *inode;
569
570 /* We're not holding a reference to ->s_dentry dentry but the
571 * field will stay valid as long as sysfs_assoc_lock is held.
572 */
573 spin_lock(&sysfs_assoc_lock);
574 spin_lock(&dcache_lock);
575
576 /* drop dentry if it's there and dput() didn't kill it yet */
577 if (sd->s_dentry && sd->s_dentry->d_inode) {
578 dentry = dget_locked(sd->s_dentry);
579 spin_lock(&dentry->d_lock);
580 __d_drop(dentry);
581 spin_unlock(&dentry->d_lock);
582 }
583
584 spin_unlock(&dcache_lock);
585 spin_unlock(&sysfs_assoc_lock);
586
Tejun Heo51225032007-06-14 04:27:25 +0900587 dput(dentry);
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900588
589 /* adjust nlink and update timestamp */
590 inode = ilookup(sysfs_sb, sd->s_ino);
591 if (inode) {
592 mutex_lock(&inode->i_mutex);
593
594 inode->i_ctime = CURRENT_TIME;
595 drop_nlink(inode);
596 if (sysfs_type(sd) == SYSFS_DIR)
597 drop_nlink(inode);
598
599 mutex_unlock(&inode->i_mutex);
600 iput(inode);
601 }
602}
603
604/**
Tejun Heofb6896d2007-06-14 04:27:24 +0900605 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
606 * @acxt: addrm context to finish up
607 *
608 * Finish up sysfs_dirent add/remove. Resources acquired by
609 * sysfs_addrm_start() are released and removed sysfs_dirents are
610 * cleaned up. Timestamps on the parent inode are updated.
611 *
612 * LOCKING:
613 * All mutexes acquired by sysfs_addrm_start() are released.
Tejun Heofb6896d2007-06-14 04:27:24 +0900614 */
Tejun Heo990e53f2007-08-02 21:38:03 +0900615void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
Tejun Heofb6896d2007-06-14 04:27:24 +0900616{
617 /* release resources acquired by sysfs_addrm_start() */
618 mutex_unlock(&sysfs_mutex);
619 if (acxt->parent_inode) {
620 struct inode *inode = acxt->parent_inode;
621
622 /* if added/removed, update timestamps on the parent */
623 if (acxt->cnt)
624 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
625
626 mutex_unlock(&inode->i_mutex);
627 iput(inode);
Tejun Heo13b30862007-06-14 03:45:14 +0900628 }
Tejun Heofb6896d2007-06-14 04:27:24 +0900629
630 /* kill removed sysfs_dirents */
631 while (acxt->removed) {
632 struct sysfs_dirent *sd = acxt->removed;
633
634 acxt->removed = sd->s_sibling;
635 sd->s_sibling = NULL;
636
637 sysfs_drop_dentry(sd);
638 sysfs_deactivate(sd);
639 sysfs_put(sd);
640 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700641}
642
Tejun Heof0b0af42007-06-14 04:27:22 +0900643/**
644 * sysfs_find_dirent - find sysfs_dirent with the given name
645 * @parent_sd: sysfs_dirent to search under
646 * @name: name to look for
Maneesh Sonic5168652006-03-09 19:40:14 +0530647 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900648 * Look for sysfs_dirent with name @name under @parent_sd.
Maneesh Sonic5168652006-03-09 19:40:14 +0530649 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900650 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900651 * mutex_lock(sysfs_mutex)
Tejun Heof0b0af42007-06-14 04:27:22 +0900652 *
653 * RETURNS:
654 * Pointer to sysfs_dirent if found, NULL if not.
Maneesh Sonic5168652006-03-09 19:40:14 +0530655 */
Tejun Heof0b0af42007-06-14 04:27:22 +0900656struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
657 const unsigned char *name)
Maneesh Sonic5168652006-03-09 19:40:14 +0530658{
Tejun Heof0b0af42007-06-14 04:27:22 +0900659 struct sysfs_dirent *sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530660
Tejun Heof0b0af42007-06-14 04:27:22 +0900661 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
662 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
663 return sd;
664 return NULL;
665}
Maneesh Sonic5168652006-03-09 19:40:14 +0530666
Tejun Heof0b0af42007-06-14 04:27:22 +0900667/**
668 * sysfs_get_dirent - find and get sysfs_dirent with the given name
669 * @parent_sd: sysfs_dirent to search under
670 * @name: name to look for
671 *
672 * Look for sysfs_dirent with name @name under @parent_sd and get
673 * it if found.
674 *
675 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900676 * Kernel thread context (may sleep). Grabs sysfs_mutex.
Tejun Heof0b0af42007-06-14 04:27:22 +0900677 *
678 * RETURNS:
679 * Pointer to sysfs_dirent if found, NULL if not.
680 */
681struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
682 const unsigned char *name)
683{
684 struct sysfs_dirent *sd;
685
Tejun Heo3007e992007-06-14 04:27:23 +0900686 mutex_lock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900687 sd = sysfs_find_dirent(parent_sd, name);
688 sysfs_get(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900689 mutex_unlock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900690
691 return sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530692}
693
Tejun Heo608e2662007-06-14 04:27:22 +0900694static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
695 const char *name, struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heo51225032007-06-14 04:27:25 +0900698 struct sysfs_addrm_cxt acxt;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900699 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900700 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Tejun Heofc9f54b2007-06-14 03:45:17 +0900702 /* allocate */
Tejun Heo3e519032007-06-14 03:45:15 +0900703 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900704 if (!sd)
Tejun Heo51225032007-06-14 04:27:25 +0900705 return -ENOMEM;
Tejun Heo3e519032007-06-14 03:45:15 +0900706 sd->s_elem.dir.kobj = kobj;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900707
Tejun Heo51225032007-06-14 04:27:25 +0900708 /* link in */
709 sysfs_addrm_start(&acxt, parent_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900710 rc = sysfs_add_one(&acxt, sd);
711 sysfs_addrm_finish(&acxt);
Tejun Heo967e35d2007-07-18 16:38:11 +0900712
Tejun Heo23dc2792007-08-02 21:38:03 +0900713 if (rc == 0)
714 *p_sd = sd;
715 else
Tejun Heo967e35d2007-07-18 16:38:11 +0900716 sysfs_put(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900717
Tejun Heo23dc2792007-08-02 21:38:03 +0900718 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Tejun Heo608e2662007-06-14 04:27:22 +0900721int sysfs_create_subdir(struct kobject *kobj, const char *name,
722 struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Tejun Heo608e2662007-06-14 04:27:22 +0900724 return create_dir(kobj, kobj->sd, name, p_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727/**
728 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * @kobj: object we're creating directory for.
730 */
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900731int sysfs_create_dir(struct kobject * kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Tejun Heo608e2662007-06-14 04:27:22 +0900733 struct sysfs_dirent *parent_sd, *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int error = 0;
735
736 BUG_ON(!kobj);
737
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900738 if (kobj->parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900739 parent_sd = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 else
Eric W. Biederman7d0c7d62007-08-20 21:36:30 +0900741 parent_sd = &sysfs_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
750 struct nameidata *nd)
751{
Tejun Heo6cb52142007-07-31 19:15:08 +0900752 struct dentry *ret = NULL;
Tejun Heoa7a04752007-08-02 21:38:02 +0900753 struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
754 struct sysfs_dirent *sd;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900755 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Tejun Heo6cb52142007-07-31 19:15:08 +0900757 mutex_lock(&sysfs_mutex);
758
Tejun Heoa7a04752007-08-02 21:38:02 +0900759 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
760 if (sysfs_type(sd) && !strcmp(sd->s_name, dentry->d_name.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Tejun Heofc9f54b2007-06-14 03:45:17 +0900763 /* no such entry */
Tejun Heoa7a04752007-08-02 21:38:02 +0900764 if (!sd)
Tejun Heo6cb52142007-07-31 19:15:08 +0900765 goto out_unlock;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900766
767 /* attach dentry and inode */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900768 inode = sysfs_get_inode(sd);
Tejun Heo6cb52142007-07-31 19:15:08 +0900769 if (!inode) {
770 ret = ERR_PTR(-ENOMEM);
771 goto out_unlock;
772 }
Tejun Heo3007e992007-06-14 04:27:23 +0900773
Eric W. Biederman119dd522007-08-20 21:36:29 +0900774 d_instantiate(dentry, inode);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900775 sysfs_attach_dentry(sd, dentry);
776
Tejun Heo6cb52142007-07-31 19:15:08 +0900777 out_unlock:
Tejun Heo3007e992007-06-14 04:27:23 +0900778 mutex_unlock(&sysfs_mutex);
Tejun Heo6cb52142007-07-31 19:15:08 +0900779 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800782const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530784 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785};
786
Tejun Heo608e2662007-06-14 04:27:22 +0900787static void remove_dir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Tejun Heofb6896d2007-06-14 04:27:24 +0900789 struct sysfs_addrm_cxt acxt;
790
791 sysfs_addrm_start(&acxt, sd->s_parent);
Tejun Heofb6896d2007-06-14 04:27:24 +0900792 sysfs_remove_one(&acxt, sd);
793 sysfs_addrm_finish(&acxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Tejun Heo608e2662007-06-14 04:27:22 +0900796void sysfs_remove_subdir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Tejun Heo608e2662007-06-14 04:27:22 +0900798 remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801
Tejun Heo608e2662007-06-14 04:27:22 +0900802static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Tejun Heofb6896d2007-06-14 04:27:24 +0900804 struct sysfs_addrm_cxt acxt;
Tejun Heo0c73f182007-06-14 03:45:18 +0900805 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Tejun Heo608e2662007-06-14 04:27:22 +0900807 if (!dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return;
809
Tejun Heo608e2662007-06-14 04:27:22 +0900810 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
Tejun Heofb6896d2007-06-14 04:27:24 +0900811 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900812 pos = &dir_sd->s_children;
Tejun Heo0c73f182007-06-14 03:45:18 +0900813 while (*pos) {
814 struct sysfs_dirent *sd = *pos;
815
Tejun Heo41fc1c22007-08-02 21:38:03 +0900816 if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR)
Tejun Heofb6896d2007-06-14 04:27:24 +0900817 sysfs_remove_one(&acxt, sd);
Tejun Heo41fc1c22007-08-02 21:38:03 +0900818 else
Tejun Heo0c73f182007-06-14 03:45:18 +0900819 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Tejun Heofb6896d2007-06-14 04:27:24 +0900821 sysfs_addrm_finish(&acxt);
Tejun Heo0ab66082007-06-14 03:45:16 +0900822
Tejun Heo608e2662007-06-14 04:27:22 +0900823 remove_dir(dir_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700824}
825
826/**
827 * sysfs_remove_dir - remove an object's directory.
828 * @kobj: object.
829 *
830 * The only thing special about this is that we remove any files in
831 * the directory before we remove the directory, and we've inlined
832 * what used to be sysfs_rmdir() below, instead of calling separately.
833 */
834
835void sysfs_remove_dir(struct kobject * kobj)
836{
Tejun Heo608e2662007-06-14 04:27:22 +0900837 struct sysfs_dirent *sd = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +0900838
Tejun Heo5f995322007-06-14 04:27:23 +0900839 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +0900840 kobj->sd = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900841 spin_unlock(&sysfs_assoc_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +0900842
Tejun Heo608e2662007-06-14 04:27:22 +0900843 __sysfs_remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900846int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900848 struct sysfs_dirent *sd;
849 struct dentry *parent = NULL;
Tejun Heo51225032007-06-14 04:27:25 +0900850 struct dentry *old_dentry = NULL, *new_dentry = NULL;
851 const char *dup_name = NULL;
Tejun Heo996b7372007-06-14 03:45:14 +0900852 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Tejun Heoff869de2007-08-02 21:38:02 +0900854 /* get the original dentry */
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900855 sd = kobj->sd;
Tejun Heo51225032007-06-14 04:27:25 +0900856 old_dentry = sysfs_get_dentry(sd);
857 if (IS_ERR(old_dentry)) {
858 error = PTR_ERR(old_dentry);
859 goto out_dput;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Tejun Heoff869de2007-08-02 21:38:02 +0900862 parent = old_dentry->d_parent;
Tejun Heo51225032007-06-14 04:27:25 +0900863
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900864 /* lock parent and get dentry for new name */
865 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900867 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900868 if (IS_ERR(new_dentry)) {
869 error = PTR_ERR(new_dentry);
870 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Tejun Heo996b7372007-06-14 03:45:14 +0900872
Tejun Heo996b7372007-06-14 03:45:14 +0900873 error = -EINVAL;
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900874 if (old_dentry == new_dentry)
Tejun Heo51225032007-06-14 04:27:25 +0900875 goto out_unlock;
Tejun Heo996b7372007-06-14 03:45:14 +0900876
877 error = -EEXIST;
878 if (new_dentry->d_inode)
Tejun Heo51225032007-06-14 04:27:25 +0900879 goto out_unlock;
Tejun Heo996b7372007-06-14 03:45:14 +0900880
Tejun Heo0c096b52007-06-14 03:45:15 +0900881 /* rename kobject and sysfs_dirent */
882 error = -ENOMEM;
883 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
884 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900885 goto out_drop;
886
Tejun Heo0c096b52007-06-14 03:45:15 +0900887 error = kobject_set_name(kobj, "%s", new_name);
888 if (error)
Tejun Heo51225032007-06-14 04:27:25 +0900889 goto out_drop;
Tejun Heo0c096b52007-06-14 03:45:15 +0900890
Tejun Heo6cb52142007-07-31 19:15:08 +0900891 mutex_lock(&sysfs_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900892 dup_name = sd->s_name;
Tejun Heo0c096b52007-06-14 03:45:15 +0900893 sd->s_name = new_name;
Tejun Heoff869de2007-08-02 21:38:02 +0900894 mutex_unlock(&sysfs_mutex);
Tejun Heo0c096b52007-06-14 03:45:15 +0900895
Tejun Heoff869de2007-08-02 21:38:02 +0900896 /* rename */
Tejun Heo996b7372007-06-14 03:45:14 +0900897 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900898 d_move(sd->s_dentry, new_dentry);
Tejun Heo996b7372007-06-14 03:45:14 +0900899
Tejun Heo996b7372007-06-14 03:45:14 +0900900 error = 0;
901 goto out_unlock;
902
903 out_drop:
904 d_drop(new_dentry);
Tejun Heo996b7372007-06-14 03:45:14 +0900905 out_unlock:
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900906 mutex_unlock(&parent->d_inode->i_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900907 out_dput:
908 kfree(dup_name);
Tejun Heo51225032007-06-14 04:27:25 +0900909 dput(old_dentry);
910 dput(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return error;
912}
913
Tejun Heo51225032007-06-14 04:27:25 +0900914int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
Cornelia Huck8a824722006-11-20 17:07:51 +0100915{
Tejun Heo51225032007-06-14 04:27:25 +0900916 struct sysfs_dirent *sd = kobj->sd;
917 struct sysfs_dirent *new_parent_sd;
918 struct dentry *old_parent, *new_parent = NULL;
919 struct dentry *old_dentry = NULL, *new_dentry = NULL;
Cornelia Huck8a824722006-11-20 17:07:51 +0100920 int error;
921
Tejun Heo51225032007-06-14 04:27:25 +0900922 BUG_ON(!sd->s_parent);
923 new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100924
Tejun Heo51225032007-06-14 04:27:25 +0900925 /* get dentries */
926 old_dentry = sysfs_get_dentry(sd);
927 if (IS_ERR(old_dentry)) {
928 error = PTR_ERR(old_dentry);
929 goto out_dput;
930 }
931 old_parent = sd->s_parent->s_dentry;
932
933 new_parent = sysfs_get_dentry(new_parent_sd);
934 if (IS_ERR(new_parent)) {
935 error = PTR_ERR(new_parent);
936 goto out_dput;
937 }
938
939 if (old_parent->d_inode == new_parent->d_inode) {
940 error = 0;
941 goto out_dput; /* nothing to move */
942 }
Cornelia Huck8a824722006-11-20 17:07:51 +0100943again:
Tejun Heo51225032007-06-14 04:27:25 +0900944 mutex_lock(&old_parent->d_inode->i_mutex);
945 if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
946 mutex_unlock(&old_parent->d_inode->i_mutex);
Cornelia Huck8a824722006-11-20 17:07:51 +0100947 goto again;
948 }
949
Greg Kroah-Hartman19c38de2007-09-12 15:06:57 -0700950 new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj)));
Cornelia Huck8a824722006-11-20 17:07:51 +0100951 if (IS_ERR(new_dentry)) {
952 error = PTR_ERR(new_dentry);
Tejun Heo51225032007-06-14 04:27:25 +0900953 goto out_unlock;
Cornelia Huck8a824722006-11-20 17:07:51 +0100954 } else
955 error = 0;
956 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900957 d_move(sd->s_dentry, new_dentry);
Cornelia Huck8a824722006-11-20 17:07:51 +0100958 dput(new_dentry);
959
960 /* Remove from old parent's list and insert into new parent's list. */
Tejun Heo3007e992007-06-14 04:27:23 +0900961 mutex_lock(&sysfs_mutex);
962
Tejun Heo0c73f182007-06-14 03:45:18 +0900963 sysfs_unlink_sibling(sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900964 sysfs_get(new_parent_sd);
965 sysfs_put(sd->s_parent);
966 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900967 sysfs_link_sibling(sd);
Cornelia Huck8a824722006-11-20 17:07:51 +0100968
Tejun Heo3007e992007-06-14 04:27:23 +0900969 mutex_unlock(&sysfs_mutex);
Cornelia Huck8a824722006-11-20 17:07:51 +0100970
Tejun Heo51225032007-06-14 04:27:25 +0900971 out_unlock:
972 mutex_unlock(&new_parent->d_inode->i_mutex);
973 mutex_unlock(&old_parent->d_inode->i_mutex);
974 out_dput:
975 dput(new_parent);
976 dput(old_dentry);
977 dput(new_dentry);
Cornelia Huck8a824722006-11-20 17:07:51 +0100978 return error;
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981static int sysfs_dir_open(struct inode *inode, struct file *file)
982{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800983 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900985 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Tejun Heo3e519032007-06-14 03:45:15 +0900987 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heo3007e992007-06-14 04:27:23 +0900988 if (sd) {
989 mutex_lock(&sysfs_mutex);
Tejun Heofb6896d2007-06-14 04:27:24 +0900990 sd->s_parent = sysfs_get(parent_sd);
991 sysfs_link_sibling(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900992 mutex_unlock(&sysfs_mutex);
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Tejun Heoa26cd722007-06-14 03:45:14 +0900995 file->private_data = sd;
996 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static int sysfs_dir_close(struct inode *inode, struct file *file)
1000{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 struct sysfs_dirent * cursor = file->private_data;
1002
Tejun Heo3007e992007-06-14 04:27:23 +09001003 mutex_lock(&sysfs_mutex);
Tejun Heo0c73f182007-06-14 03:45:18 +09001004 sysfs_unlink_sibling(cursor);
Tejun Heo3007e992007-06-14 04:27:23 +09001005 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 release_sysfs_dirent(cursor);
1008
1009 return 0;
1010}
1011
1012/* Relationship between s_mode and the DT_xxx types */
1013static inline unsigned char dt_type(struct sysfs_dirent *sd)
1014{
1015 return (sd->s_mode >> 12) & 15;
1016}
1017
1018static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1019{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -08001020 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
1022 struct sysfs_dirent *cursor = filp->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +09001023 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 ino_t ino;
1025 int i = filp->f_pos;
1026
1027 switch (i) {
1028 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +09001029 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1031 break;
1032 filp->f_pos++;
1033 i++;
1034 /* fallthrough */
1035 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +09001036 if (parent_sd->s_parent)
1037 ino = parent_sd->s_parent->s_ino;
1038 else
1039 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1041 break;
1042 filp->f_pos++;
1043 i++;
1044 /* fallthrough */
1045 default:
Tejun Heo3007e992007-06-14 04:27:23 +09001046 mutex_lock(&sysfs_mutex);
1047
Tejun Heo0c73f182007-06-14 03:45:18 +09001048 pos = &parent_sd->s_children;
1049 while (*pos != cursor)
1050 pos = &(*pos)->s_sibling;
Akinobu Mita1bfba4e2006-06-26 00:24:40 -07001051
Tejun Heo0c73f182007-06-14 03:45:18 +09001052 /* unlink cursor */
1053 *pos = cursor->s_sibling;
1054
1055 if (filp->f_pos == 2)
1056 pos = &parent_sd->s_children;
1057
1058 for ( ; *pos; pos = &(*pos)->s_sibling) {
1059 struct sysfs_dirent *next = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 const char * name;
1061 int len;
1062
Tejun Heob402d722007-06-14 04:27:21 +09001063 if (!sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 continue;
1065
Tejun Heo0c096b52007-06-14 03:45:15 +09001066 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +09001068 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 if (filldir(dirent, name, len, filp->f_pos, ino,
1071 dt_type(next)) < 0)
Tejun Heo0c73f182007-06-14 03:45:18 +09001072 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 filp->f_pos++;
1075 }
Tejun Heo0c73f182007-06-14 03:45:18 +09001076
1077 /* put cursor back in */
1078 cursor->s_sibling = *pos;
1079 *pos = cursor;
Tejun Heo3007e992007-06-14 04:27:23 +09001080
1081 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 return 0;
1084}
1085
1086static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
1087{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -08001088 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 switch (origin) {
1091 case 1:
1092 offset += file->f_pos;
1093 case 0:
1094 if (offset >= 0)
1095 break;
1096 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -EINVAL;
1098 }
1099 if (offset != file->f_pos) {
Tejun Heo3007e992007-06-14 04:27:23 +09001100 mutex_lock(&sysfs_mutex);
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 file->f_pos = offset;
1103 if (file->f_pos >= 2) {
1104 struct sysfs_dirent *sd = dentry->d_fsdata;
1105 struct sysfs_dirent *cursor = file->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +09001106 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 loff_t n = file->f_pos - 2;
1108
Tejun Heo0c73f182007-06-14 03:45:18 +09001109 sysfs_unlink_sibling(cursor);
1110
1111 pos = &sd->s_children;
1112 while (n && *pos) {
1113 struct sysfs_dirent *next = *pos;
Tejun Heob402d722007-06-14 04:27:21 +09001114 if (sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 n--;
Tejun Heo0c73f182007-06-14 03:45:18 +09001116 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Tejun Heo0c73f182007-06-14 03:45:18 +09001118
1119 cursor->s_sibling = *pos;
1120 *pos = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
Tejun Heo3007e992007-06-14 04:27:23 +09001122
1123 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Tejun Heo3007e992007-06-14 04:27:23 +09001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return offset;
1127}
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};