[PATCH] DVB: Other DVB core updates
[linux-2.6/history.git] / lib / kobject.c
blob2430e2be81e32f0c32170b471cc5d6e7fba917bb
1 /*
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.
13 #undef DEBUG
15 #include <linux/kobject.h>
16 #include <linux/string.h>
17 #include <linux/module.h>
18 #include <linux/stat.h>
20 /**
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;
36 int error = 0;
37 int i;
39 if (t && t->default_attrs) {
40 for (i = 0; (attr = t->default_attrs[i]); i++) {
41 if ((error = sysfs_create_file(kobj,attr)))
42 break;
45 return error;
48 static int create_dir(struct kobject * kobj)
50 int error = 0;
51 if (kobject_name(kobj)) {
52 error = sysfs_create_dir(kobj);
53 if (!error) {
54 if ((error = populate_dir(kobj)))
55 sysfs_remove_dir(kobj);
58 return error;
62 static inline struct kobject * to_kobj(struct list_head * entry)
64 return container_of(entry,struct kobject,entry);
68 #ifdef CONFIG_HOTPLUG
69 static int get_kobj_path_length(struct kset *kset, struct kobject *kobj)
71 int length = 1;
72 struct kobject * parent = kobj;
74 /* walk up the ancestors until we hit the one pointing to the
75 * root.
76 * Add 1 to strlen for leading '/' of each level.
78 do {
79 length += strlen(kobject_name(parent)) + 1;
80 parent = parent->parent;
81 } while (parent);
82 return length;
85 static void fill_kobj_path(struct kset *kset, struct kobject *kobj, char *path, int length)
87 struct kobject * parent;
89 --length;
90 for (parent = kobj; parent; parent = parent->parent) {
91 int cur = strlen(kobject_name(parent));
92 /* back up enough to print this name with '/' */
93 length -= cur;
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)
109 char *argv [3];
110 char **envp = NULL;
111 char *buffer = NULL;
112 char *scratch;
113 int i = 0;
114 int retval;
115 int kobj_path_length;
116 char *kobj_path = NULL;
117 char *name = NULL;
118 unsigned long seq;
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))
124 return;
127 pr_debug ("%s\n", __FUNCTION__);
129 if (!hotplug_path[0])
130 return;
132 envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
133 if (!envp)
134 return;
135 memset (envp, 0x00, NUM_ENVP * sizeof (char *));
137 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
138 if (!buffer)
139 goto exit;
141 if (kset->hotplug_ops->name)
142 name = kset->hotplug_ops->name(kset, kobj);
143 if (name == NULL)
144 name = kset->kobj.name;
146 argv [0] = hotplug_path;
147 argv [1] = name;
148 argv [2] = 0;
150 /* minimal command environment */
151 envp [i++] = "HOME=/";
152 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
154 scratch = buffer;
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);
168 if (!kobj_path)
169 goto exit;
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));
181 if (retval) {
182 pr_debug ("%s - hotplug() returned %d\n",
183 __FUNCTION__, retval);
184 goto exit;
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);
191 if (retval)
192 pr_debug ("%s - call_usermodehelper returned %d\n",
193 __FUNCTION__, retval);
195 exit:
196 kfree(kobj_path);
197 kfree(buffer);
198 kfree(envp);
199 return;
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) {
209 do {
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);
217 #else
218 void kobject_hotplug(const char *action, struct kobject *kobj)
220 return;
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.
239 * @kobj: kobject.
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)
249 if (kobj->kset) {
250 down_write(&kobj->kset->subsys->rwsem);
251 list_del_init(&kobj->entry);
252 up_write(&kobj->kset->subsys->rwsem);
254 kobject_put(kobj);
258 * kobject_add - add an object to the hierarchy.
259 * @kobj: object.
262 int kobject_add(struct kobject * kobj)
264 int error = 0;
265 struct kobject * parent;
267 if (!(kobj = kobject_get(kobj)))
268 return -ENOENT;
269 if (!kobj->k_name)
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>" );
277 if (kobj->kset) {
278 down_write(&kobj->kset->subsys->rwsem);
280 if (!parent)
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);
289 if (error) {
290 unlink(kobj);
291 if (parent)
292 kobject_put(parent);
293 } else {
294 kobject_hotplug("add", kobj);
297 return error;
302 * kobject_register - initialize and add an object.
303 * @kobj: object in question.
306 int kobject_register(struct kobject * kobj)
308 int error = 0;
309 if (kobj) {
310 kobject_init(kobj);
311 error = kobject_add(kobj);
312 if (error) {
313 printk("kobject_register failed for %s (%d)\n",
314 kobject_name(kobj),error);
315 dump_stack();
317 } else
318 error = -EINVAL;
319 return error;
324 * kobject_set_name - Set the name of an object
325 * @kobj: object.
326 * @name: name.
328 * If strlen(name) < KOBJ_NAME_LEN, then use a dynamically allocated
329 * string that @kobj->k_name points to. Otherwise, use the static
330 * @kobj->name array.
333 int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
335 int error = 0;
336 int limit = KOBJ_NAME_LEN;
337 int need;
338 va_list args;
339 char * name;
341 va_start(args,fmt);
343 * First, try the static array
345 need = vsnprintf(kobj->name,limit,fmt,args);
346 if (need < limit)
347 name = kobj->name;
348 else {
350 * Need more space? Allocate it and try again
352 name = kmalloc(need,GFP_KERNEL);
353 if (!name) {
354 error = -ENOMEM;
355 goto Done;
357 limit = need;
358 need = vsnprintf(name,limit,fmt,args);
360 /* Still? Give up. */
361 if (need > limit) {
362 kfree(name);
363 error = -EFAULT;
364 goto Done;
368 /* Free the old name, if necessary. */
369 if (kobj->k_name && kobj->k_name != kobj->name)
370 kfree(kobj->k_name);
372 /* Now, set the new name */
373 kobj->k_name = name;
374 Done:
375 va_end(args);
376 return error;
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 void kobject_rename(struct kobject * kobj, char *new_name)
390 kobj = kobject_get(kobj);
391 if (!kobj)
392 return;
393 sysfs_rename_dir(kobj, new_name);
394 kobject_put(kobj);
398 * kobject_del - unlink kobject from hierarchy.
399 * @kobj: object.
402 void kobject_del(struct kobject * kobj)
404 kobject_hotplug("remove", kobj);
405 sysfs_remove_dir(kobj);
406 unlink(kobj);
410 * kobject_unregister - remove object from hierarchy and decrement refcount.
411 * @kobj: object going away.
414 void kobject_unregister(struct kobject * kobj)
416 pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
417 kobject_del(kobj);
418 kobject_put(kobj);
422 * kobject_get - increment refcount for object.
423 * @kobj: object.
426 struct kobject * kobject_get(struct kobject * kobj)
428 if (kobj) {
429 WARN_ON(!atomic_read(&kobj->refcount));
430 atomic_inc(&kobj->refcount);
432 return kobj;
436 * kobject_cleanup - free kobject resources.
437 * @kobj: object.
440 void kobject_cleanup(struct kobject * kobj)
442 struct kobj_type * t = get_ktype(kobj);
443 struct kset * s = kobj->kset;
444 struct kobject * parent = kobj->parent;
446 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
447 if (kobj->k_name != kobj->name)
448 kfree(kobj->k_name);
449 kobj->k_name = NULL;
450 if (t && t->release)
451 t->release(kobj);
452 if (s)
453 kset_put(s);
454 if (parent)
455 kobject_put(parent);
459 * kobject_put - decrement refcount for object.
460 * @kobj: object.
462 * Decrement the refcount, and if 0, call kobject_cleanup().
465 void kobject_put(struct kobject * kobj)
467 if (atomic_dec_and_test(&kobj->refcount))
468 kobject_cleanup(kobj);
473 * kset_init - initialize a kset for use
474 * @k: kset
477 void kset_init(struct kset * k)
479 kobject_init(&k->kobj);
480 INIT_LIST_HEAD(&k->list);
485 * kset_add - add a kset object to the hierarchy.
486 * @k: kset.
488 * Simply, this adds the kset's embedded kobject to the
489 * hierarchy.
490 * We also try to make sure that the kset's embedded kobject
491 * has a parent before it is added. We only care if the embedded
492 * kobject is not part of a kset itself, since kobject_add()
493 * assigns a parent in that case.
494 * If that is the case, and the kset has a controlling subsystem,
495 * then we set the kset's parent to be said subsystem.
498 int kset_add(struct kset * k)
500 if (!k->kobj.parent && !k->kobj.kset && k->subsys)
501 k->kobj.parent = &k->subsys->kset.kobj;
503 return kobject_add(&k->kobj);
508 * kset_register - initialize and add a kset.
509 * @k: kset.
512 int kset_register(struct kset * k)
514 kset_init(k);
515 return kset_add(k);
520 * kset_unregister - remove a kset.
521 * @k: kset.
524 void kset_unregister(struct kset * k)
526 kobject_unregister(&k->kobj);
531 * kset_find_obj - search for object in kset.
532 * @kset: kset we're looking in.
533 * @name: object's name.
535 * Lock kset via @kset->subsys, and iterate over @kset->list,
536 * looking for a matching kobject. Return object if found.
539 struct kobject * kset_find_obj(struct kset * kset, const char * name)
541 struct list_head * entry;
542 struct kobject * ret = NULL;
544 down_read(&kset->subsys->rwsem);
545 list_for_each(entry,&kset->list) {
546 struct kobject * k = to_kobj(entry);
547 if (kobject_name(k) && (!strcmp(kobject_name(k),name))) {
548 ret = k;
549 break;
552 up_read(&kset->subsys->rwsem);
553 return ret;
557 void subsystem_init(struct subsystem * s)
559 init_rwsem(&s->rwsem);
560 kset_init(&s->kset);
564 * subsystem_register - register a subsystem.
565 * @s: the subsystem we're registering.
567 * Once we register the subsystem, we want to make sure that
568 * the kset points back to this subsystem for correct usage of
569 * the rwsem.
572 int subsystem_register(struct subsystem * s)
574 int error;
576 subsystem_init(s);
577 pr_debug("subsystem %s: registering\n",s->kset.kobj.name);
579 if (!(error = kset_add(&s->kset))) {
580 if (!s->kset.subsys)
581 s->kset.subsys = s;
583 return error;
586 void subsystem_unregister(struct subsystem * s)
588 pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name);
589 kset_unregister(&s->kset);
594 * subsystem_create_file - export sysfs attribute file.
595 * @s: subsystem.
596 * @a: subsystem attribute descriptor.
599 int subsys_create_file(struct subsystem * s, struct subsys_attribute * a)
601 int error = 0;
602 if (subsys_get(s)) {
603 error = sysfs_create_file(&s->kset.kobj,&a->attr);
604 subsys_put(s);
606 return error;
611 * subsystem_remove_file - remove sysfs attribute file.
612 * @s: subsystem.
613 * @a: attribute desciptor.
616 void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a)
618 if (subsys_get(s)) {
619 sysfs_remove_file(&s->kset.kobj,&a->attr);
620 subsys_put(s);
625 EXPORT_SYMBOL(kobject_init);
626 EXPORT_SYMBOL(kobject_register);
627 EXPORT_SYMBOL(kobject_unregister);
628 EXPORT_SYMBOL(kobject_get);
629 EXPORT_SYMBOL(kobject_put);
630 EXPORT_SYMBOL(kobject_add);
631 EXPORT_SYMBOL(kobject_del);
632 EXPORT_SYMBOL(kobject_rename);
633 EXPORT_SYMBOL(kobject_hotplug);
635 EXPORT_SYMBOL(kset_register);
636 EXPORT_SYMBOL(kset_unregister);
637 EXPORT_SYMBOL(kset_find_obj);
639 EXPORT_SYMBOL(subsystem_init);
640 EXPORT_SYMBOL(subsystem_register);
641 EXPORT_SYMBOL(subsystem_unregister);
642 EXPORT_SYMBOL(subsys_create_file);
643 EXPORT_SYMBOL(subsys_remove_file);