blob: b4c482461403dabb1ffdbe8b1f10f884767cd97b [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>
Oliver Neukum94bebf42006-12-20 10:52:44 +010013#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "sysfs.h"
15
16DECLARE_RWSEM(sysfs_rename_sem);
Tejun Heodd14cbc2007-06-11 14:04:01 +090017spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Tejun Heo2b611bb2007-06-14 03:45:13 +090019static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
20static DEFINE_IDA(sysfs_ino_ida);
21
22int sysfs_alloc_ino(ino_t *pino)
23{
24 int ino, rc;
25
26 retry:
27 spin_lock(&sysfs_ino_lock);
28 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
29 spin_unlock(&sysfs_ino_lock);
30
31 if (rc == -EAGAIN) {
32 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
33 goto retry;
34 rc = -ENOMEM;
35 }
36
37 *pino = ino;
38 return rc;
39}
40
41static void sysfs_free_ino(ino_t ino)
42{
43 spin_lock(&sysfs_ino_lock);
44 ida_remove(&sysfs_ino_ida, ino);
45 spin_unlock(&sysfs_ino_lock);
46}
47
Tejun Heofa7f9122007-06-14 03:45:13 +090048void release_sysfs_dirent(struct sysfs_dirent * sd)
49{
50 if (sd->s_type & SYSFS_KOBJ_LINK) {
51 struct sysfs_symlink * sl = sd->s_element;
52 kfree(sl->link_name);
53 kobject_put(sl->target_kobj);
54 kfree(sl);
55 }
56 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +090057 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +090058 kmem_cache_free(sysfs_dir_cachep, sd);
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
62{
63 struct sysfs_dirent * sd = dentry->d_fsdata;
64
65 if (sd) {
Tejun Heodd14cbc2007-06-11 14:04:01 +090066 /* sd->s_dentry is protected with sysfs_lock. This
67 * allows sysfs_drop_dentry() to dereference it.
68 */
69 spin_lock(&sysfs_lock);
70
71 /* The dentry might have been deleted or another
72 * lookup could have happened updating sd->s_dentry to
73 * point the new dentry. Ignore if it isn't pointing
74 * to this dentry.
75 */
76 if (sd->s_dentry == dentry)
77 sd->s_dentry = NULL;
78 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 sysfs_put(sd);
80 }
81 iput(inode);
82}
83
84static struct dentry_operations sysfs_dentry_ops = {
85 .d_iput = sysfs_d_iput,
86};
87
88/*
89 * Allocates a new sysfs_dirent and links it to the parent sysfs_dirent
90 */
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070091static struct sysfs_dirent * __sysfs_new_dirent(void * element)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct sysfs_dirent * sd;
94
Robert P. J. Dayc3762222007-02-10 01:45:03 -080095 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (!sd)
97 return NULL;
98
Tejun Heo2b611bb2007-06-14 03:45:13 +090099 if (sysfs_alloc_ino(&sd->s_ino)) {
100 kmem_cache_free(sysfs_dir_cachep, sd);
101 return NULL;
102 }
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 atomic_set(&sd->s_count, 1);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300105 atomic_set(&sd->s_event, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 INIT_LIST_HEAD(&sd->s_children);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700107 INIT_LIST_HEAD(&sd->s_sibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 sd->s_element = element;
109
110 return sd;
111}
112
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700113static void __sysfs_list_dirent(struct sysfs_dirent *parent_sd,
114 struct sysfs_dirent *sd)
115{
116 if (sd)
117 list_add(&sd->s_sibling, &parent_sd->s_children);
118}
119
120static struct sysfs_dirent * sysfs_new_dirent(struct sysfs_dirent *parent_sd,
121 void * element)
122{
123 struct sysfs_dirent *sd;
124 sd = __sysfs_new_dirent(element);
125 __sysfs_list_dirent(parent_sd, sd);
126 return sd;
127}
128
Martin Waitza5802902006-04-02 13:59:55 +0200129/*
Maneesh Sonic5168652006-03-09 19:40:14 +0530130 *
131 * Return -EEXIST if there is already a sysfs element with the same name for
132 * the same parent.
133 *
134 * called with parent inode's i_mutex held
135 */
136int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
137 const unsigned char *new)
138{
139 struct sysfs_dirent * sd;
140
141 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
142 if (sd->s_element) {
143 const unsigned char *existing = sysfs_get_name(sd);
144 if (strcmp(existing, new))
145 continue;
146 else
147 return -EEXIST;
148 }
149 }
150
151 return 0;
152}
153
154
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700155static struct sysfs_dirent *
156__sysfs_make_dirent(struct dentry *dentry, void *element, mode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct sysfs_dirent * sd;
159
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700160 sd = __sysfs_new_dirent(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700162 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 sd->s_mode = mode;
165 sd->s_type = type;
166 sd->s_dentry = dentry;
167 if (dentry) {
168 dentry->d_fsdata = sysfs_get(sd);
169 dentry->d_op = &sysfs_dentry_ops;
170 }
171
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700172out:
173 return sd;
174}
175
176int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry,
177 void * element, umode_t mode, int type)
178{
179 struct sysfs_dirent *sd;
180
181 sd = __sysfs_make_dirent(dentry, element, mode, type);
182 __sysfs_list_dirent(parent_sd, sd);
183
184 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static int init_dir(struct inode * inode)
188{
189 inode->i_op = &sysfs_dir_inode_operations;
190 inode->i_fop = &sysfs_dir_operations;
191
192 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700193 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return 0;
195}
196
197static int init_file(struct inode * inode)
198{
199 inode->i_size = PAGE_SIZE;
200 inode->i_fop = &sysfs_file_operations;
201 return 0;
202}
203
204static int init_symlink(struct inode * inode)
205{
206 inode->i_op = &sysfs_symlink_inode_operations;
207 return 0;
208}
209
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900210static int create_dir(struct kobject *kobj, struct dentry *parent,
211 const char *name, struct dentry **p_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 int error;
214 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900215 struct dentry *dentry;
216 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900218 mutex_lock(&parent->d_inode->i_mutex);
219
220 dentry = lookup_one_len(name, parent, strlen(name));
221 if (IS_ERR(dentry)) {
222 error = PTR_ERR(dentry);
223 goto out_unlock;
224 }
225
226 error = -EEXIST;
227 if (sysfs_dirent_exist(parent->d_fsdata, name))
228 goto out_dput;
229
230 error = sysfs_make_dirent(parent->d_fsdata, dentry, kobj, mode,
231 SYSFS_DIR);
232 if (error)
233 goto out_drop;
234
235 error = sysfs_create(dentry, mode, init_dir);
236 if (error)
237 goto out_sput;
238
239 inc_nlink(parent->d_inode);
240 dentry->d_op = &sysfs_dentry_ops;
241 d_rehash(dentry);
242
243 *p_dentry = dentry;
244 error = 0;
245 goto out_dput;
246
247 out_sput:
248 sd = dentry->d_fsdata;
249 list_del_init(&sd->s_sibling);
250 sysfs_put(sd);
251 out_drop:
252 d_drop(dentry);
253 out_dput:
254 dput(dentry);
255 out_unlock:
256 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return error;
258}
259
260
261int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
262{
263 return create_dir(k,k->dentry,n,d);
264}
265
266/**
267 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700269 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 */
271
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700272int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct dentry * dentry = NULL;
275 struct dentry * parent;
276 int error = 0;
277
278 BUG_ON(!kobj);
279
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700280 if (shadow_parent)
281 parent = shadow_parent;
282 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 parent = kobj->parent->dentry;
284 else if (sysfs_mount && sysfs_mount->mnt_sb)
285 parent = sysfs_mount->mnt_sb->s_root;
286 else
287 return -EFAULT;
288
289 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
290 if (!error)
291 kobj->dentry = dentry;
292 return error;
293}
294
295/* attaches attribute's sysfs_dirent to the dentry corresponding to the
296 * attribute file
297 */
298static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
299{
300 struct attribute * attr = NULL;
301 struct bin_attribute * bin_attr = NULL;
302 int (* init) (struct inode *) = NULL;
303 int error = 0;
304
305 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
306 bin_attr = sd->s_element;
307 attr = &bin_attr->attr;
308 } else {
309 attr = sd->s_element;
310 init = init_file;
311 }
312
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530313 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900314 /* protect sd->s_dentry against sysfs_d_iput */
315 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530316 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900317 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530319 if (error) {
320 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return error;
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (bin_attr) {
325 dentry->d_inode->i_size = bin_attr->size;
326 dentry->d_inode->i_fop = &bin_fops;
327 }
328 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 d_rehash(dentry);
330
331 return 0;
332}
333
334static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
335{
336 int err = 0;
337
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530338 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900339 /* protect sd->s_dentry against sysfs_d_iput */
340 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530341 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900342 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
344 if (!err) {
345 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 d_rehash(dentry);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530347 } else
348 sysfs_put(sd);
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return err;
351}
352
353static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
354 struct nameidata *nd)
355{
356 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
357 struct sysfs_dirent * sd;
358 int err = 0;
359
360 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
361 if (sd->s_type & SYSFS_NOT_PINNED) {
362 const unsigned char * name = sysfs_get_name(sd);
363
364 if (strcmp(name, dentry->d_name.name))
365 continue;
366
367 if (sd->s_type & SYSFS_KOBJ_LINK)
368 err = sysfs_attach_link(sd, dentry);
369 else
370 err = sysfs_attach_attr(sd, dentry);
371 break;
372 }
373 }
374
375 return ERR_PTR(err);
376}
377
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800378const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530380 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381};
382
383static void remove_dir(struct dentry * d)
384{
385 struct dentry * parent = dget(d->d_parent);
386 struct sysfs_dirent * sd;
387
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800388 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 d_delete(d);
390 sd = d->d_fsdata;
391 list_del_init(&sd->s_sibling);
392 sysfs_put(sd);
393 if (d->d_inode)
394 simple_rmdir(parent->d_inode,d);
395
396 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
397 atomic_read(&d->d_count));
398
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800399 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dput(parent);
401}
402
403void sysfs_remove_subdir(struct dentry * d)
404{
405 remove_dir(d);
406}
407
408
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700409static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct sysfs_dirent * parent_sd;
412 struct sysfs_dirent * sd, * tmp;
413
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700414 dget(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!dentry)
416 return;
417
418 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800419 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 parent_sd = dentry->d_fsdata;
421 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
422 if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
423 continue;
424 list_del_init(&sd->s_sibling);
425 sysfs_drop_dentry(sd, dentry);
426 sysfs_put(sd);
427 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800428 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 remove_dir(dentry);
431 /**
432 * Drop reference from dget() on entrance.
433 */
434 dput(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700435}
436
437/**
438 * sysfs_remove_dir - remove an object's directory.
439 * @kobj: object.
440 *
441 * The only thing special about this is that we remove any files in
442 * the directory before we remove the directory, and we've inlined
443 * what used to be sysfs_rmdir() below, instead of calling separately.
444 */
445
446void sysfs_remove_dir(struct kobject * kobj)
447{
448 __sysfs_remove_dir(kobj->dentry);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800449 kobj->dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700452int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
453 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int error = 0;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700456 struct dentry * new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700458 if (!new_parent)
459 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700462 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700464 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!IS_ERR(new_dentry)) {
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700466 /* By allowing two different directories with the
467 * same d_parent we allow this routine to move
468 * between different shadows of the same directory
469 */
470 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode)
471 return -EINVAL;
472 else if (new_dentry->d_parent->d_inode != new_parent->d_inode)
473 error = -EINVAL;
474 else if (new_dentry == kobj->dentry)
475 error = -EINVAL;
476 else if (!new_dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 error = kobject_set_name(kobj, "%s", new_name);
478 if (!error) {
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700479 struct sysfs_dirent *sd, *parent_sd;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 d_add(new_dentry, NULL);
482 d_move(kobj->dentry, new_dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700483
484 sd = kobj->dentry->d_fsdata;
485 parent_sd = new_parent->d_fsdata;
486
487 list_del_init(&sd->s_sibling);
488 list_add(&sd->s_sibling, &parent_sd->s_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 else
491 d_drop(new_dentry);
492 } else
493 error = -EEXIST;
494 dput(new_dentry);
495 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700496 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 up_write(&sysfs_rename_sem);
498
499 return error;
500}
501
Cornelia Huck8a824722006-11-20 17:07:51 +0100502int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
503{
504 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
505 struct sysfs_dirent *new_parent_sd, *sd;
506 int error;
507
Cornelia Huck8a824722006-11-20 17:07:51 +0100508 old_parent_dentry = kobj->parent ?
509 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100510 new_parent_dentry = new_parent ?
511 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100512
Mark Lord0de15172007-03-06 01:42:03 -0800513 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
514 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100515again:
516 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
517 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
518 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
519 goto again;
520 }
521
522 new_parent_sd = new_parent_dentry->d_fsdata;
523 sd = kobj->dentry->d_fsdata;
524
525 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
526 strlen(kobj->name));
527 if (IS_ERR(new_dentry)) {
528 error = PTR_ERR(new_dentry);
529 goto out;
530 } else
531 error = 0;
532 d_add(new_dentry, NULL);
533 d_move(kobj->dentry, new_dentry);
534 dput(new_dentry);
535
536 /* Remove from old parent's list and insert into new parent's list. */
537 list_del_init(&sd->s_sibling);
538 list_add(&sd->s_sibling, &new_parent_sd->s_children);
539
540out:
541 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
542 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
543
544 return error;
545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static int sysfs_dir_open(struct inode *inode, struct file *file)
548{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800549 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
551
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800552 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 file->private_data = sysfs_new_dirent(parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800554 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 return file->private_data ? 0 : -ENOMEM;
557
558}
559
560static int sysfs_dir_close(struct inode *inode, struct file *file)
561{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800562 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct sysfs_dirent * cursor = file->private_data;
564
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800565 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800567 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 release_sysfs_dirent(cursor);
570
571 return 0;
572}
573
574/* Relationship between s_mode and the DT_xxx types */
575static inline unsigned char dt_type(struct sysfs_dirent *sd)
576{
577 return (sd->s_mode >> 12) & 15;
578}
579
580static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
581{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800582 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
584 struct sysfs_dirent *cursor = filp->private_data;
585 struct list_head *p, *q = &cursor->s_sibling;
586 ino_t ino;
587 int i = filp->f_pos;
588
589 switch (i) {
590 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900591 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
593 break;
594 filp->f_pos++;
595 i++;
596 /* fallthrough */
597 case 1:
598 ino = parent_ino(dentry);
599 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
600 break;
601 filp->f_pos++;
602 i++;
603 /* fallthrough */
604 default:
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700605 if (filp->f_pos == 2)
606 list_move(q, &parent_sd->s_children);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
609 struct sysfs_dirent *next;
610 const char * name;
611 int len;
612
613 next = list_entry(p, struct sysfs_dirent,
614 s_sibling);
615 if (!next->s_element)
616 continue;
617
618 name = sysfs_get_name(next);
619 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900620 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (filldir(dirent, name, len, filp->f_pos, ino,
623 dt_type(next)) < 0)
624 return 0;
625
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700626 list_move(q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 p = q;
628 filp->f_pos++;
629 }
630 }
631 return 0;
632}
633
634static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
635{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800636 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800638 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 switch (origin) {
640 case 1:
641 offset += file->f_pos;
642 case 0:
643 if (offset >= 0)
644 break;
645 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800646 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return -EINVAL;
648 }
649 if (offset != file->f_pos) {
650 file->f_pos = offset;
651 if (file->f_pos >= 2) {
652 struct sysfs_dirent *sd = dentry->d_fsdata;
653 struct sysfs_dirent *cursor = file->private_data;
654 struct list_head *p;
655 loff_t n = file->f_pos - 2;
656
657 list_del(&cursor->s_sibling);
658 p = sd->s_children.next;
659 while (n && p != &sd->s_children) {
660 struct sysfs_dirent *next;
661 next = list_entry(p, struct sysfs_dirent,
662 s_sibling);
663 if (next->s_element)
664 n--;
665 p = p->next;
666 }
667 list_add_tail(&cursor->s_sibling, p);
668 }
669 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800670 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return offset;
672}
673
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700674
675/**
676 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
677 * @kobj: object we're creating shadow of.
678 */
679
680int sysfs_make_shadowed_dir(struct kobject *kobj,
681 void * (*follow_link)(struct dentry *, struct nameidata *))
682{
683 struct inode *inode;
684 struct inode_operations *i_op;
685
686 inode = kobj->dentry->d_inode;
687 if (inode->i_op != &sysfs_dir_inode_operations)
688 return -EINVAL;
689
690 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
691 if (!i_op)
692 return -ENOMEM;
693
694 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
695 i_op->follow_link = follow_link;
696
697 /* Locking of inode->i_op?
698 * Since setting i_op is a single word write and they
699 * are atomic we should be ok here.
700 */
701 inode->i_op = i_op;
702 return 0;
703}
704
705/**
706 * sysfs_create_shadow_dir - create a shadow directory for an object.
707 * @kobj: object we're creating directory for.
708 *
709 * sysfs_make_shadowed_dir must already have been called on this
710 * directory.
711 */
712
713struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
714{
715 struct sysfs_dirent *sd;
716 struct dentry *parent, *dir, *shadow;
717 struct inode *inode;
718
719 dir = kobj->dentry;
720 inode = dir->d_inode;
721 parent = dir->d_parent;
722 shadow = ERR_PTR(-EINVAL);
723 if (!sysfs_is_shadowed_inode(inode))
724 goto out;
725
726 shadow = d_alloc(parent, &dir->d_name);
727 if (!shadow)
728 goto nomem;
729
730 sd = __sysfs_make_dirent(shadow, kobj, inode->i_mode, SYSFS_DIR);
731 if (!sd)
732 goto nomem;
733
734 d_instantiate(shadow, igrab(inode));
735 inc_nlink(inode);
736 inc_nlink(parent->d_inode);
737 shadow->d_op = &sysfs_dentry_ops;
738
739 dget(shadow); /* Extra count - pin the dentry in core */
740
741out:
742 return shadow;
743nomem:
744 dput(shadow);
745 shadow = ERR_PTR(-ENOMEM);
746 goto out;
747}
748
749/**
750 * sysfs_remove_shadow_dir - remove an object's directory.
751 * @shadow: dentry of shadow directory
752 *
753 * The only thing special about this is that we remove any files in
754 * the directory before we remove the directory, and we've inlined
755 * what used to be sysfs_rmdir() below, instead of calling separately.
756 */
757
758void sysfs_remove_shadow_dir(struct dentry *shadow)
759{
760 __sysfs_remove_dir(shadow);
761}
762
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800763const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 .open = sysfs_dir_open,
765 .release = sysfs_dir_close,
766 .llseek = sysfs_dir_lseek,
767 .read = generic_read_dir,
768 .readdir = sysfs_readdir,
769};