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);
62 static inline struct kobject * to_kobj(struct list_head * entry)
64 return container_of(entry,struct kobject,entry);
69 static int get_kobj_path_length(struct kset *kset, struct kobject *kobj)
72 struct kobject * parent = kobj;
74 /* walk up the ancestors until we hit the one pointing to the
76 * Add 1 to strlen for leading '/' of each level.
79 length += strlen(kobject_name(parent)) + 1;
80 parent = parent->parent;
85 static void fill_kobj_path(struct kset *kset, struct kobject *kobj, char *path, int length)
87 struct kobject * parent;
90 for (parent = kobj; parent; parent = parent->parent) {
91 int cur = strlen(kobject_name(parent));
92 /* back up enough to print this name with '/' */
94 strncpy (path + length, kobject_name(parent), cur);
95 *(path + --length) = '/';
98 pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
101 #define BUFFER_SIZE 1024 /* should be enough memory for the env */
102 #define NUM_ENVP 32 /* number of env pointers */
103 static unsigned long sequence_num;
104 static spinlock_t sequence_lock = SPIN_LOCK_UNLOCKED;
106 static void kset_hotplug(const char *action, struct kset *kset,
107 struct kobject *kobj)
115 int kobj_path_length;
116 char *kobj_path = NULL;
120 /* If the kset has a filter operation, call it. If it returns
121 failure, no hotplug event is required. */
122 if (kset->hotplug_ops->filter) {
123 if (!kset->hotplug_ops->filter(kset, kobj))
127 pr_debug ("%s\n", __FUNCTION__);
129 if (!hotplug_path[0])
132 envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
135 memset (envp, 0x00, NUM_ENVP * sizeof (char *));
137 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
141 if (kset->hotplug_ops->name)
142 name = kset->hotplug_ops->name(kset, kobj);
144 name = kset->kobj.name;
146 argv [0] = hotplug_path;
150 /* minimal command environment */
151 envp [i++] = "HOME=/";
152 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
156 envp [i++] = scratch;
157 scratch += sprintf(scratch, "ACTION=%s", action) + 1;
159 spin_lock(&sequence_lock);
160 seq = sequence_num++;
161 spin_unlock(&sequence_lock);
163 envp [i++] = scratch;
164 scratch += sprintf(scratch, "SEQNUM=%ld", seq) + 1;
166 kobj_path_length = get_kobj_path_length (kset, kobj);
167 kobj_path = kmalloc (kobj_path_length, GFP_KERNEL);
170 memset (kobj_path, 0x00, kobj_path_length);
171 fill_kobj_path (kset, kobj, kobj_path, kobj_path_length);
173 envp [i++] = scratch;
174 scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
176 if (kset->hotplug_ops->hotplug) {
177 /* have the kset specific function add its stuff */
178 retval = kset->hotplug_ops->hotplug (kset, kobj,
179 &envp[i], NUM_ENVP - i, scratch,
180 BUFFER_SIZE - (scratch - buffer));
182 pr_debug ("%s - hotplug() returned %d\n",
183 __FUNCTION__, retval);
188 pr_debug ("%s: %s %s %s %s %s %s %s\n", __FUNCTION__, argv[0], argv[1],
189 envp[0], envp[1], envp[2], envp[3], envp[4]);
190 retval = call_usermodehelper (argv[0], argv, envp, 0);
192 pr_debug ("%s - call_usermodehelper returned %d\n",
193 __FUNCTION__, retval);
202 void kobject_hotplug(const char *action, struct kobject *kobj)
204 struct kobject * top_kobj = kobj;
206 /* If this kobj does not belong to a kset,
207 try to find a parent that does. */
208 if (!top_kobj->kset && top_kobj->parent) {
210 top_kobj = top_kobj->parent;
211 } while (!top_kobj->kset && top_kobj->parent);
214 if (top_kobj->kset && top_kobj->kset->hotplug_ops)
215 kset_hotplug(action, top_kobj->kset, kobj);
218 void kobject_hotplug(const char *action, struct kobject *kobj)
222 #endif /* CONFIG_HOTPLUG */
225 * kobject_init - initialize object.
226 * @kobj: object in question.
229 void kobject_init(struct kobject * kobj)
231 atomic_set(&kobj->refcount,1);
232 INIT_LIST_HEAD(&kobj->entry);
233 kobj->kset = kset_get(kobj->kset);
238 * unlink - remove kobject from kset list.
241 * Remove the kobject from the kset list and decrement
242 * its parent's refcount.
243 * This is separated out, so we can use it in both
244 * kobject_del() and kobject_add() on error.
247 static void unlink(struct kobject * kobj)
250 down_write(&kobj->kset->subsys->rwsem);
251 list_del_init(&kobj->entry);
252 up_write(&kobj->kset->subsys->rwsem);
258 * kobject_add - add an object to the hierarchy.
262 int kobject_add(struct kobject * kobj)
265 struct kobject * parent;
267 if (!(kobj = kobject_get(kobj)))
270 kobj->k_name = kobj->name;
271 parent = kobject_get(kobj->parent);
273 pr_debug("kobject %s: registering. parent: %s, set: %s\n",
274 kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
275 kobj->kset ? kobj->kset->kobj.name : "<NULL>" );
278 down_write(&kobj->kset->subsys->rwsem);
281 parent = kobject_get(&kobj->kset->kobj);
283 list_add_tail(&kobj->entry,&kobj->kset->list);
284 up_write(&kobj->kset->subsys->rwsem);
286 kobj->parent = parent;
288 error = create_dir(kobj);
294 kobject_hotplug("add", kobj);
302 * kobject_register - initialize and add an object.
303 * @kobj: object in question.
306 int kobject_register(struct kobject * kobj)
311 error = kobject_add(kobj);
313 printk("kobject_register failed for %s (%d)\n",
314 kobject_name(kobj),error);
324 * kobject_set_name - Set the name of an object
328 * If strlen(name) < KOBJ_NAME_LEN, then use a dynamically allocated
329 * string that @kobj->k_name points to. Otherwise, use the static
333 int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
336 int limit = KOBJ_NAME_LEN;
343 * First, try the static array
345 need = vsnprintf(kobj->name,limit,fmt,args);
350 * Need more space? Allocate it and try again
353 name = kmalloc(limit,GFP_KERNEL);
358 need = vsnprintf(name,limit,fmt,args);
360 /* Still? Give up. */
368 /* Free the old name, if necessary. */
369 if (kobj->k_name && kobj->k_name != kobj->name)
372 /* Now, set the new name */
379 EXPORT_SYMBOL(kobject_set_name);
383 * kobject_rename - change the name of an object
384 * @kobj: object in question.
385 * @new_name: object's new name
388 int kobject_rename(struct kobject * kobj, char *new_name)
392 kobj = kobject_get(kobj);
395 error = sysfs_rename_dir(kobj, new_name);
402 * kobject_del - unlink kobject from hierarchy.
406 void kobject_del(struct kobject * kobj)
408 kobject_hotplug("remove", kobj);
409 sysfs_remove_dir(kobj);
414 * kobject_unregister - remove object from hierarchy and decrement refcount.
415 * @kobj: object going away.
418 void kobject_unregister(struct kobject * kobj)
420 pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
426 * kobject_get - increment refcount for object.
430 struct kobject * kobject_get(struct kobject * kobj)
433 WARN_ON(!atomic_read(&kobj->refcount));
434 atomic_inc(&kobj->refcount);
440 * kobject_cleanup - free kobject resources.
444 void kobject_cleanup(struct kobject * kobj)
446 struct kobj_type * t = get_ktype(kobj);
447 struct kset * s = kobj->kset;
448 struct kobject * parent = kobj->parent;
450 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
451 if (kobj->k_name != kobj->name)
463 * kobject_put - decrement refcount for object.
466 * Decrement the refcount, and if 0, call kobject_cleanup().
469 void kobject_put(struct kobject * kobj)
471 if (atomic_dec_and_test(&kobj->refcount))
472 kobject_cleanup(kobj);
477 * kset_init - initialize a kset for use
481 void kset_init(struct kset * k)
483 kobject_init(&k->kobj);
484 INIT_LIST_HEAD(&k->list);
489 * kset_add - add a kset object to the hierarchy.
492 * Simply, this adds the kset's embedded kobject to the
494 * We also try to make sure that the kset's embedded kobject
495 * has a parent before it is added. We only care if the embedded
496 * kobject is not part of a kset itself, since kobject_add()
497 * assigns a parent in that case.
498 * If that is the case, and the kset has a controlling subsystem,
499 * then we set the kset's parent to be said subsystem.
502 int kset_add(struct kset * k)
504 if (!k->kobj.parent && !k->kobj.kset && k->subsys)
505 k->kobj.parent = &k->subsys->kset.kobj;
507 return kobject_add(&k->kobj);
512 * kset_register - initialize and add a kset.
516 int kset_register(struct kset * k)
524 * kset_unregister - remove a kset.
528 void kset_unregister(struct kset * k)
530 kobject_unregister(&k->kobj);
535 * kset_find_obj - search for object in kset.
536 * @kset: kset we're looking in.
537 * @name: object's name.
539 * Lock kset via @kset->subsys, and iterate over @kset->list,
540 * looking for a matching kobject. If matching object is found
541 * take a reference and return the object.
544 struct kobject * kset_find_obj(struct kset * kset, const char * name)
546 struct list_head * entry;
547 struct kobject * ret = NULL;
549 down_read(&kset->subsys->rwsem);
550 list_for_each(entry,&kset->list) {
551 struct kobject * k = to_kobj(entry);
552 if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
553 ret = kobject_get(k);
557 up_read(&kset->subsys->rwsem);
562 void subsystem_init(struct subsystem * s)
564 init_rwsem(&s->rwsem);
569 * subsystem_register - register a subsystem.
570 * @s: the subsystem we're registering.
572 * Once we register the subsystem, we want to make sure that
573 * the kset points back to this subsystem for correct usage of
577 int subsystem_register(struct subsystem * s)
582 pr_debug("subsystem %s: registering\n",s->kset.kobj.name);
584 if (!(error = kset_add(&s->kset))) {
591 void subsystem_unregister(struct subsystem * s)
593 pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name);
594 kset_unregister(&s->kset);
599 * subsystem_create_file - export sysfs attribute file.
601 * @a: subsystem attribute descriptor.
604 int subsys_create_file(struct subsystem * s, struct subsys_attribute * a)
608 error = sysfs_create_file(&s->kset.kobj,&a->attr);
616 * subsystem_remove_file - remove sysfs attribute file.
618 * @a: attribute desciptor.
621 void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a)
624 sysfs_remove_file(&s->kset.kobj,&a->attr);
630 EXPORT_SYMBOL(kobject_init);
631 EXPORT_SYMBOL(kobject_register);
632 EXPORT_SYMBOL(kobject_unregister);
633 EXPORT_SYMBOL(kobject_get);
634 EXPORT_SYMBOL(kobject_put);
635 EXPORT_SYMBOL(kobject_add);
636 EXPORT_SYMBOL(kobject_del);
637 EXPORT_SYMBOL(kobject_rename);
638 EXPORT_SYMBOL(kobject_hotplug);
640 EXPORT_SYMBOL(kset_register);
641 EXPORT_SYMBOL(kset_unregister);
642 EXPORT_SYMBOL(kset_find_obj);
644 EXPORT_SYMBOL(subsystem_init);
645 EXPORT_SYMBOL(subsystem_register);
646 EXPORT_SYMBOL(subsystem_unregister);
647 EXPORT_SYMBOL(subsys_create_file);
648 EXPORT_SYMBOL(subsys_remove_file);