blob: 9dcdf556c99c1d2b68f9bde966f2b4d393b05555 [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>
Oliver Neukum94bebf42006-12-20 10:52:44 +010012#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "sysfs.h"
14
15DECLARE_RWSEM(sysfs_rename_sem);
16
17static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
18{
19 struct sysfs_dirent * sd = dentry->d_fsdata;
20
21 if (sd) {
22 BUG_ON(sd->s_dentry != dentry);
23 sd->s_dentry = NULL;
24 sysfs_put(sd);
25 }
26 iput(inode);
27}
28
29static struct dentry_operations sysfs_dentry_ops = {
30 .d_iput = sysfs_d_iput,
31};
32
33/*
34 * Allocates a new sysfs_dirent and links it to the parent sysfs_dirent
35 */
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070036static struct sysfs_dirent * __sysfs_new_dirent(void * element)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 struct sysfs_dirent * sd;
39
40 sd = kmem_cache_alloc(sysfs_dir_cachep, GFP_KERNEL);
41 if (!sd)
42 return NULL;
43
44 memset(sd, 0, sizeof(*sd));
45 atomic_set(&sd->s_count, 1);
Juha Yrjöläeea3f892006-08-03 19:06:25 +030046 atomic_set(&sd->s_event, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 INIT_LIST_HEAD(&sd->s_children);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070048 INIT_LIST_HEAD(&sd->s_sibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 sd->s_element = element;
50
51 return sd;
52}
53
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070054static void __sysfs_list_dirent(struct sysfs_dirent *parent_sd,
55 struct sysfs_dirent *sd)
56{
57 if (sd)
58 list_add(&sd->s_sibling, &parent_sd->s_children);
59}
60
61static struct sysfs_dirent * sysfs_new_dirent(struct sysfs_dirent *parent_sd,
62 void * element)
63{
64 struct sysfs_dirent *sd;
65 sd = __sysfs_new_dirent(element);
66 __sysfs_list_dirent(parent_sd, sd);
67 return sd;
68}
69
Martin Waitza5802902006-04-02 13:59:55 +020070/*
Maneesh Sonic5168652006-03-09 19:40:14 +053071 *
72 * Return -EEXIST if there is already a sysfs element with the same name for
73 * the same parent.
74 *
75 * called with parent inode's i_mutex held
76 */
77int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
78 const unsigned char *new)
79{
80 struct sysfs_dirent * sd;
81
82 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
83 if (sd->s_element) {
84 const unsigned char *existing = sysfs_get_name(sd);
85 if (strcmp(existing, new))
86 continue;
87 else
88 return -EEXIST;
89 }
90 }
91
92 return 0;
93}
94
95
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070096static struct sysfs_dirent *
97__sysfs_make_dirent(struct dentry *dentry, void *element, mode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct sysfs_dirent * sd;
100
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700101 sd = __sysfs_new_dirent(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700103 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 sd->s_mode = mode;
106 sd->s_type = type;
107 sd->s_dentry = dentry;
108 if (dentry) {
109 dentry->d_fsdata = sysfs_get(sd);
110 dentry->d_op = &sysfs_dentry_ops;
111 }
112
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700113out:
114 return sd;
115}
116
117int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry,
118 void * element, umode_t mode, int type)
119{
120 struct sysfs_dirent *sd;
121
122 sd = __sysfs_make_dirent(dentry, element, mode, type);
123 __sysfs_list_dirent(parent_sd, sd);
124
125 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static int init_dir(struct inode * inode)
129{
130 inode->i_op = &sysfs_dir_inode_operations;
131 inode->i_fop = &sysfs_dir_operations;
132
133 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700134 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
137
138static int init_file(struct inode * inode)
139{
140 inode->i_size = PAGE_SIZE;
141 inode->i_fop = &sysfs_file_operations;
142 return 0;
143}
144
145static int init_symlink(struct inode * inode)
146{
147 inode->i_op = &sysfs_symlink_inode_operations;
148 return 0;
149}
150
151static int create_dir(struct kobject * k, struct dentry * p,
152 const char * n, struct dentry ** d)
153{
154 int error;
155 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
156
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800157 mutex_lock(&p->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700158 *d = lookup_one_len(n, p, strlen(n));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (!IS_ERR(*d)) {
Maneesh Sonic5168652006-03-09 19:40:14 +0530160 if (sysfs_dirent_exist(p->d_fsdata, n))
161 error = -EEXIST;
162 else
163 error = sysfs_make_dirent(p->d_fsdata, *d, k, mode,
164 SYSFS_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!error) {
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530166 error = sysfs_create(*d, mode, init_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!error) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700168 inc_nlink(p->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 (*d)->d_op = &sysfs_dentry_ops;
170 d_rehash(*d);
171 }
172 }
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530173 if (error && (error != -EEXIST)) {
Steven Rostedte80a5de2005-11-23 09:15:44 -0500174 struct sysfs_dirent *sd = (*d)->d_fsdata;
175 if (sd) {
176 list_del_init(&sd->s_sibling);
177 sysfs_put(sd);
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 d_drop(*d);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 dput(*d);
182 } else
183 error = PTR_ERR(*d);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800184 mutex_unlock(&p->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return error;
186}
187
188
189int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
190{
191 return create_dir(k,k->dentry,n,d);
192}
193
194/**
195 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700197 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
199
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700200int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct dentry * dentry = NULL;
203 struct dentry * parent;
204 int error = 0;
205
206 BUG_ON(!kobj);
207
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700208 if (shadow_parent)
209 parent = shadow_parent;
210 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 parent = kobj->parent->dentry;
212 else if (sysfs_mount && sysfs_mount->mnt_sb)
213 parent = sysfs_mount->mnt_sb->s_root;
214 else
215 return -EFAULT;
216
217 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
218 if (!error)
219 kobj->dentry = dentry;
220 return error;
221}
222
223/* attaches attribute's sysfs_dirent to the dentry corresponding to the
224 * attribute file
225 */
226static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
227{
228 struct attribute * attr = NULL;
229 struct bin_attribute * bin_attr = NULL;
230 int (* init) (struct inode *) = NULL;
231 int error = 0;
232
233 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
234 bin_attr = sd->s_element;
235 attr = &bin_attr->attr;
236 } else {
237 attr = sd->s_element;
238 init = init_file;
239 }
240
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530241 dentry->d_fsdata = sysfs_get(sd);
242 sd->s_dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530244 if (error) {
245 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return error;
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (bin_attr) {
250 dentry->d_inode->i_size = bin_attr->size;
251 dentry->d_inode->i_fop = &bin_fops;
252 }
253 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 d_rehash(dentry);
255
256 return 0;
257}
258
259static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
260{
261 int err = 0;
262
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530263 dentry->d_fsdata = sysfs_get(sd);
264 sd->s_dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
266 if (!err) {
267 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 d_rehash(dentry);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530269 } else
270 sysfs_put(sd);
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return err;
273}
274
275static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
276 struct nameidata *nd)
277{
278 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
279 struct sysfs_dirent * sd;
280 int err = 0;
281
282 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
283 if (sd->s_type & SYSFS_NOT_PINNED) {
284 const unsigned char * name = sysfs_get_name(sd);
285
286 if (strcmp(name, dentry->d_name.name))
287 continue;
288
289 if (sd->s_type & SYSFS_KOBJ_LINK)
290 err = sysfs_attach_link(sd, dentry);
291 else
292 err = sysfs_attach_attr(sd, dentry);
293 break;
294 }
295 }
296
297 return ERR_PTR(err);
298}
299
300struct inode_operations sysfs_dir_inode_operations = {
301 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530302 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
305static void remove_dir(struct dentry * d)
306{
307 struct dentry * parent = dget(d->d_parent);
308 struct sysfs_dirent * sd;
309
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800310 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 d_delete(d);
312 sd = d->d_fsdata;
313 list_del_init(&sd->s_sibling);
314 sysfs_put(sd);
315 if (d->d_inode)
316 simple_rmdir(parent->d_inode,d);
317
318 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
319 atomic_read(&d->d_count));
320
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800321 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 dput(parent);
323}
324
325void sysfs_remove_subdir(struct dentry * d)
326{
327 remove_dir(d);
328}
329
330
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700331static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct sysfs_dirent * parent_sd;
334 struct sysfs_dirent * sd, * tmp;
335
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700336 dget(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!dentry)
338 return;
339
340 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800341 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 parent_sd = dentry->d_fsdata;
343 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
344 if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
345 continue;
346 list_del_init(&sd->s_sibling);
347 sysfs_drop_dentry(sd, dentry);
348 sysfs_put(sd);
349 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800350 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 remove_dir(dentry);
353 /**
354 * Drop reference from dget() on entrance.
355 */
356 dput(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700357}
358
359/**
360 * sysfs_remove_dir - remove an object's directory.
361 * @kobj: object.
362 *
363 * The only thing special about this is that we remove any files in
364 * the directory before we remove the directory, and we've inlined
365 * what used to be sysfs_rmdir() below, instead of calling separately.
366 */
367
368void sysfs_remove_dir(struct kobject * kobj)
369{
370 __sysfs_remove_dir(kobj->dentry);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800371 kobj->dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700374int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
375 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 int error = 0;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700378 struct dentry * new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700380 if (!new_parent)
381 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700384 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700386 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!IS_ERR(new_dentry)) {
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700388 /* By allowing two different directories with the
389 * same d_parent we allow this routine to move
390 * between different shadows of the same directory
391 */
392 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode)
393 return -EINVAL;
394 else if (new_dentry->d_parent->d_inode != new_parent->d_inode)
395 error = -EINVAL;
396 else if (new_dentry == kobj->dentry)
397 error = -EINVAL;
398 else if (!new_dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 error = kobject_set_name(kobj, "%s", new_name);
400 if (!error) {
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700401 struct sysfs_dirent *sd, *parent_sd;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 d_add(new_dentry, NULL);
404 d_move(kobj->dentry, new_dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700405
406 sd = kobj->dentry->d_fsdata;
407 parent_sd = new_parent->d_fsdata;
408
409 list_del_init(&sd->s_sibling);
410 list_add(&sd->s_sibling, &parent_sd->s_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 else
413 d_drop(new_dentry);
414 } else
415 error = -EEXIST;
416 dput(new_dentry);
417 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700418 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 up_write(&sysfs_rename_sem);
420
421 return error;
422}
423
Cornelia Huck8a824722006-11-20 17:07:51 +0100424int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
425{
426 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
427 struct sysfs_dirent *new_parent_sd, *sd;
428 int error;
429
Cornelia Huck8a824722006-11-20 17:07:51 +0100430 old_parent_dentry = kobj->parent ?
431 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aea2007-01-08 20:16:44 +0100432 new_parent_dentry = new_parent ?
433 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100434
435again:
436 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
437 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
438 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
439 goto again;
440 }
441
442 new_parent_sd = new_parent_dentry->d_fsdata;
443 sd = kobj->dentry->d_fsdata;
444
445 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
446 strlen(kobj->name));
447 if (IS_ERR(new_dentry)) {
448 error = PTR_ERR(new_dentry);
449 goto out;
450 } else
451 error = 0;
452 d_add(new_dentry, NULL);
453 d_move(kobj->dentry, new_dentry);
454 dput(new_dentry);
455
456 /* Remove from old parent's list and insert into new parent's list. */
457 list_del_init(&sd->s_sibling);
458 list_add(&sd->s_sibling, &new_parent_sd->s_children);
459
460out:
461 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
462 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
463
464 return error;
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static int sysfs_dir_open(struct inode *inode, struct file *file)
468{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800469 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
471
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800472 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 file->private_data = sysfs_new_dirent(parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800474 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return file->private_data ? 0 : -ENOMEM;
477
478}
479
480static int sysfs_dir_close(struct inode *inode, struct file *file)
481{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800482 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct sysfs_dirent * cursor = file->private_data;
484
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800485 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800487 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 release_sysfs_dirent(cursor);
490
491 return 0;
492}
493
494/* Relationship between s_mode and the DT_xxx types */
495static inline unsigned char dt_type(struct sysfs_dirent *sd)
496{
497 return (sd->s_mode >> 12) & 15;
498}
499
500static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
501{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800502 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
504 struct sysfs_dirent *cursor = filp->private_data;
505 struct list_head *p, *q = &cursor->s_sibling;
506 ino_t ino;
507 int i = filp->f_pos;
508
509 switch (i) {
510 case 0:
511 ino = dentry->d_inode->i_ino;
512 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
513 break;
514 filp->f_pos++;
515 i++;
516 /* fallthrough */
517 case 1:
518 ino = parent_ino(dentry);
519 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
520 break;
521 filp->f_pos++;
522 i++;
523 /* fallthrough */
524 default:
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700525 if (filp->f_pos == 2)
526 list_move(q, &parent_sd->s_children);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
529 struct sysfs_dirent *next;
530 const char * name;
531 int len;
532
533 next = list_entry(p, struct sysfs_dirent,
534 s_sibling);
535 if (!next->s_element)
536 continue;
537
538 name = sysfs_get_name(next);
539 len = strlen(name);
540 if (next->s_dentry)
541 ino = next->s_dentry->d_inode->i_ino;
542 else
543 ino = iunique(sysfs_sb, 2);
544
545 if (filldir(dirent, name, len, filp->f_pos, ino,
546 dt_type(next)) < 0)
547 return 0;
548
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700549 list_move(q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 p = q;
551 filp->f_pos++;
552 }
553 }
554 return 0;
555}
556
557static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
558{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800559 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800561 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 switch (origin) {
563 case 1:
564 offset += file->f_pos;
565 case 0:
566 if (offset >= 0)
567 break;
568 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800569 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return -EINVAL;
571 }
572 if (offset != file->f_pos) {
573 file->f_pos = offset;
574 if (file->f_pos >= 2) {
575 struct sysfs_dirent *sd = dentry->d_fsdata;
576 struct sysfs_dirent *cursor = file->private_data;
577 struct list_head *p;
578 loff_t n = file->f_pos - 2;
579
580 list_del(&cursor->s_sibling);
581 p = sd->s_children.next;
582 while (n && p != &sd->s_children) {
583 struct sysfs_dirent *next;
584 next = list_entry(p, struct sysfs_dirent,
585 s_sibling);
586 if (next->s_element)
587 n--;
588 p = p->next;
589 }
590 list_add_tail(&cursor->s_sibling, p);
591 }
592 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800593 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return offset;
595}
596
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700597
598/**
599 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
600 * @kobj: object we're creating shadow of.
601 */
602
603int sysfs_make_shadowed_dir(struct kobject *kobj,
604 void * (*follow_link)(struct dentry *, struct nameidata *))
605{
606 struct inode *inode;
607 struct inode_operations *i_op;
608
609 inode = kobj->dentry->d_inode;
610 if (inode->i_op != &sysfs_dir_inode_operations)
611 return -EINVAL;
612
613 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
614 if (!i_op)
615 return -ENOMEM;
616
617 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
618 i_op->follow_link = follow_link;
619
620 /* Locking of inode->i_op?
621 * Since setting i_op is a single word write and they
622 * are atomic we should be ok here.
623 */
624 inode->i_op = i_op;
625 return 0;
626}
627
628/**
629 * sysfs_create_shadow_dir - create a shadow directory for an object.
630 * @kobj: object we're creating directory for.
631 *
632 * sysfs_make_shadowed_dir must already have been called on this
633 * directory.
634 */
635
636struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
637{
638 struct sysfs_dirent *sd;
639 struct dentry *parent, *dir, *shadow;
640 struct inode *inode;
641
642 dir = kobj->dentry;
643 inode = dir->d_inode;
644 parent = dir->d_parent;
645 shadow = ERR_PTR(-EINVAL);
646 if (!sysfs_is_shadowed_inode(inode))
647 goto out;
648
649 shadow = d_alloc(parent, &dir->d_name);
650 if (!shadow)
651 goto nomem;
652
653 sd = __sysfs_make_dirent(shadow, kobj, inode->i_mode, SYSFS_DIR);
654 if (!sd)
655 goto nomem;
656
657 d_instantiate(shadow, igrab(inode));
658 inc_nlink(inode);
659 inc_nlink(parent->d_inode);
660 shadow->d_op = &sysfs_dentry_ops;
661
662 dget(shadow); /* Extra count - pin the dentry in core */
663
664out:
665 return shadow;
666nomem:
667 dput(shadow);
668 shadow = ERR_PTR(-ENOMEM);
669 goto out;
670}
671
672/**
673 * sysfs_remove_shadow_dir - remove an object's directory.
674 * @shadow: dentry of shadow directory
675 *
676 * The only thing special about this is that we remove any files in
677 * the directory before we remove the directory, and we've inlined
678 * what used to be sysfs_rmdir() below, instead of calling separately.
679 */
680
681void sysfs_remove_shadow_dir(struct dentry *shadow)
682{
683 __sysfs_remove_dir(shadow);
684}
685
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800686const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 .open = sysfs_dir_open,
688 .release = sysfs_dir_close,
689 .llseek = sysfs_dir_lseek,
690 .read = generic_read_dir,
691 .readdir = sysfs_readdir,
692};