2 * kobject.c - library routines for handling generic kernel objects
4 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
6 * This file is released under the GPLv2.
9 * Please see the file Documentation/kobject.txt for critical information
10 * about using the kobject interface.
15 #include <linux/kobject.h>
16 #include <linux/string.h>
17 #include <linux/module.h>
18 #include <linux/stat.h>
21 * populate_dir - populate directory with attributes.
22 * @kobj: object we're working on.
24 * Most subsystems have a set of default attributes that
25 * are associated with an object that registers with them.
26 * This is a helper called during object registration that
27 * loops through the default attributes of the subsystem
28 * and creates attributes files for them in sysfs.
32 static int populate_dir(struct kobject
* kobj
)
34 struct kobj_type
* t
= get_ktype(kobj
);
35 struct attribute
* attr
;
39 if (t
&& t
->default_attrs
) {
40 for (i
= 0; (attr
= t
->default_attrs
[i
]) != NULL
; i
++) {
41 if ((error
= sysfs_create_file(kobj
,attr
)))
48 static int create_dir(struct kobject
* kobj
)
51 if (kobject_name(kobj
)) {
52 error
= sysfs_create_dir(kobj
);
54 if ((error
= populate_dir(kobj
)))
55 sysfs_remove_dir(kobj
);
61 static inline struct kobject
* to_kobj(struct list_head
* entry
)
63 return container_of(entry
,struct kobject
,entry
);
66 static int get_kobj_path_length(struct kset
*kset
, struct kobject
*kobj
)
69 struct kobject
* parent
= kobj
;
71 /* walk up the ancestors until we hit the one pointing to the
73 * Add 1 to strlen for leading '/' of each level.
76 length
+= strlen(kobject_name(parent
)) + 1;
77 parent
= parent
->parent
;
82 static void fill_kobj_path(struct kset
*kset
, struct kobject
*kobj
, char *path
, int length
)
84 struct kobject
* parent
;
87 for (parent
= kobj
; parent
; parent
= parent
->parent
) {
88 int cur
= strlen(kobject_name(parent
));
89 /* back up enough to print this name with '/' */
91 strncpy (path
+ length
, kobject_name(parent
), cur
);
92 *(path
+ --length
) = '/';
95 pr_debug("%s: path = '%s'\n",__FUNCTION__
,path
);
99 * kobject_get_path - generate and return the path associated with a given kobj
100 * and kset pair. The result must be freed by the caller with kfree().
102 * @kset: kset in question, with which to build the path
103 * @kobj: kobject in question, with which to build the path
104 * @gfp_mask: the allocation type used to allocate the path
106 char * kobject_get_path(struct kset
*kset
, struct kobject
*kobj
, int gfp_mask
)
111 len
= get_kobj_path_length(kset
, kobj
);
112 path
= kmalloc(len
, gfp_mask
);
115 memset(path
, 0x00, len
);
116 fill_kobj_path(kset
, kobj
, path
, len
);
121 #ifdef CONFIG_HOTPLUG
123 #define BUFFER_SIZE 1024 /* should be enough memory for the env */
124 #define NUM_ENVP 32 /* number of env pointers */
125 static unsigned long sequence_num
;
126 static spinlock_t sequence_lock
= SPIN_LOCK_UNLOCKED
;
128 static void kset_hotplug(const char *action
, struct kset
*kset
,
129 struct kobject
*kobj
)
137 char *kobj_path
= NULL
;
141 /* If the kset has a filter operation, call it. If it returns
142 failure, no hotplug event is required. */
143 if (kset
->hotplug_ops
->filter
) {
144 if (!kset
->hotplug_ops
->filter(kset
, kobj
))
148 pr_debug ("%s\n", __FUNCTION__
);
150 if (!hotplug_path
[0])
153 envp
= kmalloc(NUM_ENVP
* sizeof (char *), GFP_KERNEL
);
156 memset (envp
, 0x00, NUM_ENVP
* sizeof (char *));
158 buffer
= kmalloc(BUFFER_SIZE
, GFP_KERNEL
);
162 if (kset
->hotplug_ops
->name
)
163 name
= kset
->hotplug_ops
->name(kset
, kobj
);
165 name
= kset
->kobj
.name
;
167 argv
[0] = hotplug_path
;
171 /* minimal command environment */
172 envp
[i
++] = "HOME=/";
173 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
177 envp
[i
++] = scratch
;
178 scratch
+= sprintf(scratch
, "ACTION=%s", action
) + 1;
180 spin_lock(&sequence_lock
);
181 seq
= sequence_num
++;
182 spin_unlock(&sequence_lock
);
184 envp
[i
++] = scratch
;
185 scratch
+= sprintf(scratch
, "SEQNUM=%ld", seq
) + 1;
187 kobj_path
= kobject_get_path(kset
, kobj
, GFP_KERNEL
);
191 envp
[i
++] = scratch
;
192 scratch
+= sprintf (scratch
, "DEVPATH=%s", kobj_path
) + 1;
194 if (kset
->hotplug_ops
->hotplug
) {
195 /* have the kset specific function add its stuff */
196 retval
= kset
->hotplug_ops
->hotplug (kset
, kobj
,
197 &envp
[i
], NUM_ENVP
- i
, scratch
,
198 BUFFER_SIZE
- (scratch
- buffer
));
200 pr_debug ("%s - hotplug() returned %d\n",
201 __FUNCTION__
, retval
);
206 pr_debug ("%s: %s %s %s %s %s %s %s\n", __FUNCTION__
, argv
[0], argv
[1],
207 envp
[0], envp
[1], envp
[2], envp
[3], envp
[4]);
208 retval
= call_usermodehelper (argv
[0], argv
, envp
, 0);
210 pr_debug ("%s - call_usermodehelper returned %d\n",
211 __FUNCTION__
, retval
);
220 void kobject_hotplug(const char *action
, struct kobject
*kobj
)
222 struct kobject
* top_kobj
= kobj
;
224 /* If this kobj does not belong to a kset,
225 try to find a parent that does. */
226 if (!top_kobj
->kset
&& top_kobj
->parent
) {
228 top_kobj
= top_kobj
->parent
;
229 } while (!top_kobj
->kset
&& top_kobj
->parent
);
232 if (top_kobj
->kset
&& top_kobj
->kset
->hotplug_ops
)
233 kset_hotplug(action
, top_kobj
->kset
, kobj
);
236 void kobject_hotplug(const char *action
, struct kobject
*kobj
)
240 #endif /* CONFIG_HOTPLUG */
243 * kobject_init - initialize object.
244 * @kobj: object in question.
246 void kobject_init(struct kobject
* kobj
)
248 kref_init(&kobj
->kref
);
249 INIT_LIST_HEAD(&kobj
->entry
);
250 kobj
->kset
= kset_get(kobj
->kset
);
255 * unlink - remove kobject from kset list.
258 * Remove the kobject from the kset list and decrement
259 * its parent's refcount.
260 * This is separated out, so we can use it in both
261 * kobject_del() and kobject_add() on error.
264 static void unlink(struct kobject
* kobj
)
267 down_write(&kobj
->kset
->subsys
->rwsem
);
268 list_del_init(&kobj
->entry
);
269 up_write(&kobj
->kset
->subsys
->rwsem
);
275 * kobject_add - add an object to the hierarchy.
279 int kobject_add(struct kobject
* kobj
)
282 struct kobject
* parent
;
284 if (!(kobj
= kobject_get(kobj
)))
287 kobj
->k_name
= kobj
->name
;
288 parent
= kobject_get(kobj
->parent
);
290 pr_debug("kobject %s: registering. parent: %s, set: %s\n",
291 kobject_name(kobj
), parent
? kobject_name(parent
) : "<NULL>",
292 kobj
->kset
? kobj
->kset
->kobj
.name
: "<NULL>" );
295 down_write(&kobj
->kset
->subsys
->rwsem
);
298 parent
= kobject_get(&kobj
->kset
->kobj
);
300 list_add_tail(&kobj
->entry
,&kobj
->kset
->list
);
301 up_write(&kobj
->kset
->subsys
->rwsem
);
303 kobj
->parent
= parent
;
305 error
= create_dir(kobj
);
311 kobject_hotplug("add", kobj
);
319 * kobject_register - initialize and add an object.
320 * @kobj: object in question.
323 int kobject_register(struct kobject
* kobj
)
328 error
= kobject_add(kobj
);
330 printk("kobject_register failed for %s (%d)\n",
331 kobject_name(kobj
),error
);
341 * kobject_set_name - Set the name of an object
345 * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
346 * string that @kobj->k_name points to. Otherwise, use the static
350 int kobject_set_name(struct kobject
* kobj
, const char * fmt
, ...)
353 int limit
= KOBJ_NAME_LEN
;
360 * First, try the static array
362 need
= vsnprintf(kobj
->name
,limit
,fmt
,args
);
367 * Need more space? Allocate it and try again
370 name
= kmalloc(limit
,GFP_KERNEL
);
375 need
= vsnprintf(name
,limit
,fmt
,args
);
377 /* Still? Give up. */
385 /* Free the old name, if necessary. */
386 if (kobj
->k_name
&& kobj
->k_name
!= kobj
->name
)
389 /* Now, set the new name */
396 EXPORT_SYMBOL(kobject_set_name
);
400 * kobject_rename - change the name of an object
401 * @kobj: object in question.
402 * @new_name: object's new name
405 int kobject_rename(struct kobject
* kobj
, char *new_name
)
409 kobj
= kobject_get(kobj
);
412 error
= sysfs_rename_dir(kobj
, new_name
);
419 * kobject_del - unlink kobject from hierarchy.
423 void kobject_del(struct kobject
* kobj
)
425 kobject_hotplug("remove", kobj
);
426 sysfs_remove_dir(kobj
);
431 * kobject_unregister - remove object from hierarchy and decrement refcount.
432 * @kobj: object going away.
435 void kobject_unregister(struct kobject
* kobj
)
437 pr_debug("kobject %s: unregistering\n",kobject_name(kobj
));
443 * kobject_get - increment refcount for object.
447 struct kobject
* kobject_get(struct kobject
* kobj
)
450 kref_get(&kobj
->kref
);
455 * kobject_cleanup - free kobject resources.
459 void kobject_cleanup(struct kobject
* kobj
)
461 struct kobj_type
* t
= get_ktype(kobj
);
462 struct kset
* s
= kobj
->kset
;
463 struct kobject
* parent
= kobj
->parent
;
465 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj
));
466 if (kobj
->k_name
!= kobj
->name
)
477 static void kobject_release(struct kref
*kref
)
479 kobject_cleanup(container_of(kref
, struct kobject
, kref
));
483 * kobject_put - decrement refcount for object.
486 * Decrement the refcount, and if 0, call kobject_cleanup().
488 void kobject_put(struct kobject
* kobj
)
491 kref_put(&kobj
->kref
, kobject_release
);
496 * kset_init - initialize a kset for use
500 void kset_init(struct kset
* k
)
502 kobject_init(&k
->kobj
);
503 INIT_LIST_HEAD(&k
->list
);
508 * kset_add - add a kset object to the hierarchy.
511 * Simply, this adds the kset's embedded kobject to the
513 * We also try to make sure that the kset's embedded kobject
514 * has a parent before it is added. We only care if the embedded
515 * kobject is not part of a kset itself, since kobject_add()
516 * assigns a parent in that case.
517 * If that is the case, and the kset has a controlling subsystem,
518 * then we set the kset's parent to be said subsystem.
521 int kset_add(struct kset
* k
)
523 if (!k
->kobj
.parent
&& !k
->kobj
.kset
&& k
->subsys
)
524 k
->kobj
.parent
= &k
->subsys
->kset
.kobj
;
526 return kobject_add(&k
->kobj
);
531 * kset_register - initialize and add a kset.
535 int kset_register(struct kset
* k
)
543 * kset_unregister - remove a kset.
547 void kset_unregister(struct kset
* k
)
549 kobject_unregister(&k
->kobj
);
554 * kset_find_obj - search for object in kset.
555 * @kset: kset we're looking in.
556 * @name: object's name.
558 * Lock kset via @kset->subsys, and iterate over @kset->list,
559 * looking for a matching kobject. If matching object is found
560 * take a reference and return the object.
563 struct kobject
* kset_find_obj(struct kset
* kset
, const char * name
)
565 struct list_head
* entry
;
566 struct kobject
* ret
= NULL
;
568 down_read(&kset
->subsys
->rwsem
);
569 list_for_each(entry
,&kset
->list
) {
570 struct kobject
* k
= to_kobj(entry
);
571 if (kobject_name(k
) && !strcmp(kobject_name(k
),name
)) {
572 ret
= kobject_get(k
);
576 up_read(&kset
->subsys
->rwsem
);
581 void subsystem_init(struct subsystem
* s
)
583 init_rwsem(&s
->rwsem
);
588 * subsystem_register - register a subsystem.
589 * @s: the subsystem we're registering.
591 * Once we register the subsystem, we want to make sure that
592 * the kset points back to this subsystem for correct usage of
596 int subsystem_register(struct subsystem
* s
)
601 pr_debug("subsystem %s: registering\n",s
->kset
.kobj
.name
);
603 if (!(error
= kset_add(&s
->kset
))) {
610 void subsystem_unregister(struct subsystem
* s
)
612 pr_debug("subsystem %s: unregistering\n",s
->kset
.kobj
.name
);
613 kset_unregister(&s
->kset
);
618 * subsystem_create_file - export sysfs attribute file.
620 * @a: subsystem attribute descriptor.
623 int subsys_create_file(struct subsystem
* s
, struct subsys_attribute
* a
)
627 error
= sysfs_create_file(&s
->kset
.kobj
,&a
->attr
);
635 * subsystem_remove_file - remove sysfs attribute file.
637 * @a: attribute desciptor.
640 void subsys_remove_file(struct subsystem
* s
, struct subsys_attribute
* a
)
643 sysfs_remove_file(&s
->kset
.kobj
,&a
->attr
);
648 EXPORT_SYMBOL(kobject_get_path
);
649 EXPORT_SYMBOL(kobject_init
);
650 EXPORT_SYMBOL(kobject_register
);
651 EXPORT_SYMBOL(kobject_unregister
);
652 EXPORT_SYMBOL(kobject_get
);
653 EXPORT_SYMBOL(kobject_put
);
654 EXPORT_SYMBOL(kobject_add
);
655 EXPORT_SYMBOL(kobject_del
);
656 EXPORT_SYMBOL(kobject_rename
);
657 EXPORT_SYMBOL(kobject_hotplug
);
659 EXPORT_SYMBOL(kset_register
);
660 EXPORT_SYMBOL(kset_unregister
);
661 EXPORT_SYMBOL(kset_find_obj
);
663 EXPORT_SYMBOL(subsystem_init
);
664 EXPORT_SYMBOL(subsystem_register
);
665 EXPORT_SYMBOL(subsystem_unregister
);
666 EXPORT_SYMBOL(subsys_create_file
);
667 EXPORT_SYMBOL(subsys_remove_file
);