blob: 5d50e1ddfbd16d188bec1dd0a351cc5a184d654d [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{
Tejun Heo13b30862007-06-14 03:45:14 +090050 struct sysfs_dirent *parent_sd;
51
52 repeat:
53 parent_sd = sd->s_parent;
54
Tejun Heofa7f9122007-06-14 03:45:13 +090055 if (sd->s_type & SYSFS_KOBJ_LINK) {
56 struct sysfs_symlink * sl = sd->s_element;
57 kfree(sl->link_name);
58 kobject_put(sl->target_kobj);
59 kfree(sl);
60 }
61 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +090062 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +090063 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +090064
65 sd = parent_sd;
66 if (sd && atomic_dec_and_test(&sd->s_count))
67 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +090068}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
71{
72 struct sysfs_dirent * sd = dentry->d_fsdata;
73
74 if (sd) {
Tejun Heodd14cbc2007-06-11 14:04:01 +090075 /* sd->s_dentry is protected with sysfs_lock. This
76 * allows sysfs_drop_dentry() to dereference it.
77 */
78 spin_lock(&sysfs_lock);
79
80 /* The dentry might have been deleted or another
81 * lookup could have happened updating sd->s_dentry to
82 * point the new dentry. Ignore if it isn't pointing
83 * to this dentry.
84 */
85 if (sd->s_dentry == dentry)
86 sd->s_dentry = NULL;
87 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 sysfs_put(sd);
89 }
90 iput(inode);
91}
92
93static struct dentry_operations sysfs_dentry_ops = {
94 .d_iput = sysfs_d_iput,
95};
96
Tejun Heoa26cd722007-06-14 03:45:14 +090097struct sysfs_dirent *sysfs_new_dirent(void *element, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct sysfs_dirent * sd;
100
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800101 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!sd)
103 return NULL;
104
Tejun Heo2b611bb2007-06-14 03:45:13 +0900105 if (sysfs_alloc_ino(&sd->s_ino)) {
106 kmem_cache_free(sysfs_dir_cachep, sd);
107 return NULL;
108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 atomic_set(&sd->s_count, 1);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300111 atomic_set(&sd->s_event, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 INIT_LIST_HEAD(&sd->s_children);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700113 INIT_LIST_HEAD(&sd->s_sibling);
Tejun Heoa26cd722007-06-14 03:45:14 +0900114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 sd->s_element = element;
Tejun Heoa26cd722007-06-14 03:45:14 +0900116 sd->s_mode = mode;
117 sd->s_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 return sd;
120}
121
Tejun Heoa26cd722007-06-14 03:45:14 +0900122void sysfs_attach_dirent(struct sysfs_dirent *sd,
123 struct sysfs_dirent *parent_sd, struct dentry *dentry)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700124{
Tejun Heoa26cd722007-06-14 03:45:14 +0900125 if (dentry) {
126 sd->s_dentry = dentry;
127 dentry->d_fsdata = sysfs_get(sd);
128 dentry->d_op = &sysfs_dentry_ops;
129 }
130
Tejun Heo13b30862007-06-14 03:45:14 +0900131 if (parent_sd) {
132 sd->s_parent = sysfs_get(parent_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700133 list_add(&sd->s_sibling, &parent_sd->s_children);
Tejun Heo13b30862007-06-14 03:45:14 +0900134 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700135}
136
Martin Waitza5802902006-04-02 13:59:55 +0200137/*
Maneesh Sonic5168652006-03-09 19:40:14 +0530138 *
139 * Return -EEXIST if there is already a sysfs element with the same name for
140 * the same parent.
141 *
142 * called with parent inode's i_mutex held
143 */
144int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
145 const unsigned char *new)
146{
147 struct sysfs_dirent * sd;
148
149 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
150 if (sd->s_element) {
151 const unsigned char *existing = sysfs_get_name(sd);
152 if (strcmp(existing, new))
153 continue;
154 else
155 return -EEXIST;
156 }
157 }
158
159 return 0;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static int init_dir(struct inode * inode)
163{
164 inode->i_op = &sysfs_dir_inode_operations;
165 inode->i_fop = &sysfs_dir_operations;
166
167 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700168 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170}
171
172static int init_file(struct inode * inode)
173{
174 inode->i_size = PAGE_SIZE;
175 inode->i_fop = &sysfs_file_operations;
176 return 0;
177}
178
179static int init_symlink(struct inode * inode)
180{
181 inode->i_op = &sysfs_symlink_inode_operations;
182 return 0;
183}
184
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900185static int create_dir(struct kobject *kobj, struct dentry *parent,
186 const char *name, struct dentry **p_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int error;
189 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900190 struct dentry *dentry;
191 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900193 mutex_lock(&parent->d_inode->i_mutex);
194
195 dentry = lookup_one_len(name, parent, strlen(name));
196 if (IS_ERR(dentry)) {
197 error = PTR_ERR(dentry);
198 goto out_unlock;
199 }
200
201 error = -EEXIST;
202 if (sysfs_dirent_exist(parent->d_fsdata, name))
203 goto out_dput;
204
Tejun Heoa26cd722007-06-14 03:45:14 +0900205 error = -ENOMEM;
206 sd = sysfs_new_dirent(kobj, mode, SYSFS_DIR);
207 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900208 goto out_drop;
Tejun Heoa26cd722007-06-14 03:45:14 +0900209 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900210
211 error = sysfs_create(dentry, mode, init_dir);
212 if (error)
213 goto out_sput;
214
215 inc_nlink(parent->d_inode);
216 dentry->d_op = &sysfs_dentry_ops;
217 d_rehash(dentry);
218
219 *p_dentry = dentry;
220 error = 0;
221 goto out_dput;
222
223 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900224 list_del_init(&sd->s_sibling);
225 sysfs_put(sd);
226 out_drop:
227 d_drop(dentry);
228 out_dput:
229 dput(dentry);
230 out_unlock:
231 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return error;
233}
234
235
236int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
237{
238 return create_dir(k,k->dentry,n,d);
239}
240
241/**
242 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700244 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700247int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct dentry * dentry = NULL;
250 struct dentry * parent;
251 int error = 0;
252
253 BUG_ON(!kobj);
254
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700255 if (shadow_parent)
256 parent = shadow_parent;
257 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 parent = kobj->parent->dentry;
259 else if (sysfs_mount && sysfs_mount->mnt_sb)
260 parent = sysfs_mount->mnt_sb->s_root;
261 else
262 return -EFAULT;
263
264 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
265 if (!error)
266 kobj->dentry = dentry;
267 return error;
268}
269
270/* attaches attribute's sysfs_dirent to the dentry corresponding to the
271 * attribute file
272 */
273static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
274{
275 struct attribute * attr = NULL;
276 struct bin_attribute * bin_attr = NULL;
277 int (* init) (struct inode *) = NULL;
278 int error = 0;
279
280 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
281 bin_attr = sd->s_element;
282 attr = &bin_attr->attr;
283 } else {
284 attr = sd->s_element;
285 init = init_file;
286 }
287
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530288 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900289 /* protect sd->s_dentry against sysfs_d_iput */
290 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530291 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900292 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530294 if (error) {
295 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return error;
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 if (bin_attr) {
300 dentry->d_inode->i_size = bin_attr->size;
301 dentry->d_inode->i_fop = &bin_fops;
302 }
303 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 d_rehash(dentry);
305
306 return 0;
307}
308
309static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
310{
311 int err = 0;
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 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
319 if (!err) {
320 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 d_rehash(dentry);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530322 } else
323 sysfs_put(sd);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return err;
326}
327
328static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
329 struct nameidata *nd)
330{
331 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
332 struct sysfs_dirent * sd;
333 int err = 0;
334
335 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
336 if (sd->s_type & SYSFS_NOT_PINNED) {
337 const unsigned char * name = sysfs_get_name(sd);
338
339 if (strcmp(name, dentry->d_name.name))
340 continue;
341
342 if (sd->s_type & SYSFS_KOBJ_LINK)
343 err = sysfs_attach_link(sd, dentry);
344 else
345 err = sysfs_attach_attr(sd, dentry);
346 break;
347 }
348 }
349
350 return ERR_PTR(err);
351}
352
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800353const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530355 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356};
357
358static void remove_dir(struct dentry * d)
359{
360 struct dentry * parent = dget(d->d_parent);
361 struct sysfs_dirent * sd;
362
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800363 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 d_delete(d);
365 sd = d->d_fsdata;
366 list_del_init(&sd->s_sibling);
367 sysfs_put(sd);
368 if (d->d_inode)
369 simple_rmdir(parent->d_inode,d);
370
371 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
372 atomic_read(&d->d_count));
373
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800374 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 dput(parent);
376}
377
378void sysfs_remove_subdir(struct dentry * d)
379{
380 remove_dir(d);
381}
382
383
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700384static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct sysfs_dirent * parent_sd;
387 struct sysfs_dirent * sd, * tmp;
388
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700389 dget(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!dentry)
391 return;
392
393 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800394 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 parent_sd = dentry->d_fsdata;
396 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
397 if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
398 continue;
399 list_del_init(&sd->s_sibling);
400 sysfs_drop_dentry(sd, dentry);
401 sysfs_put(sd);
402 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800403 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 remove_dir(dentry);
406 /**
407 * Drop reference from dget() on entrance.
408 */
409 dput(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700410}
411
412/**
413 * sysfs_remove_dir - remove an object's directory.
414 * @kobj: object.
415 *
416 * The only thing special about this is that we remove any files in
417 * the directory before we remove the directory, and we've inlined
418 * what used to be sysfs_rmdir() below, instead of calling separately.
419 */
420
421void sysfs_remove_dir(struct kobject * kobj)
422{
423 __sysfs_remove_dir(kobj->dentry);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800424 kobj->dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700427int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
428 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Tejun Heo996b7372007-06-14 03:45:14 +0900430 int error;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700431 struct dentry * new_dentry;
Tejun Heo996b7372007-06-14 03:45:14 +0900432 struct sysfs_dirent *sd, *parent_sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700434 if (!new_parent)
435 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700438 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700440 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900441 if (IS_ERR(new_dentry)) {
442 error = PTR_ERR(new_dentry);
443 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Tejun Heo996b7372007-06-14 03:45:14 +0900445
446 /* By allowing two different directories with the same
447 * d_parent we allow this routine to move between different
448 * shadows of the same directory
449 */
450 error = -EINVAL;
451 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
452 new_dentry->d_parent->d_inode != new_parent->d_inode ||
453 new_dentry == kobj->dentry)
454 goto out_dput;
455
456 error = -EEXIST;
457 if (new_dentry->d_inode)
458 goto out_dput;
459
460 error = kobject_set_name(kobj, "%s", new_name);
461 if (error)
462 goto out_drop;
463
464 d_add(new_dentry, NULL);
465 d_move(kobj->dentry, new_dentry);
466
467 sd = kobj->dentry->d_fsdata;
468 parent_sd = new_parent->d_fsdata;
469
470 list_del_init(&sd->s_sibling);
471 list_add(&sd->s_sibling, &parent_sd->s_children);
472
473 error = 0;
474 goto out_unlock;
475
476 out_drop:
477 d_drop(new_dentry);
478 out_dput:
479 dput(new_dentry);
480 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700481 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 up_write(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return error;
484}
485
Cornelia Huck8a824722006-11-20 17:07:51 +0100486int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
487{
488 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
489 struct sysfs_dirent *new_parent_sd, *sd;
490 int error;
491
Cornelia Huck8a824722006-11-20 17:07:51 +0100492 old_parent_dentry = kobj->parent ?
493 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aea2007-01-08 20:16:44 +0100494 new_parent_dentry = new_parent ?
495 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100496
Mark Lord0de15172007-03-06 01:42:03 -0800497 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
498 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100499again:
500 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
501 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
502 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
503 goto again;
504 }
505
506 new_parent_sd = new_parent_dentry->d_fsdata;
507 sd = kobj->dentry->d_fsdata;
508
509 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
510 strlen(kobj->name));
511 if (IS_ERR(new_dentry)) {
512 error = PTR_ERR(new_dentry);
513 goto out;
514 } else
515 error = 0;
516 d_add(new_dentry, NULL);
517 d_move(kobj->dentry, new_dentry);
518 dput(new_dentry);
519
520 /* Remove from old parent's list and insert into new parent's list. */
521 list_del_init(&sd->s_sibling);
522 list_add(&sd->s_sibling, &new_parent_sd->s_children);
523
524out:
525 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
526 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
527
528 return error;
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531static int sysfs_dir_open(struct inode *inode, struct file *file)
532{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800533 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900535 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800537 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heoa26cd722007-06-14 03:45:14 +0900538 sd = sysfs_new_dirent(NULL, 0, 0);
539 if (sd)
540 sysfs_attach_dirent(sd, parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800541 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Tejun Heoa26cd722007-06-14 03:45:14 +0900543 file->private_data = sd;
544 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static int sysfs_dir_close(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 * cursor = file->private_data;
551
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800552 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800554 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 release_sysfs_dirent(cursor);
557
558 return 0;
559}
560
561/* Relationship between s_mode and the DT_xxx types */
562static inline unsigned char dt_type(struct sysfs_dirent *sd)
563{
564 return (sd->s_mode >> 12) & 15;
565}
566
567static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
568{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800569 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
571 struct sysfs_dirent *cursor = filp->private_data;
572 struct list_head *p, *q = &cursor->s_sibling;
573 ino_t ino;
574 int i = filp->f_pos;
575
576 switch (i) {
577 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900578 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
580 break;
581 filp->f_pos++;
582 i++;
583 /* fallthrough */
584 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900585 if (parent_sd->s_parent)
586 ino = parent_sd->s_parent->s_ino;
587 else
588 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
590 break;
591 filp->f_pos++;
592 i++;
593 /* fallthrough */
594 default:
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700595 if (filp->f_pos == 2)
596 list_move(q, &parent_sd->s_children);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
599 struct sysfs_dirent *next;
600 const char * name;
601 int len;
602
603 next = list_entry(p, struct sysfs_dirent,
604 s_sibling);
605 if (!next->s_element)
606 continue;
607
608 name = sysfs_get_name(next);
609 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900610 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (filldir(dirent, name, len, filp->f_pos, ino,
613 dt_type(next)) < 0)
614 return 0;
615
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700616 list_move(q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 p = q;
618 filp->f_pos++;
619 }
620 }
621 return 0;
622}
623
624static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
625{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800626 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800628 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 switch (origin) {
630 case 1:
631 offset += file->f_pos;
632 case 0:
633 if (offset >= 0)
634 break;
635 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800636 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return -EINVAL;
638 }
639 if (offset != file->f_pos) {
640 file->f_pos = offset;
641 if (file->f_pos >= 2) {
642 struct sysfs_dirent *sd = dentry->d_fsdata;
643 struct sysfs_dirent *cursor = file->private_data;
644 struct list_head *p;
645 loff_t n = file->f_pos - 2;
646
647 list_del(&cursor->s_sibling);
648 p = sd->s_children.next;
649 while (n && p != &sd->s_children) {
650 struct sysfs_dirent *next;
651 next = list_entry(p, struct sysfs_dirent,
652 s_sibling);
653 if (next->s_element)
654 n--;
655 p = p->next;
656 }
657 list_add_tail(&cursor->s_sibling, p);
658 }
659 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800660 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return offset;
662}
663
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700664
665/**
666 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
667 * @kobj: object we're creating shadow of.
668 */
669
670int sysfs_make_shadowed_dir(struct kobject *kobj,
671 void * (*follow_link)(struct dentry *, struct nameidata *))
672{
673 struct inode *inode;
674 struct inode_operations *i_op;
675
676 inode = kobj->dentry->d_inode;
677 if (inode->i_op != &sysfs_dir_inode_operations)
678 return -EINVAL;
679
680 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
681 if (!i_op)
682 return -ENOMEM;
683
684 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
685 i_op->follow_link = follow_link;
686
687 /* Locking of inode->i_op?
688 * Since setting i_op is a single word write and they
689 * are atomic we should be ok here.
690 */
691 inode->i_op = i_op;
692 return 0;
693}
694
695/**
696 * sysfs_create_shadow_dir - create a shadow directory for an object.
697 * @kobj: object we're creating directory for.
698 *
699 * sysfs_make_shadowed_dir must already have been called on this
700 * directory.
701 */
702
703struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
704{
Tejun Heo13b30862007-06-14 03:45:14 +0900705 struct dentry *dir = kobj->dentry;
706 struct inode *inode = dir->d_inode;
707 struct dentry *parent = dir->d_parent;
708 struct sysfs_dirent *parent_sd = parent->d_fsdata;
709 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700710 struct sysfs_dirent *sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700711
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700712 shadow = ERR_PTR(-EINVAL);
713 if (!sysfs_is_shadowed_inode(inode))
714 goto out;
715
716 shadow = d_alloc(parent, &dir->d_name);
717 if (!shadow)
718 goto nomem;
719
Tejun Heoa26cd722007-06-14 03:45:14 +0900720 sd = sysfs_new_dirent(kobj, inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700721 if (!sd)
722 goto nomem;
Tejun Heo13b30862007-06-14 03:45:14 +0900723 /* point to parent_sd but don't attach to it */
724 sd->s_parent = sysfs_get(parent_sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900725 sysfs_attach_dirent(sd, NULL, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700726
727 d_instantiate(shadow, igrab(inode));
728 inc_nlink(inode);
729 inc_nlink(parent->d_inode);
730 shadow->d_op = &sysfs_dentry_ops;
731
732 dget(shadow); /* Extra count - pin the dentry in core */
733
734out:
735 return shadow;
736nomem:
737 dput(shadow);
738 shadow = ERR_PTR(-ENOMEM);
739 goto out;
740}
741
742/**
743 * sysfs_remove_shadow_dir - remove an object's directory.
744 * @shadow: dentry of shadow directory
745 *
746 * The only thing special about this is that we remove any files in
747 * the directory before we remove the directory, and we've inlined
748 * what used to be sysfs_rmdir() below, instead of calling separately.
749 */
750
751void sysfs_remove_shadow_dir(struct dentry *shadow)
752{
753 __sysfs_remove_dir(shadow);
754}
755
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800756const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .open = sysfs_dir_open,
758 .release = sysfs_dir_close,
759 .llseek = sysfs_dir_lseek,
760 .read = generic_read_dir,
761 .readdir = sysfs_readdir,
762};