blob: f16aa7e3eafc6a62ef0758720d1a2a37cc02a2fd [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
Tejun Heoa26cd722007-06-14 03:45:14 +090088struct sysfs_dirent *sysfs_new_dirent(void *element, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct sysfs_dirent * sd;
91
Robert P. J. Dayc3762222007-02-10 01:45:03 -080092 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (!sd)
94 return NULL;
95
Tejun Heo2b611bb2007-06-14 03:45:13 +090096 if (sysfs_alloc_ino(&sd->s_ino)) {
97 kmem_cache_free(sysfs_dir_cachep, sd);
98 return NULL;
99 }
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 atomic_set(&sd->s_count, 1);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300102 atomic_set(&sd->s_event, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 INIT_LIST_HEAD(&sd->s_children);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700104 INIT_LIST_HEAD(&sd->s_sibling);
Tejun Heoa26cd722007-06-14 03:45:14 +0900105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 sd->s_element = element;
Tejun Heoa26cd722007-06-14 03:45:14 +0900107 sd->s_mode = mode;
108 sd->s_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 return sd;
111}
112
Tejun Heoa26cd722007-06-14 03:45:14 +0900113void sysfs_attach_dirent(struct sysfs_dirent *sd,
114 struct sysfs_dirent *parent_sd, struct dentry *dentry)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700115{
Tejun Heoa26cd722007-06-14 03:45:14 +0900116 if (dentry) {
117 sd->s_dentry = dentry;
118 dentry->d_fsdata = sysfs_get(sd);
119 dentry->d_op = &sysfs_dentry_ops;
120 }
121
122 if (parent_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700123 list_add(&sd->s_sibling, &parent_sd->s_children);
124}
125
Martin Waitza5802902006-04-02 13:59:55 +0200126/*
Maneesh Sonic5168652006-03-09 19:40:14 +0530127 *
128 * Return -EEXIST if there is already a sysfs element with the same name for
129 * the same parent.
130 *
131 * called with parent inode's i_mutex held
132 */
133int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
134 const unsigned char *new)
135{
136 struct sysfs_dirent * sd;
137
138 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
139 if (sd->s_element) {
140 const unsigned char *existing = sysfs_get_name(sd);
141 if (strcmp(existing, new))
142 continue;
143 else
144 return -EEXIST;
145 }
146 }
147
148 return 0;
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int init_dir(struct inode * inode)
152{
153 inode->i_op = &sysfs_dir_inode_operations;
154 inode->i_fop = &sysfs_dir_operations;
155
156 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700157 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
159}
160
161static int init_file(struct inode * inode)
162{
163 inode->i_size = PAGE_SIZE;
164 inode->i_fop = &sysfs_file_operations;
165 return 0;
166}
167
168static int init_symlink(struct inode * inode)
169{
170 inode->i_op = &sysfs_symlink_inode_operations;
171 return 0;
172}
173
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900174static int create_dir(struct kobject *kobj, struct dentry *parent,
175 const char *name, struct dentry **p_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 int error;
178 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900179 struct dentry *dentry;
180 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900182 mutex_lock(&parent->d_inode->i_mutex);
183
184 dentry = lookup_one_len(name, parent, strlen(name));
185 if (IS_ERR(dentry)) {
186 error = PTR_ERR(dentry);
187 goto out_unlock;
188 }
189
190 error = -EEXIST;
191 if (sysfs_dirent_exist(parent->d_fsdata, name))
192 goto out_dput;
193
Tejun Heoa26cd722007-06-14 03:45:14 +0900194 error = -ENOMEM;
195 sd = sysfs_new_dirent(kobj, mode, SYSFS_DIR);
196 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900197 goto out_drop;
Tejun Heoa26cd722007-06-14 03:45:14 +0900198 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900199
200 error = sysfs_create(dentry, mode, init_dir);
201 if (error)
202 goto out_sput;
203
204 inc_nlink(parent->d_inode);
205 dentry->d_op = &sysfs_dentry_ops;
206 d_rehash(dentry);
207
208 *p_dentry = dentry;
209 error = 0;
210 goto out_dput;
211
212 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900213 list_del_init(&sd->s_sibling);
214 sysfs_put(sd);
215 out_drop:
216 d_drop(dentry);
217 out_dput:
218 dput(dentry);
219 out_unlock:
220 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return error;
222}
223
224
225int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
226{
227 return create_dir(k,k->dentry,n,d);
228}
229
230/**
231 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700233 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700236int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct dentry * dentry = NULL;
239 struct dentry * parent;
240 int error = 0;
241
242 BUG_ON(!kobj);
243
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700244 if (shadow_parent)
245 parent = shadow_parent;
246 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 parent = kobj->parent->dentry;
248 else if (sysfs_mount && sysfs_mount->mnt_sb)
249 parent = sysfs_mount->mnt_sb->s_root;
250 else
251 return -EFAULT;
252
253 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
254 if (!error)
255 kobj->dentry = dentry;
256 return error;
257}
258
259/* attaches attribute's sysfs_dirent to the dentry corresponding to the
260 * attribute file
261 */
262static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
263{
264 struct attribute * attr = NULL;
265 struct bin_attribute * bin_attr = NULL;
266 int (* init) (struct inode *) = NULL;
267 int error = 0;
268
269 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
270 bin_attr = sd->s_element;
271 attr = &bin_attr->attr;
272 } else {
273 attr = sd->s_element;
274 init = init_file;
275 }
276
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530277 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900278 /* protect sd->s_dentry against sysfs_d_iput */
279 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530280 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900281 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530283 if (error) {
284 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return error;
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (bin_attr) {
289 dentry->d_inode->i_size = bin_attr->size;
290 dentry->d_inode->i_fop = &bin_fops;
291 }
292 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 d_rehash(dentry);
294
295 return 0;
296}
297
298static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
299{
300 int err = 0;
301
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530302 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900303 /* protect sd->s_dentry against sysfs_d_iput */
304 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530305 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900306 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
308 if (!err) {
309 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 d_rehash(dentry);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530311 } else
312 sysfs_put(sd);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return err;
315}
316
317static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
318 struct nameidata *nd)
319{
320 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
321 struct sysfs_dirent * sd;
322 int err = 0;
323
324 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
325 if (sd->s_type & SYSFS_NOT_PINNED) {
326 const unsigned char * name = sysfs_get_name(sd);
327
328 if (strcmp(name, dentry->d_name.name))
329 continue;
330
331 if (sd->s_type & SYSFS_KOBJ_LINK)
332 err = sysfs_attach_link(sd, dentry);
333 else
334 err = sysfs_attach_attr(sd, dentry);
335 break;
336 }
337 }
338
339 return ERR_PTR(err);
340}
341
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800342const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530344 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345};
346
347static void remove_dir(struct dentry * d)
348{
349 struct dentry * parent = dget(d->d_parent);
350 struct sysfs_dirent * sd;
351
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800352 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 d_delete(d);
354 sd = d->d_fsdata;
355 list_del_init(&sd->s_sibling);
356 sysfs_put(sd);
357 if (d->d_inode)
358 simple_rmdir(parent->d_inode,d);
359
360 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
361 atomic_read(&d->d_count));
362
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800363 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 dput(parent);
365}
366
367void sysfs_remove_subdir(struct dentry * d)
368{
369 remove_dir(d);
370}
371
372
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700373static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct sysfs_dirent * parent_sd;
376 struct sysfs_dirent * sd, * tmp;
377
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700378 dget(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!dentry)
380 return;
381
382 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800383 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 parent_sd = dentry->d_fsdata;
385 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
386 if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
387 continue;
388 list_del_init(&sd->s_sibling);
389 sysfs_drop_dentry(sd, dentry);
390 sysfs_put(sd);
391 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800392 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 remove_dir(dentry);
395 /**
396 * Drop reference from dget() on entrance.
397 */
398 dput(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700399}
400
401/**
402 * sysfs_remove_dir - remove an object's directory.
403 * @kobj: object.
404 *
405 * The only thing special about this is that we remove any files in
406 * the directory before we remove the directory, and we've inlined
407 * what used to be sysfs_rmdir() below, instead of calling separately.
408 */
409
410void sysfs_remove_dir(struct kobject * kobj)
411{
412 __sysfs_remove_dir(kobj->dentry);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800413 kobj->dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700416int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
417 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Tejun Heo996b7372007-06-14 03:45:14 +0900419 int error;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700420 struct dentry * new_dentry;
Tejun Heo996b7372007-06-14 03:45:14 +0900421 struct sysfs_dirent *sd, *parent_sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700423 if (!new_parent)
424 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700427 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700429 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900430 if (IS_ERR(new_dentry)) {
431 error = PTR_ERR(new_dentry);
432 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Tejun Heo996b7372007-06-14 03:45:14 +0900434
435 /* By allowing two different directories with the same
436 * d_parent we allow this routine to move between different
437 * shadows of the same directory
438 */
439 error = -EINVAL;
440 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
441 new_dentry->d_parent->d_inode != new_parent->d_inode ||
442 new_dentry == kobj->dentry)
443 goto out_dput;
444
445 error = -EEXIST;
446 if (new_dentry->d_inode)
447 goto out_dput;
448
449 error = kobject_set_name(kobj, "%s", new_name);
450 if (error)
451 goto out_drop;
452
453 d_add(new_dentry, NULL);
454 d_move(kobj->dentry, new_dentry);
455
456 sd = kobj->dentry->d_fsdata;
457 parent_sd = new_parent->d_fsdata;
458
459 list_del_init(&sd->s_sibling);
460 list_add(&sd->s_sibling, &parent_sd->s_children);
461
462 error = 0;
463 goto out_unlock;
464
465 out_drop:
466 d_drop(new_dentry);
467 out_dput:
468 dput(new_dentry);
469 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700470 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 up_write(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return error;
473}
474
Cornelia Huck8a824722006-11-20 17:07:51 +0100475int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
476{
477 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
478 struct sysfs_dirent *new_parent_sd, *sd;
479 int error;
480
Cornelia Huck8a824722006-11-20 17:07:51 +0100481 old_parent_dentry = kobj->parent ?
482 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100483 new_parent_dentry = new_parent ?
484 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100485
Mark Lord0de15172007-03-06 01:42:03 -0800486 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
487 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100488again:
489 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
490 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
491 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
492 goto again;
493 }
494
495 new_parent_sd = new_parent_dentry->d_fsdata;
496 sd = kobj->dentry->d_fsdata;
497
498 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
499 strlen(kobj->name));
500 if (IS_ERR(new_dentry)) {
501 error = PTR_ERR(new_dentry);
502 goto out;
503 } else
504 error = 0;
505 d_add(new_dentry, NULL);
506 d_move(kobj->dentry, new_dentry);
507 dput(new_dentry);
508
509 /* Remove from old parent's list and insert into new parent's list. */
510 list_del_init(&sd->s_sibling);
511 list_add(&sd->s_sibling, &new_parent_sd->s_children);
512
513out:
514 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
515 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
516
517 return error;
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static int sysfs_dir_open(struct inode *inode, struct file *file)
521{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800522 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900524 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800526 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heoa26cd722007-06-14 03:45:14 +0900527 sd = sysfs_new_dirent(NULL, 0, 0);
528 if (sd)
529 sysfs_attach_dirent(sd, parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800530 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Tejun Heoa26cd722007-06-14 03:45:14 +0900532 file->private_data = sd;
533 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536static int sysfs_dir_close(struct inode *inode, struct file *file)
537{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800538 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct sysfs_dirent * cursor = file->private_data;
540
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800541 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800543 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 release_sysfs_dirent(cursor);
546
547 return 0;
548}
549
550/* Relationship between s_mode and the DT_xxx types */
551static inline unsigned char dt_type(struct sysfs_dirent *sd)
552{
553 return (sd->s_mode >> 12) & 15;
554}
555
556static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
557{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800558 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
560 struct sysfs_dirent *cursor = filp->private_data;
561 struct list_head *p, *q = &cursor->s_sibling;
562 ino_t ino;
563 int i = filp->f_pos;
564
565 switch (i) {
566 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900567 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
569 break;
570 filp->f_pos++;
571 i++;
572 /* fallthrough */
573 case 1:
574 ino = parent_ino(dentry);
575 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
576 break;
577 filp->f_pos++;
578 i++;
579 /* fallthrough */
580 default:
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700581 if (filp->f_pos == 2)
582 list_move(q, &parent_sd->s_children);
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
585 struct sysfs_dirent *next;
586 const char * name;
587 int len;
588
589 next = list_entry(p, struct sysfs_dirent,
590 s_sibling);
591 if (!next->s_element)
592 continue;
593
594 name = sysfs_get_name(next);
595 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900596 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (filldir(dirent, name, len, filp->f_pos, ino,
599 dt_type(next)) < 0)
600 return 0;
601
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700602 list_move(q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 p = q;
604 filp->f_pos++;
605 }
606 }
607 return 0;
608}
609
610static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
611{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800612 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800614 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 switch (origin) {
616 case 1:
617 offset += file->f_pos;
618 case 0:
619 if (offset >= 0)
620 break;
621 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800622 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EINVAL;
624 }
625 if (offset != file->f_pos) {
626 file->f_pos = offset;
627 if (file->f_pos >= 2) {
628 struct sysfs_dirent *sd = dentry->d_fsdata;
629 struct sysfs_dirent *cursor = file->private_data;
630 struct list_head *p;
631 loff_t n = file->f_pos - 2;
632
633 list_del(&cursor->s_sibling);
634 p = sd->s_children.next;
635 while (n && p != &sd->s_children) {
636 struct sysfs_dirent *next;
637 next = list_entry(p, struct sysfs_dirent,
638 s_sibling);
639 if (next->s_element)
640 n--;
641 p = p->next;
642 }
643 list_add_tail(&cursor->s_sibling, p);
644 }
645 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800646 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return offset;
648}
649
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700650
651/**
652 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
653 * @kobj: object we're creating shadow of.
654 */
655
656int sysfs_make_shadowed_dir(struct kobject *kobj,
657 void * (*follow_link)(struct dentry *, struct nameidata *))
658{
659 struct inode *inode;
660 struct inode_operations *i_op;
661
662 inode = kobj->dentry->d_inode;
663 if (inode->i_op != &sysfs_dir_inode_operations)
664 return -EINVAL;
665
666 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
667 if (!i_op)
668 return -ENOMEM;
669
670 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
671 i_op->follow_link = follow_link;
672
673 /* Locking of inode->i_op?
674 * Since setting i_op is a single word write and they
675 * are atomic we should be ok here.
676 */
677 inode->i_op = i_op;
678 return 0;
679}
680
681/**
682 * sysfs_create_shadow_dir - create a shadow directory for an object.
683 * @kobj: object we're creating directory for.
684 *
685 * sysfs_make_shadowed_dir must already have been called on this
686 * directory.
687 */
688
689struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
690{
691 struct sysfs_dirent *sd;
692 struct dentry *parent, *dir, *shadow;
693 struct inode *inode;
694
695 dir = kobj->dentry;
696 inode = dir->d_inode;
697 parent = dir->d_parent;
698 shadow = ERR_PTR(-EINVAL);
699 if (!sysfs_is_shadowed_inode(inode))
700 goto out;
701
702 shadow = d_alloc(parent, &dir->d_name);
703 if (!shadow)
704 goto nomem;
705
Tejun Heoa26cd722007-06-14 03:45:14 +0900706 sd = sysfs_new_dirent(kobj, inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700707 if (!sd)
708 goto nomem;
Tejun Heoa26cd722007-06-14 03:45:14 +0900709 sysfs_attach_dirent(sd, NULL, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700710
711 d_instantiate(shadow, igrab(inode));
712 inc_nlink(inode);
713 inc_nlink(parent->d_inode);
714 shadow->d_op = &sysfs_dentry_ops;
715
716 dget(shadow); /* Extra count - pin the dentry in core */
717
718out:
719 return shadow;
720nomem:
721 dput(shadow);
722 shadow = ERR_PTR(-ENOMEM);
723 goto out;
724}
725
726/**
727 * sysfs_remove_shadow_dir - remove an object's directory.
728 * @shadow: dentry of shadow directory
729 *
730 * The only thing special about this is that we remove any files in
731 * the directory before we remove the directory, and we've inlined
732 * what used to be sysfs_rmdir() below, instead of calling separately.
733 */
734
735void sysfs_remove_shadow_dir(struct dentry *shadow)
736{
737 __sysfs_remove_dir(shadow);
738}
739
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800740const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .open = sysfs_dir_open,
742 .release = sysfs_dir_close,
743 .llseek = sysfs_dir_lseek,
744 .read = generic_read_dir,
745 .readdir = sysfs_readdir,
746};