]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/sysfs/dir.c
driver-core: make devt_attr and uevent_attr static
[mirror_ubuntu-artful-kernel.git] / fs / sysfs / dir.c
CommitLineData
1da177e4
LT
1/*
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>
5f45f1a7 11#include <linux/namei.h>
2b611bb7 12#include <linux/idr.h>
94bebf4d 13#include <asm/semaphore.h>
1da177e4
LT
14#include "sysfs.h"
15
16DECLARE_RWSEM(sysfs_rename_sem);
dd14cbc9 17spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
aecdceda 18spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
1da177e4 19
2b611bb7
TH
20static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
21static DEFINE_IDA(sysfs_ino_ida);
22
23int sysfs_alloc_ino(ino_t *pino)
24{
25 int ino, rc;
26
27 retry:
28 spin_lock(&sysfs_ino_lock);
29 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
30 spin_unlock(&sysfs_ino_lock);
31
32 if (rc == -EAGAIN) {
33 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
34 goto retry;
35 rc = -ENOMEM;
36 }
37
38 *pino = ino;
39 return rc;
40}
41
42static void sysfs_free_ino(ino_t ino)
43{
44 spin_lock(&sysfs_ino_lock);
45 ida_remove(&sysfs_ino_ida, ino);
46 spin_unlock(&sysfs_ino_lock);
47}
48
fa7f912a
TH
49void release_sysfs_dirent(struct sysfs_dirent * sd)
50{
13b3086d
TH
51 struct sysfs_dirent *parent_sd;
52
53 repeat:
54 parent_sd = sd->s_parent;
55
0ab66088
TH
56 /* If @sd is being released after deletion, s_active is write
57 * locked. If @sd is cursor for directory walk or being
58 * released prematurely, s_active has no reader or writer.
59 *
60 * sysfs_deactivate() lies to lockdep that s_active is
61 * unlocked immediately. Lie one more time to cover the
62 * previous lie.
63 */
64 if (!down_write_trylock(&sd->s_active))
65 rwsem_acquire(&sd->s_active.dep_map,
66 SYSFS_S_ACTIVE_DEACTIVATE, 0, _RET_IP_);
67 up_write(&sd->s_active);
68
3e519038 69 if (sd->s_type & SYSFS_KOBJ_LINK)
2b29ac25 70 sysfs_put(sd->s_elem.symlink.target_sd);
0c096b50
TH
71 if (sd->s_type & SYSFS_COPY_NAME)
72 kfree(sd->s_name);
fa7f912a 73 kfree(sd->s_iattr);
2b611bb7 74 sysfs_free_ino(sd->s_ino);
fa7f912a 75 kmem_cache_free(sysfs_dir_cachep, sd);
13b3086d
TH
76
77 sd = parent_sd;
78 if (sd && atomic_dec_and_test(&sd->s_count))
79 goto repeat;
fa7f912a
TH
80}
81
1da177e4
LT
82static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
83{
84 struct sysfs_dirent * sd = dentry->d_fsdata;
85
86 if (sd) {
dd14cbc9
TH
87 /* sd->s_dentry is protected with sysfs_lock. This
88 * allows sysfs_drop_dentry() to dereference it.
89 */
90 spin_lock(&sysfs_lock);
91
92 /* The dentry might have been deleted or another
93 * lookup could have happened updating sd->s_dentry to
94 * point the new dentry. Ignore if it isn't pointing
95 * to this dentry.
96 */
97 if (sd->s_dentry == dentry)
98 sd->s_dentry = NULL;
99 spin_unlock(&sysfs_lock);
1da177e4
LT
100 sysfs_put(sd);
101 }
102 iput(inode);
103}
104
105static struct dentry_operations sysfs_dentry_ops = {
106 .d_iput = sysfs_d_iput,
107};
108
3e519038 109struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
1da177e4 110{
0c096b50
TH
111 char *dup_name = NULL;
112 struct sysfs_dirent *sd = NULL;
113
114 if (type & SYSFS_COPY_NAME) {
115 name = dup_name = kstrdup(name, GFP_KERNEL);
116 if (!name)
117 goto err_out;
118 }
1da177e4 119
c3762229 120 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
1da177e4 121 if (!sd)
0c096b50 122 goto err_out;
1da177e4 123
0c096b50
TH
124 if (sysfs_alloc_ino(&sd->s_ino))
125 goto err_out;
2b611bb7 126
1da177e4 127 atomic_set(&sd->s_count, 1);
eea3f891 128 atomic_set(&sd->s_event, 1);
0ab66088 129 init_rwsem(&sd->s_active);
1da177e4 130 INIT_LIST_HEAD(&sd->s_children);
b592fcfe 131 INIT_LIST_HEAD(&sd->s_sibling);
a26cd722 132
0c096b50 133 sd->s_name = name;
a26cd722
TH
134 sd->s_mode = mode;
135 sd->s_type = type;
1da177e4
LT
136
137 return sd;
0c096b50
TH
138
139 err_out:
140 kfree(dup_name);
141 kmem_cache_free(sysfs_dir_cachep, sd);
142 return NULL;
1da177e4
LT
143}
144
198a2a84
TH
145static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
146{
147 dentry->d_op = &sysfs_dentry_ops;
148 dentry->d_fsdata = sysfs_get(sd);
149
150 /* protect sd->s_dentry against sysfs_d_iput */
151 spin_lock(&sysfs_lock);
152 sd->s_dentry = dentry;
153 spin_unlock(&sysfs_lock);
154
155 d_rehash(dentry);
156}
157
a26cd722
TH
158void sysfs_attach_dirent(struct sysfs_dirent *sd,
159 struct sysfs_dirent *parent_sd, struct dentry *dentry)
b592fcfe 160{
198a2a84
TH
161 if (dentry)
162 sysfs_attach_dentry(sd, dentry);
b592fcfe 163
13b3086d
TH
164 if (parent_sd) {
165 sd->s_parent = sysfs_get(parent_sd);
a26cd722 166 list_add(&sd->s_sibling, &parent_sd->s_children);
13b3086d 167 }
b592fcfe
EB
168}
169
a580290c 170/*
c516865c
MS
171 *
172 * Return -EEXIST if there is already a sysfs element with the same name for
173 * the same parent.
174 *
175 * called with parent inode's i_mutex held
176 */
177int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
178 const unsigned char *new)
179{
180 struct sysfs_dirent * sd;
181
182 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
3e519038 183 if (sd->s_type) {
0c096b50 184 if (strcmp(sd->s_name, new))
c516865c
MS
185 continue;
186 else
187 return -EEXIST;
188 }
189 }
190
191 return 0;
192}
193
1da177e4
LT
194static int init_dir(struct inode * inode)
195{
196 inode->i_op = &sysfs_dir_inode_operations;
197 inode->i_fop = &sysfs_dir_operations;
198
199 /* directory inodes start off with i_nlink == 2 (for "." entry) */
d8c76e6f 200 inc_nlink(inode);
1da177e4
LT
201 return 0;
202}
203
204static int init_file(struct inode * inode)
205{
206 inode->i_size = PAGE_SIZE;
207 inode->i_fop = &sysfs_file_operations;
208 return 0;
209}
210
211static int init_symlink(struct inode * inode)
212{
213 inode->i_op = &sysfs_symlink_inode_operations;
214 return 0;
215}
216
dfeb9fb0
TH
217static int create_dir(struct kobject *kobj, struct dentry *parent,
218 const char *name, struct dentry **p_dentry)
1da177e4
LT
219{
220 int error;
221 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
dfeb9fb0
TH
222 struct dentry *dentry;
223 struct sysfs_dirent *sd;
1da177e4 224
dfeb9fb0
TH
225 mutex_lock(&parent->d_inode->i_mutex);
226
227 dentry = lookup_one_len(name, parent, strlen(name));
228 if (IS_ERR(dentry)) {
229 error = PTR_ERR(dentry);
230 goto out_unlock;
231 }
232
233 error = -EEXIST;
234 if (sysfs_dirent_exist(parent->d_fsdata, name))
235 goto out_dput;
236
a26cd722 237 error = -ENOMEM;
3e519038 238 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
a26cd722 239 if (!sd)
dfeb9fb0 240 goto out_drop;
3e519038 241 sd->s_elem.dir.kobj = kobj;
dfeb9fb0 242
198a2a84 243 error = sysfs_create(sd, dentry, mode, init_dir);
dfeb9fb0
TH
244 if (error)
245 goto out_sput;
246
247 inc_nlink(parent->d_inode);
198a2a84 248 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
dfeb9fb0
TH
249
250 *p_dentry = dentry;
251 error = 0;
252 goto out_dput;
253
254 out_sput:
dfeb9fb0
TH
255 list_del_init(&sd->s_sibling);
256 sysfs_put(sd);
257 out_drop:
258 d_drop(dentry);
259 out_dput:
260 dput(dentry);
261 out_unlock:
262 mutex_unlock(&parent->d_inode->i_mutex);
1da177e4
LT
263 return error;
264}
265
266
267int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
268{
269 return create_dir(k,k->dentry,n,d);
270}
271
272/**
273 * sysfs_create_dir - create a directory for an object.
1da177e4 274 * @kobj: object we're creating directory for.
b592fcfe 275 * @shadow_parent: parent parent object.
1da177e4
LT
276 */
277
b592fcfe 278int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
1da177e4
LT
279{
280 struct dentry * dentry = NULL;
281 struct dentry * parent;
282 int error = 0;
283
284 BUG_ON(!kobj);
285
b592fcfe
EB
286 if (shadow_parent)
287 parent = shadow_parent;
288 else if (kobj->parent)
1da177e4
LT
289 parent = kobj->parent->dentry;
290 else if (sysfs_mount && sysfs_mount->mnt_sb)
291 parent = sysfs_mount->mnt_sb->s_root;
292 else
293 return -EFAULT;
294
295 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
296 if (!error)
297 kobj->dentry = dentry;
298 return error;
299}
300
301/* attaches attribute's sysfs_dirent to the dentry corresponding to the
302 * attribute file
303 */
304static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
305{
306 struct attribute * attr = NULL;
307 struct bin_attribute * bin_attr = NULL;
308 int (* init) (struct inode *) = NULL;
309 int error = 0;
310
311 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
3e519038 312 bin_attr = sd->s_elem.bin_attr.bin_attr;
1da177e4
LT
313 attr = &bin_attr->attr;
314 } else {
3e519038 315 attr = sd->s_elem.attr.attr;
1da177e4
LT
316 init = init_file;
317 }
318
198a2a84
TH
319 error = sysfs_create(sd, dentry,
320 (attr->mode & S_IALLUGO) | S_IFREG, init);
321 if (error)
1da177e4
LT
322 return error;
323
324 if (bin_attr) {
325 dentry->d_inode->i_size = bin_attr->size;
326 dentry->d_inode->i_fop = &bin_fops;
327 }
198a2a84
TH
328
329 sysfs_attach_dentry(sd, dentry);
1da177e4
LT
330
331 return 0;
332}
333
334static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
335{
198a2a84 336 int err;
1da177e4 337
198a2a84
TH
338 err = sysfs_create(sd, dentry, S_IFLNK|S_IRWXUGO, init_symlink);
339 if (!err)
340 sysfs_attach_dentry(sd, dentry);
6fa5c828 341
1da177e4
LT
342 return err;
343}
344
345static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
346 struct nameidata *nd)
347{
348 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
349 struct sysfs_dirent * sd;
350 int err = 0;
351
352 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
353 if (sd->s_type & SYSFS_NOT_PINNED) {
0c096b50 354 if (strcmp(sd->s_name, dentry->d_name.name))
1da177e4
LT
355 continue;
356
357 if (sd->s_type & SYSFS_KOBJ_LINK)
358 err = sysfs_attach_link(sd, dentry);
359 else
360 err = sysfs_attach_attr(sd, dentry);
361 break;
362 }
363 }
364
365 return ERR_PTR(err);
366}
367
c5ef1c42 368const struct inode_operations sysfs_dir_inode_operations = {
1da177e4 369 .lookup = sysfs_lookup,
988d186d 370 .setattr = sysfs_setattr,
1da177e4
LT
371};
372
373static void remove_dir(struct dentry * d)
374{
dbde0fcf
TH
375 struct dentry *parent = d->d_parent;
376 struct sysfs_dirent *sd = d->d_fsdata;
1da177e4 377
1b1dcc1b 378 mutex_lock(&parent->d_inode->i_mutex);
dbde0fcf 379
1da177e4 380 list_del_init(&sd->s_sibling);
1da177e4
LT
381
382 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
383 atomic_read(&d->d_count));
384
1b1dcc1b 385 mutex_unlock(&parent->d_inode->i_mutex);
0ab66088 386
dbde0fcf 387 sysfs_drop_dentry(sd);
0ab66088
TH
388 sysfs_deactivate(sd);
389 sysfs_put(sd);
1da177e4
LT
390}
391
392void sysfs_remove_subdir(struct dentry * d)
393{
394 remove_dir(d);
395}
396
397
b592fcfe 398static void __sysfs_remove_dir(struct dentry *dentry)
1da177e4 399{
0ab66088 400 LIST_HEAD(removed);
1da177e4
LT
401 struct sysfs_dirent * parent_sd;
402 struct sysfs_dirent * sd, * tmp;
403
404 if (!dentry)
405 return;
406
407 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
1b1dcc1b 408 mutex_lock(&dentry->d_inode->i_mutex);
1da177e4
LT
409 parent_sd = dentry->d_fsdata;
410 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
3e519038 411 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
1da177e4 412 continue;
0ab66088 413 list_move(&sd->s_sibling, &removed);
1da177e4 414 }
1b1dcc1b 415 mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4 416
0ab66088
TH
417 list_for_each_entry_safe(sd, tmp, &removed, s_sibling) {
418 list_del_init(&sd->s_sibling);
dbde0fcf 419 sysfs_drop_dentry(sd);
0ab66088
TH
420 sysfs_deactivate(sd);
421 sysfs_put(sd);
422 }
423
1da177e4 424 remove_dir(dentry);
b592fcfe
EB
425}
426
427/**
428 * sysfs_remove_dir - remove an object's directory.
429 * @kobj: object.
430 *
431 * The only thing special about this is that we remove any files in
432 * the directory before we remove the directory, and we've inlined
433 * what used to be sysfs_rmdir() below, instead of calling separately.
434 */
435
436void sysfs_remove_dir(struct kobject * kobj)
437{
aecdceda
TH
438 struct dentry *d = kobj->dentry;
439
440 spin_lock(&kobj_sysfs_assoc_lock);
641e6f30 441 kobj->dentry = NULL;
aecdceda
TH
442 spin_unlock(&kobj_sysfs_assoc_lock);
443
444 __sysfs_remove_dir(d);
1da177e4
LT
445}
446
b592fcfe
EB
447int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
448 const char *new_name)
1da177e4 449{
0c096b50
TH
450 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
451 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
452 struct dentry *new_dentry;
453 char *dup_name;
996b7376 454 int error;
1da177e4 455
b592fcfe
EB
456 if (!new_parent)
457 return -EFAULT;
1da177e4
LT
458
459 down_write(&sysfs_rename_sem);
b592fcfe 460 mutex_lock(&new_parent->d_inode->i_mutex);
1da177e4 461
b592fcfe 462 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
996b7376
TH
463 if (IS_ERR(new_dentry)) {
464 error = PTR_ERR(new_dentry);
465 goto out_unlock;
1da177e4 466 }
996b7376
TH
467
468 /* By allowing two different directories with the same
469 * d_parent we allow this routine to move between different
470 * shadows of the same directory
471 */
472 error = -EINVAL;
473 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
474 new_dentry->d_parent->d_inode != new_parent->d_inode ||
475 new_dentry == kobj->dentry)
476 goto out_dput;
477
478 error = -EEXIST;
479 if (new_dentry->d_inode)
480 goto out_dput;
481
0c096b50
TH
482 /* rename kobject and sysfs_dirent */
483 error = -ENOMEM;
484 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
485 if (!new_name)
486 goto out_drop;
487
996b7376
TH
488 error = kobject_set_name(kobj, "%s", new_name);
489 if (error)
0c096b50 490 goto out_free;
996b7376 491
0c096b50
TH
492 kfree(sd->s_name);
493 sd->s_name = new_name;
494
495 /* move under the new parent */
996b7376
TH
496 d_add(new_dentry, NULL);
497 d_move(kobj->dentry, new_dentry);
498
996b7376
TH
499 list_del_init(&sd->s_sibling);
500 list_add(&sd->s_sibling, &parent_sd->s_children);
501
502 error = 0;
503 goto out_unlock;
504
0c096b50
TH
505 out_free:
506 kfree(dup_name);
996b7376
TH
507 out_drop:
508 d_drop(new_dentry);
509 out_dput:
510 dput(new_dentry);
511 out_unlock:
b592fcfe 512 mutex_unlock(&new_parent->d_inode->i_mutex);
1da177e4 513 up_write(&sysfs_rename_sem);
1da177e4
LT
514 return error;
515}
516
8a82472f
CH
517int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
518{
519 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
520 struct sysfs_dirent *new_parent_sd, *sd;
521 int error;
522
8a82472f
CH
523 old_parent_dentry = kobj->parent ?
524 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
c744aeae
CH
525 new_parent_dentry = new_parent ?
526 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
8a82472f 527
0de1517e
ML
528 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
529 return 0; /* nothing to move */
8a82472f
CH
530again:
531 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
532 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
533 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
534 goto again;
535 }
536
537 new_parent_sd = new_parent_dentry->d_fsdata;
538 sd = kobj->dentry->d_fsdata;
539
540 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
541 strlen(kobj->name));
542 if (IS_ERR(new_dentry)) {
543 error = PTR_ERR(new_dentry);
544 goto out;
545 } else
546 error = 0;
547 d_add(new_dentry, NULL);
548 d_move(kobj->dentry, new_dentry);
549 dput(new_dentry);
550
551 /* Remove from old parent's list and insert into new parent's list. */
552 list_del_init(&sd->s_sibling);
553 list_add(&sd->s_sibling, &new_parent_sd->s_children);
554
555out:
556 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
557 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
558
559 return error;
560}
561
1da177e4
LT
562static int sysfs_dir_open(struct inode *inode, struct file *file)
563{
f427f5d5 564 struct dentry * dentry = file->f_path.dentry;
1da177e4 565 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
a26cd722 566 struct sysfs_dirent * sd;
1da177e4 567
1b1dcc1b 568 mutex_lock(&dentry->d_inode->i_mutex);
3e519038 569 sd = sysfs_new_dirent("_DIR_", 0, 0);
a26cd722
TH
570 if (sd)
571 sysfs_attach_dirent(sd, parent_sd, NULL);
1b1dcc1b 572 mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4 573
a26cd722
TH
574 file->private_data = sd;
575 return sd ? 0 : -ENOMEM;
1da177e4
LT
576}
577
578static int sysfs_dir_close(struct inode *inode, struct file *file)
579{
f427f5d5 580 struct dentry * dentry = file->f_path.dentry;
1da177e4
LT
581 struct sysfs_dirent * cursor = file->private_data;
582
1b1dcc1b 583 mutex_lock(&dentry->d_inode->i_mutex);
1da177e4 584 list_del_init(&cursor->s_sibling);
1b1dcc1b 585 mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4
LT
586
587 release_sysfs_dirent(cursor);
588
589 return 0;
590}
591
592/* Relationship between s_mode and the DT_xxx types */
593static inline unsigned char dt_type(struct sysfs_dirent *sd)
594{
595 return (sd->s_mode >> 12) & 15;
596}
597
598static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
599{
f427f5d5 600 struct dentry *dentry = filp->f_path.dentry;
1da177e4
LT
601 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
602 struct sysfs_dirent *cursor = filp->private_data;
603 struct list_head *p, *q = &cursor->s_sibling;
604 ino_t ino;
605 int i = filp->f_pos;
606
607 switch (i) {
608 case 0:
dc351252 609 ino = parent_sd->s_ino;
1da177e4
LT
610 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
611 break;
612 filp->f_pos++;
613 i++;
614 /* fallthrough */
615 case 1:
13b3086d
TH
616 if (parent_sd->s_parent)
617 ino = parent_sd->s_parent->s_ino;
618 else
619 ino = parent_sd->s_ino;
1da177e4
LT
620 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
621 break;
622 filp->f_pos++;
623 i++;
624 /* fallthrough */
625 default:
1bfba4e8
AM
626 if (filp->f_pos == 2)
627 list_move(q, &parent_sd->s_children);
628
1da177e4
LT
629 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
630 struct sysfs_dirent *next;
631 const char * name;
632 int len;
633
634 next = list_entry(p, struct sysfs_dirent,
635 s_sibling);
3e519038 636 if (!next->s_type)
1da177e4
LT
637 continue;
638
0c096b50 639 name = next->s_name;
1da177e4 640 len = strlen(name);
dc351252 641 ino = next->s_ino;
1da177e4
LT
642
643 if (filldir(dirent, name, len, filp->f_pos, ino,
644 dt_type(next)) < 0)
645 return 0;
646
1bfba4e8 647 list_move(q, p);
1da177e4
LT
648 p = q;
649 filp->f_pos++;
650 }
651 }
652 return 0;
653}
654
655static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
656{
f427f5d5 657 struct dentry * dentry = file->f_path.dentry;
1da177e4 658
1b1dcc1b 659 mutex_lock(&dentry->d_inode->i_mutex);
1da177e4
LT
660 switch (origin) {
661 case 1:
662 offset += file->f_pos;
663 case 0:
664 if (offset >= 0)
665 break;
666 default:
f427f5d5 667 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
1da177e4
LT
668 return -EINVAL;
669 }
670 if (offset != file->f_pos) {
671 file->f_pos = offset;
672 if (file->f_pos >= 2) {
673 struct sysfs_dirent *sd = dentry->d_fsdata;
674 struct sysfs_dirent *cursor = file->private_data;
675 struct list_head *p;
676 loff_t n = file->f_pos - 2;
677
678 list_del(&cursor->s_sibling);
679 p = sd->s_children.next;
680 while (n && p != &sd->s_children) {
681 struct sysfs_dirent *next;
682 next = list_entry(p, struct sysfs_dirent,
683 s_sibling);
3e519038 684 if (next->s_type)
1da177e4
LT
685 n--;
686 p = p->next;
687 }
688 list_add_tail(&cursor->s_sibling, p);
689 }
690 }
1b1dcc1b 691 mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4
LT
692 return offset;
693}
694
b592fcfe
EB
695
696/**
697 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
698 * @kobj: object we're creating shadow of.
699 */
700
701int sysfs_make_shadowed_dir(struct kobject *kobj,
702 void * (*follow_link)(struct dentry *, struct nameidata *))
703{
704 struct inode *inode;
705 struct inode_operations *i_op;
706
707 inode = kobj->dentry->d_inode;
708 if (inode->i_op != &sysfs_dir_inode_operations)
709 return -EINVAL;
710
711 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
712 if (!i_op)
713 return -ENOMEM;
714
715 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
716 i_op->follow_link = follow_link;
717
718 /* Locking of inode->i_op?
719 * Since setting i_op is a single word write and they
720 * are atomic we should be ok here.
721 */
722 inode->i_op = i_op;
723 return 0;
724}
725
726/**
727 * sysfs_create_shadow_dir - create a shadow directory for an object.
728 * @kobj: object we're creating directory for.
729 *
730 * sysfs_make_shadowed_dir must already have been called on this
731 * directory.
732 */
733
734struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
735{
13b3086d
TH
736 struct dentry *dir = kobj->dentry;
737 struct inode *inode = dir->d_inode;
738 struct dentry *parent = dir->d_parent;
739 struct sysfs_dirent *parent_sd = parent->d_fsdata;
740 struct dentry *shadow;
b592fcfe 741 struct sysfs_dirent *sd;
b592fcfe 742
b592fcfe
EB
743 shadow = ERR_PTR(-EINVAL);
744 if (!sysfs_is_shadowed_inode(inode))
745 goto out;
746
747 shadow = d_alloc(parent, &dir->d_name);
748 if (!shadow)
749 goto nomem;
750
3e519038 751 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
b592fcfe
EB
752 if (!sd)
753 goto nomem;
3e519038 754 sd->s_elem.dir.kobj = kobj;
13b3086d
TH
755 /* point to parent_sd but don't attach to it */
756 sd->s_parent = sysfs_get(parent_sd);
a26cd722 757 sysfs_attach_dirent(sd, NULL, shadow);
b592fcfe
EB
758
759 d_instantiate(shadow, igrab(inode));
760 inc_nlink(inode);
761 inc_nlink(parent->d_inode);
b592fcfe
EB
762
763 dget(shadow); /* Extra count - pin the dentry in core */
764
765out:
766 return shadow;
767nomem:
768 dput(shadow);
769 shadow = ERR_PTR(-ENOMEM);
770 goto out;
771}
772
773/**
774 * sysfs_remove_shadow_dir - remove an object's directory.
775 * @shadow: dentry of shadow directory
776 *
777 * The only thing special about this is that we remove any files in
778 * the directory before we remove the directory, and we've inlined
779 * what used to be sysfs_rmdir() below, instead of calling separately.
780 */
781
782void sysfs_remove_shadow_dir(struct dentry *shadow)
783{
784 __sysfs_remove_dir(shadow);
785}
786
4b6f5d20 787const struct file_operations sysfs_dir_operations = {
1da177e4
LT
788 .open = sysfs_dir_open,
789 .release = sysfs_dir_close,
790 .llseek = sysfs_dir_lseek,
791 .read = generic_read_dir,
792 .readdir = sysfs_readdir,
793};