blob: 40596a0eee526db8fac44d8667bcb83e638c9e34 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dir.c - Operations for sysfs directories.
3 */
4
5#undef DEBUG
6
7#include <linux/fs.h>
8#include <linux/mount.h>
9#include <linux/module.h>
10#include <linux/kobject.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070011#include <linux/namei.h>
Tejun Heo2b611bb2007-06-14 03:45:13 +090012#include <linux/idr.h>
Tejun Heo8619f972007-06-14 03:45:18 +090013#include <linux/completion.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010014#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "sysfs.h"
16
17DECLARE_RWSEM(sysfs_rename_sem);
Tejun Heodd14cbc2007-06-11 14:04:01 +090018spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
Tejun Heoaecdced2007-06-14 03:45:15 +090019spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Tejun Heo2b611bb2007-06-14 03:45:13 +090021static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
22static DEFINE_IDA(sysfs_ino_ida);
23
Tejun Heob6b4a432007-06-14 03:45:18 +090024/**
25 * sysfs_get_active - get an active reference to sysfs_dirent
26 * @sd: sysfs_dirent to get an active reference to
27 *
28 * Get an active reference of @sd. This function is noop if @sd
29 * is NULL.
30 *
31 * RETURNS:
32 * Pointer to @sd on success, NULL on failure.
33 */
34struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
35{
Tejun Heo8619f972007-06-14 03:45:18 +090036 if (unlikely(!sd))
37 return NULL;
38
39 while (1) {
40 int v, t;
41
42 v = atomic_read(&sd->s_active);
43 if (unlikely(v < 0))
44 return NULL;
45
46 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
47 if (likely(t == v))
48 return sd;
49 if (t < 0)
50 return NULL;
51
52 cpu_relax();
Tejun Heob6b4a432007-06-14 03:45:18 +090053 }
Tejun Heob6b4a432007-06-14 03:45:18 +090054}
55
56/**
57 * sysfs_put_active - put an active reference to sysfs_dirent
58 * @sd: sysfs_dirent to put an active reference to
59 *
60 * Put an active reference to @sd. This function is noop if @sd
61 * is NULL.
62 */
63void sysfs_put_active(struct sysfs_dirent *sd)
64{
Tejun Heo8619f972007-06-14 03:45:18 +090065 struct completion *cmpl;
66 int v;
67
68 if (unlikely(!sd))
69 return;
70
71 v = atomic_dec_return(&sd->s_active);
72 if (likely(v != SD_DEACTIVATED_BIAS))
73 return;
74
75 /* atomic_dec_return() is a mb(), we'll always see the updated
76 * sd->s_sibling.next.
77 */
78 cmpl = (void *)sd->s_sibling.next;
79 complete(cmpl);
Tejun Heob6b4a432007-06-14 03:45:18 +090080}
81
82/**
83 * sysfs_get_active_two - get active references to sysfs_dirent and parent
84 * @sd: sysfs_dirent of interest
85 *
86 * Get active reference to @sd and its parent. Parent's active
87 * reference is grabbed first. This function is noop if @sd is
88 * NULL.
89 *
90 * RETURNS:
91 * Pointer to @sd on success, NULL on failure.
92 */
93struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
94{
95 if (sd) {
96 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
97 return NULL;
98 if (unlikely(!sysfs_get_active(sd))) {
99 sysfs_put_active(sd->s_parent);
100 return NULL;
101 }
102 }
103 return sd;
104}
105
106/**
107 * sysfs_put_active_two - put active references to sysfs_dirent and parent
108 * @sd: sysfs_dirent of interest
109 *
110 * Put active references to @sd and its parent. This function is
111 * noop if @sd is NULL.
112 */
113void sysfs_put_active_two(struct sysfs_dirent *sd)
114{
115 if (sd) {
116 sysfs_put_active(sd);
117 sysfs_put_active(sd->s_parent);
118 }
119}
120
121/**
122 * sysfs_deactivate - deactivate sysfs_dirent
123 * @sd: sysfs_dirent to deactivate
124 *
Tejun Heo8619f972007-06-14 03:45:18 +0900125 * Deny new active references and drain existing ones.
Tejun Heob6b4a432007-06-14 03:45:18 +0900126 */
127void sysfs_deactivate(struct sysfs_dirent *sd)
128{
Tejun Heo8619f972007-06-14 03:45:18 +0900129 DECLARE_COMPLETION_ONSTACK(wait);
130 int v;
Tejun Heob6b4a432007-06-14 03:45:18 +0900131
Tejun Heo8619f972007-06-14 03:45:18 +0900132 BUG_ON(!list_empty(&sd->s_sibling));
133 sd->s_sibling.next = (void *)&wait;
134
135 /* atomic_add_return() is a mb(), put_active() will always see
136 * the updated sd->s_sibling.next.
Tejun Heob6b4a432007-06-14 03:45:18 +0900137 */
Tejun Heo8619f972007-06-14 03:45:18 +0900138 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
139
140 if (v != SD_DEACTIVATED_BIAS)
141 wait_for_completion(&wait);
142
143 INIT_LIST_HEAD(&sd->s_sibling);
Tejun Heob6b4a432007-06-14 03:45:18 +0900144}
145
Tejun Heo42b37df2007-06-14 03:45:17 +0900146static int sysfs_alloc_ino(ino_t *pino)
Tejun Heo2b611bb2007-06-14 03:45:13 +0900147{
148 int ino, rc;
149
150 retry:
151 spin_lock(&sysfs_ino_lock);
152 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
153 spin_unlock(&sysfs_ino_lock);
154
155 if (rc == -EAGAIN) {
156 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
157 goto retry;
158 rc = -ENOMEM;
159 }
160
161 *pino = ino;
162 return rc;
163}
164
165static void sysfs_free_ino(ino_t ino)
166{
167 spin_lock(&sysfs_ino_lock);
168 ida_remove(&sysfs_ino_ida, ino);
169 spin_unlock(&sysfs_ino_lock);
170}
171
Tejun Heofa7f9122007-06-14 03:45:13 +0900172void release_sysfs_dirent(struct sysfs_dirent * sd)
173{
Tejun Heo13b30862007-06-14 03:45:14 +0900174 struct sysfs_dirent *parent_sd;
175
176 repeat:
177 parent_sd = sd->s_parent;
178
Tejun Heo3e519032007-06-14 03:45:15 +0900179 if (sd->s_type & SYSFS_KOBJ_LINK)
Tejun Heo2b29ac22007-06-14 03:45:15 +0900180 sysfs_put(sd->s_elem.symlink.target_sd);
Tejun Heo0c096b52007-06-14 03:45:15 +0900181 if (sd->s_type & SYSFS_COPY_NAME)
182 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +0900183 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +0900184 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +0900185 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900186
187 sd = parent_sd;
188 if (sd && atomic_dec_and_test(&sd->s_count))
189 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +0900190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
193{
194 struct sysfs_dirent * sd = dentry->d_fsdata;
195
196 if (sd) {
Tejun Heodd14cbc2007-06-11 14:04:01 +0900197 /* sd->s_dentry is protected with sysfs_lock. This
198 * allows sysfs_drop_dentry() to dereference it.
199 */
200 spin_lock(&sysfs_lock);
201
202 /* The dentry might have been deleted or another
203 * lookup could have happened updating sd->s_dentry to
204 * point the new dentry. Ignore if it isn't pointing
205 * to this dentry.
206 */
207 if (sd->s_dentry == dentry)
208 sd->s_dentry = NULL;
209 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 sysfs_put(sd);
211 }
212 iput(inode);
213}
214
215static struct dentry_operations sysfs_dentry_ops = {
216 .d_iput = sysfs_d_iput,
217};
218
Tejun Heo3e519032007-06-14 03:45:15 +0900219struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Tejun Heo0c096b52007-06-14 03:45:15 +0900221 char *dup_name = NULL;
222 struct sysfs_dirent *sd = NULL;
223
224 if (type & SYSFS_COPY_NAME) {
225 name = dup_name = kstrdup(name, GFP_KERNEL);
226 if (!name)
227 goto err_out;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800230 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!sd)
Tejun Heo0c096b52007-06-14 03:45:15 +0900232 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Tejun Heo0c096b52007-06-14 03:45:15 +0900234 if (sysfs_alloc_ino(&sd->s_ino))
235 goto err_out;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 atomic_set(&sd->s_count, 1);
Tejun Heo8619f972007-06-14 03:45:18 +0900238 atomic_set(&sd->s_active, 0);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300239 atomic_set(&sd->s_event, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 INIT_LIST_HEAD(&sd->s_children);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700241 INIT_LIST_HEAD(&sd->s_sibling);
Tejun Heoa26cd722007-06-14 03:45:14 +0900242
Tejun Heo0c096b52007-06-14 03:45:15 +0900243 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900244 sd->s_mode = mode;
245 sd->s_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900248
249 err_out:
250 kfree(dup_name);
251 kmem_cache_free(sysfs_dir_cachep, sd);
252 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Tejun Heo198a2a82007-06-14 03:45:16 +0900255static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
256{
257 dentry->d_op = &sysfs_dentry_ops;
258 dentry->d_fsdata = sysfs_get(sd);
259
260 /* protect sd->s_dentry against sysfs_d_iput */
261 spin_lock(&sysfs_lock);
262 sd->s_dentry = dentry;
263 spin_unlock(&sysfs_lock);
264
265 d_rehash(dentry);
266}
267
Tejun Heoa26cd722007-06-14 03:45:14 +0900268void sysfs_attach_dirent(struct sysfs_dirent *sd,
269 struct sysfs_dirent *parent_sd, struct dentry *dentry)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700270{
Tejun Heo198a2a82007-06-14 03:45:16 +0900271 if (dentry)
272 sysfs_attach_dentry(sd, dentry);
Tejun Heoa26cd722007-06-14 03:45:14 +0900273
Tejun Heo13b30862007-06-14 03:45:14 +0900274 if (parent_sd) {
275 sd->s_parent = sysfs_get(parent_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700276 list_add(&sd->s_sibling, &parent_sd->s_children);
Tejun Heo13b30862007-06-14 03:45:14 +0900277 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700278}
279
Martin Waitza5802902006-04-02 13:59:55 +0200280/*
Maneesh Sonic5168652006-03-09 19:40:14 +0530281 *
282 * Return -EEXIST if there is already a sysfs element with the same name for
283 * the same parent.
284 *
285 * called with parent inode's i_mutex held
286 */
287int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
288 const unsigned char *new)
289{
290 struct sysfs_dirent * sd;
291
292 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Tejun Heo3e519032007-06-14 03:45:15 +0900293 if (sd->s_type) {
Tejun Heo0c096b52007-06-14 03:45:15 +0900294 if (strcmp(sd->s_name, new))
Maneesh Sonic5168652006-03-09 19:40:14 +0530295 continue;
296 else
297 return -EEXIST;
298 }
299 }
300
301 return 0;
302}
303
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900304static int create_dir(struct kobject *kobj, struct dentry *parent,
305 const char *name, struct dentry **p_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 int error;
308 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900309 struct dentry *dentry;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900310 struct inode *inode;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900311 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900313 mutex_lock(&parent->d_inode->i_mutex);
314
Tejun Heofc9f54b2007-06-14 03:45:17 +0900315 /* allocate */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900316 dentry = lookup_one_len(name, parent, strlen(name));
317 if (IS_ERR(dentry)) {
318 error = PTR_ERR(dentry);
319 goto out_unlock;
320 }
321
322 error = -EEXIST;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900323 if (dentry->d_inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900324 goto out_dput;
325
Tejun Heoa26cd722007-06-14 03:45:14 +0900326 error = -ENOMEM;
Tejun Heo3e519032007-06-14 03:45:15 +0900327 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900328 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900329 goto out_drop;
Tejun Heo3e519032007-06-14 03:45:15 +0900330 sd->s_elem.dir.kobj = kobj;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900331
Tejun Heo8312a8d2007-06-14 03:45:17 +0900332 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900333 if (!inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900334 goto out_sput;
335
Tejun Heo8312a8d2007-06-14 03:45:17 +0900336 if (inode->i_state & I_NEW) {
337 inode->i_op = &sysfs_dir_inode_operations;
338 inode->i_fop = &sysfs_dir_operations;
339 /* directory inodes start off with i_nlink == 2 (for ".") */
340 inc_nlink(inode);
341 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900342
343 /* link in */
344 error = -EEXIST;
345 if (sysfs_dirent_exist(parent->d_fsdata, name))
346 goto out_iput;
347
348 sysfs_instantiate(dentry, inode);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900349 inc_nlink(parent->d_inode);
Tejun Heo198a2a82007-06-14 03:45:16 +0900350 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900351
352 *p_dentry = dentry;
353 error = 0;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900354 goto out_unlock; /* pin directory dentry in core */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900355
Tejun Heofc9f54b2007-06-14 03:45:17 +0900356 out_iput:
357 iput(inode);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900358 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900359 sysfs_put(sd);
360 out_drop:
361 d_drop(dentry);
362 out_dput:
363 dput(dentry);
364 out_unlock:
365 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return error;
367}
368
369
370int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
371{
372 return create_dir(k,k->dentry,n,d);
373}
374
375/**
376 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700378 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
380
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700381int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct dentry * dentry = NULL;
384 struct dentry * parent;
385 int error = 0;
386
387 BUG_ON(!kobj);
388
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700389 if (shadow_parent)
390 parent = shadow_parent;
391 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 parent = kobj->parent->dentry;
393 else if (sysfs_mount && sysfs_mount->mnt_sb)
394 parent = sysfs_mount->mnt_sb->s_root;
395 else
396 return -EFAULT;
397
398 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
399 if (!error)
400 kobj->dentry = dentry;
401 return error;
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
405 struct nameidata *nd)
406{
407 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
408 struct sysfs_dirent * sd;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900409 struct inode *inode;
410 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Tejun Heofc9f54b2007-06-14 03:45:17 +0900413 if ((sd->s_type & SYSFS_NOT_PINNED) &&
414 !strcmp(sd->s_name, dentry->d_name.name)) {
415 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 break;
417 }
418 }
419
Tejun Heofc9f54b2007-06-14 03:45:17 +0900420 /* no such entry */
421 if (!found)
422 return NULL;
423
424 /* attach dentry and inode */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900425 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900426 if (!inode)
427 return ERR_PTR(-ENOMEM);
428
Tejun Heo8312a8d2007-06-14 03:45:17 +0900429 if (inode->i_state & I_NEW) {
430 /* initialize inode according to type */
431 if (sd->s_type & SYSFS_KOBJ_ATTR) {
432 inode->i_size = PAGE_SIZE;
433 inode->i_fop = &sysfs_file_operations;
434 } else if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
435 struct bin_attribute *bin_attr =
436 sd->s_elem.bin_attr.bin_attr;
437 inode->i_size = bin_attr->size;
438 inode->i_fop = &bin_fops;
439 } else if (sd->s_type & SYSFS_KOBJ_LINK)
440 inode->i_op = &sysfs_symlink_inode_operations;
441 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900442
443 sysfs_instantiate(dentry, inode);
444 sysfs_attach_dentry(sd, dentry);
445
446 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800449const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530451 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452};
453
454static void remove_dir(struct dentry * d)
455{
Tejun Heodbde0fc2007-06-14 03:45:16 +0900456 struct dentry *parent = d->d_parent;
457 struct sysfs_dirent *sd = d->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800459 mutex_lock(&parent->d_inode->i_mutex);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 list_del_init(&sd->s_sibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
464 atomic_read(&d->d_count));
465
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800466 mutex_unlock(&parent->d_inode->i_mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900467
Tejun Heodbde0fc2007-06-14 03:45:16 +0900468 sysfs_drop_dentry(sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900469 sysfs_deactivate(sd);
470 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473void sysfs_remove_subdir(struct dentry * d)
474{
475 remove_dir(d);
476}
477
478
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700479static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Tejun Heo0ab66082007-06-14 03:45:16 +0900481 LIST_HEAD(removed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct sysfs_dirent * parent_sd;
483 struct sysfs_dirent * sd, * tmp;
484
485 if (!dentry)
486 return;
487
488 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800489 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 parent_sd = dentry->d_fsdata;
491 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
Tejun Heo3e519032007-06-14 03:45:15 +0900492 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 continue;
Tejun Heo0ab66082007-06-14 03:45:16 +0900494 list_move(&sd->s_sibling, &removed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800496 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Tejun Heo0ab66082007-06-14 03:45:16 +0900498 list_for_each_entry_safe(sd, tmp, &removed, s_sibling) {
499 list_del_init(&sd->s_sibling);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900500 sysfs_drop_dentry(sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900501 sysfs_deactivate(sd);
502 sysfs_put(sd);
503 }
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 remove_dir(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700506}
507
508/**
509 * sysfs_remove_dir - remove an object's directory.
510 * @kobj: object.
511 *
512 * The only thing special about this is that we remove any files in
513 * the directory before we remove the directory, and we've inlined
514 * what used to be sysfs_rmdir() below, instead of calling separately.
515 */
516
517void sysfs_remove_dir(struct kobject * kobj)
518{
Tejun Heoaecdced2007-06-14 03:45:15 +0900519 struct dentry *d = kobj->dentry;
520
521 spin_lock(&kobj_sysfs_assoc_lock);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800522 kobj->dentry = NULL;
Tejun Heoaecdced2007-06-14 03:45:15 +0900523 spin_unlock(&kobj_sysfs_assoc_lock);
524
525 __sysfs_remove_dir(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700528int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
529 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Tejun Heo0c096b52007-06-14 03:45:15 +0900531 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
532 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
533 struct dentry *new_dentry;
534 char *dup_name;
Tejun Heo996b7372007-06-14 03:45:14 +0900535 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700537 if (!new_parent)
538 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700541 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700543 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900544 if (IS_ERR(new_dentry)) {
545 error = PTR_ERR(new_dentry);
546 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Tejun Heo996b7372007-06-14 03:45:14 +0900548
549 /* By allowing two different directories with the same
550 * d_parent we allow this routine to move between different
551 * shadows of the same directory
552 */
553 error = -EINVAL;
554 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
555 new_dentry->d_parent->d_inode != new_parent->d_inode ||
556 new_dentry == kobj->dentry)
557 goto out_dput;
558
559 error = -EEXIST;
560 if (new_dentry->d_inode)
561 goto out_dput;
562
Tejun Heo0c096b52007-06-14 03:45:15 +0900563 /* rename kobject and sysfs_dirent */
564 error = -ENOMEM;
565 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
566 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900567 goto out_drop;
568
Tejun Heo0c096b52007-06-14 03:45:15 +0900569 error = kobject_set_name(kobj, "%s", new_name);
570 if (error)
571 goto out_free;
572
573 kfree(sd->s_name);
574 sd->s_name = new_name;
575
576 /* move under the new parent */
Tejun Heo996b7372007-06-14 03:45:14 +0900577 d_add(new_dentry, NULL);
578 d_move(kobj->dentry, new_dentry);
579
Tejun Heo996b7372007-06-14 03:45:14 +0900580 list_del_init(&sd->s_sibling);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900581 sysfs_get(parent_sd);
582 sysfs_put(sd->s_parent);
583 sd->s_parent = parent_sd;
Tejun Heo996b7372007-06-14 03:45:14 +0900584 list_add(&sd->s_sibling, &parent_sd->s_children);
585
586 error = 0;
587 goto out_unlock;
588
Tejun Heo0c096b52007-06-14 03:45:15 +0900589 out_free:
590 kfree(dup_name);
Tejun Heo996b7372007-06-14 03:45:14 +0900591 out_drop:
592 d_drop(new_dentry);
593 out_dput:
594 dput(new_dentry);
595 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700596 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 up_write(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return error;
599}
600
Cornelia Huck8a824722006-11-20 17:07:51 +0100601int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
602{
603 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
604 struct sysfs_dirent *new_parent_sd, *sd;
605 int error;
606
Cornelia Huck8a824722006-11-20 17:07:51 +0100607 old_parent_dentry = kobj->parent ?
608 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100609 new_parent_dentry = new_parent ?
610 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100611
Mark Lord0de15172007-03-06 01:42:03 -0800612 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
613 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100614again:
615 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
616 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
617 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
618 goto again;
619 }
620
621 new_parent_sd = new_parent_dentry->d_fsdata;
622 sd = kobj->dentry->d_fsdata;
623
624 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
625 strlen(kobj->name));
626 if (IS_ERR(new_dentry)) {
627 error = PTR_ERR(new_dentry);
628 goto out;
629 } else
630 error = 0;
631 d_add(new_dentry, NULL);
632 d_move(kobj->dentry, new_dentry);
633 dput(new_dentry);
634
635 /* Remove from old parent's list and insert into new parent's list. */
636 list_del_init(&sd->s_sibling);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900637 sysfs_get(new_parent_sd);
638 sysfs_put(sd->s_parent);
639 sd->s_parent = new_parent_sd;
Cornelia Huck8a824722006-11-20 17:07:51 +0100640 list_add(&sd->s_sibling, &new_parent_sd->s_children);
641
642out:
643 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
644 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
645
646 return error;
647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static int sysfs_dir_open(struct inode *inode, struct file *file)
650{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800651 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900653 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800655 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heo3e519032007-06-14 03:45:15 +0900656 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heoa26cd722007-06-14 03:45:14 +0900657 if (sd)
658 sysfs_attach_dirent(sd, parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800659 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Tejun Heoa26cd722007-06-14 03:45:14 +0900661 file->private_data = sd;
662 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665static int sysfs_dir_close(struct inode *inode, struct file *file)
666{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800667 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct sysfs_dirent * cursor = file->private_data;
669
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800670 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800672 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 release_sysfs_dirent(cursor);
675
676 return 0;
677}
678
679/* Relationship between s_mode and the DT_xxx types */
680static inline unsigned char dt_type(struct sysfs_dirent *sd)
681{
682 return (sd->s_mode >> 12) & 15;
683}
684
685static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
686{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800687 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
689 struct sysfs_dirent *cursor = filp->private_data;
690 struct list_head *p, *q = &cursor->s_sibling;
691 ino_t ino;
692 int i = filp->f_pos;
693
694 switch (i) {
695 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900696 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
698 break;
699 filp->f_pos++;
700 i++;
701 /* fallthrough */
702 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900703 if (parent_sd->s_parent)
704 ino = parent_sd->s_parent->s_ino;
705 else
706 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
708 break;
709 filp->f_pos++;
710 i++;
711 /* fallthrough */
712 default:
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700713 if (filp->f_pos == 2)
714 list_move(q, &parent_sd->s_children);
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
717 struct sysfs_dirent *next;
718 const char * name;
719 int len;
720
721 next = list_entry(p, struct sysfs_dirent,
722 s_sibling);
Tejun Heo3e519032007-06-14 03:45:15 +0900723 if (!next->s_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 continue;
725
Tejun Heo0c096b52007-06-14 03:45:15 +0900726 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900728 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 if (filldir(dirent, name, len, filp->f_pos, ino,
731 dt_type(next)) < 0)
732 return 0;
733
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700734 list_move(q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 p = q;
736 filp->f_pos++;
737 }
738 }
739 return 0;
740}
741
742static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
743{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800744 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800746 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 switch (origin) {
748 case 1:
749 offset += file->f_pos;
750 case 0:
751 if (offset >= 0)
752 break;
753 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800754 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EINVAL;
756 }
757 if (offset != file->f_pos) {
758 file->f_pos = offset;
759 if (file->f_pos >= 2) {
760 struct sysfs_dirent *sd = dentry->d_fsdata;
761 struct sysfs_dirent *cursor = file->private_data;
762 struct list_head *p;
763 loff_t n = file->f_pos - 2;
764
765 list_del(&cursor->s_sibling);
766 p = sd->s_children.next;
767 while (n && p != &sd->s_children) {
768 struct sysfs_dirent *next;
769 next = list_entry(p, struct sysfs_dirent,
770 s_sibling);
Tejun Heo3e519032007-06-14 03:45:15 +0900771 if (next->s_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 n--;
773 p = p->next;
774 }
775 list_add_tail(&cursor->s_sibling, p);
776 }
777 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800778 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return offset;
780}
781
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700782
783/**
784 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
785 * @kobj: object we're creating shadow of.
786 */
787
788int sysfs_make_shadowed_dir(struct kobject *kobj,
789 void * (*follow_link)(struct dentry *, struct nameidata *))
790{
791 struct inode *inode;
792 struct inode_operations *i_op;
793
794 inode = kobj->dentry->d_inode;
795 if (inode->i_op != &sysfs_dir_inode_operations)
796 return -EINVAL;
797
798 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
799 if (!i_op)
800 return -ENOMEM;
801
802 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
803 i_op->follow_link = follow_link;
804
805 /* Locking of inode->i_op?
806 * Since setting i_op is a single word write and they
807 * are atomic we should be ok here.
808 */
809 inode->i_op = i_op;
810 return 0;
811}
812
813/**
814 * sysfs_create_shadow_dir - create a shadow directory for an object.
815 * @kobj: object we're creating directory for.
816 *
817 * sysfs_make_shadowed_dir must already have been called on this
818 * directory.
819 */
820
821struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
822{
Tejun Heo13b30862007-06-14 03:45:14 +0900823 struct dentry *dir = kobj->dentry;
824 struct inode *inode = dir->d_inode;
825 struct dentry *parent = dir->d_parent;
826 struct sysfs_dirent *parent_sd = parent->d_fsdata;
827 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700828 struct sysfs_dirent *sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700829
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700830 shadow = ERR_PTR(-EINVAL);
831 if (!sysfs_is_shadowed_inode(inode))
832 goto out;
833
834 shadow = d_alloc(parent, &dir->d_name);
835 if (!shadow)
836 goto nomem;
837
Tejun Heo3e519032007-06-14 03:45:15 +0900838 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700839 if (!sd)
840 goto nomem;
Tejun Heo3e519032007-06-14 03:45:15 +0900841 sd->s_elem.dir.kobj = kobj;
Tejun Heo13b30862007-06-14 03:45:14 +0900842 /* point to parent_sd but don't attach to it */
843 sd->s_parent = sysfs_get(parent_sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900844 sysfs_attach_dirent(sd, NULL, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700845
846 d_instantiate(shadow, igrab(inode));
847 inc_nlink(inode);
848 inc_nlink(parent->d_inode);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700849
850 dget(shadow); /* Extra count - pin the dentry in core */
851
852out:
853 return shadow;
854nomem:
855 dput(shadow);
856 shadow = ERR_PTR(-ENOMEM);
857 goto out;
858}
859
860/**
861 * sysfs_remove_shadow_dir - remove an object's directory.
862 * @shadow: dentry of shadow directory
863 *
864 * The only thing special about this is that we remove any files in
865 * the directory before we remove the directory, and we've inlined
866 * what used to be sysfs_rmdir() below, instead of calling separately.
867 */
868
869void sysfs_remove_shadow_dir(struct dentry *shadow)
870{
871 __sysfs_remove_dir(shadow);
872}
873
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800874const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 .open = sysfs_dir_open,
876 .release = sysfs_dir_close,
877 .llseek = sysfs_dir_lseek,
878 .read = generic_read_dir,
879 .readdir = sysfs_readdir,
880};